Advertisement
Guest User

Untitled

a guest
Nov 19th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 36.29 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5.  
  6.  
  7. public interface IState
  8. {
  9. void OnEnter();
  10. void Update();
  11. void FixedUpdate();
  12. void OnExit();
  13. }
  14.  
  15. public enum AnimationState
  16. {
  17. Walk = 0,
  18. SpeedMatch = 0,
  19. Idle = 0,
  20. Action = 2,
  21. Dead = 3,
  22. GoldWalk = 4,
  23. }
  24.  
  25. public enum CurrentPath
  26. {
  27. Up,
  28. Mid,
  29. Down
  30. }
  31. public enum Opposition
  32. {
  33. Elfs,
  34. Pieten
  35. }
  36.  
  37. public enum UnitClass
  38. {
  39. Melee,
  40. Ranged,
  41. Caster,
  42. Buff,
  43. Collector,
  44. Base
  45.  
  46. }
  47. public delegate void GotHit(int recievedDamage, int currentHealth);
  48. public class UnitBehaviours : MonoBehaviour
  49. {
  50. public Opposition _Side;
  51. public IState _State;
  52. public AnimationState _AnimationState;
  53. public UnitClass _Class;
  54.  
  55. public Transform _Sprite;
  56. public Animator _Animator;
  57. public CapsuleCollider _Colloder;
  58. public SpriteRenderer _Renderer;
  59.  
  60. public LaneManager _PathChoice;
  61.  
  62. public List<Transform> _Points;
  63. public Transform[] _Path;
  64. public Transform _Destination;
  65. public int _DestinationIndex;
  66. public bool _PathSet, HasGold, _IsBuffed;
  67.  
  68. // BaseStats
  69. public int _BaseHealth, _BaseDamage, _BaseGold;
  70. public float _BaseSpeed, _BaseRange;
  71.  
  72. public Vector3 _BaseRangeOffset;
  73. // currenStats
  74. public int _Health, _Damage, _Gold;
  75. public float _Speed, _Range;
  76. public Vector3 _RangeOffset;
  77. public float _CastTimeInSecondes, _DistanceToProceed;
  78. public int _Cost;
  79.  
  80.  
  81. public event GotHit _GotHit;
  82.  
  83. public UnitBehaviours _Tower;
  84.  
  85. [SerializeField]public ParticleSystem _BuffParticle, _ActionParticle;
  86.  
  87.  
  88. private void OnDrawGizmos()
  89. {
  90.  
  91. _Colloder = GetComponent<CapsuleCollider>();
  92. Gizmos.color = Color.green;
  93. Gizmos.DrawWireSphere(transform.position, _DistanceToProceed);
  94. Gizmos.color = Color.red;
  95. Gizmos.DrawWireSphere((transform.position + (new Vector3(0, _Colloder.height) * 0.5f)) + ( _RangeOffset), _Range);
  96. }
  97. private void Awake()
  98. {
  99. _Health = _BaseHealth;
  100. _Damage = _BaseDamage;
  101. _Gold = _BaseGold;
  102. _Speed = _BaseSpeed;
  103. _Range = _BaseRange;
  104. _RangeOffset = _BaseRangeOffset;
  105.  
  106. _Colloder = GetComponent<CapsuleCollider>();
  107. _Animator = _Sprite.GetComponent<Animator>();
  108. _Renderer = _Sprite.GetComponent<SpriteRenderer>();
  109.  
  110.  
  111. foreach (Transform child in GameObject.Find("WayPoints").transform)
  112. {
  113. _Points.Add(child);
  114. }
  115.  
  116. switch (_Side)
  117. {
  118. case Opposition.Elfs:
  119. _PathChoice = GameObject.Find("LaneChooseElfs").GetComponent<LaneManager>();
  120. _Tower = GameObject.Find("BaseElf").GetComponent<UnitBehaviours>();
  121.  
  122. break;
  123. case Opposition.Pieten:
  124. _Points.Reverse();
  125. _RangeOffset.x = -Mathf.Abs(_RangeOffset.x);
  126. _PathChoice = GameObject.Find("LaneChoosePieten").GetComponent<LaneManager>();
  127. _Tower = GameObject.Find("BasePiet").GetComponent<UnitBehaviours>();
  128. break;
  129. }
  130.  
  131.  
  132. if (_Class == UnitClass.Collector)
  133. {
  134. _Colloder.enabled = false;
  135. _Path = new Transform[]
  136. {
  137. _Points[0],
  138. _Points[2],
  139. };
  140. }
  141. else
  142. {
  143. _Path = new Transform[]
  144. {
  145. _Points[0],
  146. _Points[2],
  147. _Points[5],
  148. _Points[7]
  149. };
  150. }
  151.  
  152.  
  153. _DestinationIndex = 1;
  154. _Destination = _Path[_DestinationIndex];
  155.  
  156. _State = new Walk(this);
  157.  
  158. }
  159. void Start()
  160. {
  161. if (_Class != UnitClass.Base)
  162. {
  163. transform.position = _Path[0].position;
  164. }
  165. }
  166.  
  167. public void LoadScene(int sceneindex)
  168. {
  169. Application.LoadLevel(sceneindex);
  170. }
  171. void FixedUpdate()
  172. {
  173. _State.FixedUpdate();
  174. }
  175. void Update()
  176. {
  177. _State.Update();
  178.  
  179. if (_Class != UnitClass.Collector)
  180. {
  181.  
  182. if (_PathSet == false)
  183. {
  184. switch (_PathChoice._ChosenPath)
  185. {
  186. case ChosenPath.Up:
  187. _Path = new Transform[]
  188. {
  189. _Points[0],
  190. _Points[2],
  191. _Points[1],
  192. _Points[6],
  193. _Points[5],
  194. _Points[7]
  195. };
  196.  
  197. break;
  198. case ChosenPath.Mid:
  199. _Path = new Transform[]
  200. {
  201. _Points[0],
  202. _Points[2],
  203. _Points[5],
  204. _Points[7]
  205. };
  206. break;
  207. case ChosenPath.Down:
  208. _Path = new Transform[]
  209. {
  210. _Points[0],
  211. _Points[2],
  212. _Points[3],
  213. _Points[4],
  214. _Points[5],
  215. _Points[7]
  216. };
  217. break;
  218. }
  219. }
  220. }
  221.  
  222. if (_IsBuffed)
  223. {
  224. _BuffParticle.gameObject.SetActive(true);
  225. }
  226.  
  227. if (_Health <= 0)
  228. {
  229. if (_Class == UnitClass.Base)
  230. {
  231. if (_Side == Opposition.Elfs)
  232. {
  233. LoadScene(4);
  234. }
  235. else if (_Side == Opposition.Pieten)
  236. {
  237. LoadScene(3);
  238. }
  239. }
  240. }
  241. }
  242. public void SetAnimation(AnimationState aninemationstate)
  243. {
  244. _AnimationState = aninemationstate;
  245. }
  246. public void SwitchState(IState state)
  247. {
  248. _State.OnExit();
  249. _State = state;
  250. _State.OnEnter();
  251. _Animator.SetInteger("State", (int)_AnimationState);
  252. }
  253.  
  254.  
  255. public void SubtractHealth(int recievedDamage)
  256. {
  257. _Health -= recievedDamage;
  258. }
  259. public void AddHealth(int recievedHealth)
  260. {
  261. _Health += recievedHealth;
  262. }
  263. public void RecieveHit(int recievedDamage)
  264. {
  265. _State = new HitStun(this, _State, recievedDamage);
  266. _State.OnEnter();
  267. SubtractHealth(recievedDamage);
  268. if (_GotHit != null)
  269. {
  270. _GotHit(recievedDamage, _Health);
  271. }
  272.  
  273. }
  274.  
  275.  
  276. }
  277. public class Idle : IState
  278. {
  279. UnitBehaviours _Unit;
  280. public Idle(UnitBehaviours unit)
  281. {
  282. _Unit = unit;
  283. }
  284. public void OnEnter()
  285. {
  286. _Unit.SetAnimation(AnimationState.Idle);
  287. }
  288.  
  289. public void Update()
  290. {
  291. Collider[] hits = Physics.OverlapSphere((_Unit.transform.position + (new Vector3(0, _Unit._Colloder.height) * 0.5f)) + ( _Unit._RangeOffset), _Unit._Range);
  292.  
  293. if (hits.Length == 0)
  294. {
  295. //_Unit.SwitchState(new Walk(_Unit));
  296. }
  297.  
  298. if (hits.Length == 1)
  299. {
  300. if (hits[0].gameObject == _Unit.gameObject)
  301. {
  302. _Unit.SwitchState(new Walk(_Unit));
  303. }
  304. }
  305.  
  306.  
  307. if (hits.Length > 1)
  308. {
  309. for (int i = 0; i < hits.Length; i++)
  310. {
  311. switch (_Unit._Side)
  312. {
  313. case Opposition.Elfs:
  314. if (hits[i].gameObject.GetComponent<UnitBehaviours>()._Side == Opposition.Pieten)
  315. {
  316. _Unit.SwitchState(new Action(_Unit));
  317. }
  318. break;
  319. case Opposition.Pieten:
  320. if (hits[i].gameObject.GetComponent<UnitBehaviours>()._Side == Opposition.Elfs)
  321. {
  322. _Unit.SwitchState(new Action(_Unit));
  323. }
  324. break;
  325. }
  326. }
  327. }
  328.  
  329. if (_Unit._Health <= 0)
  330. {
  331. _Unit.SwitchState(new Dead(_Unit));
  332. }
  333. }
  334.  
  335. public void FixedUpdate()
  336. {
  337.  
  338. }
  339.  
  340. public void OnExit()
  341. {
  342.  
  343. }
  344. }
  345. public class Walk : IState
  346. {
  347. UnitBehaviours _Unit;
  348. Vector3 _Direction;
  349. float _Speed;
  350. public Walk(UnitBehaviours unit)
  351. {
  352. _Unit = unit;
  353. _Speed = _Unit._Speed;
  354. }
  355. public void OnEnter()
  356. {
  357. _Unit.SetAnimation(AnimationState.Walk);
  358.  
  359. }
  360.  
  361. public void Update()
  362. {
  363. WayPointManager();
  364.  
  365. if (_Unit._Class != UnitClass.Collector)
  366. {
  367. CheckForConflict();
  368. }
  369.  
  370.  
  371. if (_Unit._Health <= 0)
  372. {
  373. _Unit.SwitchState(new Dead(_Unit));
  374. }
  375. _Unit.transform.Translate(_Direction.normalized * _Unit._Speed * Time.deltaTime);
  376. }
  377.  
  378. public void FixedUpdate()
  379. {
  380.  
  381. }
  382. private void WayPointManager()
  383. {
  384.  
  385. if (_Unit._Class == UnitClass.Collector)
  386. {
  387. if (Vector3.Distance(_Unit.transform.position, _Unit._Destination.position) <= _Unit._DistanceToProceed)
  388. {
  389. if (_Unit._DestinationIndex == 0)
  390. {
  391. _Unit._DestinationIndex = 1;
  392. _Unit._Destination = _Unit._Path[_Unit._DestinationIndex];
  393. _Unit._Renderer.flipX ^= true;
  394. }
  395. else if (_Unit._DestinationIndex == 1)
  396. {
  397. _Unit._DestinationIndex = 0;
  398. _Unit._Destination = _Unit._Path[_Unit._DestinationIndex];
  399. _Unit._Renderer.flipX ^= true;
  400. _Unit.SwitchState(new Action(_Unit));
  401. }
  402. }
  403. }
  404. else
  405. {
  406. if (Vector3.Distance(_Unit.transform.position, _Unit._Destination.position) <= _Unit._DistanceToProceed)
  407. {
  408. if (_Unit._DestinationIndex != _Unit._Path.Length - 1)
  409. {
  410. _Unit._DestinationIndex++;
  411. _Unit._Destination = _Unit._Path[_Unit._DestinationIndex];
  412. if (_Unit._DestinationIndex == 2)
  413. {
  414. _Unit._PathSet = true;
  415. }
  416. }
  417. }
  418. }
  419. _Direction = (_Unit._Destination.position - _Unit.transform.position);
  420. }
  421.  
  422. private void CheckForConflict()
  423. {
  424. Collider[] hits = Physics.OverlapSphere((_Unit.transform.position + (new Vector3(0, _Unit._Colloder.height) * 0.5f)) + ( _Unit._RangeOffset), _Unit._Range);
  425.  
  426.  
  427. for (int i = 0; i < hits.Length; i++)
  428. {
  429. switch (_Unit._Side)
  430. {
  431. case Opposition.Elfs:
  432. if (hits[i].gameObject.GetComponent<UnitBehaviours>()._Side == Opposition.Pieten && hits[i].GetComponent<UnitBehaviours>()._AnimationState != AnimationState.Dead)
  433. {
  434. _Unit.SwitchState(new Action(_Unit));
  435. }
  436. if (hits[i].gameObject.GetComponent<UnitBehaviours>()._Class != UnitClass.Collector)
  437. {
  438. if (hits[i].gameObject.GetComponent<UnitBehaviours>()._Side == Opposition.Elfs && hits[i].GetComponent<UnitBehaviours>()._AnimationState != AnimationState.Dead && hits[i].gameObject != _Unit.gameObject && hits[i].gameObject.GetComponent<UnitBehaviours>()._AnimationState != AnimationState.Walk && hits[i].gameObject.GetComponent<UnitBehaviours>()._AnimationState != AnimationState.SpeedMatch)
  439. {
  440. if (_Unit._Class == UnitClass.Caster && hits[i].gameObject.GetComponent<UnitBehaviours>()._AnimationState == AnimationState.Action)
  441. {
  442. _Unit.SwitchState(new Action(_Unit));
  443. }
  444. else if (_Unit._Class == UnitClass.Ranged && hits[i].gameObject.GetComponent<UnitBehaviours>()._AnimationState == AnimationState.Action)
  445. {
  446. Collider[] hits2 = Physics.OverlapSphere((hits[i].transform.position + (new Vector3(0, _Unit._Colloder.height) * 0.5f)) + ( _Unit._RangeOffset), _Unit._Range);
  447. bool attack = false;
  448.  
  449. for (int u = 0; u < hits2.Length; u++)
  450. {
  451.  
  452. if (hits2[u] != hits[i] && hits2[u].gameObject.GetComponent<UnitBehaviours>()._Side == Opposition.Pieten && hits[i].gameObject.GetComponent<UnitBehaviours>()._Class != UnitClass.Buff && attack == false)
  453. {
  454. attack = true;
  455.  
  456.  
  457. }
  458. else if (attack == false)
  459. {
  460. attack = false;
  461.  
  462. }
  463. }
  464.  
  465. if (attack == true)
  466. {
  467. _Unit.SwitchState(new Action(_Unit));
  468. }
  469. else
  470. {
  471. _Unit.SwitchState(new Idle(_Unit));
  472. }
  473. }
  474. else
  475. {
  476. _Unit.SwitchState(new Idle(_Unit));
  477. }
  478. }
  479. else if (hits[i].gameObject.GetComponent<UnitBehaviours>()._Side == Opposition.Elfs && hits[i].GetComponent<UnitBehaviours>()._AnimationState != AnimationState.Dead && hits[i].gameObject != _Unit.gameObject && hits[i].gameObject.GetComponent<UnitBehaviours>()._Speed < _Unit._Speed && hits[i].gameObject.GetComponent<UnitBehaviours>()._AnimationState == AnimationState.Walk)
  480. {
  481. _Unit.SwitchState(new SpeedMatch(_Unit));
  482. }
  483. else if (hits[i].gameObject.GetComponent<UnitBehaviours>()._Side == Opposition.Elfs && hits[i].GetComponent<UnitBehaviours>()._AnimationState != AnimationState.Dead && hits[i].gameObject != _Unit.gameObject && hits[i].gameObject.GetComponent<UnitBehaviours>()._Speed < _Unit._Speed && hits[i].gameObject.GetComponent<UnitBehaviours>()._AnimationState == AnimationState.SpeedMatch)
  484. {
  485. _Unit.SwitchState(new SpeedMatch(_Unit));
  486. }
  487. else if (hits[i].gameObject.GetComponent<UnitBehaviours>()._Side == Opposition.Elfs && hits[i].GetComponent<UnitBehaviours>()._AnimationState != AnimationState.Dead && hits[i].gameObject != _Unit.gameObject && hits[i].gameObject.GetComponent<UnitBehaviours>()._Speed >= _Unit._Speed && hits[i].gameObject.GetComponent<UnitBehaviours>()._AnimationState == AnimationState.SpeedMatch)
  488. {
  489. _Unit.SwitchState(new Idle(_Unit));
  490. }
  491.  
  492. }
  493.  
  494. break;
  495. case Opposition.Pieten:
  496. if (hits[i].gameObject.GetComponent<UnitBehaviours>()._Side == Opposition.Elfs && hits[i].GetComponent<UnitBehaviours>()._AnimationState != AnimationState.Dead)
  497. {
  498. _Unit.SwitchState(new Action(_Unit));
  499. }
  500.  
  501. if (hits[i].gameObject.GetComponent<UnitBehaviours>()._Class != UnitClass.Collector)
  502. {
  503. if (hits[i].gameObject.GetComponent<UnitBehaviours>()._Side == Opposition.Pieten && hits[i].GetComponent<UnitBehaviours>()._AnimationState != AnimationState.Dead && hits[i].gameObject != _Unit.gameObject && hits[i].gameObject.GetComponent<UnitBehaviours>()._AnimationState != AnimationState.Walk && hits[i].gameObject.GetComponent<UnitBehaviours>()._AnimationState != AnimationState.SpeedMatch)
  504. {
  505. if (_Unit._Class == UnitClass.Caster && hits[i].gameObject.GetComponent<UnitBehaviours>()._AnimationState == AnimationState.Action)
  506. {
  507. _Unit.SwitchState(new Action(_Unit));
  508. }
  509. else if (_Unit._Class == UnitClass.Ranged && hits[i].gameObject.GetComponent<UnitBehaviours>()._AnimationState == AnimationState.Action)
  510. {
  511. Collider[] hits2 = Physics.OverlapSphere((hits[i].transform.position + (new Vector3(0, _Unit._Colloder.height) * 0.5f)) + ( _Unit._RangeOffset), _Unit._Range);
  512. bool attack = false;
  513.  
  514. for (int u = 0; u < hits2.Length; u++)
  515. {
  516.  
  517. if (hits2[u] != hits[i] && hits2[u].gameObject.GetComponent<UnitBehaviours>()._Side == Opposition.Elfs && hits[i].gameObject.GetComponent<UnitBehaviours>()._Class != UnitClass.Buff && attack == false )
  518. {
  519. attack = true;
  520.  
  521.  
  522. }
  523. else if (attack == false)
  524. {
  525. attack = false;
  526.  
  527. }
  528. }
  529.  
  530. if (attack == true)
  531. {
  532. _Unit.SwitchState(new Action(_Unit));
  533. }
  534. else
  535. {
  536. _Unit.SwitchState(new Idle(_Unit));
  537. }
  538. }
  539. else
  540. {
  541. _Unit.SwitchState(new Idle(_Unit));
  542. }
  543. }
  544. else if (hits[i].gameObject.GetComponent<UnitBehaviours>()._Side == Opposition.Pieten && hits[i].GetComponent<UnitBehaviours>()._AnimationState != AnimationState.Dead && hits[i].gameObject != _Unit.gameObject && hits[i].gameObject.GetComponent<UnitBehaviours>()._Speed < _Unit._Speed && hits[i].gameObject.GetComponent<UnitBehaviours>()._AnimationState == AnimationState.Walk)
  545. {
  546. _Unit.SwitchState(new SpeedMatch(_Unit));
  547. }
  548. else if (hits[i].gameObject.GetComponent<UnitBehaviours>()._Side == Opposition.Pieten && hits[i].GetComponent<UnitBehaviours>()._AnimationState != AnimationState.Dead && hits[i].gameObject != _Unit.gameObject && hits[i].gameObject.GetComponent<UnitBehaviours>()._Speed < _Unit._Speed && hits[i].gameObject.GetComponent<UnitBehaviours>()._AnimationState == AnimationState.SpeedMatch)
  549. {
  550. _Unit.SwitchState(new SpeedMatch(_Unit));
  551. }
  552. else if (hits[i].gameObject.GetComponent<UnitBehaviours>()._Side == Opposition.Pieten && hits[i].GetComponent<UnitBehaviours>()._AnimationState != AnimationState.Dead && hits[i].gameObject != _Unit.gameObject && hits[i].gameObject.GetComponent<UnitBehaviours>()._Speed >= _Unit._Speed && hits[i].gameObject.GetComponent<UnitBehaviours>()._AnimationState == AnimationState.SpeedMatch)
  553. {
  554. _Unit.SwitchState(new Idle(_Unit));
  555. }
  556.  
  557. }
  558. break;
  559. }
  560. }
  561.  
  562. }
  563. public void OnExit()
  564. {
  565. _Unit._Speed = _Speed;
  566. }
  567. }
  568.  
  569. public class SpeedMatch : IState
  570. {
  571. UnitBehaviours _Unit;
  572. Vector3 _Direction;
  573. float _Speed;
  574. public SpeedMatch(UnitBehaviours unit)
  575. {
  576. _Unit = unit;
  577. _Speed = _Unit._Speed;
  578. }
  579. public void OnEnter()
  580. {
  581. _Unit.SetAnimation(AnimationState.SpeedMatch);
  582. }
  583.  
  584. public void Update()
  585. {
  586. Collider[] hits = Physics.OverlapSphere((_Unit.transform.position + (new Vector3(0, _Unit._Colloder.height) * 0.5f)) + ( _Unit._RangeOffset), _Unit._Range);
  587. if (hits.Length == 1)
  588. {
  589. if (hits[0].gameObject == _Unit.gameObject)
  590. {
  591. _Unit.SwitchState(new Walk(_Unit));
  592. }
  593.  
  594. }
  595. WayPointManager();
  596. if (_Unit._Class != UnitClass.Collector)
  597. {
  598. CheckForConflict();
  599. }
  600. if (_Unit._Health <= 0)
  601. {
  602. _Unit.SwitchState(new Dead(_Unit));
  603. }
  604. }
  605.  
  606. public void FixedUpdate()
  607. {
  608. _Unit.transform.Translate(_Direction.normalized * _Unit._Speed * Time.fixedDeltaTime);
  609. }
  610. private void WayPointManager()
  611. {
  612. if (Vector3.Distance(_Unit.transform.position, _Unit._Destination.position) <= _Unit._DistanceToProceed)
  613. {
  614. if (_Unit._DestinationIndex != _Unit._Path.Length - 1)
  615. {
  616. _Unit._DestinationIndex++;
  617. _Unit._Destination = _Unit._Path[_Unit._DestinationIndex];
  618. if (_Unit._DestinationIndex == 2)
  619. {
  620. _Unit._PathSet = true;
  621. }
  622. }
  623. }
  624. _Direction = (_Unit._Destination.position - _Unit.transform.position);
  625. }
  626.  
  627. private void CheckForConflict()
  628. {
  629. Collider[] hits = Physics.OverlapSphere((_Unit.transform.position + (new Vector3(0, _Unit._Colloder.height) * 0.5f)) + ( _Unit._RangeOffset), _Unit._Range);
  630. if (hits.Length == 1)
  631. {
  632. if (hits[0].gameObject == _Unit.gameObject)
  633. {
  634. _Unit.SwitchState(new Walk(_Unit));
  635. }
  636.  
  637. }
  638. for (int i = 0; i < hits.Length; i++)
  639. {
  640.  
  641.  
  642. switch (_Unit._Side)
  643. {
  644. case Opposition.Elfs:
  645. if (hits[i].gameObject.GetComponent<UnitBehaviours>()._Side == Opposition.Pieten && hits[i].GetComponent<UnitBehaviours>()._AnimationState != AnimationState.Dead)
  646. {
  647. _Unit.SwitchState(new Action(_Unit));
  648. }
  649.  
  650. if (hits[i].gameObject.GetComponent<UnitBehaviours>()._Class != UnitClass.Collector)
  651. {
  652. if (hits[i].gameObject.GetComponent<UnitBehaviours>()._Side == Opposition.Elfs && hits[i].GetComponent<UnitBehaviours>()._AnimationState != AnimationState.Dead && hits[i].gameObject != _Unit.gameObject && hits[i].gameObject.GetComponent<UnitBehaviours>()._AnimationState != AnimationState.Walk && hits[i].gameObject.GetComponent<UnitBehaviours>()._AnimationState != AnimationState.SpeedMatch)
  653. {
  654. if (_Unit._Class == UnitClass.Caster && hits[i].gameObject.GetComponent<UnitBehaviours>()._AnimationState == AnimationState.Action)
  655. {
  656. _Unit.SwitchState(new Action(_Unit));
  657. }
  658. else if (_Unit._Class == UnitClass.Ranged && hits[i].gameObject.GetComponent<UnitBehaviours>()._AnimationState == AnimationState.Action)
  659. {
  660. Collider[] hits2 = Physics.OverlapSphere((hits[i].transform.position + (new Vector3(0, _Unit._Colloder.height) * 0.5f)) + ( _Unit._RangeOffset), _Unit._Range);
  661.  
  662. for (int u = 0; u < hits2.Length; u++)
  663. {
  664.  
  665. if (hits2[u] != hits[i] && hits2[u].gameObject.GetComponent<UnitBehaviours>()._Side == Opposition.Pieten && hits[i].gameObject.GetComponent<UnitBehaviours>()._Class != UnitClass.Buff)
  666. {
  667. _Unit.SwitchState(new Action(_Unit));
  668.  
  669. break;
  670. }
  671. else
  672. {
  673. _Unit.SwitchState(new Idle(_Unit));
  674. break;
  675. }
  676. }
  677. }
  678. else
  679. {
  680. _Unit.SwitchState(new Idle(_Unit));
  681. }
  682.  
  683. }
  684. if (hits[i].gameObject.GetComponent<UnitBehaviours>()._Side == Opposition.Elfs && hits[i].GetComponent<UnitBehaviours>()._AnimationState != AnimationState.Dead && hits[i].gameObject != _Unit.gameObject && hits[i].gameObject.GetComponent<UnitBehaviours>()._Speed < _Unit._Speed && hits[i].gameObject.GetComponent<UnitBehaviours>()._AnimationState == AnimationState.Walk)
  685. {
  686. _Unit._Speed = hits[i].gameObject.GetComponent<UnitBehaviours>()._Speed;
  687. }
  688. if (hits[i].gameObject.GetComponent<UnitBehaviours>()._Side == Opposition.Elfs && hits[i].GetComponent<UnitBehaviours>()._AnimationState != AnimationState.Dead && hits[i].gameObject != _Unit.gameObject && hits[i].gameObject.GetComponent<UnitBehaviours>()._Speed == _Unit._Speed && hits[i].gameObject.GetComponent<UnitBehaviours>()._AnimationState == AnimationState.Walk)
  689. {
  690. _Unit.SwitchState(new Idle(_Unit));
  691. }
  692.  
  693. }
  694. break;
  695. case Opposition.Pieten:
  696. if (hits[i].gameObject.GetComponent<UnitBehaviours>()._Side == Opposition.Elfs && hits[i].GetComponent<UnitBehaviours>()._AnimationState != AnimationState.Dead)
  697. {
  698. _Unit.SwitchState(new Action(_Unit));
  699. }
  700.  
  701. if (hits[i].gameObject.GetComponent<UnitBehaviours>()._Class != UnitClass.Collector)
  702. {
  703.  
  704.  
  705. if (hits[i].gameObject.GetComponent<UnitBehaviours>()._Side == Opposition.Pieten && hits[i].GetComponent<UnitBehaviours>()._AnimationState != AnimationState.Dead && hits[i].gameObject != _Unit.gameObject && hits[i].gameObject.GetComponent<UnitBehaviours>()._AnimationState != AnimationState.Walk && hits[i].gameObject.GetComponent<UnitBehaviours>()._AnimationState != AnimationState.SpeedMatch)
  706. {
  707. if (_Unit._Class == UnitClass.Caster && hits[i].gameObject.GetComponent<UnitBehaviours>()._AnimationState == AnimationState.Action)
  708. {
  709. _Unit.SwitchState(new Action(_Unit));
  710. }
  711. else if (_Unit._Class == UnitClass.Ranged && hits[i].gameObject.GetComponent<UnitBehaviours>()._AnimationState == AnimationState.Action)
  712. {
  713. Collider[] hits2 = Physics.OverlapSphere((hits[i].transform.position + (new Vector3(0, _Unit._Colloder.height) * 0.5f)) + ( _Unit._RangeOffset), _Unit._Range);
  714.  
  715. for (int u = 0; u < hits2.Length; u++)
  716. {
  717.  
  718. if (hits2[u] != hits[i] && hits2[u].gameObject.GetComponent<UnitBehaviours>()._Side == Opposition.Elfs && hits[i].gameObject.GetComponent<UnitBehaviours>()._Class != UnitClass.Buff)
  719. {
  720. _Unit.SwitchState(new Action(_Unit));
  721.  
  722. break;
  723. }
  724. else
  725. {
  726. _Unit.SwitchState(new Idle(_Unit));
  727. break;
  728. }
  729. }
  730. }
  731. else
  732. {
  733. _Unit.SwitchState(new Idle(_Unit));
  734. }
  735.  
  736. }
  737. if (hits[i].gameObject.GetComponent<UnitBehaviours>()._Side == Opposition.Pieten && hits[i].GetComponent<UnitBehaviours>()._AnimationState != AnimationState.Dead && hits[i].gameObject != _Unit.gameObject && hits[i].gameObject.GetComponent<UnitBehaviours>()._Speed < _Unit._Speed && hits[i].gameObject.GetComponent<UnitBehaviours>()._AnimationState == AnimationState.Walk)
  738. {
  739. _Unit._Speed = hits[i].gameObject.GetComponent<UnitBehaviours>()._Speed;
  740. }
  741. if (hits[i].gameObject.GetComponent<UnitBehaviours>()._Side == Opposition.Pieten && hits[i].GetComponent<UnitBehaviours>()._AnimationState != AnimationState.Dead && hits[i].gameObject != _Unit.gameObject && hits[i].gameObject.GetComponent<UnitBehaviours>()._Speed == _Unit._Speed && hits[i].gameObject.GetComponent<UnitBehaviours>()._AnimationState == AnimationState.Walk)
  742. {
  743. _Unit.SwitchState(new Idle(_Unit));
  744. }
  745.  
  746. }
  747.  
  748. break;
  749. }
  750. }
  751.  
  752. }
  753. public void OnExit()
  754. {
  755. _Unit._Speed = _Speed;
  756. }
  757. }
  758. public class GoldWalk : IState
  759. {
  760. UnitBehaviours _Unit;
  761. Vector3 _Direction;
  762. float _Speed;
  763. public GoldWalk(UnitBehaviours unit)
  764. {
  765. _Unit = unit;
  766. _Speed = _Unit._Speed;
  767. }
  768. public void OnEnter()
  769. {
  770. _Unit.SetAnimation(AnimationState.GoldWalk);
  771.  
  772. }
  773.  
  774. public void Update()
  775. {
  776. WayPointManager();
  777.  
  778.  
  779. if (_Unit._Health <= 0)
  780. {
  781. _Unit.SwitchState(new Dead(_Unit));
  782. }
  783. _Unit.transform.Translate(_Direction.normalized * _Unit._Speed * Time.deltaTime);
  784. }
  785.  
  786. public void FixedUpdate()
  787. {
  788.  
  789. }
  790. private void WayPointManager()
  791. {
  792.  
  793.  
  794. if (Vector3.Distance(_Unit.transform.position, _Unit._Destination.position) <= _Unit._DistanceToProceed)
  795. {
  796. if (_Unit._DestinationIndex == 0)
  797. {
  798. _Unit._DestinationIndex = 1;
  799. _Unit._Destination = _Unit._Path[_Unit._DestinationIndex];
  800. _Unit._Renderer.flipX ^= true;
  801. _Unit._Tower._Gold += _Unit._Gold;
  802. _Unit.SwitchState(new Walk(_Unit));
  803.  
  804. }
  805. else if (_Unit._DestinationIndex == 1)
  806. {
  807. _Unit._DestinationIndex = 0;
  808. _Unit._Destination = _Unit._Path[_Unit._DestinationIndex];
  809. _Unit._Renderer.flipX ^= true;
  810. _Unit.SwitchState(new Action(_Unit));
  811. }
  812. }
  813.  
  814.  
  815. _Direction = (_Unit._Destination.position - _Unit.transform.position);
  816. }
  817.  
  818.  
  819. public void OnExit()
  820. {
  821. _Unit._Speed = _Speed;
  822. }
  823. }
  824. public class Action : IState
  825. {
  826. UnitBehaviours _Unit;
  827. float _Height, _Radius;
  828. public Action(UnitBehaviours unit)
  829. {
  830. _Unit = unit;
  831. }
  832. public void OnEnter()
  833. {
  834. _Unit.SetAnimation(AnimationState.Action);
  835. if (_Unit._Class == UnitClass.Collector)
  836. {
  837. _Radius = _Unit._Colloder.radius;
  838. _Height = _Unit._Colloder.height;
  839. _Unit._Colloder.radius = 0;
  840. _Unit._Colloder.height = 0;
  841. }
  842. }
  843.  
  844. public void Update()
  845. {
  846. if (_Unit._Class != UnitClass.Collector)
  847. {
  848. Collider[] hits = Physics.OverlapSphere((_Unit.transform.position + (new Vector3(0, _Unit._Colloder.height) * 0.5f)) + ( _Unit._RangeOffset), _Unit._Range);
  849. if (hits.Length == 0)
  850. {
  851. _Unit.SwitchState(new Walk(_Unit));
  852. }
  853. if (hits.Length == 1)
  854. {
  855. if (hits[0].gameObject == _Unit.gameObject)
  856. {
  857. _Unit.SwitchState(new Walk(_Unit));
  858. }
  859.  
  860. }
  861.  
  862. if (hits.Length > 1)
  863. {
  864. for (int i = 0; i < hits.Length; i++)
  865. {
  866.  
  867. if (hits[i].GetComponent<UnitBehaviours>()._AnimationState == AnimationState.Dead)
  868. {
  869. try
  870. {
  871. if (hits.Length > i)
  872. {
  873. if (hits[i + 1].GetComponent<UnitBehaviours>()._AnimationState == AnimationState.Dead && _Unit._AnimationState != AnimationState.Action)
  874. {
  875. _Unit.SwitchState(new Walk(_Unit));
  876. }
  877. }
  878. }
  879. catch
  880. {
  881.  
  882. }
  883.  
  884. }
  885. }
  886.  
  887. if (_Unit._Health <= 0)
  888. {
  889. _Unit.SwitchState(new Dead(_Unit));
  890. }
  891.  
  892. }
  893. }
  894.  
  895. }
  896.  
  897. public void FixedUpdate()
  898. {
  899.  
  900. }
  901.  
  902. public void OnExit()
  903. {
  904. if (_Unit._ActionParticle != null)
  905. {
  906. _Unit._ActionParticle.Stop();
  907. }
  908. if (_Unit._Class == UnitClass.Collector)
  909. {
  910. _Unit._Colloder.radius = _Radius;
  911. _Unit._Colloder.height = _Height;
  912. }
  913. }
  914. }
  915.  
  916. public class HitStun : IState
  917. {
  918. UnitBehaviours _Unit;
  919. IState _PreviousState;
  920. float _TimeInHitLag;
  921.  
  922. Vector2 _Preposition;
  923.  
  924. int _Damage;
  925. public HitStun(UnitBehaviours unit, IState previousState, int damage)
  926. {
  927. _Unit = unit;
  928. _PreviousState = previousState;
  929. _Damage = damage;
  930. _TimeInHitLag = _Damage * Time.deltaTime * 1.6f;
  931.  
  932. }
  933. public void OnEnter()
  934. {
  935. _Unit._Animator.speed = 0;
  936. _Unit._Renderer.color = Color.red;
  937. _Preposition = _Unit._Sprite.position;
  938.  
  939. }
  940.  
  941. public void Update()
  942. {
  943.  
  944. _TimeInHitLag -= Time.deltaTime;
  945. _Unit._Sprite.position = _Preposition + (Random.insideUnitCircle * _Damage * 0.05f);
  946.  
  947.  
  948.  
  949. if (_TimeInHitLag <= 0)
  950. {
  951. _Unit.SwitchState(_PreviousState);
  952. }
  953. }
  954.  
  955. public void FixedUpdate()
  956. {
  957.  
  958. }
  959.  
  960. public void OnExit()
  961. {
  962. _Unit._Renderer.color = Color.white;
  963. _Unit._Animator.speed = 1;
  964.  
  965. _Unit._Sprite.position = _Preposition;
  966. }
  967. }
  968.  
  969. public class HitLag : IState
  970. {
  971. UnitBehaviours _Unit;
  972. IState _PreviousState;
  973. float _TimeInHitLag;
  974. Vector2 _Preposition;
  975. int _Damage;
  976.  
  977. public HitLag(UnitBehaviours unit, IState previousState, int damage)
  978. {
  979. _Unit = unit;
  980. _PreviousState = previousState;
  981. _Damage = damage;
  982. _TimeInHitLag = _Damage * Time.deltaTime * 1.6f;
  983. _Preposition = _Unit.transform.position;
  984. }
  985. public void OnEnter()
  986. {
  987. _Unit._Animator.speed = 0;
  988.  
  989. }
  990.  
  991. public void Update()
  992. {
  993. _TimeInHitLag -= Time.deltaTime;
  994.  
  995. if (_TimeInHitLag <= 0)
  996. {
  997. _Unit.SwitchState(_PreviousState);
  998. }
  999. }
  1000.  
  1001. public void FixedUpdate()
  1002. {
  1003.  
  1004. }
  1005.  
  1006. public void OnExit()
  1007. {
  1008. _Unit._Animator.speed = 1;
  1009.  
  1010. _Unit.transform.position = _Preposition;
  1011. }
  1012. }
  1013.  
  1014. public class Dead : IState
  1015. {
  1016. UnitBehaviours _Unit;
  1017.  
  1018. public Dead(UnitBehaviours unit)
  1019. {
  1020. _Unit = unit;
  1021.  
  1022. }
  1023. public void OnEnter()
  1024. {
  1025. _Unit._Colloder.enabled = false;
  1026. _Unit._Renderer.color = Color.white;
  1027. _Unit.SetAnimation(AnimationState.Dead);
  1028. }
  1029.  
  1030. public void Update()
  1031. {
  1032.  
  1033. }
  1034.  
  1035. public void FixedUpdate()
  1036. {
  1037.  
  1038. }
  1039.  
  1040. public void OnExit()
  1041. {
  1042.  
  1043. }
  1044. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement