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.

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