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.

178 lines
5.5 KiB

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