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.

173 lines
5.3 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. /// Upon collision with a floating block, collect its
  51. /// Upon collision with the end portal, end of level
  52. /// </summary>
  53. /// <param name="other">name of collided object</param>
  54. void OnTriggerEnter (Collider other)
  55. {
  56. if (other.gameObject.CompareTag ("New Block")) {
  57. other.gameObject.SetActive (false);
  58. Debug.Log ("You picked up a new coding block!");
  59. }
  60. if (other.gameObject.CompareTag ("End Portal")) {
  61. //Application.LoadLevel
  62. Debug.Log ("You finished this level!");
  63. }
  64. }
  65. /// <summary>
  66. /// Rotates to point in specific direction based on current direction
  67. /// </summary>
  68. /// <param name="direction">Local direction to point</param>
  69. public void Rotate (Direction direction)
  70. {
  71. transform.forward = direction.ToVector (transform);
  72. }
  73. /// <summary>
  74. /// Jumps in specefied direction, picks between Long Jump and Jumping up
  75. /// </summary>
  76. /// <param name="direction">Direction to Jump</param>
  77. public void Jump (Direction direction)
  78. {
  79. //if there is a block infront JumpUp else LongJump
  80. if (Block.isBlockAtPosition (CurrentBlock.position + direction.ToVector (transform) + Vector3.up, 1, ~Ignore))
  81. JumpUp (direction);
  82. else
  83. JumpLong (direction);
  84. }
  85. #endregion Class Implementation
  86. #region Private Functions
  87. /// <summary>
  88. /// Jumps up obstacle of specific height. Jumps as high as possible
  89. /// </summary>
  90. /// <param name="direction">Direction of obstacle</param>
  91. /// <param name="height">max height</param>
  92. private void JumpUp (Direction direction, int height = 1)
  93. {
  94. //setting up variables
  95. Vector3 position; // position wanted
  96. Block hit; //output of block detection
  97. Block moveTo = CurrentBlock; //block we'll actually move to
  98. //Check blocks in going up then move to the heighest walkable one
  99. for (int i = 0; i <= height; i++) {
  100. //next position up the tower
  101. position = CurrentBlock.position + direction.ToVector (transform) + (Vector3.up * i);
  102. //if block is walkable set it to last known position
  103. if (Block.isBlockAtPosition (position, 1, ~Ignore, out hit) && hit.isWalkable (~Ignore))
  104. moveTo = hit;
  105. }
  106. //set current block && move
  107. CurrentBlock = moveTo;
  108. transform.position = CurrentBlock.VisualPosition;
  109. }
  110. /// <summary>
  111. /// Long jumps forward a specified distance. Can Jump gaps. Stops at obstruction
  112. /// </summary>
  113. /// <param name="direction">Direction to Jump</param>
  114. /// <param name="Distance">Max distance</param>
  115. private void JumpLong (Direction direction, int Distance = 2)
  116. {
  117. //setting up variables
  118. Vector3 position; // position wanted
  119. Block hit; //output of block detection
  120. Block moveTo = CurrentBlock; //block we'll actually move to
  121. //Check blocks in front until we hit an obstruction or went the distance
  122. for (int i = 1; i <= Distance; i++) {
  123. //Next position to MoveTo
  124. position = CurrentBlock.position + (direction.ToVector (transform) * i);
  125. //if jump is obstructed, stop and go to last known block
  126. if (Block.isBlockAtPosition (position + Vector3.up, 1, ~Ignore))
  127. break;
  128. //If block at Position is walkable set it to last known position
  129. if (Block.isBlockAtPosition (position, 1, ~Ignore, out hit) && hit.isWalkable (~Ignore))
  130. moveTo = hit;
  131. //else if block down one is walkable
  132. else if (Block.isBlockAtPosition (position + Vector3.down, 1, ~Ignore, out hit) && hit.isWalkable (~Ignore))
  133. moveTo = hit;
  134. }//end for
  135. //Set Current Block and move
  136. CurrentBlock = moveTo;
  137. transform.position = CurrentBlock.VisualPosition;
  138. }
  139. #endregion Private Functions
  140. }