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.

186 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. #region Inspector Fields
  10. [SerializeField]
  11. private Inventory startingInventory;
  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. [Tooltip("Current Inventory of the player")]
  19. public Inventory Inventory;
  20. #endregion Inspector Fields
  21. #region Unity Functions
  22. private void Awake ()
  23. {
  24. Inventory.Clone(startingInventory);
  25. //If no starting block find one below it
  26. if (CurrentBlock == null)
  27. Block.isBlockAtPosition (transform.position + Vector3.down / 2, 1, ~Ignore, out CurrentBlock);
  28. //move to starting block
  29. transform.position = CurrentBlock.VisualPosition;
  30. }
  31. #endregion Unity Functions
  32. #region Class Implementation
  33. /// <summary>
  34. /// Moves one block in specefied direction, Can walk off obstacles
  35. /// </summary>
  36. /// <param name="direction">direction to walk</param>
  37. /// <remarks>Technically is same as JumpLong(1) but kept seperate to avoid confusion</remarks>
  38. public void Move (Direction direction)
  39. {
  40. //setting up variables
  41. Vector3 position = CurrentBlock.position + direction.ToVector (transform); // position wanted
  42. Block hit; //output of block detection
  43. Block moveTo = CurrentBlock; //block we'll actually move to
  44. //if move is obstucted no where to move
  45. if (Block.isBlockAtPosition (position + Vector3.up, 1, ~Ignore))
  46. return;
  47. //If block at Position is walkable set it to moveTo
  48. if (Block.isBlockAtPosition (position, 1, ~Ignore, out hit) && hit.isWalkable (~Ignore))
  49. moveTo = hit;
  50. //else if block down one is walkable
  51. else if (Block.isBlockAtPosition (position + Vector3.down, 1, ~Ignore, out hit) && hit.isWalkable (~Ignore))
  52. moveTo = hit;
  53. //set current block && move
  54. CurrentBlock = moveTo;
  55. transform.position = CurrentBlock.VisualPosition;
  56. }
  57. /// <summary>
  58. /// Upon collision with a floating block, collect its
  59. /// Upon collision with the end portal, end of level
  60. /// </summary>
  61. /// <param name="other">name of collided object</param>
  62. void OnTriggerEnter (Collider other)
  63. {
  64. Collectable collectable = other.GetComponentInChildren<Collectable>();
  65. if (other != null) {
  66. collectable.OnCollect(this);
  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. }