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.

183 lines
5.6 KiB

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