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.

148 lines
4.9 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. [Header("Spawn Settings")]
  23. [Tooltip("Can this block be spawned on")]
  24. public bool isSpawnable = false;
  25. [Tooltip("Direction Player is poting at when spawned")]
  26. public Direction SpawnDirection = Direction.Forward;
  27. #endregion InspectorFields
  28. #region ReadOnly Properties
  29. /// <summary>
  30. /// Blocks position in global space
  31. /// </summary>
  32. public Vector3 position { get { return transform.position; } }
  33. /// <summary>
  34. /// Position character should stand in global space
  35. /// </summary>
  36. public Vector3 VisualPosition { get { return position + VisualOffset + Vector3.up * 0.5f; } }
  37. #endregion ReadOnly Properties
  38. #region Public Functions
  39. /// <summary>
  40. /// Is a block valid to walk on
  41. /// </summary>
  42. /// <param name="layerMask">Layers to check for when checking for blocks above</param>
  43. /// <returns></returns>
  44. public bool isWalkable(LayerMask layerMask)
  45. {
  46. //checks if there is no block above this one and that this is tagged as walkable
  47. return (is_Walkable && !isBlockAtPosition(position + Vector3.up, 1, layerMask));
  48. }
  49. #endregion Public Functions
  50. #region Editor Functions
  51. private void OnDrawGizmos()
  52. {
  53. if (!isSpawnable)
  54. return;
  55. Vector3 DrawPosition = VisualPosition + Vector3.up * 0.4f;
  56. Vector3 Perp = Quaternion.Euler(0, 90, 0) * SpawnDirection.ToVector();
  57. DebugExtensions.DrawCube(DrawPosition, 0.4f, Color.magenta, 0);
  58. //Eyes
  59. Vector3 eyePosition = DrawPosition + SpawnDirection.ToVector() * 0.4f;
  60. DebugExtensions.DrawCube(eyePosition + Perp * 0.2f, 0.1f, Color.magenta, 0);
  61. DebugExtensions.DrawCube(eyePosition - Perp * 0.2f, 0.1f, Color.magenta, 0);
  62. //ears
  63. Vector3 earPosition = DrawPosition + SpawnDirection.ToVector() * 0.2f + Vector3.up * 0.4f;
  64. Vector3 earScale = Quaternion.LookRotation(SpawnDirection.ToVector()) * new Vector3(0.1f, 0.1f, 0.05f);
  65. DebugExtensions.DrawCube(earPosition + Perp * 0.3f, earScale, Color.magenta, 0);
  66. DebugExtensions.DrawCube(earPosition - Perp * 0.3f, earScale, Color.magenta, 0);
  67. }
  68. #endregion Editor Functions
  69. #region Static Functions
  70. /// <summary>
  71. /// Checks if there is a block at supplied position
  72. /// </summary>
  73. /// <param name="position">position to check at</param>
  74. /// <param name="Scale">Scale of block. (should be 1)</param>
  75. /// <param name="layerMask">Layers to check on</param>
  76. /// <param name="hit">Block hit</param>
  77. /// <returns>if a block is at position</returns>
  78. public static bool isBlockAtPosition(Vector3 position, float Scale, LayerMask layerMask, out Block hit)
  79. {
  80. //Turn scale into halfextent and shrink a bit so it doesn't hit bordering blocks
  81. Vector3 halfExtent = Vector3.one * ((Scale - 0.1f) / 2);
  82. //Get every collider which is at position
  83. Collider[] cols = Physics.OverlapBox(position, halfExtent, Quaternion.identity, layerMask);
  84. //Filter colliders for only GameObjects with an Block component
  85. Block[] blocks = cols.Where(p => p.GetComponent<Block>() != null).Select(p => p.GetComponent<Block>()).ToArray();
  86. //Draw cube, for visuals
  87. DebugExtensions.DrawCube(position, halfExtent, Color.cyan, 1, false);
  88. //if didn't hit anyblocks return false
  89. if (blocks.Length == 0)
  90. {
  91. hit = null;
  92. return false;
  93. }
  94. else
  95. {
  96. //else get the closest block to disered position, (in case we hit mulitple blocks)
  97. hit = Utility.minBy(blocks, p => Vector3.Distance(p.transform.position, position));
  98. return true;
  99. }
  100. }
  101. /// <summary>
  102. /// Checks if there is a block at supplied position
  103. /// </summary>
  104. /// <param name="position">position to check at</param>
  105. /// <param name="Scale">Scale of block. (should be 1)</param>
  106. /// <param name="layerMask">Layers to check on</param>
  107. /// <returns>if a block is at position</returns>
  108. public static bool isBlockAtPosition(Vector3 position, float scale, LayerMask layerMask)
  109. {
  110. //Return Overloaded function above
  111. Block hit;
  112. return (isBlockAtPosition(position, scale, layerMask, out hit));
  113. }
  114. #endregion
  115. }