Advertisement
Crazist

CoinInUnits

Aug 18th, 2022
782
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.46 KB | None | 0 0
  1. namespace GamePlay.CoinsInUnits
  2. {
  3.     [RequireComponent(typeof(NavMeshAgent))]
  4.     public class CoinInUnits : IUpdate
  5.     {
  6.         public bool IsGoingForACoin { get; private set; }
  7.        
  8.         private CollisionComponent collisionComponent;
  9.         private int coins;
  10.         private int maxCoins;
  11.         private Pools CoinPool;
  12.         private GameObject prefab;
  13.         private NavMeshAgent navMesh;
  14.         private IJob State;
  15.         public CoinInUnits(CollisionComponent _collisionComponent, int _coins, int _maxCoins, Pools _CoinPool, GameObject _prefab, IJob _State)
  16.         {
  17.             State = _State;
  18.             prefab = _prefab;
  19.             navMesh = prefab.GetComponent<NavMeshAgent>();
  20.             CoinPool = _CoinPool;
  21.             collisionComponent = _collisionComponent;
  22.             coins = _coins;
  23.             maxCoins = _maxCoins;
  24.         }
  25.  
  26.         private GameObject CheckIfThereIsAnyCoinClose()
  27.         {
  28.             var coins = CoinPool.GetEngagedElements();
  29.             if(coins != null)
  30.             {
  31.                 foreach (var coin in coins)
  32.                 {
  33.                     Vector3 difference = new Vector3(
  34.                     coin.transform.position.x - prefab.transform.position.x,
  35.                     coin.transform.position.y - prefab.transform.position.y,
  36.                     coin.transform.position.z - prefab.transform.position.z);
  37.  
  38.                     double distance = Math.Sqrt(
  39.                     Math.Pow(difference.x, 2d) +
  40.                     Math.Pow(difference.y, 2d) +
  41.                     Math.Pow(difference.z, 2d));
  42.  
  43.                     if (distance < 12d)
  44.                     {
  45.                         return coin;
  46.                     }
  47.                 }
  48.             }
  49.            return null;
  50.         }
  51.  
  52.         private void FindTheWay(GameObject coin)
  53.         {
  54.             if (coin != null && coin.activeInHierarchy)
  55.             {
  56.                 navMesh.SetDestination(coin.transform.position);
  57.             }
  58.         }
  59.         public void DropCoin()
  60.         {
  61.             if (coins > 0)
  62.             {
  63.                 coins--;
  64.             }
  65.         }
  66.         public void AddCoin()
  67.         {
  68.             coins++;
  69.             if(coins == 1)
  70.             {
  71.                 State = new CitizenState();
  72.             }
  73.         }
  74.         public void OnUpdate()
  75.         {
  76.             if(coins <= maxCoins)
  77.             FindTheWay(CheckIfThereIsAnyCoinClose());
  78.         }
  79.     }
  80. }
  81.  
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement