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.

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