Shielding

Shield behavior now provides protection from damage at the expense of heat and energy usage
Weapon scripts check for the presence of a shield when detecting collisions (TODO: TEST ALL WEAPON TYPES)
Shields can be toggled on/off (G key)
Database cache now included in repo (saves some confusion)
This commit is contained in:
Reinier Vens 2021-03-06 01:04:26 +00:00
commit 8920b48977
19 changed files with 377 additions and 95 deletions

View file

@ -13,8 +13,8 @@ public class ShieldData : BehaviorData
[InspectableField, JsonProperty("efficiency"), Key(1), RuntimeInspectable]
public PerformanceStat Efficiency = new PerformanceStat();
[InspectableField, JsonProperty("shielding"), Key(2), RuntimeInspectable]
public PerformanceStat Shielding = new PerformanceStat();
[InspectableField, JsonProperty("energy"), Key(2), RuntimeInspectable]
public PerformanceStat EnergyUsage = new PerformanceStat();
public override IBehavior CreateInstance(ItemManager context, Entity entity, EquippedItem item)
{
@ -27,6 +27,9 @@ public class Shield : IBehavior
public Entity Entity { get; }
public EquippedItem Item { get; }
public ItemManager Context { get; }
public float Efficiency { get; private set; }
public float EnergyUsage { get; private set; }
public BehaviorData Data => _data;
@ -38,12 +41,23 @@ public class Shield : IBehavior
_data = data;
Entity = entity;
Item = item;
// TODO: Detect hits on Ship, keep a list of callbacks that modify the hit by reference
}
public bool Execute(float delta)
{
Efficiency = Context.Evaluate(_data.Efficiency, Item);
EnergyUsage = Context.Evaluate(_data.EnergyUsage, Item);
return true;
}
public bool CanTakeHit(DamageType type, float damage)
{
return Entity.CanConsumeEnergy(damage * EnergyUsage);
}
public void TakeHit(DamageType type, float damage)
{
Entity.TryConsumeEnergy(damage * EnergyUsage);
Item.AddHeat(damage / Efficiency);
}
}

View file

@ -96,13 +96,11 @@ public abstract class Entity
get => _weapons;
}
public Shield Shield { get; private set; }
public float TargetRange { get; private set; }
public float MaxTemp { get; private set; }
public float MinTemp { get; private set; }
public bool ShieldEnabled // TODO: MAKE SHIELDS
{
get => true;
}
public ItemManager ItemManager { get; }
public int AssignedPopulation => PopulationAssignments.Sum(pa => pa.AssignedPopulation);
public float Mass { get; private set; }
@ -349,12 +347,14 @@ public abstract class Entity
{
if (b is Weapon weapon)
_weapons.Remove(weapon);
if(b is Capacitor capacitor)
if (b is Capacitor capacitor)
_capacitors.Remove(capacitor);
if(b is Reactor reactor)
if (b is Reactor reactor)
_reactors.Remove(reactor);
if(b is Radiator heatsink)
if (b is Radiator heatsink)
_heatsinks.Remove(heatsink);
if (b is Shield)
Shield = null;
}
return item.EquippableItem;
@ -531,6 +531,8 @@ public abstract class Entity
_reactors.Add(reactor);
if(b is Radiator heatsink)
_heatsinks.Add(heatsink);
if (b is Shield shield)
Shield = shield;
}
equippedItem.OnOnline += () => ItemOnline.OnNext(equippedItem);
@ -584,6 +586,13 @@ public abstract class Entity
return true;
}
public bool CanConsumeEnergy(float energy)
{
var capEnergy = _capacitors.Sum(cap => cap.Charge);
int onlineReactors = _reactors.Count(reactor=>reactor.Item.Online);
return capEnergy > energy || onlineReactors > 0;
}
public bool TryConsumeEnergy(float energy)
{
if (energy < .01f) return true;
@ -860,6 +869,11 @@ public class EquippedItem
public Shape InsetShape { get; }
public int SortPosition;
public bool Active
{
get => _online && Enabled;
}
public bool Online
{
@ -937,7 +951,7 @@ public class EquippedItem
if (!previouslyOnline && _online) OnOnline?.Invoke();
if (previouslyOnline && !_online) OnOffline?.Invoke();
if (Enabled && _online)
if (Active)
{
foreach (var group in BehaviorGroups.Values)
{