Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Mattdevelops.Bow.Unit
- {
- public interface IDestructible
- {
- void Destruct();
- }
- }
- using UnityEngine;
- namespace Mattdevelops.Bow.UnitSystem
- {
- public interface IUnitBehaviour : IUnitControllerProvider
- {
- void OnUnitDestroyed();
- void OnInitialize();
- Transform Transform { get; }
- }
- }
- using Mattdevelops.Bow.Unit;
- namespace Mattdevelops.Bow.UnitSystem
- {
- public interface IUnitController : IDestructible
- {
- IUnitPart[] Parts { get; }
- IUnitBehaviour UnitBehaviour { get; }
- void AddPart(IUnitPart part);
- void RemovePart(IUnitPart part);
- void RemovePart(int id);
- void Initialize();
- void Reset();
- }
- }
- namespace Mattdevelops.Bow.UnitSystem
- {
- public interface IUnitControllerProvider
- {
- IUnitController UnitController { get; }
- }
- }
- using Mattdevelops.Bow.Unit;
- namespace Mattdevelops.Bow.UnitSystem
- {
- public interface IUnitPart : IDestructible
- {
- int Id { get; }
- bool IsEssential { get; set; }
- void AddDependency(IUnitPart part);
- IUnitController UnitController { get; }
- IUnitPartBehaviour UnitPartBehaviour { get; }
- }
- }
- namespace Mattdevelops.Bow.UnitSystem
- {
- public interface IUnitPartBehaviour : IUnitPartProvider
- {
- void OnInitializePart();
- void OnDestruct();
- }
- }
- using System.Collections.Generic;
- using System.Linq;
- using DarkTonic.PoolBoss;
- using Destructible2D;
- using Mattdevelops.Bow.UnitSystem.UnitDestructProcessor;
- using UnityEngine;
- using UnityEngine.Events;
- namespace Mattdevelops.Bow.UnitSystem
- {
- public class UnitBehaviour : MonoBehaviour, IUnitBehaviour
- {
- [SerializeField] private DestructProcessorFlags _processors;
- [SerializeField] private Transform _destuctParticles;
- [SerializeField] private Transform _impactParticles;
- private List<D2dDestructible> _destructibles = new List<D2dDestructible>();
- public Transform Transform => transform;
- public IUnitController UnitController { get; private set; }
- public UnityEvent onHit;
- public UnityEvent onDestruct;
- public void OnInitialize()
- {
- _destructibles = GetComponentsInChildren<D2dDestructible>().ToList();
- for (int i = 0; i < _destructibles.Count; i++)
- {
- _destructibles[i].OnAlphaDataModified.AddListener(OnHit);
- }
- }
- public void Destruct()
- {
- UnitController.Destruct();
- }
- private void OnHit(D2dRect rect)
- {
- onHit.Invoke();
- }
- private void OnSpawned()
- {
- UnitController = new UnitController(this, GetProcessorPipeline());
- }
- private void OnDespawned()
- {
- for (int i = 0; i < _destructibles.Count; i++)
- {
- if (_destructibles[i] != null)
- {
- _destructibles[i].OnAlphaDataModified.RemoveListener(OnHit);
- }
- }
- }
- public void OnUnitDestroyed()
- {
- onDestruct.Invoke();
- if (_destuctParticles)
- _destuctParticles.SpawnInPool(transform.position, _destuctParticles.transform.rotation);
- if (_impactParticles)
- _impactParticles.SpawnInPool(transform.position, _impactParticles.transform.rotation);
- transform.DespawnWithChildren();
- }
- private IUnitDestructProcessor[] GetProcessorPipeline()
- {
- var proc = new List<IUnitDestructProcessor>();
- if (_processors.HasFlag(DestructProcessorFlags.Essential))
- proc.Add(new EssentialProcessor());
- if (_processors.HasFlag(DestructProcessorFlags.OnlyEssentials))
- proc.Add(new OnlyEssentialsProcessor());
- return proc.ToArray();
- }
- }
- }
- using System.Collections.Generic;
- using Mattdevelops.Bow.Unit;
- using Mattdevelops.Bow.UnitSystem.UnitDestructProcessor;
- namespace Mattdevelops.Bow.UnitSystem
- {
- public class UnitController : IUnitController
- {
- public IUnitBehaviour UnitBehaviour { get; }
- private readonly Dictionary<int, IUnitPart> _parts = new Dictionary<int, IUnitPart>();
- private readonly IUnitDestructProcessor[] _destructPipeline;
- private bool _isDestructing;
- public UnitController(IUnitBehaviour unitBehaviour,
- IUnitDestructProcessor[] destructPipeline = null)
- {
- UnitBehaviour = unitBehaviour;
- _destructPipeline = destructPipeline;
- }
- public IUnitPart[] Parts
- {
- get
- {
- var parts = new IUnitPart[_parts.Count];
- _parts.Values.CopyTo(parts, 0);
- return parts;
- }
- }
- public void Initialize()
- {
- for (int i = 0; i < _parts.Count; i++)
- {
- _parts[i].UnitPartBehaviour.OnInitializePart();
- }
- UnitBehaviour.OnInitialize();
- }
- public void Reset()
- {
- _parts.Clear();
- _isDestructing = false;
- }
- public void Destruct()
- {
- _isDestructing = true;
- var ids = new List<int>(_parts.Count);
- foreach (var key in _parts.Keys)
- {
- ids.Add(key);
- }
- foreach (var id in ids)
- {
- var part = _parts[id];
- RemovePart(id);
- part.UnitPartBehaviour.OnDestruct();
- }
- UnitBehaviour.OnUnitDestroyed();
- }
- public void AddPart(IUnitPart part)
- {
- if (!_parts.ContainsKey(part.Id))
- _parts.Add(part.Id, part);
- }
- public void RemovePart(IUnitPart part)
- {
- _parts.Remove(part.Id);
- if (ShouldDestruct(part))
- {
- Destruct();
- }
- }
- public void RemovePart(int id)
- {
- if (_parts.ContainsKey(id))
- {
- RemovePart(_parts[id]);
- }
- }
- private bool ShouldDestruct(IUnitPart part)
- {
- if (_isDestructing || (!_isDestructing && _destructPipeline == null))
- return false;
- foreach (var pipe in _destructPipeline)
- {
- if (pipe.ShouldDestruct(this, part))
- {
- return true;
- }
- }
- return false;
- }
- }
- }
- using System.Collections.Generic;
- using Mattdevelops.Bow.Unit;
- namespace Mattdevelops.Bow.UnitSystem
- {
- public class UnitPart : IUnitPart
- {
- public int Id { get; }
- public IUnitPartBehaviour UnitPartBehaviour { get; }
- public bool IsEssential { get; set; }
- public IUnitController UnitController { get; }
- private readonly Dictionary<int, IUnitPart> _dependencies = new Dictionary<int, IUnitPart>();
- public UnitPart(int id, IUnitPartBehaviour unitPartBehaviour, IUnitController unitController,
- bool isEssential = false)
- {
- Id = id;
- UnitPartBehaviour = unitPartBehaviour;
- UnitController = unitController;
- IsEssential = isEssential;
- unitController.AddPart(this);
- }
- public void AddDependency(IUnitPart part)
- {
- if (part != null && !_dependencies.ContainsKey(part.Id))
- {
- _dependencies.Add(part.Id, part);
- }
- }
- public void Destruct()
- {
- foreach (var dependency in _dependencies)
- {
- dependency.Value.Destruct();
- }
- _dependencies.Clear();
- UnitPartBehaviour.OnDestruct();
- UnitController.RemovePart(this);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement