Advertisement
JohnnyTurbo

Bloons TD Tower Placement Controller

Nov 7th, 2020
2,957
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.83 KB | None | 0 0
  1. using TMG.BloonsTD.Stats;
  2. using TMG.BloonsTD.Helpers;
  3. using UnityEngine;
  4.  
  5. namespace TMG.BloonsTD.Controllers
  6. {
  7.     public class TowerPlacementController : MonoBehaviour
  8.     {
  9.         private TowerProperties _towerProperties;
  10.         private CircleCollider2D _collider;
  11.         private LayerMask _towerPlacementMask;
  12.         private bool _fullyOffPath;
  13.         private bool _partiallyOnPath;
  14.         private bool _outOfBounds;
  15.         private bool _overlappingTower;
  16.         private Vector3[] _edgePoints;
  17.         private float _colliderRadius;
  18.  
  19.         public bool IsValidPlacementPosition
  20.         {
  21.             get
  22.             {
  23.                 if (_outOfBounds) { return false; }
  24.                 if (_overlappingTower) { return false; }
  25.                 if (_towerProperties.CanBePlacedOffPath && _fullyOffPath) { return true; }
  26.                 if (_towerProperties.CanBePlacedOnPath && IsFullyOnPath) { return true; }
  27.                 return false;
  28.             }
  29.         }
  30.  
  31.         private bool IsFullyOnPath => _partiallyOnPath && CheckIfFullyOnPath();
  32.  
  33.         public TowerProperties TowerProperties
  34.         {
  35.             get => _towerProperties;
  36.             set => _towerProperties = value;
  37.         }
  38.  
  39.         private void Awake()
  40.         {
  41.             _collider = GetComponent<CircleCollider2D>();
  42.             if (_collider == null)
  43.             {
  44.                 _collider = gameObject.AddComponent<CircleCollider2D>();
  45.             }
  46.         }
  47.  
  48.         private void Start()
  49.         {
  50.             _colliderRadius = _towerProperties.ColliderRadius;
  51.             _collider.radius = _colliderRadius;
  52.             _fullyOffPath = true;
  53.             _outOfBounds = true;
  54.             SetupEdgePoints();
  55.         }
  56.  
  57.         private void SetupEdgePoints()
  58.         {
  59.             _edgePoints = new Vector3[9];
  60.             _edgePoints[0] = Vector3.zero;
  61.             for (int i = 1; i < _edgePoints.Length; i++)
  62.             {
  63.                 var degree = (i - 1) * 45f;
  64.                 var x = _colliderRadius * Mathf.Cos(degree * Mathf.Deg2Rad);
  65.                 var y = _colliderRadius * Mathf.Sin(degree * Mathf.Deg2Rad);
  66.                 _edgePoints[i] = new Vector3(x, y);
  67.             }
  68.         }
  69.  
  70.         private bool CheckIfFullyOnPath()
  71.         {
  72.             foreach (var edgePoint in _edgePoints)
  73.             {
  74.                 Vector3 pointToCheck = transform.position + edgePoint;
  75.  
  76.                 Collider2D pathCollider = Physics2D.OverlapPoint(pointToCheck, 1 << BloonsReferences.PathLayer);
  77.  
  78.                 if (pathCollider == null)
  79.                 {
  80.                     return false;
  81.                 }
  82.             }
  83.             return true;
  84.         }
  85.  
  86.         private void OnTriggerEnter2D(Collider2D other)
  87.         {
  88.             if (other.gameObject.layer.Equals(BloonsReferences.PathLayer))
  89.             {
  90.                 _partiallyOnPath = true;
  91.                 _fullyOffPath = false;
  92.             }
  93.  
  94.             if (other.gameObject.layer.Equals(BloonsReferences.OutOfBoundsLayer))
  95.             {
  96.                 _outOfBounds = true;
  97.             }
  98.  
  99.             if (other.gameObject.layer.Equals(BloonsReferences.TowerLayer))
  100.             {
  101.                 _overlappingTower = true;
  102.             }
  103.         }
  104.  
  105.         private void OnTriggerExit2D(Collider2D other)
  106.         {
  107.             if (other.gameObject.layer.Equals(BloonsReferences.PathLayer))
  108.             {
  109.                 _partiallyOnPath = false;
  110.                 _fullyOffPath = true;
  111.             }
  112.            
  113.             if (other.gameObject.layer.Equals(BloonsReferences.OutOfBoundsLayer))
  114.             {
  115.                 _outOfBounds = false;
  116.             }
  117.            
  118.             if (other.gameObject.layer.Equals(BloonsReferences.TowerLayer))
  119.             {
  120.                 _overlappingTower = false;
  121.             }
  122.         }
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement