You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
5.4 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class Character : MonoBehaviour
  5. {
  6. #region Inspector Fields
  7. [SerializeField]
  8. [Tooltip("Will move to this block at start, else will try and find a block below")]
  9. private Block CurrentBlock;
  10. [SerializeField]
  11. [Tooltip("Layers to ignore when checking for blocks")]
  12. private LayerMask Ignore;
  13. #endregion Inspector Fields
  14. #region Unity Functions
  15. private void Awake()
  16. {
  17. //If no starting block find one below it
  18. if (CurrentBlock == null)
  19. Block.isBlockAtPosition(transform.position + Vector3.down/2, 1, ~Ignore, out CurrentBlock);
  20. //move to starting block
  21. transform.position = CurrentBlock.VisualPosition;
  22. }
  23. #endregion Unity Functions
  24. #region Class Implementation
  25. /// <summary>
  26. /// Moves one block in specefied direction, Can walk off obstacles
  27. /// </summary>
  28. /// <param name="direction">direction to walk</param>
  29. /// <remarks>Technically is same as JumpLong(1) but kept seperate to avoid confusion</remarks>
  30. public void Move(Direction direction)
  31. {
  32. //setting up variables
  33. Vector3 position = CurrentBlock.position + direction.ToVector(transform); // position wanted
  34. Block hit; //output of block detection
  35. Block moveTo = CurrentBlock; //block we'll actually move to
  36. //if move is obstucted no where to move
  37. if (Block.isBlockAtPosition(position + Vector3.up, 1, ~Ignore))
  38. return;
  39. //If block at Position is walkable set it to moveTo
  40. if (Block.isBlockAtPosition(position, 1, ~Ignore, out hit) && hit.isWalkable(~Ignore))
  41. moveTo = hit;
  42. //else if block down one is walkable
  43. else if (Block.isBlockAtPosition(position + Vector3.down, 1, ~Ignore, out hit) && hit.isWalkable(~Ignore))
  44. moveTo = hit;
  45. //set current block && move
  46. CurrentBlock = moveTo;
  47. transform.position = CurrentBlock.VisualPosition;
  48. }
  49. /// <summary>
  50. /// Rotates to point in specific direction based on current direction
  51. /// </summary>
  52. /// <param name="direction">Local direction to point</param>
  53. public void Rotate(Direction direction)
  54. {
  55. transform.forward = direction.ToVector(transform);
  56. }
  57. /// <summary>
  58. /// Jumps in specefied direction, picks between Long Jump and Jumping up
  59. /// </summary>
  60. /// <param name="direction">Direction to Jump</param>
  61. public void Jump(Direction direction)
  62. {
  63. //if there is a block infront JumpUp else LongJump
  64. if (Block.isBlockAtPosition(CurrentBlock.position + direction.ToVector(transform) + Vector3.up, 1, ~Ignore))
  65. JumpUp(direction);
  66. else
  67. JumpLong(direction);
  68. }
  69. #endregion Class Implementation
  70. #region Private Functions
  71. /// <summary>
  72. /// Jumps up obstacle of specific height. Jumps as high as possible
  73. /// </summary>
  74. /// <param name="direction">Direction of obstacle</param>
  75. /// <param name="height">max height</param>
  76. private void JumpUp(Direction direction,int height = 1)
  77. {
  78. //setting up variables
  79. Vector3 position; // position wanted
  80. Block hit; //output of block detection
  81. Block moveTo = CurrentBlock; //block we'll actually move to
  82. //Check blocks in going up then move to the heighest walkable one
  83. for (int i = 0; i <= height; i++)
  84. {
  85. //next position up the tower
  86. position = CurrentBlock.position + direction.ToVector(transform) + (Vector3.up * i);
  87. //if block is walkable set it to last known position
  88. if (Block.isBlockAtPosition(position, 1, ~Ignore, out hit) && hit.isWalkable(~Ignore))
  89. moveTo = hit;
  90. }
  91. //set current block && move
  92. CurrentBlock = moveTo;
  93. transform.position = CurrentBlock.VisualPosition;
  94. }
  95. /// <summary>
  96. /// Long jumps forward a specified distance. Can Jump gaps. Stops at obstruction
  97. /// </summary>
  98. /// <param name="direction">Direction to Jump</param>
  99. /// <param name="Distance">Max distance</param>
  100. private void JumpLong(Direction direction, int Distance = 2)
  101. {
  102. //setting up variables
  103. Vector3 position; // position wanted
  104. Block hit; //output of block detection
  105. Block moveTo = CurrentBlock; //block we'll actually move to
  106. //Check blocks in front until we hit an obstruction or went the distance
  107. for (int i = 1; i <= Distance; i++)
  108. {
  109. //Next position to MoveTo
  110. position = CurrentBlock.position + (direction.ToVector(transform) * i);
  111. //if jump is obstructed, stop and go to last known block
  112. if (Block.isBlockAtPosition(position + Vector3.up, 1, ~Ignore))
  113. break;
  114. //If block at Position is walkable set it to last known position
  115. if (Block.isBlockAtPosition(position, 1, ~Ignore, out hit) && hit.isWalkable(~Ignore))
  116. moveTo = hit;
  117. //else if block down one is walkable
  118. else if (Block.isBlockAtPosition(position + Vector3.down, 1, ~Ignore, out hit) && hit.isWalkable(~Ignore))
  119. moveTo = hit;
  120. }//end for
  121. //Set Current Block and move
  122. CurrentBlock = moveTo;
  123. transform.position = CurrentBlock.VisualPosition;
  124. }
  125. #endregion Private Functions
  126. }