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.

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