Behavior Refactoring
Behaviors and PerformanceStats are no longer associated solely with Ships, instead there's a generic Entity class which will also be used for stations and turrets.
This commit is contained in:
parent
fd92c80876
commit
7cd24147ae
41 changed files with 544 additions and 341 deletions
|
|
@ -31,6 +31,17 @@ public class UpdateOrderAttribute : Attribute
|
|||
}
|
||||
}
|
||||
|
||||
[AttributeUsage(AttributeTargets.Class)]
|
||||
public class EntityTypeRestrictionAttribute : Attribute
|
||||
{
|
||||
public readonly Type Type;
|
||||
|
||||
public EntityTypeRestrictionAttribute(Type type)
|
||||
{
|
||||
Type = type;
|
||||
}
|
||||
}
|
||||
|
||||
[AttributeUsage(AttributeTargets.Class)]
|
||||
public class InspectableAttribute : Attribute { }
|
||||
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ using MessagePack;
|
|||
using Newtonsoft.Json;
|
||||
|
||||
[InspectableField, MessagePackObject, JsonObject(MemberSerialization.OptIn)]
|
||||
public class AfterburnerBehaviorData : IItemBehaviorData
|
||||
public class AfterburnerData : IBehaviorData
|
||||
{
|
||||
[InspectableField, JsonProperty("thrust"), Key(0)]
|
||||
public PerformanceStat ThrustModifier = new PerformanceStat();
|
||||
|
|
@ -16,55 +16,50 @@ public class AfterburnerBehaviorData : IItemBehaviorData
|
|||
[InspectableField, JsonProperty("torque"), Key(2)]
|
||||
public PerformanceStat TorqueModifier = new PerformanceStat();
|
||||
|
||||
public IItemBehavior CreateInstance(GameContext context, Ship ship, Gear item)
|
||||
public IBehavior CreateInstance(GameContext context, Entity entity, Gear item)
|
||||
{
|
||||
return new AfterburnerBehavior(context, this, ship, item);
|
||||
return new Afterburner(context, this, entity, item);
|
||||
}
|
||||
}
|
||||
|
||||
public class AfterburnerBehavior : IActivatedItemBehavior
|
||||
public class Afterburner : IActivatedBehavior
|
||||
{
|
||||
private List<Dictionary<IItemBehavior,float>> _modifiers = new List<Dictionary<IItemBehavior, float>>();
|
||||
private AfterburnerBehaviorData _data;
|
||||
private ThrusterBehaviorData[] _thrusters;
|
||||
private List<Dictionary<IBehavior,float>> _modifiers = new List<Dictionary<IBehavior, float>>();
|
||||
private AfterburnerData _data;
|
||||
private Thruster _thruster;
|
||||
|
||||
public Ship Ship { get; }
|
||||
public Entity Entity { get; }
|
||||
public Gear Item { get; }
|
||||
public GameContext Context { get; }
|
||||
|
||||
public IItemBehaviorData Data => _data;
|
||||
public IBehaviorData Data => _data;
|
||||
|
||||
public AfterburnerBehavior(GameContext context, AfterburnerBehaviorData data, Ship ship, Gear item)
|
||||
public Afterburner(GameContext context, AfterburnerData data, Entity entity, Gear item)
|
||||
{
|
||||
Context = context;
|
||||
_data = data;
|
||||
Ship = ship;
|
||||
Entity = entity;
|
||||
Item = item;
|
||||
}
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
_thrusters = Ship.GetBehaviorData<ThrusterBehaviorData>().ToArray();
|
||||
_thruster = Entity.GetBehaviors<Thruster>().FirstOrDefault();
|
||||
}
|
||||
|
||||
public void Update(float delta)
|
||||
{
|
||||
}
|
||||
|
||||
public void FixedUpdate(float delta)
|
||||
{
|
||||
}
|
||||
|
||||
public void Activate()
|
||||
{
|
||||
if (_thrusters.Length == 0) return;
|
||||
if (_thruster == null) return;
|
||||
|
||||
foreach (var thruster in _thrusters)
|
||||
{
|
||||
var thrustMod = thruster.Thrust.GetScaleModifiers(Ship);
|
||||
thrustMod.Add(this,Context.Evaluate(_data.ThrustModifier,Item, Ship));
|
||||
_modifiers.Add(thrustMod);
|
||||
}
|
||||
var thrustMod = ((ThrusterData) _thruster.Data).Thrust.GetScaleModifiers(Entity);
|
||||
thrustMod.Add(this,Context.Evaluate(_data.ThrustModifier,Item, Entity));
|
||||
_modifiers.Add(thrustMod);
|
||||
|
||||
Entity.AxisOverrides[_thruster] = 1;
|
||||
|
||||
// var speedMod = (Ship.Hull.ItemData as HullData).TopSpeed.GetScaleModifiers(Ship);
|
||||
// _modifiers.Add(speedMod);
|
||||
|
|
@ -73,13 +68,11 @@ public class AfterburnerBehavior : IActivatedItemBehavior
|
|||
// var torqueMod = (Ship.GetEquipped(HardpointType.Thruster).ItemData as ThrusterData).Torque.GetScaleModifiers(Ship);
|
||||
// _modifiers.Add(torqueMod);
|
||||
// torqueMod.Add(this,Context.Evaluate(_data.TorqueModifier,Item, Ship));
|
||||
|
||||
Ship.ForceThrust = true;
|
||||
}
|
||||
|
||||
public void Deactivate()
|
||||
{
|
||||
Ship.ForceThrust = false;
|
||||
Entity.AxisOverrides.Remove(_thruster);
|
||||
foreach (var mod in _modifiers)
|
||||
{
|
||||
mod.Remove(this);
|
||||
50
Assets/Scripts/ServerShared/Behaviors/Behaviors.cs
Normal file
50
Assets/Scripts/ServerShared/Behaviors/Behaviors.cs
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using JsonKnownTypes;
|
||||
using MessagePack;
|
||||
using Newtonsoft.Json;
|
||||
using Unity.Mathematics;
|
||||
using static Unity.Mathematics.math;
|
||||
using static Unity.Mathematics.noise;
|
||||
|
||||
public interface IBehavior
|
||||
{
|
||||
void Initialize();
|
||||
void Update(float delta);
|
||||
Entity Entity { get; }
|
||||
Gear Item { get; }
|
||||
GameContext Context { get; }
|
||||
IBehaviorData Data { get; }
|
||||
}
|
||||
|
||||
public interface IActivatedBehavior : IBehavior
|
||||
{
|
||||
void Activate();
|
||||
void Deactivate();
|
||||
}
|
||||
|
||||
public interface IAnalogBehavior : IBehavior
|
||||
{
|
||||
void SetAxis(float value);
|
||||
}
|
||||
|
||||
[InspectableField,
|
||||
Union(0, typeof(CannonData)),
|
||||
Union(1, typeof(LauncherData)),
|
||||
Union(2, typeof(ReactorData)),
|
||||
Union(3, typeof(RadiatorData)),
|
||||
Union(4, typeof(AfterburnerData)),
|
||||
Union(5, typeof(SensorData)),
|
||||
Union(6, typeof(ReflectorData)),
|
||||
Union(7, typeof(ShieldData)),
|
||||
Union(8, typeof(ThrusterData)),
|
||||
Union(9, typeof(TurningData)),
|
||||
Union(10, typeof(VelocityConversionData)),
|
||||
Union(11, typeof(VelocityLimitData)),
|
||||
JsonConverter(typeof(JsonKnownTypesConverter<IBehaviorData>)), JsonObject(MemberSerialization.OptIn)]
|
||||
public interface IBehaviorData
|
||||
{
|
||||
IBehavior CreateInstance(GameContext context, Entity entity, Gear item);
|
||||
}
|
||||
|
|
@ -2,7 +2,7 @@ using MessagePack;
|
|||
using Newtonsoft.Json;
|
||||
|
||||
[InspectableField, MessagePackObject, JsonObject(MemberSerialization.OptIn)]
|
||||
public class CannonBehaviorData : WeaponBehaviorData, IItemBehaviorData
|
||||
public class CannonData : WeaponData, IBehaviorData
|
||||
{
|
||||
[InspectablePrefab, JsonProperty("bullet"), Key(9)]
|
||||
public string BulletPrefab;
|
||||
|
|
@ -16,27 +16,27 @@ public class CannonBehaviorData : WeaponBehaviorData, IItemBehaviorData
|
|||
[InspectableField, JsonProperty("bulletVelocity"), Key(12)]
|
||||
public PerformanceStat Velocity = new PerformanceStat();
|
||||
|
||||
public IItemBehavior CreateInstance(GameContext context, Ship ship, Gear item)
|
||||
public IBehavior CreateInstance(GameContext context, Entity entity, Gear item)
|
||||
{
|
||||
return new CannonBehavior(context, this, ship, item);
|
||||
return new Cannon(context, this, entity, item);
|
||||
}
|
||||
}
|
||||
|
||||
public class CannonBehavior : IActivatedItemBehavior
|
||||
public class Cannon : IActivatedBehavior
|
||||
{
|
||||
private bool _firing;
|
||||
private float _cooldown; // normalized
|
||||
private CannonBehaviorData _cannon;
|
||||
public Ship Ship { get; }
|
||||
private CannonData _cannon;
|
||||
public Entity Entity { get; }
|
||||
public Gear Item { get; }
|
||||
public GameContext Context { get; }
|
||||
private float _firingVisibility;
|
||||
|
||||
public CannonBehavior(GameContext context, CannonBehaviorData c, Ship ship, Gear item)
|
||||
public Cannon(GameContext context, CannonData c, Entity entity, Gear item)
|
||||
{
|
||||
Context = context;
|
||||
_cannon = c;
|
||||
Ship = ship;
|
||||
Entity = entity;
|
||||
Item = item;
|
||||
}
|
||||
|
||||
|
|
@ -87,18 +87,15 @@ public class CannonBehavior : IActivatedItemBehavior
|
|||
|
||||
public void Update(float delta)
|
||||
{
|
||||
_cooldown -= delta / Context.Evaluate(_cannon.Cooldown, Item, Ship);
|
||||
_cooldown -= delta / Context.Evaluate(_cannon.Cooldown, Item, Entity);
|
||||
|
||||
_firingVisibility *= Context.Evaluate(_cannon.VisibilityDecay, Item, Entity);
|
||||
|
||||
if (_firingVisibility < 0.01f)
|
||||
{
|
||||
Ship.VisibilitySources.Remove(this);
|
||||
Entity.VisibilitySources.Remove(this);
|
||||
}
|
||||
}
|
||||
|
||||
public void FixedUpdate(float delta)
|
||||
{
|
||||
_firingVisibility *= Context.Evaluate(_cannon.VisibilityDecay, Item, Ship);
|
||||
}
|
||||
|
||||
public IItemBehaviorData Data => _cannon;
|
||||
public IBehaviorData Data => _cannon;
|
||||
}
|
||||
|
|
@ -5,7 +5,7 @@ using Newtonsoft.Json;
|
|||
using Unity.Mathematics;
|
||||
|
||||
[InspectableField, MessagePackObject, JsonObject(MemberSerialization.OptIn)]
|
||||
public class LauncherBehaviorData : IItemBehaviorData
|
||||
public class LauncherData : IBehaviorData
|
||||
{
|
||||
[InspectablePrefab, JsonProperty("missile"), Key(9)]
|
||||
public string MissilePrefab;
|
||||
|
|
@ -37,30 +37,30 @@ public class LauncherBehaviorData : IItemBehaviorData
|
|||
[InspectableField, JsonProperty("lockOnTime"), Key(18)]
|
||||
public PerformanceStat LockOnTime = new PerformanceStat();
|
||||
|
||||
public IItemBehavior CreateInstance(GameContext context, Ship ship, Gear item)
|
||||
public IBehavior CreateInstance(GameContext context, Entity entity, Gear item)
|
||||
{
|
||||
return new LauncherBehavior(context, this, ship, item);
|
||||
return new Launcher(context, this, entity, item);
|
||||
}
|
||||
}
|
||||
|
||||
public class LauncherBehavior : IActivatedItemBehavior
|
||||
public class Launcher : IActivatedBehavior
|
||||
{
|
||||
private bool _locking;
|
||||
private float _lockingTimer;
|
||||
private float _cooldown; // normalized
|
||||
private bool _locked;
|
||||
|
||||
private LauncherBehaviorData _launcher;
|
||||
public Ship Ship { get; }
|
||||
private LauncherData _launcher;
|
||||
public Entity Entity { get; }
|
||||
public Gear Item { get; }
|
||||
public GameContext Context { get; }
|
||||
private float _firingVisibility;
|
||||
|
||||
public LauncherBehavior(GameContext context, LauncherBehaviorData m, Ship ship, Gear item)
|
||||
public Launcher(GameContext context, LauncherData m, Entity entity, Gear item)
|
||||
{
|
||||
Context = context;
|
||||
_launcher = m;
|
||||
Ship = ship;
|
||||
Entity = entity;
|
||||
Item = item;
|
||||
// _audio = hp.Proxy.GetComponent<AudioSource>();
|
||||
// if(_audio==null)
|
||||
|
|
@ -160,13 +160,7 @@ public class LauncherBehavior : IActivatedItemBehavior
|
|||
// Hardpoint.Ship.Visibility += _firingVisibility;
|
||||
// _firingVisibility = 0;
|
||||
// }
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void FixedUpdate(float delta)
|
||||
{
|
||||
}
|
||||
|
||||
public IItemBehaviorData Data => _launcher;
|
||||
public IBehaviorData Data => _launcher;
|
||||
}
|
||||
46
Assets/Scripts/ServerShared/Behaviors/Radiator.cs
Normal file
46
Assets/Scripts/ServerShared/Behaviors/Radiator.cs
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
using MessagePack;
|
||||
using Newtonsoft.Json;
|
||||
using Unity.Mathematics;
|
||||
using static Unity.Mathematics.math;
|
||||
|
||||
[MessagePackObject, JsonObject(MemberSerialization.OptIn)]
|
||||
public class RadiatorData : IBehaviorData
|
||||
{
|
||||
[InspectableField, JsonProperty("emissivity"), Key(0)]
|
||||
public PerformanceStat Emissivity = new PerformanceStat();
|
||||
|
||||
public IBehavior CreateInstance(GameContext context, Entity entity, Gear item)
|
||||
{
|
||||
return new Radiator(context, this, entity, item);
|
||||
}
|
||||
}
|
||||
|
||||
public class Radiator : IBehavior
|
||||
{
|
||||
public Entity Entity { get; }
|
||||
public Gear Item { get; }
|
||||
public GameContext Context { get; }
|
||||
|
||||
public IBehaviorData Data => _data;
|
||||
|
||||
private RadiatorData _data;
|
||||
|
||||
public Radiator(GameContext context, RadiatorData data, Entity entity, Gear item)
|
||||
{
|
||||
Context = context;
|
||||
_data = data;
|
||||
Entity = entity;
|
||||
Item = item;
|
||||
}
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
}
|
||||
|
||||
public void Update(float delta)
|
||||
{
|
||||
var rad = pow(Entity.Temperature, Context.GlobalData.HeatRadiationPower) * Context.GlobalData.HeatRadiationMultiplier;
|
||||
Entity.Temperature -= rad * delta;
|
||||
Entity.VisibilitySources[this] = rad;
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/ServerShared/Behaviors/Radiator.cs.meta
Normal file
3
Assets/Scripts/ServerShared/Behaviors/Radiator.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: f438ee8944644dd6925e2ee9d65229e8
|
||||
timeCreated: 1586415683
|
||||
|
|
@ -2,7 +2,7 @@ using MessagePack;
|
|||
using Newtonsoft.Json;
|
||||
|
||||
[MessagePackObject, JsonObject(MemberSerialization.OptIn)]
|
||||
public class ReactorBehaviorData : IItemBehaviorData
|
||||
public class ReactorData : IBehaviorData
|
||||
{
|
||||
[InspectableField, JsonProperty("charge"), Key(0)]
|
||||
public PerformanceStat Charge = new PerformanceStat();
|
||||
|
|
@ -19,58 +19,53 @@ public class ReactorBehaviorData : IItemBehaviorData
|
|||
[InspectableField, JsonProperty("underload"), Key(4)]
|
||||
public PerformanceStat UnderloadRecovery = new PerformanceStat();
|
||||
|
||||
public IItemBehavior CreateInstance(GameContext context, Ship ship, Gear item)
|
||||
public IBehavior CreateInstance(GameContext context, Entity entity, Gear item)
|
||||
{
|
||||
return new ReactorBehavior(context, this, ship, item);
|
||||
return new Reactor(context, this, entity, item);
|
||||
}
|
||||
}
|
||||
|
||||
public class ReactorBehavior : IItemBehavior
|
||||
public class Reactor : IBehavior
|
||||
{
|
||||
private ReactorBehaviorData _data;
|
||||
private ReactorData _data;
|
||||
|
||||
public Ship Ship { get; }
|
||||
public Entity Entity { get; }
|
||||
public Gear Item { get; }
|
||||
public GameContext Context { get; }
|
||||
|
||||
public IItemBehaviorData Data => _data;
|
||||
public IBehaviorData Data => _data;
|
||||
|
||||
public ReactorBehavior(GameContext context, ReactorBehaviorData data, Ship ship, Gear item)
|
||||
public Reactor(GameContext context, ReactorData data, Entity entity, Gear item)
|
||||
{
|
||||
Context = context;
|
||||
_data = data;
|
||||
Ship = ship;
|
||||
Entity = entity;
|
||||
Item = item;
|
||||
}
|
||||
|
||||
public void FixedUpdate(float delta)
|
||||
{
|
||||
var cap = Context.Evaluate(_data.Capacitance, Item, Ship);
|
||||
var charge = Context.Evaluate(_data.Charge, Item, Ship) * delta;
|
||||
var efficiency = Context.Evaluate(_data.Efficiency, Item, Ship);
|
||||
|
||||
Ship.AddHeat(charge / efficiency);
|
||||
Ship.Charge += charge;
|
||||
|
||||
if (Ship.Charge > cap)
|
||||
{
|
||||
Ship.AddHeat(-(Ship.Charge - cap) / efficiency * (1 - 1 / Context.Evaluate(_data.UnderloadRecovery, Item, Ship)));
|
||||
Ship.Charge = cap;
|
||||
}
|
||||
|
||||
if (Ship.Charge < 0)
|
||||
{
|
||||
Ship.AddHeat( -Ship.Charge / Context.Evaluate(_data.OverloadEfficiency, Item, Ship));
|
||||
Ship.Charge = 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
}
|
||||
|
||||
public void Update(float delta)
|
||||
{
|
||||
var cap = Context.Evaluate(_data.Capacitance, Item, Entity);
|
||||
var charge = Context.Evaluate(_data.Charge, Item, Entity) * delta;
|
||||
var efficiency = Context.Evaluate(_data.Efficiency, Item, Entity);
|
||||
|
||||
Entity.AddHeat(charge / efficiency);
|
||||
Entity.Energy += charge;
|
||||
|
||||
if (Entity.Energy > cap)
|
||||
{
|
||||
Entity.AddHeat(-(Entity.Energy - cap) / efficiency * (1 - 1 / Context.Evaluate(_data.UnderloadRecovery, Item, Entity)));
|
||||
Entity.Energy = cap;
|
||||
}
|
||||
|
||||
if (Entity.Energy < 0)
|
||||
{
|
||||
Entity.AddHeat( -Entity.Energy / Context.Evaluate(_data.OverloadEfficiency, Item, Entity));
|
||||
Entity.Energy = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
46
Assets/Scripts/ServerShared/Behaviors/Reflector.cs
Normal file
46
Assets/Scripts/ServerShared/Behaviors/Reflector.cs
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
using MessagePack;
|
||||
using Newtonsoft.Json;
|
||||
using Unity.Mathematics;
|
||||
using static Unity.Mathematics.math;
|
||||
|
||||
[MessagePackObject, JsonObject(MemberSerialization.OptIn)]
|
||||
public class ReflectorData : IBehaviorData
|
||||
{
|
||||
[InspectableField, JsonProperty("crossSection"), Key(0)]
|
||||
public PerformanceStat CrossSection = new PerformanceStat();
|
||||
|
||||
// [InspectableAnimationCurve, JsonProperty("visibility"), Key(1)]
|
||||
// public float4[] VisibilityCurve;
|
||||
|
||||
public IBehavior CreateInstance(GameContext context, Entity entity, Gear item)
|
||||
{
|
||||
return new Reflector(context, this, entity, item);
|
||||
}
|
||||
}
|
||||
|
||||
public class Reflector : IBehavior
|
||||
{
|
||||
public Entity Entity { get; }
|
||||
public Gear Item { get; }
|
||||
public GameContext Context { get; }
|
||||
|
||||
public IBehaviorData Data => _data;
|
||||
|
||||
private ReflectorData _data;
|
||||
|
||||
public Reflector(GameContext context, ReflectorData data, Entity entity, Gear item)
|
||||
{
|
||||
Context = context;
|
||||
_data = data;
|
||||
Entity = entity;
|
||||
Item = item;
|
||||
}
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
}
|
||||
|
||||
public void Update(float delta)
|
||||
{
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/ServerShared/Behaviors/Reflector.cs.meta
Normal file
3
Assets/Scripts/ServerShared/Behaviors/Reflector.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: d3eb7e73847b493991239c1beb8404c8
|
||||
timeCreated: 1586415699
|
||||
|
|
@ -3,7 +3,7 @@ using MessagePack;
|
|||
using Newtonsoft.Json;
|
||||
|
||||
[InspectableField, MessagePackObject, JsonObject(MemberSerialization.OptIn)]
|
||||
public class SensorBehaviorData : IItemBehaviorData
|
||||
public class SensorData : IBehaviorData
|
||||
{
|
||||
[InspectableField, JsonProperty("radiance"), Key(0)]
|
||||
public PerformanceStat Radiance = new PerformanceStat();
|
||||
|
|
@ -17,26 +17,26 @@ public class SensorBehaviorData : IItemBehaviorData
|
|||
[InspectableField, JsonProperty("range"), Key(3)]
|
||||
public PerformanceStat Range = new PerformanceStat();
|
||||
|
||||
public IItemBehavior CreateInstance(GameContext context, Ship ship, Gear item)
|
||||
public IBehavior CreateInstance(GameContext context, Entity entity, Gear item)
|
||||
{
|
||||
return new SensorBehavior(context, this, ship, item);
|
||||
return new Sensor(context, this, entity, item);
|
||||
}
|
||||
}
|
||||
|
||||
public class SensorBehavior : IItemBehavior
|
||||
public class Sensor : IBehavior
|
||||
{
|
||||
private SensorBehaviorData _data;
|
||||
private SensorData _data;
|
||||
|
||||
public Ship Ship { get; }
|
||||
public Entity Entity { get; }
|
||||
public Gear Item { get; }
|
||||
public GameContext Context { get; }
|
||||
|
||||
public IItemBehaviorData Data => _data;
|
||||
public IBehaviorData Data => _data;
|
||||
|
||||
public SensorBehavior(GameContext context, SensorBehaviorData data, Ship ship, Gear item)
|
||||
public Sensor(GameContext context, SensorData data, Entity entity, Gear item)
|
||||
{
|
||||
_data = data;
|
||||
Ship = ship;
|
||||
Entity = entity;
|
||||
Item = item;
|
||||
Context = context;
|
||||
}
|
||||
|
|
@ -65,8 +65,4 @@ public class SensorBehavior : IItemBehavior
|
|||
// Hardpoint.Ship.VisibilitySources[this] = _data.Radiance.Evaluate(Hardpoint) / _data.RadianceMasking.Evaluate(Hardpoint);
|
||||
// TODO: Handle Active Detection / Visibility From Reflected Radiance
|
||||
}
|
||||
|
||||
public void FixedUpdate(float delta)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
@ -4,7 +4,7 @@ using Unity.Mathematics;
|
|||
using static Unity.Mathematics.math;
|
||||
|
||||
[MessagePackObject, JsonObject(MemberSerialization.OptIn)]
|
||||
public class ShieldBehaviorData : IItemBehaviorData
|
||||
public class ShieldData : IBehaviorData
|
||||
{
|
||||
[InspectableField, JsonProperty("efficiency"), Key(0)]
|
||||
public PerformanceStat Efficiency = new PerformanceStat();
|
||||
|
|
@ -12,36 +12,32 @@ public class ShieldBehaviorData : IItemBehaviorData
|
|||
[InspectableField, JsonProperty("shielding"), Key(1)]
|
||||
public PerformanceStat Shielding = new PerformanceStat();
|
||||
|
||||
public IItemBehavior CreateInstance(GameContext context, Ship ship, Gear item)
|
||||
public IBehavior CreateInstance(GameContext context, Entity entity, Gear item)
|
||||
{
|
||||
return new ShieldBehavior(context, this, ship, item);
|
||||
return new Shield(context, this, entity, item);
|
||||
}
|
||||
}
|
||||
|
||||
public class ShieldBehavior : IItemBehavior
|
||||
public class Shield : IBehavior
|
||||
{
|
||||
public Ship Ship { get; }
|
||||
public Entity Entity { get; }
|
||||
public Gear Item { get; }
|
||||
public GameContext Context { get; }
|
||||
|
||||
public IItemBehaviorData Data => _data;
|
||||
public IBehaviorData Data => _data;
|
||||
|
||||
private ShieldBehaviorData _data;
|
||||
private ShieldData _data;
|
||||
|
||||
public ShieldBehavior(GameContext context, ShieldBehaviorData data, Ship ship, Gear item)
|
||||
public Shield(GameContext context, ShieldData data, Entity entity, Gear item)
|
||||
{
|
||||
Context = context;
|
||||
_data = data;
|
||||
Ship = ship;
|
||||
Entity = entity;
|
||||
Item = item;
|
||||
|
||||
// TODO: Detect hits on Ship, keep a list of callbacks that modify the hit by reference
|
||||
}
|
||||
|
||||
public void FixedUpdate(float delta)
|
||||
{
|
||||
}
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
}
|
||||
|
|
@ -3,8 +3,8 @@ using Newtonsoft.Json;
|
|||
using Unity.Mathematics;
|
||||
using static Unity.Mathematics.math;
|
||||
|
||||
[MessagePackObject, JsonObject(MemberSerialization.OptIn)]
|
||||
public class ThrusterBehaviorData : IItemBehaviorData
|
||||
[MessagePackObject, JsonObject(MemberSerialization.OptIn), EntityTypeRestriction(typeof(Ship))]
|
||||
public class ThrusterData : IBehaviorData
|
||||
{
|
||||
[InspectableField, JsonProperty("thrust"), Key(0)]
|
||||
public PerformanceStat Thrust = new PerformanceStat();
|
||||
|
|
@ -18,29 +18,29 @@ public class ThrusterBehaviorData : IItemBehaviorData
|
|||
[InspectableField, JsonProperty("heat"), Key(3)]
|
||||
public PerformanceStat Heat = new PerformanceStat();
|
||||
|
||||
public IItemBehavior CreateInstance(GameContext context, Ship ship, Gear item)
|
||||
public IBehavior CreateInstance(GameContext context, Entity entity, Gear item)
|
||||
{
|
||||
return new ThrusterBehavior(context, this, ship, item);
|
||||
return new Thruster(context, this, entity, item);
|
||||
}
|
||||
}
|
||||
|
||||
public class ThrusterBehavior : IAnalogItemBehavior
|
||||
public class Thruster : IAnalogBehavior
|
||||
{
|
||||
public Ship Ship { get; }
|
||||
public Entity Entity { get; }
|
||||
public Gear Item { get; }
|
||||
public GameContext Context { get; }
|
||||
|
||||
public IItemBehaviorData Data => _data;
|
||||
public IBehaviorData Data => _data;
|
||||
|
||||
private ThrusterBehaviorData _data;
|
||||
private ThrusterData _data;
|
||||
|
||||
private float _thrust;
|
||||
|
||||
public ThrusterBehavior(GameContext context, ThrusterBehaviorData data, Ship ship, Gear item)
|
||||
public Thruster(GameContext context, ThrusterData data, Entity entity, Gear item)
|
||||
{
|
||||
Context = context;
|
||||
_data = data;
|
||||
Ship = ship;
|
||||
Entity = entity;
|
||||
Item = item;
|
||||
}
|
||||
|
||||
|
|
@ -49,19 +49,14 @@ public class ThrusterBehavior : IAnalogItemBehavior
|
|||
_thrust = saturate(value);
|
||||
}
|
||||
|
||||
public void FixedUpdate(float delta)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
}
|
||||
|
||||
public void Update(float delta)
|
||||
{
|
||||
Ship.Velocity += _thrust * Context.Evaluate(_data.Thrust, Item, Ship) * delta;
|
||||
Ship.AddHeat(_thrust * Context.Evaluate(_data.Heat, Item, Ship) * delta);
|
||||
Ship.VisibilitySources[this] = _thrust * Context.Evaluate(_data.Visibility, Item, Ship);
|
||||
((Ship) Entity).Velocity += _thrust * Context.Evaluate(_data.Thrust, Item, Entity) * delta;
|
||||
Entity.AddHeat(_thrust * Context.Evaluate(_data.Heat, Item, Entity) * delta);
|
||||
Entity.VisibilitySources[this] = _thrust * Context.Evaluate(_data.Visibility, Item, Entity);
|
||||
}
|
||||
}
|
||||
|
|
@ -4,7 +4,7 @@ using Unity.Mathematics;
|
|||
using static Unity.Mathematics.math;
|
||||
|
||||
[MessagePackObject, JsonObject(MemberSerialization.OptIn)]
|
||||
public class TurningBehaviorData : IItemBehaviorData
|
||||
public class TurningData : IBehaviorData
|
||||
{
|
||||
[InspectableField, JsonProperty("torque"), Key(0)]
|
||||
public PerformanceStat Torque = new PerformanceStat();
|
||||
|
|
@ -15,29 +15,29 @@ public class TurningBehaviorData : IItemBehaviorData
|
|||
[InspectableField, JsonProperty("heat"), Key(2)]
|
||||
public PerformanceStat Heat = new PerformanceStat();
|
||||
|
||||
public IItemBehavior CreateInstance(GameContext context, Ship ship, Gear item)
|
||||
public IBehavior CreateInstance(GameContext context, Entity entity, Gear item)
|
||||
{
|
||||
return new TurningBehavior(context, this, ship, item);
|
||||
return new Turning(context, this, entity, item);
|
||||
}
|
||||
}
|
||||
|
||||
public class TurningBehavior : IAnalogItemBehavior
|
||||
public class Turning : IAnalogBehavior
|
||||
{
|
||||
public Ship Ship { get; }
|
||||
public Entity Entity { get; }
|
||||
public Gear Item { get; }
|
||||
public GameContext Context { get; }
|
||||
|
||||
public IItemBehaviorData Data => _data;
|
||||
public IBehaviorData Data => _data;
|
||||
|
||||
private TurningBehaviorData _data;
|
||||
private TurningData _data;
|
||||
|
||||
private float _turning;
|
||||
|
||||
public TurningBehavior(GameContext context, TurningBehaviorData data, Ship ship, Gear item)
|
||||
public Turning(GameContext context, TurningData data, Entity entity, Gear item)
|
||||
{
|
||||
Context = context;
|
||||
_data = data;
|
||||
Ship = ship;
|
||||
Entity = entity;
|
||||
Item = item;
|
||||
}
|
||||
|
||||
|
|
@ -46,19 +46,14 @@ public class TurningBehavior : IAnalogItemBehavior
|
|||
_turning = clamp(value, -1, 1);
|
||||
}
|
||||
|
||||
public void FixedUpdate(float delta)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
}
|
||||
|
||||
public void Update(float delta)
|
||||
{
|
||||
Ship.Direction = mul(Ship.Direction, Unity.Mathematics.float2x2.Rotate(_turning * Context.Evaluate(_data.Torque, Item, Ship) * delta));
|
||||
Ship.AddHeat(abs(_turning) * Context.Evaluate(_data.Heat, Item, Ship) * delta);
|
||||
Ship.VisibilitySources[this] = abs(_turning) * Context.Evaluate(_data.Visibility, Item, Ship);
|
||||
Entity.Direction = mul(Entity.Direction, Unity.Mathematics.float2x2.Rotate(_turning * Context.Evaluate(_data.Torque, Item, Entity) * delta));
|
||||
Entity.AddHeat(abs(_turning) * Context.Evaluate(_data.Heat, Item, Entity) * delta);
|
||||
Entity.VisibilitySources[this] = abs(_turning) * Context.Evaluate(_data.Visibility, Item, Entity);
|
||||
}
|
||||
}
|
||||
43
Assets/Scripts/ServerShared/Behaviors/VelocityConversion.cs
Normal file
43
Assets/Scripts/ServerShared/Behaviors/VelocityConversion.cs
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
using MessagePack;
|
||||
using Newtonsoft.Json;
|
||||
using Unity.Mathematics;
|
||||
using static Unity.Mathematics.math;
|
||||
|
||||
[MessagePackObject, JsonObject(MemberSerialization.OptIn)]
|
||||
public class VelocityConversionData : IBehaviorData
|
||||
{
|
||||
[InspectableField, JsonProperty("traction"), Key(0)]
|
||||
public PerformanceStat Traction = new PerformanceStat();
|
||||
|
||||
public IBehavior CreateInstance(GameContext context, Entity entity, Gear item)
|
||||
{
|
||||
return new VelocityConversion(context, this, entity, item);
|
||||
}
|
||||
}
|
||||
|
||||
public class VelocityConversion : IBehavior
|
||||
{
|
||||
public Entity Entity { get; }
|
||||
public Gear Item { get; }
|
||||
public GameContext Context { get; }
|
||||
|
||||
public IBehaviorData Data => _data;
|
||||
|
||||
private VelocityConversionData _data;
|
||||
|
||||
public VelocityConversion(GameContext context, VelocityConversionData data, Entity entity, Gear item)
|
||||
{
|
||||
Context = context;
|
||||
_data = data;
|
||||
Entity = entity;
|
||||
Item = item;
|
||||
}
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
}
|
||||
|
||||
public void Update(float delta)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 38e5a766311c40aba86de1b72d2f410e
|
||||
timeCreated: 1586422223
|
||||
43
Assets/Scripts/ServerShared/Behaviors/VelocityLimit.cs
Normal file
43
Assets/Scripts/ServerShared/Behaviors/VelocityLimit.cs
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
using MessagePack;
|
||||
using Newtonsoft.Json;
|
||||
using Unity.Mathematics;
|
||||
using static Unity.Mathematics.math;
|
||||
|
||||
[MessagePackObject, JsonObject(MemberSerialization.OptIn)]
|
||||
public class VelocityLimitData : IBehaviorData
|
||||
{
|
||||
[InspectableField, JsonProperty("topSpeed"), Key(0)]
|
||||
public PerformanceStat TopSpeed = new PerformanceStat();
|
||||
|
||||
public IBehavior CreateInstance(GameContext context, Entity entity, Gear item)
|
||||
{
|
||||
return new VelocityLimit(context, this, entity, item);
|
||||
}
|
||||
}
|
||||
|
||||
public class VelocityLimit : IBehavior
|
||||
{
|
||||
public Entity Entity { get; }
|
||||
public Gear Item { get; }
|
||||
public GameContext Context { get; }
|
||||
|
||||
public IBehaviorData Data => _data;
|
||||
|
||||
private VelocityLimitData _data;
|
||||
|
||||
public VelocityLimit(GameContext context, VelocityLimitData data, Entity entity, Gear item)
|
||||
{
|
||||
Context = context;
|
||||
_data = data;
|
||||
Entity = entity;
|
||||
Item = item;
|
||||
}
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
}
|
||||
|
||||
public void Update(float delta)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 3e82fb4fadeb477ba6eec01f2d169f6e
|
||||
timeCreated: 1586415720
|
||||
|
|
@ -1,9 +1,9 @@
|
|||
using MessagePack;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
[Union(0, typeof(CannonBehaviorData)),
|
||||
Union(1, typeof(LauncherBehaviorData))]
|
||||
public abstract class WeaponBehaviorData
|
||||
[Union(0, typeof(CannonData)),
|
||||
Union(1, typeof(LauncherData))]
|
||||
public abstract class WeaponData
|
||||
{
|
||||
[InspectableField, JsonProperty("damageType"), Key(0)]
|
||||
public DamageType DamageType;
|
||||
|
|
@ -47,7 +47,7 @@ public class BlueprintStatEffect
|
|||
[InspectableField, MessagePackObject, JsonObject(MemberSerialization.OptIn)]
|
||||
public class StatReference
|
||||
{
|
||||
[InspectableType(typeof(IItemBehaviorData)), JsonProperty("behavior"), Key(1)]
|
||||
[InspectableType(typeof(IBehaviorData)), JsonProperty("behavior"), Key(1)]
|
||||
public string Behavior;
|
||||
|
||||
[InspectableField, JsonProperty("stat"), Key(2)]
|
||||
|
|
|
|||
88
Assets/Scripts/ServerShared/Entity.cs
Normal file
88
Assets/Scripts/ServerShared/Entity.cs
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using Unity.Mathematics;
|
||||
using static Unity.Mathematics.math;
|
||||
|
||||
public class Entity
|
||||
{
|
||||
public Dictionary<IAnalogBehavior, float> AxisOverrides = new Dictionary<IAnalogBehavior, float>();
|
||||
public readonly Dictionary<object, float> VisibilitySources = new Dictionary<object, float>();
|
||||
public float Temperature;
|
||||
public float Energy;
|
||||
public float2 Position;
|
||||
public float2 Direction;
|
||||
|
||||
protected readonly GameContext Context;
|
||||
protected readonly List<IBehavior> Behaviors;
|
||||
protected readonly Dictionary<Guid, List<IActivatedBehavior>> Bindings;
|
||||
protected readonly Dictionary<IAnalogBehavior, float> Axes;
|
||||
protected readonly List<Gear> Items;
|
||||
protected readonly List<ItemInstance> Cargo;
|
||||
|
||||
public float Mass { get; private set; }
|
||||
public float SpecificHeat { get; private set; }
|
||||
public float Visibility => VisibilitySources.Values.Sum();
|
||||
|
||||
public Entity(GameContext context, IEnumerable<Gear> items, IEnumerable<ItemInstance> cargo)
|
||||
{
|
||||
Context = context;
|
||||
|
||||
Items = items.ToList();
|
||||
Cargo = cargo.ToList();
|
||||
|
||||
Mass = Items.Sum(i => i.Mass) +
|
||||
Cargo.Sum(ii => ii.Mass);
|
||||
|
||||
SpecificHeat = Items.Sum(i => i.HeatCapacity) +
|
||||
Cargo.Sum(ii => ii.HeatCapacity);
|
||||
|
||||
Behaviors = Items
|
||||
.Where(i=>i.ItemData.Behaviors?.Any()??false)
|
||||
.SelectMany(i => i.ItemData.Behaviors
|
||||
.Select(bd => bd.CreateInstance(Context, this, i)))
|
||||
.OrderBy(b => b.GetType().GetCustomAttribute<UpdateOrderAttribute>()?.Order ?? 0).ToList();
|
||||
|
||||
Bindings = Behaviors
|
||||
.Where(b => b is IActivatedBehavior)
|
||||
.GroupBy(b => b.Item.ID, (guid, behaviors) => new {guid, behaviors})
|
||||
.ToDictionary(g => g.guid, g => g.behaviors.Cast<IActivatedBehavior>().ToList());
|
||||
|
||||
Axes = Behaviors
|
||||
.Where(b => b is IAnalogBehavior)
|
||||
.ToDictionary(b => b as IAnalogBehavior, b => 0f);
|
||||
}
|
||||
|
||||
public IEnumerable<T> GetBehaviors<T>() where T : class, IBehavior
|
||||
{
|
||||
foreach (var behavior in Behaviors)
|
||||
if (behavior is T b)
|
||||
yield return b;
|
||||
}
|
||||
|
||||
public IEnumerable<T> GetBehaviorData<T>() where T : class, IBehaviorData
|
||||
{
|
||||
foreach (var behavior in Behaviors)
|
||||
if (behavior.Data is T b)
|
||||
yield return b;
|
||||
}
|
||||
|
||||
public void AddHeat(float heat)
|
||||
{
|
||||
Temperature += heat / SpecificHeat;
|
||||
}
|
||||
|
||||
public virtual void Update(float delta)
|
||||
{
|
||||
foreach(var analogBehavior in Axes.Keys)
|
||||
analogBehavior.SetAxis(AxisOverrides.ContainsKey(analogBehavior) ? AxisOverrides[analogBehavior] : Axes[analogBehavior]);
|
||||
|
||||
foreach(var behavior in Behaviors)
|
||||
behavior.Update(delta);
|
||||
|
||||
foreach (var item in Items.Where(item => item.ItemData.Performance(Temperature) < .01f))
|
||||
item.Durability -= delta;
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/ServerShared/Entity.cs.meta
Normal file
3
Assets/Scripts/ServerShared/Entity.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: c6107172d8b1422b86efab26975753d7
|
||||
timeCreated: 1586417357
|
||||
|
|
@ -165,17 +165,17 @@ public class GameContext
|
|||
}
|
||||
|
||||
// Returns stat using ship temperature and modifiers
|
||||
public float Evaluate(PerformanceStat stat, Gear item, Ship ship)
|
||||
public float Evaluate(PerformanceStat stat, Gear item, Entity entity)
|
||||
{
|
||||
var itemData = GetData(item);
|
||||
|
||||
var heat = !stat.HeatDependent ? 1 : pow(itemData.Performance(ship.Temperature), Evaluate(itemData.HeatExponent,item));
|
||||
var heat = !stat.HeatDependent ? 1 : pow(itemData.Performance(entity.Temperature), Evaluate(itemData.HeatExponent,item));
|
||||
var durability = !stat.DurabilityDependent ? 1 : pow(item.Durability / itemData.Durability, Evaluate(itemData.DurabilityExponent,item));
|
||||
var quality = pow(Quality(stat, item), stat.QualityExponent);
|
||||
|
||||
var scaleModifier = stat.GetScaleModifiers(ship).Values.Aggregate(1.0f, (current, mod) => current * mod);
|
||||
var scaleModifier = stat.GetScaleModifiers(entity).Values.Aggregate(1.0f, (current, mod) => current * mod);
|
||||
|
||||
var constantModifier = stat.GetConstantModifiers(ship).Values.Sum();
|
||||
var constantModifier = stat.GetConstantModifiers(entity).Values.Sum();
|
||||
|
||||
var result = lerp(stat.Min, stat.Max, heat * durability * quality) * scaleModifier + constantModifier;
|
||||
if (float.IsNaN(result))
|
||||
|
|
|
|||
|
|
@ -1,45 +0,0 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using JsonKnownTypes;
|
||||
using MessagePack;
|
||||
using Newtonsoft.Json;
|
||||
using Unity.Mathematics;
|
||||
using static Unity.Mathematics.math;
|
||||
using static Unity.Mathematics.noise;
|
||||
|
||||
public interface IItemBehavior
|
||||
{
|
||||
void Initialize();
|
||||
void Update(float delta);
|
||||
void FixedUpdate(float delta);
|
||||
Ship Ship { get; }
|
||||
Gear Item { get; }
|
||||
GameContext Context { get; }
|
||||
IItemBehaviorData Data { get; }
|
||||
}
|
||||
|
||||
public interface IActivatedItemBehavior : IItemBehavior
|
||||
{
|
||||
void Activate();
|
||||
void Deactivate();
|
||||
}
|
||||
|
||||
public interface IAnalogItemBehavior : IItemBehavior
|
||||
{
|
||||
void SetAxis(float value);
|
||||
}
|
||||
|
||||
[InspectableField,
|
||||
Union(0, typeof(CannonBehaviorData)),
|
||||
Union(1, typeof(LauncherBehaviorData)),
|
||||
Union(2, typeof(ReactorBehaviorData)),
|
||||
Union(4, typeof(AfterburnerBehaviorData)),
|
||||
Union(5, typeof(SensorBehaviorData)),
|
||||
JsonConverter(typeof(JsonKnownTypesConverter<IItemBehaviorData>)), JsonObject(MemberSerialization.OptIn)]
|
||||
// [Union(3, typeof(PlusUltraBehaviorData))]
|
||||
public interface IItemBehaviorData
|
||||
{
|
||||
IItemBehavior CreateInstance(GameContext context, Ship ship, Gear item);
|
||||
}
|
||||
|
|
@ -94,7 +94,7 @@ public abstract class EquippableItemData : CraftedItemData
|
|||
public PerformanceStat HeatExponent = new PerformanceStat();
|
||||
|
||||
[InspectableField, JsonProperty("behaviors"), Key(13)]
|
||||
public List<IItemBehaviorData> Behaviors = new List<IItemBehaviorData>();
|
||||
public List<IBehaviorData> Behaviors = new List<IBehaviorData>();
|
||||
|
||||
[IgnoreMember]
|
||||
public abstract HardpointType HardpointType { get; }
|
||||
|
|
@ -143,25 +143,10 @@ public class HullData : EquippableItemData
|
|||
[InspectableField, JsonProperty("hullCapacity"), Key(14)]
|
||||
public PerformanceStat Capacity = new PerformanceStat();
|
||||
|
||||
[InspectableField, JsonProperty("traction"), Key(15)]
|
||||
public PerformanceStat Traction = new PerformanceStat();
|
||||
|
||||
[InspectableField, JsonProperty("topSpeed"), Key(16)]
|
||||
public PerformanceStat TopSpeed = new PerformanceStat();
|
||||
|
||||
[InspectableField, JsonProperty("emissivity"), Key(17)]
|
||||
public PerformanceStat Emissivity = new PerformanceStat();
|
||||
|
||||
[InspectableField, JsonProperty("crossSection"), Key(18)]
|
||||
public PerformanceStat CrossSection = new PerformanceStat();
|
||||
|
||||
[InspectableField, JsonProperty("hardpoints"), Key(19)]
|
||||
[InspectableField, JsonProperty("hardpoints"), Key(15)]
|
||||
public List<HardpointData> Hardpoints = new List<HardpointData>();
|
||||
|
||||
[InspectableAnimationCurve, JsonProperty("visibility"), Key(20)]
|
||||
public float4[] VisibilityCurve;
|
||||
|
||||
[InspectablePrefab, JsonProperty("prefab"), Key(21)]
|
||||
[InspectablePrefab, JsonProperty("prefab"), Key(16)]
|
||||
public string Prefab;
|
||||
|
||||
[IgnoreMember] public override HardpointType HardpointType => HardpointType.Hull;
|
||||
|
|
@ -193,30 +178,30 @@ public class PerformanceStat
|
|||
|
||||
// [JsonProperty("ingredient"), Key(5)] public Guid? Ingredient;
|
||||
|
||||
[IgnoreMember] private Dictionary<Ship,Dictionary<IItemBehavior,float>> _scaleModifiers;
|
||||
[IgnoreMember] private Dictionary<Ship,Dictionary<IItemBehavior,float>> _constantModifiers;
|
||||
[IgnoreMember] private Dictionary<Entity,Dictionary<IBehavior,float>> _scaleModifiers;
|
||||
[IgnoreMember] private Dictionary<Entity,Dictionary<IBehavior,float>> _constantModifiers;
|
||||
|
||||
[IgnoreMember]
|
||||
private Dictionary<Ship, Dictionary<IItemBehavior, float>> ScaleModifiers =>
|
||||
_scaleModifiers ?? (_scaleModifiers = new Dictionary<Ship, Dictionary<IItemBehavior, float>>());
|
||||
private Dictionary<Entity, Dictionary<IBehavior, float>> ScaleModifiers =>
|
||||
_scaleModifiers ?? (_scaleModifiers = new Dictionary<Entity, Dictionary<IBehavior, float>>());
|
||||
|
||||
[IgnoreMember]
|
||||
private Dictionary<Ship, Dictionary<IItemBehavior, float>> ConstantModifiers =>
|
||||
_constantModifiers ?? (_constantModifiers = new Dictionary<Ship, Dictionary<IItemBehavior, float>>());
|
||||
private Dictionary<Entity, Dictionary<IBehavior, float>> ConstantModifiers =>
|
||||
_constantModifiers ?? (_constantModifiers = new Dictionary<Entity, Dictionary<IBehavior, float>>());
|
||||
|
||||
public Dictionary<IItemBehavior, float> GetScaleModifiers(Ship ship)
|
||||
public Dictionary<IBehavior, float> GetScaleModifiers(Entity entity)
|
||||
{
|
||||
if(!ScaleModifiers.ContainsKey(ship))
|
||||
ScaleModifiers[ship] = new Dictionary<IItemBehavior, float>();
|
||||
if(!ScaleModifiers.ContainsKey(entity))
|
||||
ScaleModifiers[entity] = new Dictionary<IBehavior, float>();
|
||||
|
||||
return ScaleModifiers[ship];
|
||||
return ScaleModifiers[entity];
|
||||
}
|
||||
|
||||
public Dictionary<IItemBehavior, float> GetConstantModifiers(Ship ship)
|
||||
public Dictionary<IBehavior, float> GetConstantModifiers(Entity entity)
|
||||
{
|
||||
if(!ConstantModifiers.ContainsKey(ship))
|
||||
ConstantModifiers[ship] = new Dictionary<IItemBehavior, float>();
|
||||
if(!ConstantModifiers.ContainsKey(entity))
|
||||
ConstantModifiers[entity] = new Dictionary<IBehavior, float>();
|
||||
|
||||
return ConstantModifiers[ship];
|
||||
return ConstantModifiers[entity];
|
||||
}
|
||||
}
|
||||
|
|
@ -8,7 +8,7 @@ using Unity.Mathematics;
|
|||
using static Unity.Mathematics.math;
|
||||
|
||||
[MessagePackObject]
|
||||
public class Ship : DatabaseEntry
|
||||
public class Ship : Entity
|
||||
{
|
||||
[Key("hull")]
|
||||
public Gear Hull;
|
||||
|
|
@ -19,26 +19,17 @@ public class Ship : DatabaseEntry
|
|||
|
||||
// [Key("bindings")] public Dictionary<KeyCode,Guid> Bindings = new Dictionary<KeyCode,Guid>();
|
||||
//[IgnoreMember] public int HullHardpointCount;
|
||||
[IgnoreMember] public Dictionary<Guid, IActivatedItemBehavior[]> ItemBindings;
|
||||
[IgnoreMember] public IItemBehavior[] ItemBehaviors;
|
||||
|
||||
[IgnoreMember] public bool ForceThrust;
|
||||
// [IgnoreMember] public Dictionary<Targetable, float> Contacts = new Dictionary<Targetable, float>();
|
||||
[IgnoreMember] public float Temperature;
|
||||
[IgnoreMember] public float Charge;
|
||||
// [IgnoreMember] public Targetable Target;
|
||||
[IgnoreMember] public float2 Position;
|
||||
[IgnoreMember] public float2 Direction;
|
||||
[IgnoreMember] public float2 Velocity;
|
||||
[IgnoreMember] public Dictionary<object, float> VisibilitySources = new Dictionary<object, float>();
|
||||
|
||||
private HullData _hullData;
|
||||
private bool _hydrated = false;
|
||||
|
||||
[IgnoreMember] public float Mass { get; private set; }
|
||||
[IgnoreMember] public float SpecificHeat { get; private set; }
|
||||
|
||||
//[IgnoreMember] public float Visibility => VisibilitySources.Values.Sum();
|
||||
|
||||
public Ship(GameContext context, IEnumerable<Gear> items, IEnumerable<ItemInstance> cargo) : base(context, items, cargo)
|
||||
{
|
||||
}
|
||||
|
||||
public Gear GetEquipped(HardpointType type)
|
||||
{
|
||||
|
|
@ -53,53 +44,15 @@ public class Ship : DatabaseEntry
|
|||
return null;
|
||||
}
|
||||
|
||||
public IEnumerable<T> GetBehaviors<T>() where T : class, IItemBehavior
|
||||
{
|
||||
foreach (var behavior in ItemBehaviors)
|
||||
if (behavior is T b)
|
||||
yield return b;
|
||||
}
|
||||
|
||||
public IEnumerable<T> GetBehaviorData<T>() where T : class, IItemBehaviorData
|
||||
{
|
||||
foreach (var behavior in ItemBehaviors)
|
||||
if (behavior.Data is T b)
|
||||
yield return b;
|
||||
}
|
||||
|
||||
public void AddHeat(float heat)
|
||||
{
|
||||
Temperature += heat / SpecificHeat;
|
||||
}
|
||||
|
||||
public void Hydrate()
|
||||
{
|
||||
GenerateBehaviors();
|
||||
|
||||
Mass = Hull.Mass +
|
||||
Hardpoints.Sum(hp => hp.Item?.Mass ?? 0) +
|
||||
Cargo.Sum(ii => ii.Mass);
|
||||
|
||||
SpecificHeat = Hull.HeatCapacity +
|
||||
Hardpoints.Sum(hp => hp.Item?.HeatCapacity ?? 0) +
|
||||
Cargo.Sum(ii => ii.HeatCapacity);
|
||||
|
||||
//HullHardpointCount = Hardpoints.Count(hp => hp.HardpointData.Type == HardpointType.Hull);
|
||||
_hydrated = true;
|
||||
}
|
||||
|
||||
private void GenerateBehaviors()
|
||||
{
|
||||
ItemBehaviors = Hardpoints.Where(hp=>hp.Item!=null)
|
||||
.Where(hp=>hp.Item.ItemData.Behaviors?.Any()??false)
|
||||
.SelectMany(hp => hp.Item.ItemData.Behaviors
|
||||
.Select(bd => bd.CreateInstance(Context, this, hp.Item)))
|
||||
.OrderBy(b => b.GetType().GetCustomAttribute<UpdateOrderAttribute>()?.Order ?? 0).ToArray();
|
||||
|
||||
ItemBindings = ItemBehaviors
|
||||
.Where(b => b is IActivatedItemBehavior)
|
||||
.GroupBy(b => b.Item.ID, (guid, behaviors) => new {guid, behaviors})
|
||||
.ToDictionary(g => g.guid, g => g.behaviors.Cast<IActivatedItemBehavior>().ToArray());
|
||||
}
|
||||
|
||||
// public void GenerateBindings()
|
||||
|
|
@ -162,8 +115,9 @@ public class Ship : DatabaseEntry
|
|||
// }
|
||||
// }
|
||||
|
||||
public void FixedUpdate(float delta)
|
||||
public override void Update(float delta)
|
||||
{
|
||||
base.Update(delta);
|
||||
// foreach (var kvp in Contacts.ToArray())
|
||||
// if (kvp.Key == null || Time.time - kvp.Value > GlobalData.Instance.TargetPersistenceDuration)
|
||||
// {
|
||||
|
|
@ -171,30 +125,6 @@ public class Ship : DatabaseEntry
|
|||
// if (kvp.Key == Target)
|
||||
// Target = null;
|
||||
// }
|
||||
|
||||
if (_hydrated)
|
||||
{
|
||||
var hull = Hull.ItemData;
|
||||
|
||||
var rad = pow(Temperature, Context.GlobalData.HeatRadiationPower) * Context.GlobalData.HeatRadiationMultiplier;
|
||||
Temperature -= rad * delta;
|
||||
VisibilitySources[this] = rad;
|
||||
|
||||
foreach(var behavior in ItemBehaviors)
|
||||
behavior.FixedUpdate(delta);
|
||||
|
||||
foreach (var hardpoint in Hardpoints)
|
||||
{
|
||||
if (hardpoint.Item.ItemData.Performance(Temperature) < .01f)
|
||||
hardpoint.Item.Durability -= delta;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Update(float delta)
|
||||
{
|
||||
foreach(var behavior in ItemBehaviors)
|
||||
behavior.Update(delta);
|
||||
}
|
||||
|
||||
// public void UpdateInput(float delta)
|
||||
|
|
|
|||
Loading…
Reference in a new issue