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.

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