From 855fba4b031ec3b8d23c6844632a174e42e164e4 Mon Sep 17 00:00:00 2001 From: agm-114 Date: Sun, 1 Dec 2024 11:00:24 +0530 Subject: [PATCH] Area effect changes. --- .../Dynamic Systems/Area/Core/AreaEffect.cs | 56 +++++++++---- AGMLIB/Dynamic Systems/Area/RepairEffect.cs | 16 ++-- AGMLIB/Dynamic Systems/Area/ResupplyEffect.cs | 81 +++++++++++-------- 3 files changed, 94 insertions(+), 59 deletions(-) diff --git a/AGMLIB/Dynamic Systems/Area/Core/AreaEffect.cs b/AGMLIB/Dynamic Systems/Area/Core/AreaEffect.cs index 23a9dae..a8190cd 100644 --- a/AGMLIB/Dynamic Systems/Area/Core/AreaEffect.cs +++ b/AGMLIB/Dynamic Systems/Area/Core/AreaEffect.cs @@ -31,17 +31,33 @@ public class AreaEffect : ActiveSettings private Color? _oldcolor; private float _updateAccum = 0f; - public Material Material => _followingInstance.GetComponentInChildren().material; + public Material Material + { + get + { + //if (_followingInstance == null) + // Common.Hint("Following Instance Null"); + MeshRenderer renderer = _followingInstance.GetComponentInChildren(); + //if (renderer == null) + // Common.Hint("Following mesh Null"); + //if(_followingInstance.GetComponentInChildren(includeInactive:true).material == null) + // Common.Hint("Following mat Null"); + + return _followingInstance.GetComponentInChildren(includeInactive: true).material; + } + } //public List> ShipEffects = new(); - //public List> ModularMissileEffects = new(); + //public List> ModularMissileEffects = new(); public List Effects; public void Fire() { - StopFire();//_EmissionColor + if (_followingInstance != null) + return; + //StopFire();//_EmissionColor //Debug.LogError("Creating Sphere"); string PrefabName = "Stock/E70 'Interruption' Jammer"; HullComponent goodewar = BundleManager.Instance.AllComponents.FirstOrDefault(x => x.SaveKey == PrefabName); @@ -55,7 +71,7 @@ public void Fire() if(prefab == null) Debug.LogError("Did not get following prefab"); else - _followingInstance = NetworkObjectPooler.Instance?.GetNextOrNew(prefab, transform.position, transform.rotation ); + _followingInstance = NetworkObjectPooler.Instance.GetNextOrNew(prefab, transform.position, transform.rotation); if (_followingInstance is ISettableEWarParameters settableEWarParameters) { settableEWarParameters.SetParams(SignatureType.Radar, omni: true, 360f, Radius, 1, 0, 0, 0f, true); @@ -69,14 +85,17 @@ public void Fire() public void StopFire() { - if (_followingInstance != null) - { - if(_oldcolor != null) - Material.SetColor(matproperty, _oldcolor.Value); - _followingInstance.RepoolSelf(); - _followingInstance = null; - _oldcolor = null; - } + + if (_followingInstance == null) + return; + //Debug.LogError("Stopping Sphere"); + + if (_oldcolor != null) + Material.SetColor(matproperty, _oldcolor.Value); + //DestroyImmediate(_followingInstance); + _followingInstance.RepoolSelf(); + _followingInstance = null; + _oldcolor = null; } // Start is called before the first frame update @@ -86,7 +105,7 @@ void Start() { effect.AreaEffect = this; effect.Setup(); - } + } if (Trigger == null) { SphereCollider sphereCollider = gameObject.GetOrAddComponent(); @@ -95,7 +114,7 @@ void Start() } else if(Trigger is SphereCollider sphere) { - //Radius = sphere.radius; + Radius = sphere.radius; } Trigger.isTrigger = true; @@ -108,6 +127,7 @@ void Start() protected override void FixedUpdate() { base.FixedUpdate(); + //Debug.LogError($"{Buttonstate} current buttonstate | targetbuttonstate {activateButtonState}"); if (InEditor) return; //Debug.LogError("Core Fixed Update"); @@ -130,6 +150,7 @@ protected override void FixedUpdate() } } + _laststate = active; @@ -137,11 +158,12 @@ protected override void FixedUpdate() if(CustomVFX) { - if (active && _followingInstance == null) + if (active) Fire(); - else if (!active) + else StopFire(); - if ((_updateEveryFrame || _updateAccum >= _updateInterval) && _followingInstance != null) + + if (_followingInstance != null && (_updateEveryFrame || _updateAccum >= _updateInterval)) { _updateAccum = 0f; _followingInstance.transform.position = base.transform.position; diff --git a/AGMLIB/Dynamic Systems/Area/RepairEffect.cs b/AGMLIB/Dynamic Systems/Area/RepairEffect.cs index a108d2b..3996ac6 100644 --- a/AGMLIB/Dynamic Systems/Area/RepairEffect.cs +++ b/AGMLIB/Dynamic Systems/Area/RepairEffect.cs @@ -46,13 +46,14 @@ public class RepairEffect : FalloffEffect protected float _accum = 0; public override void FixedUpdate() { - //Debug.LogError("Generic Fixed Update"); + // _accum += Time.fixedDeltaTime; if(_accum < 1) return; _accum -= 1; if (Active) { + //Debug.LogError("Generic repair Fixed Update"); IEnumerable> ships = new List>(); IEnumerable lockers = new List(); foreach (Ship target in Targets.Where(target => target != null)) @@ -66,21 +67,20 @@ public override void FixedUpdate() parts = parts.OrderByDescending(h => h.DCPriority); parts = parts.Take(MaxRepairsPerHull); } - if(parts.Count() > 0) - ships.Append(parts); + if(parts.Any()) + ships = ships.Append(parts); else { - IEnumerable shiplockers = root.GetComponentsInChildren().ToList().OrderByDescending(h => h.DCPriority).ConvertAll(locker => RestoreStatus.GetRestoreStatus(locker)).Where(status => status.NeedsRestores).Take(1); - if(shiplockers.Count() > 0) - { - lockers.First(); - } + IEnumerable hulllockers = root.GetComponentsInChildren().ToList().OrderByDescending(h => h.DCPriority).ConvertAll(locker => RestoreStatus.GetRestoreStatus(locker)).Where(status => status.NeedsRestores).Take(1); + if (hulllockers.Any()) + lockers = lockers.Append(hulllockers.First()); } } float repairsperhull = RepairPerSecond / (ships.Count() + lockers.Count()); foreach(IEnumerable repairparts in ships) { + //continue; float repairsperpart = repairsperhull / repairparts.Count(); foreach(HullPart hullPart in repairparts) diff --git a/AGMLIB/Dynamic Systems/Area/ResupplyEffect.cs b/AGMLIB/Dynamic Systems/Area/ResupplyEffect.cs index 6f03a7e..b4a1fbc 100644 --- a/AGMLIB/Dynamic Systems/Area/ResupplyEffect.cs +++ b/AGMLIB/Dynamic Systems/Area/ResupplyEffect.cs @@ -1,67 +1,80 @@ using static Ships.BulkMagazineComponent; +using static UnityEngine.UI.CanvasScaler; namespace AGMLIB.Dynamic_Systems.Area { public class ResupplyEffect : FalloffEffect { + public float PointsPerSecond = 1; + + private float ReloadPoints = 0; + + public bool BulkMagazines = true; + public bool CellLaunchers = true; + + [SerializeField] protected ISimpleFilter _simplefilter; + public override void TargetFixedUpdate(Ship target) { + ReloadPoints += PointsPerSecond * Time.fixedDeltaTime; if (target == AreaEffect.Ship) return; if (AreaEffect.ShipController?.GetIFF(target?.Controller?.OwnedBy) == IFF.Enemy) return; + + Transform targettransform = target.gameObject.transform.root; //Debug.LogError("Resupply Ship"); bool dirtymissiles = false; + + List>> test1 = new(); + if(BulkMagazines) + test1.AddRange(targettransform.GetComponentsInChildren().ConvertAll(comp => comp.Magazines.Select(mag => new KeyValuePair(mag, comp)))); + if(CellLaunchers) + test1.AddRange(targettransform.GetComponentsInChildren().ConvertAll(comp => comp.Missiles .Select(mag => new KeyValuePair(mag, comp)))); + IEnumerable> test2 = test1.SelectMany(list => list).Where(pair => pair.Key.QuantityAvailable < pair.Key.PeakQuantity); + if (_simplefilter != null) + test2 = test2.Where(pair => _simplefilter.IsAmmoCompatible(pair.Key.AmmoType)); + IOrderedEnumerable> test3 = test2.OrderBy(kvp => kvp.Key.PercentageAvailable); - foreach (BulkMagazineComponent hullComponent in target.gameObject.transform.root.GetComponentsInChildren()) - { - BulkMagazineData magdata = hullComponent.gameObject.GetComponent().saveData as BulkMagazineData; - List mags = hullComponent.Magazines.ToList(); - foreach (IMagazine mag in hullComponent.Magazines) - { - if(mag.QuantityAvailable < mag.PeakQuantity) - { - Debug.LogError($"{AreaEffect.Ship.gameObject.name} Resupply Ship {target.gameObject.name} {mag.AmmoType.MunitionName} {mag.QuantityAvailable}/{mag.PeakQuantity}"); - hullComponent.AddToMagazine(mag.AmmoType, 1u); - - AreaEffect.Hull.MyShip.AmmoFeed.GetAmmoSource(mag.AmmoType).Withdraw(1u); - - } - } - foreach (Magazine.MagSaveData targetdata in magdata.Load) - { - IMunition munition = AreaEffect.Hull.MyShip.Fleet.AvailableMunitions.GetMunition(targetdata.MunitionKey); + foreach (var kvp in test3) + { + IMagazine sink = kvp.Key; + IMagazine source = AreaEffect.Hull.MyShip.AmmoFeed.GetAmmoSource(sink.AmmoType); + HullComponent hullComponent = kvp.Value; - IMagazine mag = mags.Find(x => x.AmmoType == munition); + if (sink.AmmoType.PointCost > ReloadPoints) + return; - //if(mag.) - //hullComponent.AddToMagazine(mags, 1); - } - } - foreach (CellLauncherComponent hullComponent in target.gameObject.transform.root.GetComponentsInChildren()) - { - IMagazineProvider magazineProvider = hullComponent as IMagazineProvider; + uint reloadamount = (uint)Math.Min((uint)Math.Min(source.QuantityAvailable, sink.PeakQuantity - sink.QuantityAvailable), ReloadPoints / (float)sink.AmmoType.PointCost); + ReloadPoints -= reloadamount * sink.AmmoType.PointCost; + //BulkMagazineData magdata = hullComponent.gameObject.GetComponent().saveData as BulkMagazineData; + //List mags = hullComponent.Magazines.ToList(); - foreach (IMagazine mag in hullComponent.Missiles) + + //Debug.LogError($"{AreaEffect.Ship.gameObject.name} Resupply Ship {target.gameObject.name} {sink.AmmoType.MunitionName} {sink.QuantityAvailable}/{sink.PeakQuantity}"); + if(hullComponent is BulkMagazineComponent magcomp) + magcomp.AddToMagazine(sink.AmmoType, reloadamount); + else if( hullComponent is CellLauncherComponent cellcomp) { - if (mag.QuantityAvailable < mag.PeakQuantity) - { - dirtymissiles = true; + IMagazineProvider magazineProvider = cellcomp as IMagazineProvider; + //Debug.LogError("movin missiles"); + dirtymissiles = true; - magazineProvider.AddToMagazine(mag.AmmoType, 1); - AreaEffect.Hull.MyShip.AmmoFeed.GetAmmoSource(mag.AmmoType).Withdraw(1); - } + magazineProvider.AddToMagazine(sink.AmmoType, reloadamount); + } - } + source.Withdraw(reloadamount); } + + //target.BuildMissileGroups(); if(dirtymissiles) target?.BuildMissileMagazineTracker();