using System; using System.Collections.Generic; using System.Linq; using Sirenix.OdinInspector; using Sirenix.Utilities; using VOID.Generic.Dict; using VOID.Generic.Objects.Abstract; using VOID.Generic.Objects.Unit.AI; using VOID.Generic.Objects.Unit.Events; namespace VOID.Generic.Objects.Unit { [Serializable] public class UnitObjectSenses : ObjectComponent { #region Initialize protected override void SelfInitialize() { // Fill _unitsInSight with all enum keys Enum.GetValues(typeof(AttitudeType)) .OfType() .ForEach(attitude => _unitsInSight.Add(attitude, new List())); } protected override void EventInitialize() { parent.unitEvents.onSightEnter.AddListener(OnSightEnter); parent.unitEvents.onSightExit.AddListener(OnSightExit); } protected override void Initialize() { UnitSight.Create(parent); UnitHear.Create(parent); } #endregion [Title("Units & objects in sight")] private Dictionary> _unitsInSight = new(); private List _objectsInSight = new(); public List GetUnitsInSightByAttitude(AttitudeType attitudeType) { return _unitsInSight[attitudeType]; } public List GetObjectsInSight() { return _objectsInSight; } private void OnSightEnter(SenseSightEvent sightEvent) { if (sightEvent.sightObject is UnitObject otherUnit) { var attitude = parent.unitAttitude.GetAttitudeWith(otherUnit); _unitsInSight[attitude].Add(otherUnit); } else { _objectsInSight.Add(sightEvent.sightObject); } } private void OnSightExit(SenseSightEvent sightEvent) { if (sightEvent.sightObject is UnitObject otherUnit) { _unitsInSight.Values.ForEach(units => units.Remove(otherUnit)); } _objectsInSight.Remove(sightEvent.sightObject); } } }