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.

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