Advertisement
Guest User

Code

a guest
Oct 15th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.11 KB | None | 0 0
  1. namespace Mattdevelops.Bow.Unit
  2. {
  3. public interface IDestructible
  4. {
  5. void Destruct();
  6. }
  7. }
  8.  
  9.  
  10. using UnityEngine;
  11.  
  12. namespace Mattdevelops.Bow.UnitSystem
  13. {
  14. public interface IUnitBehaviour : IUnitControllerProvider
  15. {
  16. void OnUnitDestroyed();
  17. void OnInitialize();
  18. Transform Transform { get; }
  19. }
  20. }
  21.  
  22. using Mattdevelops.Bow.Unit;
  23.  
  24. namespace Mattdevelops.Bow.UnitSystem
  25. {
  26. public interface IUnitController : IDestructible
  27. {
  28. IUnitPart[] Parts { get; }
  29. IUnitBehaviour UnitBehaviour { get; }
  30. void AddPart(IUnitPart part);
  31. void RemovePart(IUnitPart part);
  32. void RemovePart(int id);
  33. void Initialize();
  34. void Reset();
  35. }
  36. }
  37. namespace Mattdevelops.Bow.UnitSystem
  38. {
  39. public interface IUnitControllerProvider
  40. {
  41. IUnitController UnitController { get; }
  42. }
  43. }
  44. using Mattdevelops.Bow.Unit;
  45.  
  46. namespace Mattdevelops.Bow.UnitSystem
  47. {
  48. public interface IUnitPart : IDestructible
  49. {
  50. int Id { get; }
  51. bool IsEssential { get; set; }
  52. void AddDependency(IUnitPart part);
  53. IUnitController UnitController { get; }
  54. IUnitPartBehaviour UnitPartBehaviour { get; }
  55. }
  56. }
  57.  
  58. namespace Mattdevelops.Bow.UnitSystem
  59. {
  60. public interface IUnitPartBehaviour : IUnitPartProvider
  61. {
  62. void OnInitializePart();
  63. void OnDestruct();
  64. }
  65. }
  66.  
  67. using System.Collections.Generic;
  68. using System.Linq;
  69. using DarkTonic.PoolBoss;
  70. using Destructible2D;
  71. using Mattdevelops.Bow.UnitSystem.UnitDestructProcessor;
  72. using UnityEngine;
  73. using UnityEngine.Events;
  74.  
  75. namespace Mattdevelops.Bow.UnitSystem
  76. {
  77. public class UnitBehaviour : MonoBehaviour, IUnitBehaviour
  78. {
  79. [SerializeField] private DestructProcessorFlags _processors;
  80. [SerializeField] private Transform _destuctParticles;
  81. [SerializeField] private Transform _impactParticles;
  82.  
  83. private List<D2dDestructible> _destructibles = new List<D2dDestructible>();
  84.  
  85.  
  86. public Transform Transform => transform;
  87.  
  88. public IUnitController UnitController { get; private set; }
  89.  
  90. public UnityEvent onHit;
  91. public UnityEvent onDestruct;
  92.  
  93. public void OnInitialize()
  94. {
  95. _destructibles = GetComponentsInChildren<D2dDestructible>().ToList();
  96. for (int i = 0; i < _destructibles.Count; i++)
  97. {
  98. _destructibles[i].OnAlphaDataModified.AddListener(OnHit);
  99. }
  100. }
  101.  
  102. public void Destruct()
  103. {
  104. UnitController.Destruct();
  105. }
  106.  
  107. private void OnHit(D2dRect rect)
  108. {
  109. onHit.Invoke();
  110. }
  111.  
  112. private void OnSpawned()
  113. {
  114. UnitController = new UnitController(this, GetProcessorPipeline());
  115. }
  116.  
  117. private void OnDespawned()
  118. {
  119. for (int i = 0; i < _destructibles.Count; i++)
  120. {
  121. if (_destructibles[i] != null)
  122. {
  123. _destructibles[i].OnAlphaDataModified.RemoveListener(OnHit);
  124. }
  125. }
  126. }
  127.  
  128.  
  129. public void OnUnitDestroyed()
  130. {
  131. onDestruct.Invoke();
  132. if (_destuctParticles)
  133. _destuctParticles.SpawnInPool(transform.position, _destuctParticles.transform.rotation);
  134. if (_impactParticles)
  135. _impactParticles.SpawnInPool(transform.position, _impactParticles.transform.rotation);
  136. transform.DespawnWithChildren();
  137. }
  138.  
  139. private IUnitDestructProcessor[] GetProcessorPipeline()
  140. {
  141. var proc = new List<IUnitDestructProcessor>();
  142. if (_processors.HasFlag(DestructProcessorFlags.Essential))
  143. proc.Add(new EssentialProcessor());
  144. if (_processors.HasFlag(DestructProcessorFlags.OnlyEssentials))
  145. proc.Add(new OnlyEssentialsProcessor());
  146.  
  147. return proc.ToArray();
  148. }
  149. }
  150. }
  151. using System.Collections.Generic;
  152. using Mattdevelops.Bow.Unit;
  153. using Mattdevelops.Bow.UnitSystem.UnitDestructProcessor;
  154.  
  155. namespace Mattdevelops.Bow.UnitSystem
  156. {
  157. public class UnitController : IUnitController
  158. {
  159. public IUnitBehaviour UnitBehaviour { get; }
  160.  
  161. private readonly Dictionary<int, IUnitPart> _parts = new Dictionary<int, IUnitPart>();
  162. private readonly IUnitDestructProcessor[] _destructPipeline;
  163. private bool _isDestructing;
  164.  
  165. public UnitController(IUnitBehaviour unitBehaviour,
  166. IUnitDestructProcessor[] destructPipeline = null)
  167. {
  168. UnitBehaviour = unitBehaviour;
  169. _destructPipeline = destructPipeline;
  170. }
  171.  
  172.  
  173. public IUnitPart[] Parts
  174. {
  175. get
  176. {
  177. var parts = new IUnitPart[_parts.Count];
  178. _parts.Values.CopyTo(parts, 0);
  179. return parts;
  180. }
  181. }
  182.  
  183. public void Initialize()
  184. {
  185. for (int i = 0; i < _parts.Count; i++)
  186. {
  187. _parts[i].UnitPartBehaviour.OnInitializePart();
  188. }
  189.  
  190. UnitBehaviour.OnInitialize();
  191. }
  192.  
  193. public void Reset()
  194. {
  195. _parts.Clear();
  196. _isDestructing = false;
  197. }
  198.  
  199.  
  200. public void Destruct()
  201. {
  202. _isDestructing = true;
  203. var ids = new List<int>(_parts.Count);
  204. foreach (var key in _parts.Keys)
  205. {
  206. ids.Add(key);
  207. }
  208.  
  209. foreach (var id in ids)
  210. {
  211. var part = _parts[id];
  212. RemovePart(id);
  213. part.UnitPartBehaviour.OnDestruct();
  214. }
  215.  
  216. UnitBehaviour.OnUnitDestroyed();
  217. }
  218.  
  219.  
  220. public void AddPart(IUnitPart part)
  221. {
  222. if (!_parts.ContainsKey(part.Id))
  223. _parts.Add(part.Id, part);
  224. }
  225.  
  226. public void RemovePart(IUnitPart part)
  227. {
  228. _parts.Remove(part.Id);
  229.  
  230. if (ShouldDestruct(part))
  231. {
  232. Destruct();
  233. }
  234. }
  235.  
  236. public void RemovePart(int id)
  237. {
  238. if (_parts.ContainsKey(id))
  239. {
  240. RemovePart(_parts[id]);
  241. }
  242. }
  243.  
  244. private bool ShouldDestruct(IUnitPart part)
  245. {
  246. if (_isDestructing || (!_isDestructing && _destructPipeline == null))
  247. return false;
  248. foreach (var pipe in _destructPipeline)
  249. {
  250. if (pipe.ShouldDestruct(this, part))
  251. {
  252. return true;
  253. }
  254. }
  255.  
  256. return false;
  257. }
  258. }
  259. }
  260. using System.Collections.Generic;
  261. using Mattdevelops.Bow.Unit;
  262.  
  263. namespace Mattdevelops.Bow.UnitSystem
  264. {
  265. public class UnitPart : IUnitPart
  266. {
  267. public int Id { get; }
  268. public IUnitPartBehaviour UnitPartBehaviour { get; }
  269. public bool IsEssential { get; set; }
  270. public IUnitController UnitController { get; }
  271.  
  272. private readonly Dictionary<int, IUnitPart> _dependencies = new Dictionary<int, IUnitPart>();
  273.  
  274. public UnitPart(int id, IUnitPartBehaviour unitPartBehaviour, IUnitController unitController,
  275. bool isEssential = false)
  276. {
  277. Id = id;
  278. UnitPartBehaviour = unitPartBehaviour;
  279. UnitController = unitController;
  280. IsEssential = isEssential;
  281. unitController.AddPart(this);
  282. }
  283.  
  284. public void AddDependency(IUnitPart part)
  285. {
  286. if (part != null && !_dependencies.ContainsKey(part.Id))
  287. {
  288. _dependencies.Add(part.Id, part);
  289. }
  290. }
  291.  
  292. public void Destruct()
  293. {
  294. foreach (var dependency in _dependencies)
  295. {
  296. dependency.Value.Destruct();
  297. }
  298.  
  299. _dependencies.Clear();
  300. UnitPartBehaviour.OnDestruct();
  301. UnitController.RemovePart(this);
  302. }
  303. }
  304. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement