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.

253 lines
8.7 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.Linq;
  5. /// <summary>
  6. /// Class which defines blocks around the level
  7. /// </summary>
  8. [RequireComponent(typeof(BoxCollider))]
  9. public class Block : MonoBehaviour
  10. {
  11. #region Inspector Fields
  12. [SerializeField]
  13. [Tooltip("Offset from the top of the block from which a character should stand")]
  14. private Vector3 VisualOffset = Vector3.zero;
  15. [SerializeField]
  16. [Tooltip("Can this type of block be walked on")]
  17. public bool is_Walkable = true;
  18. [Tooltip("Is this block underwater?")]
  19. public bool isWater = false;
  20. [Tooltip("Is this block at the bottom of a pit?")]
  21. public bool isPit = false;
  22. public bool isCrystals = false;
  23. public bool isRock = false;
  24. public bool isCollectableSpawnable = false;
  25. [Header("Spawn Settings")]
  26. [Tooltip("Can this block be spawned on")]
  27. public bool isSpawnable = false;
  28. [Tooltip("Direction Player is poting at when spawned")]
  29. public Direction SpawnDirection = Direction.Forward;
  30. #endregion InspectorFields
  31. #region Private Functions
  32. /// <summary>
  33. /// List of current players on this block
  34. /// </summary>
  35. public Character CurrentPlayer { get; protected set; }
  36. #endregion Private Functions
  37. #region ReadOnly Properties
  38. /// <summary>
  39. /// Blocks position in global space
  40. /// </summary>
  41. public Vector3 position { get { return transform.position; } }
  42. /// <summary>
  43. /// Position character should stand in global space
  44. /// </summary>
  45. public Vector3 VisualPosition { get { return position + VisualOffset + Vector3.up * 0.5f; } }
  46. #endregion ReadOnly Properties
  47. #region Public Functions
  48. /// <summary>
  49. /// Is a block valid to walk on
  50. /// </summary>
  51. /// <param name="layerMask">Layers to check for when checking for blocks above</param>
  52. /// <returns></returns>
  53. public bool isWalkable(LayerMask layerMask)
  54. {
  55. //checks if there is no block above this one and that this is tagged as walkable
  56. return (is_Walkable && !isBlockAtPosition(position + Vector3.up, 1, layerMask));
  57. }
  58. /// <summary>
  59. /// Is called when a player moves onto this block
  60. ///
  61. /// Should be implemented by a derived class
  62. /// </summary>
  63. /// <param name="player">Player which moved on to block</param>
  64. ///<param name="moveDirection">The direction the player moved to get to this block</param>
  65. public virtual IEnumerator OnWalkedOnByPlayer(Character player, Vector3 moveDirection)
  66. {
  67. yield return StartCoroutine(DoPush(player, moveDirection));
  68. }
  69. /// <summary>
  70. /// Is called when a player moves off of block
  71. ///
  72. /// Should be implemented by a derived class
  73. /// </summary>
  74. /// <param name="player">Player which moved on to block</param>
  75. public virtual void OnLeftByPlayer(Character player)
  76. {
  77. CurrentPlayer = null;
  78. }
  79. /// <summary>
  80. /// Called to deal with players colliding on this block
  81. /// </summary>
  82. /// <param name="newPlayer">Player which is moving into this block</param>
  83. /// <param name="moveDirection">The direction the player moved to get to this block</param>
  84. public virtual IEnumerator DoPush(Character newPlayer, Vector3 moveDirection)
  85. {
  86. if (CurrentPlayer == null)
  87. {
  88. CurrentPlayer = newPlayer;
  89. yield break;
  90. }
  91. Block pushBlock = GetPushLocation(moveDirection, ~CurrentPlayer.Ignore);
  92. if (pushBlock != this)
  93. {
  94. Character oldPlayer = CurrentPlayer;
  95. CurrentPlayer = newPlayer;
  96. yield return StartCoroutine(oldPlayer.MoveToBlock(pushBlock, Character.Animation.Hit, 1));
  97. }
  98. else
  99. {
  100. Block returnBlock = GetPushLocation(-moveDirection, ~newPlayer.Ignore);
  101. if (returnBlock != this)
  102. yield return StartCoroutine(newPlayer.MoveToBlock(returnBlock, Character.Animation.Hit, 1));
  103. }
  104. }
  105. #endregion Public Functions
  106. #region Protected Functions
  107. protected Block GetPushLocation(Vector3 pushDirection, LayerMask ignoreMask)
  108. {
  109. //setting up variables
  110. Vector3 newPosition = position + pushDirection; // position wanted
  111. Block hit; //output of block detection
  112. //if move is obstucted no where to move
  113. if (Block.isBlockAtPosition(newPosition + Vector3.up, 1, ignoreMask))
  114. return this;
  115. //If block at Position is walkable set it to moveTo
  116. if (Block.isBlockAtPosition(newPosition, 1, ignoreMask, out hit) && hit.isWalkable(ignoreMask))
  117. {
  118. return hit;
  119. }
  120. //else if block down one is walkable
  121. else if (Block.isBlockAtPosition(newPosition + Vector3.down, 1, ignoreMask, out hit) && hit.isWalkable(ignoreMask))
  122. {
  123. return hit;
  124. }
  125. return this;
  126. }
  127. #endregion Protected Functions
  128. #region Editor Functions
  129. private void OnDrawGizmos()
  130. {
  131. if (!isSpawnable)
  132. return;
  133. Vector3 DrawPosition = VisualPosition + Vector3.up * 0.4f;
  134. Vector3 Perp = Quaternion.Euler(0, 90, 0) * SpawnDirection.ToVector();
  135. DebugExtensions.DrawCube(DrawPosition, 0.4f, Color.magenta, 0);
  136. //Eyes
  137. Vector3 eyePosition = DrawPosition + SpawnDirection.ToVector() * 0.4f;
  138. DebugExtensions.DrawCube(eyePosition + Perp * 0.2f, 0.1f, Color.magenta, 0);
  139. DebugExtensions.DrawCube(eyePosition - Perp * 0.2f, 0.1f, Color.magenta, 0);
  140. //ears
  141. Vector3 earPosition = DrawPosition + SpawnDirection.ToVector() * 0.2f + Vector3.up * 0.4f;
  142. Vector3 earScale = Quaternion.LookRotation(SpawnDirection.ToVector()) * new Vector3(0.1f, 0.1f, 0.05f);
  143. DebugExtensions.DrawCube(earPosition + Perp * 0.3f, earScale, Color.magenta, 0);
  144. DebugExtensions.DrawCube(earPosition - Perp * 0.3f, earScale, Color.magenta, 0);
  145. }
  146. #endregion Editor Functions
  147. #region Static Functions
  148. /// <summary>
  149. /// Checks if there is a block at supplied position
  150. /// </summary>
  151. /// <param name="position">position to check at</param>
  152. /// <param name="Scale">Scale of block. (should be 1)</param>
  153. /// <param name="layerMask">Layers to check on</param>
  154. /// <param name="hit">Block hit</param>
  155. /// <returns>if a block is at position</returns>
  156. public static bool isBlockAtPosition(Vector3 position, float Scale, LayerMask layerMask, out Block hit)
  157. {
  158. //Turn scale into halfextent and shrink a bit so it doesn't hit bordering blocks
  159. Vector3 halfExtent = Vector3.one * ((Scale - 0.1f) / 2);
  160. //Get every collider which is at position
  161. Collider[] cols = Physics.OverlapBox(position, halfExtent, Quaternion.identity, layerMask);
  162. //Filter colliders for only GameObjects with an Block component
  163. Block[] blocks = cols.Where(p => p.GetComponent<Block>() != null).Select(p => p.GetComponent<Block>()).ToArray();
  164. //Draw cube, for visuals
  165. DebugExtensions.DrawCube(position, halfExtent, Color.cyan, 1, false);
  166. //if didn't hit anyblocks return false
  167. if (blocks.Length == 0)
  168. {
  169. hit = null;
  170. return false;
  171. }
  172. else
  173. {
  174. //else get the closest block to disered position, (in case we hit mulitple blocks)
  175. hit = Utility.minBy(blocks, p => Vector3.Distance(p.transform.position, position));
  176. return true;
  177. }
  178. }
  179. /// <summary>
  180. /// Checks if there is a block at supplied position
  181. /// </summary>
  182. /// <param name="position">position to check at</param>
  183. /// <param name="Scale">Scale of block. (should be 1)</param>
  184. /// <param name="layerMask">Layers to check on</param>
  185. /// <returns>if a block is at position</returns>
  186. public static bool isBlockAtPosition(Vector3 position, float scale, LayerMask layerMask)
  187. {
  188. //Return Overloaded function above
  189. Block hit;
  190. return (isBlockAtPosition(position, scale, layerMask, out hit));
  191. }
  192. /// <summary>
  193. /// Gets the block at a position or creates an airblock if there isn't one there
  194. /// </summary>
  195. /// <param name="position">position to get block at</param>
  196. /// <param name="Scale">Scale of block. (should be 1)</param>
  197. /// <param name="layerMask">Layers to check on</param>
  198. /// <returns>block at position</returns>
  199. public static Block GetOrCreateBlockAtPosition(Vector3 position, float Scale, LayerMask layerMask)
  200. {
  201. Block retVal;
  202. if (isBlockAtPosition(position, Scale, layerMask, out retVal))
  203. return retVal;
  204. GameObject newBlock = Resources.Load<GameObject>("Cube_Air");
  205. newBlock.transform.position = position;
  206. return newBlock.GetComponent<Air>();
  207. }
  208. #endregion
  209. }