diff --git a/.idea/.idea.Aetheria-Economy/.idea/contentModel.xml b/.idea/.idea.Aetheria-Economy/.idea/contentModel.xml index 962d138ae..b3a272637 100644 --- a/.idea/.idea.Aetheria-Economy/.idea/contentModel.xml +++ b/.idea/.idea.Aetheria-Economy/.idea/contentModel.xml @@ -660,26 +660,31 @@ - - - - - - - - - + + + + + + + + + + + + + + diff --git a/.idea/.idea.Economy/.idea/contentModel.xml b/.idea/.idea.Economy/.idea/contentModel.xml index 08f0909c6..8634bdab1 100644 --- a/.idea/.idea.Economy/.idea/contentModel.xml +++ b/.idea/.idea.Economy/.idea/contentModel.xml @@ -6,25 +6,29 @@ + + + + + + + + + + + + + + + - - - - - - - - - - - - + diff --git a/Assets/Scripts/ServerShared/Attributes.cs b/Assets/Scripts/ServerShared/Attributes.cs index 06b99ec0e..0c7a4b35c 100644 --- a/Assets/Scripts/ServerShared/Attributes.cs +++ b/Assets/Scripts/ServerShared/Attributes.cs @@ -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 { } diff --git a/Assets/Scripts/ServerShared/ItemBehaviors.meta b/Assets/Scripts/ServerShared/Behaviors.meta similarity index 100% rename from Assets/Scripts/ServerShared/ItemBehaviors.meta rename to Assets/Scripts/ServerShared/Behaviors.meta diff --git a/Assets/Scripts/ServerShared/ItemBehaviors/Afterburner.cs b/Assets/Scripts/ServerShared/Behaviors/Afterburner.cs similarity index 56% rename from Assets/Scripts/ServerShared/ItemBehaviors/Afterburner.cs rename to Assets/Scripts/ServerShared/Behaviors/Afterburner.cs index 073f1b28c..122e21409 100644 --- a/Assets/Scripts/ServerShared/ItemBehaviors/Afterburner.cs +++ b/Assets/Scripts/ServerShared/Behaviors/Afterburner.cs @@ -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> _modifiers = new List>(); - private AfterburnerBehaviorData _data; - private ThrusterBehaviorData[] _thrusters; + private List> _modifiers = new List>(); + 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().ToArray(); + _thruster = Entity.GetBehaviors().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); diff --git a/Assets/Scripts/ServerShared/ItemBehaviors/Afterburner.cs.meta b/Assets/Scripts/ServerShared/Behaviors/Afterburner.cs.meta similarity index 100% rename from Assets/Scripts/ServerShared/ItemBehaviors/Afterburner.cs.meta rename to Assets/Scripts/ServerShared/Behaviors/Afterburner.cs.meta diff --git a/Assets/Scripts/ServerShared/Behaviors/Behaviors.cs b/Assets/Scripts/ServerShared/Behaviors/Behaviors.cs new file mode 100644 index 000000000..4ee3f6aee --- /dev/null +++ b/Assets/Scripts/ServerShared/Behaviors/Behaviors.cs @@ -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)), JsonObject(MemberSerialization.OptIn)] +public interface IBehaviorData +{ + IBehavior CreateInstance(GameContext context, Entity entity, Gear item); +} \ No newline at end of file diff --git a/Assets/Scripts/ServerShared/ItemBehaviors/Behaviors.cs.meta b/Assets/Scripts/ServerShared/Behaviors/Behaviors.cs.meta similarity index 100% rename from Assets/Scripts/ServerShared/ItemBehaviors/Behaviors.cs.meta rename to Assets/Scripts/ServerShared/Behaviors/Behaviors.cs.meta diff --git a/Assets/Scripts/ServerShared/ItemBehaviors/Cannon.cs b/Assets/Scripts/ServerShared/Behaviors/Cannon.cs similarity index 82% rename from Assets/Scripts/ServerShared/ItemBehaviors/Cannon.cs rename to Assets/Scripts/ServerShared/Behaviors/Cannon.cs index 12ad73d3c..815173f4c 100644 --- a/Assets/Scripts/ServerShared/ItemBehaviors/Cannon.cs +++ b/Assets/Scripts/ServerShared/Behaviors/Cannon.cs @@ -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; } \ No newline at end of file diff --git a/Assets/Scripts/ServerShared/ItemBehaviors/Cannon.cs.meta b/Assets/Scripts/ServerShared/Behaviors/Cannon.cs.meta similarity index 100% rename from Assets/Scripts/ServerShared/ItemBehaviors/Cannon.cs.meta rename to Assets/Scripts/ServerShared/Behaviors/Cannon.cs.meta diff --git a/Assets/Scripts/ServerShared/ItemBehaviors/Launcher.cs b/Assets/Scripts/ServerShared/Behaviors/Launcher.cs similarity index 90% rename from Assets/Scripts/ServerShared/ItemBehaviors/Launcher.cs rename to Assets/Scripts/ServerShared/Behaviors/Launcher.cs index 9fab8cbc3..e98f76b42 100644 --- a/Assets/Scripts/ServerShared/ItemBehaviors/Launcher.cs +++ b/Assets/Scripts/ServerShared/Behaviors/Launcher.cs @@ -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(); // 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; } \ No newline at end of file diff --git a/Assets/Scripts/ServerShared/ItemBehaviors/Launcher.cs.meta b/Assets/Scripts/ServerShared/Behaviors/Launcher.cs.meta similarity index 100% rename from Assets/Scripts/ServerShared/ItemBehaviors/Launcher.cs.meta rename to Assets/Scripts/ServerShared/Behaviors/Launcher.cs.meta diff --git a/Assets/Scripts/ServerShared/Behaviors/Radiator.cs b/Assets/Scripts/ServerShared/Behaviors/Radiator.cs new file mode 100644 index 000000000..474468064 --- /dev/null +++ b/Assets/Scripts/ServerShared/Behaviors/Radiator.cs @@ -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; + } +} \ No newline at end of file diff --git a/Assets/Scripts/ServerShared/Behaviors/Radiator.cs.meta b/Assets/Scripts/ServerShared/Behaviors/Radiator.cs.meta new file mode 100644 index 000000000..d37dd3f27 --- /dev/null +++ b/Assets/Scripts/ServerShared/Behaviors/Radiator.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: f438ee8944644dd6925e2ee9d65229e8 +timeCreated: 1586415683 \ No newline at end of file diff --git a/Assets/Scripts/ServerShared/ItemBehaviors/Reactor.cs b/Assets/Scripts/ServerShared/Behaviors/Reactor.cs similarity index 50% rename from Assets/Scripts/ServerShared/ItemBehaviors/Reactor.cs rename to Assets/Scripts/ServerShared/Behaviors/Reactor.cs index f18578c55..5464dc8a1 100644 --- a/Assets/Scripts/ServerShared/ItemBehaviors/Reactor.cs +++ b/Assets/Scripts/ServerShared/Behaviors/Reactor.cs @@ -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; + } } } \ No newline at end of file diff --git a/Assets/Scripts/ServerShared/ItemBehaviors/Reactor.cs.meta b/Assets/Scripts/ServerShared/Behaviors/Reactor.cs.meta similarity index 100% rename from Assets/Scripts/ServerShared/ItemBehaviors/Reactor.cs.meta rename to Assets/Scripts/ServerShared/Behaviors/Reactor.cs.meta diff --git a/Assets/Scripts/ServerShared/Behaviors/Reflector.cs b/Assets/Scripts/ServerShared/Behaviors/Reflector.cs new file mode 100644 index 000000000..508609e0c --- /dev/null +++ b/Assets/Scripts/ServerShared/Behaviors/Reflector.cs @@ -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) + { + } +} \ No newline at end of file diff --git a/Assets/Scripts/ServerShared/Behaviors/Reflector.cs.meta b/Assets/Scripts/ServerShared/Behaviors/Reflector.cs.meta new file mode 100644 index 000000000..8f2388276 --- /dev/null +++ b/Assets/Scripts/ServerShared/Behaviors/Reflector.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: d3eb7e73847b493991239c1beb8404c8 +timeCreated: 1586415699 \ No newline at end of file diff --git a/Assets/Scripts/ServerShared/ItemBehaviors/Sensor.cs b/Assets/Scripts/ServerShared/Behaviors/Sensor.cs similarity index 79% rename from Assets/Scripts/ServerShared/ItemBehaviors/Sensor.cs rename to Assets/Scripts/ServerShared/Behaviors/Sensor.cs index a1aa59d65..729130540 100644 --- a/Assets/Scripts/ServerShared/ItemBehaviors/Sensor.cs +++ b/Assets/Scripts/ServerShared/Behaviors/Sensor.cs @@ -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) - { - } } \ No newline at end of file diff --git a/Assets/Scripts/ServerShared/ItemBehaviors/Sensor.cs.meta b/Assets/Scripts/ServerShared/Behaviors/Sensor.cs.meta similarity index 100% rename from Assets/Scripts/ServerShared/ItemBehaviors/Sensor.cs.meta rename to Assets/Scripts/ServerShared/Behaviors/Sensor.cs.meta diff --git a/Assets/Scripts/ServerShared/ItemBehaviors/Shield.cs b/Assets/Scripts/ServerShared/Behaviors/Shield.cs similarity index 60% rename from Assets/Scripts/ServerShared/ItemBehaviors/Shield.cs rename to Assets/Scripts/ServerShared/Behaviors/Shield.cs index 9f6d47219..c9e61a423 100644 --- a/Assets/Scripts/ServerShared/ItemBehaviors/Shield.cs +++ b/Assets/Scripts/ServerShared/Behaviors/Shield.cs @@ -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() { } diff --git a/Assets/Scripts/ServerShared/ItemBehaviors/Shield.cs.meta b/Assets/Scripts/ServerShared/Behaviors/Shield.cs.meta similarity index 100% rename from Assets/Scripts/ServerShared/ItemBehaviors/Shield.cs.meta rename to Assets/Scripts/ServerShared/Behaviors/Shield.cs.meta diff --git a/Assets/Scripts/ServerShared/ItemBehaviors/Thruster.cs b/Assets/Scripts/ServerShared/Behaviors/Thruster.cs similarity index 52% rename from Assets/Scripts/ServerShared/ItemBehaviors/Thruster.cs rename to Assets/Scripts/ServerShared/Behaviors/Thruster.cs index f30d1721e..7b972d3e1 100644 --- a/Assets/Scripts/ServerShared/ItemBehaviors/Thruster.cs +++ b/Assets/Scripts/ServerShared/Behaviors/Thruster.cs @@ -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); } } \ No newline at end of file diff --git a/Assets/Scripts/ServerShared/ItemBehaviors/Thruster.cs.meta b/Assets/Scripts/ServerShared/Behaviors/Thruster.cs.meta similarity index 100% rename from Assets/Scripts/ServerShared/ItemBehaviors/Thruster.cs.meta rename to Assets/Scripts/ServerShared/Behaviors/Thruster.cs.meta diff --git a/Assets/Scripts/ServerShared/ItemBehaviors/Turning.cs b/Assets/Scripts/ServerShared/Behaviors/Turning.cs similarity index 51% rename from Assets/Scripts/ServerShared/ItemBehaviors/Turning.cs rename to Assets/Scripts/ServerShared/Behaviors/Turning.cs index 8dbc7ac9a..fb5cc601d 100644 --- a/Assets/Scripts/ServerShared/ItemBehaviors/Turning.cs +++ b/Assets/Scripts/ServerShared/Behaviors/Turning.cs @@ -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); } } \ No newline at end of file diff --git a/Assets/Scripts/ServerShared/ItemBehaviors/Turning.cs.meta b/Assets/Scripts/ServerShared/Behaviors/Turning.cs.meta similarity index 100% rename from Assets/Scripts/ServerShared/ItemBehaviors/Turning.cs.meta rename to Assets/Scripts/ServerShared/Behaviors/Turning.cs.meta diff --git a/Assets/Scripts/ServerShared/Behaviors/VelocityConversion.cs b/Assets/Scripts/ServerShared/Behaviors/VelocityConversion.cs new file mode 100644 index 000000000..283fd1494 --- /dev/null +++ b/Assets/Scripts/ServerShared/Behaviors/VelocityConversion.cs @@ -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) + { + } +} \ No newline at end of file diff --git a/Assets/Scripts/ServerShared/Behaviors/VelocityConversion.cs.meta b/Assets/Scripts/ServerShared/Behaviors/VelocityConversion.cs.meta new file mode 100644 index 000000000..3750705fa --- /dev/null +++ b/Assets/Scripts/ServerShared/Behaviors/VelocityConversion.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 38e5a766311c40aba86de1b72d2f410e +timeCreated: 1586422223 \ No newline at end of file diff --git a/Assets/Scripts/ServerShared/Behaviors/VelocityLimit.cs b/Assets/Scripts/ServerShared/Behaviors/VelocityLimit.cs new file mode 100644 index 000000000..c913b516d --- /dev/null +++ b/Assets/Scripts/ServerShared/Behaviors/VelocityLimit.cs @@ -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) + { + } +} \ No newline at end of file diff --git a/Assets/Scripts/ServerShared/Behaviors/VelocityLimit.cs.meta b/Assets/Scripts/ServerShared/Behaviors/VelocityLimit.cs.meta new file mode 100644 index 000000000..e0ffc85b4 --- /dev/null +++ b/Assets/Scripts/ServerShared/Behaviors/VelocityLimit.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 3e82fb4fadeb477ba6eec01f2d169f6e +timeCreated: 1586415720 \ No newline at end of file diff --git a/Assets/Scripts/ServerShared/ItemBehaviors/Weapon.cs b/Assets/Scripts/ServerShared/Behaviors/Weapon.cs similarity index 89% rename from Assets/Scripts/ServerShared/ItemBehaviors/Weapon.cs rename to Assets/Scripts/ServerShared/Behaviors/Weapon.cs index 2390c294d..cc5a6f0e5 100644 --- a/Assets/Scripts/ServerShared/ItemBehaviors/Weapon.cs +++ b/Assets/Scripts/ServerShared/Behaviors/Weapon.cs @@ -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; diff --git a/Assets/Scripts/ServerShared/ItemBehaviors/Weapon.cs.meta b/Assets/Scripts/ServerShared/Behaviors/Weapon.cs.meta similarity index 100% rename from Assets/Scripts/ServerShared/ItemBehaviors/Weapon.cs.meta rename to Assets/Scripts/ServerShared/Behaviors/Weapon.cs.meta diff --git a/Assets/Scripts/ServerShared/BlueprintData.cs b/Assets/Scripts/ServerShared/BlueprintData.cs index 00c16d9b7..424fe0ff5 100644 --- a/Assets/Scripts/ServerShared/BlueprintData.cs +++ b/Assets/Scripts/ServerShared/BlueprintData.cs @@ -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)] diff --git a/Assets/Scripts/ServerShared/Entity.cs b/Assets/Scripts/ServerShared/Entity.cs new file mode 100644 index 000000000..1e51fc5f3 --- /dev/null +++ b/Assets/Scripts/ServerShared/Entity.cs @@ -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 AxisOverrides = new Dictionary(); + public readonly Dictionary VisibilitySources = new Dictionary(); + public float Temperature; + public float Energy; + public float2 Position; + public float2 Direction; + + protected readonly GameContext Context; + protected readonly List Behaviors; + protected readonly Dictionary> Bindings; + protected readonly Dictionary Axes; + protected readonly List Items; + protected readonly List Cargo; + + public float Mass { get; private set; } + public float SpecificHeat { get; private set; } + public float Visibility => VisibilitySources.Values.Sum(); + + public Entity(GameContext context, IEnumerable items, IEnumerable 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()?.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().ToList()); + + Axes = Behaviors + .Where(b => b is IAnalogBehavior) + .ToDictionary(b => b as IAnalogBehavior, b => 0f); + } + + public IEnumerable GetBehaviors() where T : class, IBehavior + { + foreach (var behavior in Behaviors) + if (behavior is T b) + yield return b; + } + + public IEnumerable GetBehaviorData() 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; + } +} \ No newline at end of file diff --git a/Assets/Scripts/ServerShared/Entity.cs.meta b/Assets/Scripts/ServerShared/Entity.cs.meta new file mode 100644 index 000000000..742390131 --- /dev/null +++ b/Assets/Scripts/ServerShared/Entity.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: c6107172d8b1422b86efab26975753d7 +timeCreated: 1586417357 \ No newline at end of file diff --git a/Assets/Scripts/ServerShared/GameContext.cs b/Assets/Scripts/ServerShared/GameContext.cs index 9b77e3dc9..7575e7c67 100644 --- a/Assets/Scripts/ServerShared/GameContext.cs +++ b/Assets/Scripts/ServerShared/GameContext.cs @@ -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)) diff --git a/Assets/Scripts/ServerShared/ItemBehaviors/Behaviors.cs b/Assets/Scripts/ServerShared/ItemBehaviors/Behaviors.cs deleted file mode 100644 index 3ad7b7720..000000000 --- a/Assets/Scripts/ServerShared/ItemBehaviors/Behaviors.cs +++ /dev/null @@ -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)), JsonObject(MemberSerialization.OptIn)] -// [Union(3, typeof(PlusUltraBehaviorData))] -public interface IItemBehaviorData -{ - IItemBehavior CreateInstance(GameContext context, Ship ship, Gear item); -} \ No newline at end of file diff --git a/Assets/Scripts/ServerShared/ItemData.cs b/Assets/Scripts/ServerShared/ItemData.cs index c38c6a33c..ca3e91fb0 100644 --- a/Assets/Scripts/ServerShared/ItemData.cs +++ b/Assets/Scripts/ServerShared/ItemData.cs @@ -94,7 +94,7 @@ public abstract class EquippableItemData : CraftedItemData public PerformanceStat HeatExponent = new PerformanceStat(); [InspectableField, JsonProperty("behaviors"), Key(13)] - public List Behaviors = new List(); + public List Behaviors = new List(); [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 Hardpoints = new List(); - [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> _scaleModifiers; - [IgnoreMember] private Dictionary> _constantModifiers; + [IgnoreMember] private Dictionary> _scaleModifiers; + [IgnoreMember] private Dictionary> _constantModifiers; [IgnoreMember] - private Dictionary> ScaleModifiers => - _scaleModifiers ?? (_scaleModifiers = new Dictionary>()); + private Dictionary> ScaleModifiers => + _scaleModifiers ?? (_scaleModifiers = new Dictionary>()); [IgnoreMember] - private Dictionary> ConstantModifiers => - _constantModifiers ?? (_constantModifiers = new Dictionary>()); + private Dictionary> ConstantModifiers => + _constantModifiers ?? (_constantModifiers = new Dictionary>()); - public Dictionary GetScaleModifiers(Ship ship) + public Dictionary GetScaleModifiers(Entity entity) { - if(!ScaleModifiers.ContainsKey(ship)) - ScaleModifiers[ship] = new Dictionary(); + if(!ScaleModifiers.ContainsKey(entity)) + ScaleModifiers[entity] = new Dictionary(); - return ScaleModifiers[ship]; + return ScaleModifiers[entity]; } - public Dictionary GetConstantModifiers(Ship ship) + public Dictionary GetConstantModifiers(Entity entity) { - if(!ConstantModifiers.ContainsKey(ship)) - ConstantModifiers[ship] = new Dictionary(); + if(!ConstantModifiers.ContainsKey(entity)) + ConstantModifiers[entity] = new Dictionary(); - return ConstantModifiers[ship]; + return ConstantModifiers[entity]; } } \ No newline at end of file diff --git a/Assets/Scripts/ServerShared/Ship.cs b/Assets/Scripts/ServerShared/Ship.cs index 44f960d84..d8e50196c 100644 --- a/Assets/Scripts/ServerShared/Ship.cs +++ b/Assets/Scripts/ServerShared/Ship.cs @@ -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 Bindings = new Dictionary(); //[IgnoreMember] public int HullHardpointCount; - [IgnoreMember] public Dictionary ItemBindings; - [IgnoreMember] public IItemBehavior[] ItemBehaviors; - [IgnoreMember] public bool ForceThrust; // [IgnoreMember] public Dictionary Contacts = new Dictionary(); - [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 VisibilitySources = new Dictionary(); 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 items, IEnumerable cargo) : base(context, items, cargo) + { + } public Gear GetEquipped(HardpointType type) { @@ -53,53 +44,15 @@ public class Ship : DatabaseEntry return null; } - public IEnumerable GetBehaviors() where T : class, IItemBehavior - { - foreach (var behavior in ItemBehaviors) - if (behavior is T b) - yield return b; - } - - public IEnumerable GetBehaviorData() 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()?.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().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) diff --git a/Economy.Shared.Unity.csproj b/Economy.Shared.Unity.csproj index fbbfcd3c9..344f66655 100644 --- a/Economy.Shared.Unity.csproj +++ b/Economy.Shared.Unity.csproj @@ -62,23 +62,28 @@ + + + + + + + + + + + + + + + - - - - - - - - - - diff --git a/README.md b/README.md index 13a73b7e3..ed942e72f 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,22 @@ The codebase makes heavy use of C#'s [Language Integrated Queries (LINQ)](https: All of the state which is persisted to the database inherits from the DatabaseEntry class, which uses a GUID as each entry's primary key. Whenever a reference to a database entry must be held, it should be stored as a GUID and when needed, retrieved directly from the DatabaseCache held by the GameContext. References to Database Entries *should never be stored directly!* This is because the contents of entries can be updated in realtime by the database, causing changes to be pushed to the DatabaseCache and any previously existing DatabaseEntry instances will be outdated and invalid, potentially carrying stale data. +#### Equipment + +Items in the game which can be equipped are defined as subclasses of EquippableItemData, including the HullData class which defines a space ship, station or turret, and the GearData class which defines anything that can be equipped onto a Hull. + +#### Behaviors + +Equippable Items can hold any number of Behaviors, which define the functionality of that item in game. Everything from radiating heat into space to moving a ship, firing a weapon or boosting the stats of another item is defined as a Behavior. + +#### Performance Stats + +While some stats are fixed, others can vary according to the condition the item is in. Such stats are PerformanceStats. These can vary depending on the item's remaining durability, the current temperature of the item, and the quality with which it was crafted. + +#### Blueprints + +In order to make an item craftable in-game, that item needs to be associated with one or more Blueprints. A Blueprint defines the ingredients (or components) necessary to build an item. In addition, specific ingredients can be associated with particular PerformanceStats for the resulting item's Behaviors, allowing a single item to be crafted in various ways, with its final stats varying in accordance with the supply chain and quality control of the manufacturer. + ## Contributing ### Getting the Files