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.

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