diff --git a/.idea/.idea.Aetheria-Economy/.idea/contentModel.xml b/.idea/.idea.Aetheria-Economy/.idea/contentModel.xml index 6d20e2694..021005cb9 100644 --- a/.idea/.idea.Aetheria-Economy/.idea/contentModel.xml +++ b/.idea/.idea.Aetheria-Economy/.idea/contentModel.xml @@ -1252,10 +1252,13 @@ + + + diff --git a/Assets/Scripts/Editor/DatabaseInspector.cs b/Assets/Scripts/Editor/DatabaseInspector.cs index 3c7306cd1..34870f887 100644 --- a/Assets/Scripts/Editor/DatabaseInspector.cs +++ b/Assets/Scripts/Editor/DatabaseInspector.cs @@ -484,8 +484,11 @@ public class DatabaseInspector : EditorWindow using (var h = new HorizontalScope()) { GUILayout.Label(label, GUILayout.Width(width)); - if(area) - return TextArea(value,GUILayout.Width(position.width-width-10), GUILayout.Height(100)); + if (area) + { + EditorStyles.textArea.wordWrap = true; + return DelayedTextField(value, EditorStyles.textArea, GUILayout.Width(position.width-width-10), GUILayout.Height(100)); + } return DelayedTextField(value); } } diff --git a/Assets/Scripts/ServerShared/Agents/Locomotion.cs b/Assets/Scripts/ServerShared/Agents/Locomotion.cs index 20a363c66..c2fba4235 100644 --- a/Assets/Scripts/ServerShared/Agents/Locomotion.cs +++ b/Assets/Scripts/ServerShared/Agents/Locomotion.cs @@ -14,12 +14,16 @@ public class Locomotion : AgentBehavior private Thruster _thrust; private Turning _turning; private VelocityLimit _velocityLimit; + private int _thrustAxis; + private int _turningAxis; public Locomotion(GameContext context, Entity entity, ControllerData controllerData) : base(context, entity, controllerData) { _velocityLimit = Entity.GetBehaviors().FirstOrDefault(); - _thrust = Entity.Axes.Keys.FirstOrDefault(x => x is Thruster) as Thruster; - _turning = Entity.Axes.Keys.FirstOrDefault(x => x is Turning) as Turning; + _thrust = Entity.GetBehaviors().FirstOrDefault(); + _turning = Entity.GetBehaviors().FirstOrDefault(); + _thrustAxis = Entity.GetAxis(); + _turningAxis = Entity.GetAxis(); } public override void Update(float delta) @@ -40,15 +44,15 @@ public class Locomotion : AgentBehavior { float2 tangential = Entity.Velocity - dot(normalize(diff), normalize(Entity.Velocity)) * Entity.Velocity; float2 adjustedDirection = normalize(diff) - tangential * TangentSensitivity; - Entity.Axes[_turning] = TurningInput(adjustedDirection); - Entity.Axes[_thrust] = 1; + Entity.Axes[_turningAxis].Value = TurningInput(adjustedDirection); + Entity.Axes[_thrustAxis].Value = 1; } // We have plenty of space, use exact maneuvering else { var deltaV = desiredVelocity - Entity.Velocity; - Entity.Axes[_turning] = TurningInput(deltaV); - Entity.Axes[_thrust] = ThrustInput(deltaV); + Entity.Axes[_turningAxis].Value = TurningInput(deltaV); + Entity.Axes[_thrustAxis].Value = ThrustInput(deltaV); } } } diff --git a/Assets/Scripts/ServerShared/Agents/VelocityMatch.cs b/Assets/Scripts/ServerShared/Agents/VelocityMatch.cs index c3fbfd426..28508d7c7 100644 --- a/Assets/Scripts/ServerShared/Agents/VelocityMatch.cs +++ b/Assets/Scripts/ServerShared/Agents/VelocityMatch.cs @@ -12,14 +12,18 @@ public class VelocityMatch : AgentBehavior private Thruster _thrust; private Turning _turning; private const float TargetThreshold = .1f; + private int _thrustAxis; + private int _turningAxis; private GameContext _context; public VelocityMatch(GameContext context, Entity entity, ControllerData controllerData) : base(context, entity, controllerData) { _context = context; - _thrust = Entity.Axes.Keys.FirstOrDefault(x => x is Thruster) as Thruster; - _turning = Entity.Axes.Keys.FirstOrDefault(x => x is Turning) as Turning; + _thrust = Entity.GetBehaviors().FirstOrDefault(); + _turning = Entity.GetBehaviors().FirstOrDefault(); + _thrustAxis = Entity.GetAxis(); + _turningAxis = Entity.GetAxis(); } public void Clear() @@ -34,8 +38,8 @@ public class VelocityMatch : AgentBehavior var deltaV = TargetVelocity - Entity.Velocity; if(length(deltaV) < TargetThreshold) OnMatch?.Invoke(); - Entity.Axes[_turning] = TurningInput(deltaV); - Entity.Axes[_thrust] = ThrustInput(deltaV); + Entity.Axes[_turningAxis].Value = TurningInput(deltaV); + Entity.Axes[_thrustAxis].Value = ThrustInput(deltaV); } } diff --git a/Assets/Scripts/ServerShared/Behaviors/ActivatedItemUsage.cs b/Assets/Scripts/ServerShared/Behaviors/ActivatedItemUsage.cs new file mode 100644 index 000000000..383cdfb5f --- /dev/null +++ b/Assets/Scripts/ServerShared/Behaviors/ActivatedItemUsage.cs @@ -0,0 +1,54 @@ +using MessagePack; +using Newtonsoft.Json; + +[InspectableField, MessagePackObject, JsonObject(MemberSerialization.OptIn)] +public class ActivatedItemUsageData : BehaviorData +{ + + public override IBehavior CreateInstance(GameContext context, Entity entity, Gear item) + { + return new ActivatedItemUsage(context, this, entity, item); + } +} + +public class ActivatedItemUsage : IActivatedBehavior +{ + private ActivatedItemUsageData _data; + + private Entity Entity { get; } + private Gear Item { get; } + private GameContext Context { get; } + + public BehaviorData Data => _data; + + public ActivatedItemUsage(GameContext context, ActivatedItemUsageData data, Entity entity, Gear item) + { + _data = data; + Entity = entity; + Item = item; + Context = context; + } + + public void Initialize() + { + } + + public bool Activate() + { + throw new System.NotImplementedException(); + } + + public void Deactivate() + { + throw new System.NotImplementedException(); + } + + public bool Update(float delta) + { + return true; + } + + public void Remove() + { + } +} \ No newline at end of file diff --git a/Assets/Scripts/ServerShared/Behaviors/ActivatedItemUsage.cs.meta b/Assets/Scripts/ServerShared/Behaviors/ActivatedItemUsage.cs.meta new file mode 100644 index 000000000..ef15ac809 --- /dev/null +++ b/Assets/Scripts/ServerShared/Behaviors/ActivatedItemUsage.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9965ed11abd4e8c48b7b2bd8ab6e44e8 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/ServerShared/Behaviors/Afterburner.cs b/Assets/Scripts/ServerShared/Behaviors/Afterburner.cs index 19216cfd2..5d777d89c 100644 --- a/Assets/Scripts/ServerShared/Behaviors/Afterburner.cs +++ b/Assets/Scripts/ServerShared/Behaviors/Afterburner.cs @@ -7,13 +7,13 @@ using Newtonsoft.Json; [InspectableField, MessagePackObject, JsonObject(MemberSerialization.OptIn)] public class AfterburnerData : BehaviorData { - [InspectableField, JsonProperty("thrust"), Key(0)] + [InspectableField, JsonProperty("thrust"), Key(1)] public PerformanceStat ThrustModifier = new PerformanceStat(); - [InspectableField, JsonProperty("speed"), Key(1)] + [InspectableField, JsonProperty("speed"), Key(2)] public PerformanceStat SpeedModifier = new PerformanceStat(); - [InspectableField, JsonProperty("torque"), Key(2)] + [InspectableField, JsonProperty("torque"), Key(3)] public PerformanceStat TorqueModifier = new PerformanceStat(); public override IBehavior CreateInstance(GameContext context, Entity entity, Gear item) @@ -27,6 +27,7 @@ public class Afterburner : IActivatedBehavior private List> _modifiers = new List>(); private AfterburnerData _data; private Thruster _thruster; + private int _thrustAxis; public Entity Entity { get; } public Gear Item { get; } @@ -45,21 +46,25 @@ public class Afterburner : IActivatedBehavior public void Initialize() { _thruster = Entity.GetBehaviors().FirstOrDefault(); + _thrustAxis = Entity.GetAxis(); } - public void Update(float delta) + public bool Update(float delta) { + return true; } - public void Activate() + public bool Activate() { - if (_thruster == null) return; + if (_thruster == null) return false; var thrustMod = ((ThrusterData) _thruster.Data).Thrust.GetScaleModifiers(Entity); thrustMod.Add(this,Context.Evaluate(_data.ThrustModifier,Item, Entity)); _modifiers.Add(thrustMod); - Entity.AxisOverrides[_thruster] = 1; + Entity.AxisOverrides[_thrustAxis] = 1; + + return true; // var speedMod = (Ship.Hull.ItemData as HullData).TopSpeed.GetScaleModifiers(Ship); // _modifiers.Add(speedMod); @@ -72,11 +77,15 @@ public class Afterburner : IActivatedBehavior public void Deactivate() { - Entity.AxisOverrides.Remove(_thruster); + Entity.AxisOverrides.Remove(_thrustAxis); foreach (var mod in _modifiers) { mod.Remove(this); } _modifiers.Clear(); } + + public void Remove() + { + } } \ No newline at end of file diff --git a/Assets/Scripts/ServerShared/Behaviors/Behaviors.cs b/Assets/Scripts/ServerShared/Behaviors/Behaviors.cs index fc97e1fc8..c6f394435 100644 --- a/Assets/Scripts/ServerShared/Behaviors/Behaviors.cs +++ b/Assets/Scripts/ServerShared/Behaviors/Behaviors.cs @@ -12,13 +12,14 @@ using static Unity.Mathematics.noise; public interface IBehavior { void Initialize(); - void Update(float delta); + bool Update(float delta); + void Remove(); BehaviorData Data { get; } } public interface IActivatedBehavior : IBehavior { - void Activate(); + bool Activate(); void Deactivate(); } @@ -68,8 +69,8 @@ public abstract class PersistentBehaviorData JsonConverter(typeof(JsonKnownTypesConverter)), JsonObject(MemberSerialization.OptIn)] public abstract class BehaviorData { - // [InspectableField, JsonProperty("index"), Key(0)] - // public int Index; + [InspectableField, JsonProperty("group"), Key(0)] + public int Group; public abstract IBehavior CreateInstance(GameContext context, Entity entity, Gear item); } \ No newline at end of file diff --git a/Assets/Scripts/ServerShared/Behaviors/Controller.cs b/Assets/Scripts/ServerShared/Behaviors/Controller.cs index c5abbd484..849a82fba 100644 --- a/Assets/Scripts/ServerShared/Behaviors/Controller.cs +++ b/Assets/Scripts/ServerShared/Behaviors/Controller.cs @@ -7,16 +7,16 @@ using Newtonsoft.Json; [Union(0, typeof(TowingControllerData))] public abstract class ControllerData : BehaviorData { - [InspectableField, JsonProperty("thrustSensitivity"), Key(0)] + [InspectableField, JsonProperty("thrustSensitivity"), Key(1)] public float ThrustSensitivity = 1; - [InspectableField, JsonProperty("thrustSpecificity"), Key(1)] + [InspectableField, JsonProperty("thrustSpecificity"), Key(2)] public float ThrustSpecificity = 8; - [InspectableField, JsonProperty("turningSensitivity"), Key(2)] + [InspectableField, JsonProperty("turningSensitivity"), Key(3)] public float TurningSensitivity = 1; - [InspectableField, JsonProperty("tangentSensitivity"), Key(3)] + [InspectableField, JsonProperty("tangentSensitivity"), Key(4)] public float TangentSensitivity = 4; } diff --git a/Assets/Scripts/ServerShared/Behaviors/FactoryBehavior.cs b/Assets/Scripts/ServerShared/Behaviors/FactoryBehavior.cs index b0b567327..b892c6028 100644 --- a/Assets/Scripts/ServerShared/Behaviors/FactoryBehavior.cs +++ b/Assets/Scripts/ServerShared/Behaviors/FactoryBehavior.cs @@ -10,10 +10,10 @@ using Random = Unity.Mathematics.Random; [MessagePackObject, JsonObject(MemberSerialization.OptIn)] public class FactoryData : BehaviorData { - [InspectableField, JsonProperty("toolingTime"), Key(0)] + [InspectableField, JsonProperty("toolingTime"), Key(1)] public PerformanceStat ToolingTime = new PerformanceStat(); - [InspectableField, JsonProperty("automation"), Key(1)] + [InspectableField, JsonProperty("automation"), Key(2)] public int AutomationPoints; public override IBehavior CreateInstance(GameContext context, Entity entity, Gear item) @@ -52,7 +52,7 @@ public class Factory : IBehavior, IPersistentBehavior// { } - public void Update(float delta) + public bool Update(float delta) { var blueprint = Context.Cache.Get(_blueprint); if (_retooling || _producing) @@ -198,6 +198,8 @@ public class Factory : IBehavior, IPersistentBehavior// } } } + + return true; } public PersistentBehaviorData Store() @@ -225,6 +227,10 @@ public class Factory : IBehavior, IPersistentBehavior// _assignedPopulation = factoryPersistence.AssignedPopulation; _productionQuality = factoryPersistence.ProductionQuality; } + + public void Remove() + { + } } [MessagePackObject, JsonObject(MemberSerialization.OptIn)] diff --git a/Assets/Scripts/ServerShared/Behaviors/Hitscan.cs b/Assets/Scripts/ServerShared/Behaviors/Hitscan.cs new file mode 100644 index 000000000..4fd2490fe --- /dev/null +++ b/Assets/Scripts/ServerShared/Behaviors/Hitscan.cs @@ -0,0 +1,95 @@ +using MessagePack; +using Newtonsoft.Json; + +[InspectableField, MessagePackObject, JsonObject(MemberSerialization.OptIn)] +public class HitscanData : WeaponData +{ + public override IBehavior CreateInstance(GameContext context, Entity entity, Gear item) + { + return new Hitscan(context, this, entity, item); + } +} + +public class Hitscan : IActivatedBehavior +{ + private bool _firing; + private float _cooldown; // normalized + private HitscanData _data; + private Entity Entity { get; } + private Gear Item { get; } + private GameContext Context { get; } + private float _firingVisibility; + + public BehaviorData Data => _data; + + public Hitscan(GameContext context, HitscanData c, Entity entity, Gear item) + { + Context = context; + _data = c; + Entity = entity; + Item = item; + } + + public bool Activate() + { +// Debug.Log("Activating ProjectileWeapon"); +// _firing = true; +// Observable.EveryUpdate().TakeWhile(_ => _firing).Subscribe(_ => +// { +// // Debug.Log($"Updating Observable {_cooldown}"); +// if (_cooldown < 0) +// { +// _cooldown = 1; +// Fire(0); +// if(_projectileWeapon.BurstCount>1) +// Observable.Interval(TimeSpan.FromSeconds(_projectileWeapon.BurstTime.Evaluate(Hardpoint) / (_projectileWeapon.BurstCount-1))).Take(_projectileWeapon.BurstCount-1) +// .Subscribe(l => Fire(l+1)); +// } +// }); + return true; + } + + private void Fire(long b) + { + // _firingVisibility += _projectileWeapon.Visibility.Evaluate(Hardpoint); + // Hardpoint.Temperature += _projectileWeapon.Heat.Evaluate(Hardpoint) / Hardpoint.HeatCapacity; + // var inst = GameObject.Instantiate(_projectileWeapon.BulletPrefab).transform; + // Physics.IgnoreCollision(Hardpoint.Ship.Ship.GetComponent(), inst.GetComponent()); + // var bullet = inst.GetComponent(); + // bullet.Target = Hardpoint.Ship.Ship.Target; + // bullet.Source = Hardpoint.Ship.Ship.Hitpoints; + // bullet.Lifetime = _projectileWeapon.Range.Evaluate(Hardpoint) / _projectileWeapon.Velocity.Evaluate(Hardpoint); + // bullet.Damage = _projectileWeapon.Damage.Evaluate(Hardpoint); + // inst.position = Hardpoint.Proxy.position; + // inst.rotation = Hardpoint.Proxy.rotation; + // inst.GetComponent().velocity = _projectileWeapon.Velocity.Evaluate(Hardpoint) * Vector3.RotateTowards(-inst.forward, Hardpoint.Ship.Ship.Direction, _projectileWeapon.Deflection * Mathf.Deg2Rad, 1); + // Hardpoint.Ship.Ship.AudioSource.PlayOneShot(_projectileWeapon.Sounds.RandomElement()); +// Debug.Log($"Firing bullet {b}"); + } + + public void Deactivate() + { + _firing = false; + } + + public void Initialize() + { + } + + public bool Update(float delta) + { + _cooldown -= delta / Context.Evaluate(_data.Cooldown, Item, Entity); + + _firingVisibility *= Context.Evaluate(_data.VisibilityDecay, Item, Entity); + + if (_firingVisibility < 0.01f) + { + Entity.VisibilitySources.Remove(this); + } + return true; + } + + public void Remove() + { + } +} \ No newline at end of file diff --git a/Assets/Scripts/ServerShared/Behaviors/Hitscan.cs.meta b/Assets/Scripts/ServerShared/Behaviors/Hitscan.cs.meta new file mode 100644 index 000000000..35ab6aea4 --- /dev/null +++ b/Assets/Scripts/ServerShared/Behaviors/Hitscan.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 215516570484dc343ab90cae7a8ec175 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/ServerShared/Behaviors/ItemUsage.cs b/Assets/Scripts/ServerShared/Behaviors/ItemUsage.cs new file mode 100644 index 000000000..7998124f5 --- /dev/null +++ b/Assets/Scripts/ServerShared/Behaviors/ItemUsage.cs @@ -0,0 +1,51 @@ + +using System; +using MessagePack; +using Newtonsoft.Json; + +[InspectableField, MessagePackObject, JsonObject(MemberSerialization.OptIn)] +public class ItemUsageData : BehaviorData +{ + [InspectableDatabaseLink(typeof(SimpleCommodityData)), JsonProperty("item"), Key(1)] + public Guid Item; + + [InspectableField, JsonProperty("cooldown"), Key(2)] + public PerformanceStat Cooldown = new PerformanceStat(); + + public override IBehavior CreateInstance(GameContext context, Entity entity, Gear item) + { + return new ItemUsage(context, this, entity, item); + } +} + +public class ItemUsage : IBehavior +{ + private ItemUsageData _data; + + private Entity Entity { get; } + private Gear Item { get; } + private GameContext Context { get; } + + public BehaviorData Data => _data; + + public ItemUsage(GameContext context, ItemUsageData data, Entity entity, Gear item) + { + _data = data; + Entity = entity; + Item = item; + Context = context; + } + + public void Initialize() + { + } + + public bool Update(float delta) + { + return true; + } + + public void Remove() + { + } +} \ No newline at end of file diff --git a/Assets/Scripts/ServerShared/Behaviors/ItemUsage.cs.meta b/Assets/Scripts/ServerShared/Behaviors/ItemUsage.cs.meta new file mode 100644 index 000000000..850c15f3f --- /dev/null +++ b/Assets/Scripts/ServerShared/Behaviors/ItemUsage.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 6204fd7e7e3950548bddf9738f8e5219 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/ServerShared/Behaviors/Launcher.cs b/Assets/Scripts/ServerShared/Behaviors/Launcher.cs index a831fd72f..ab94af384 100644 --- a/Assets/Scripts/ServerShared/Behaviors/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 LauncherData : BehaviorData +public class LauncherData : WeaponData { [InspectablePrefab, JsonProperty("missile"), Key(9)] public string MissilePrefab; @@ -50,16 +50,20 @@ public class Launcher : IActivatedBehavior private float _cooldown; // normalized private bool _locked; - private LauncherData _launcher; - public Entity Entity { get; } - public Gear Item { get; } - public GameContext Context { get; } + private LauncherData _data; + private Entity Entity { get; } + private Gear Item { get; } + private GameContext Context { get; } private float _firingVisibility; + + public BehaviorData Data => _data; + + public int Group => _data.Group; public Launcher(GameContext context, LauncherData m, Entity entity, Gear item) { Context = context; - _launcher = m; + _data = m; Entity = entity; Item = item; // _audio = hp.Proxy.GetComponent(); @@ -70,10 +74,11 @@ public class Launcher : IActivatedBehavior // _audio.volume = .5f; } - public void Activate() + public bool Activate() { _locking = true; _lockingTimer = 1; + return true; } private void Fire(long b) @@ -124,8 +129,9 @@ public class Launcher : IActivatedBehavior { } - public void Update(float delta) + public bool Update(float delta) { + return true; // if (Ship.Target == null) // return; // @@ -161,6 +167,8 @@ public class Launcher : IActivatedBehavior // _firingVisibility = 0; // } } - - public BehaviorData Data => _launcher; + + public void Remove() + { + } } \ No newline at end of file diff --git a/Assets/Scripts/ServerShared/Behaviors/PatrolController.cs b/Assets/Scripts/ServerShared/Behaviors/PatrolController.cs index 17adefbbc..3e1a2003a 100644 --- a/Assets/Scripts/ServerShared/Behaviors/PatrolController.cs +++ b/Assets/Scripts/ServerShared/Behaviors/PatrolController.cs @@ -9,7 +9,7 @@ using static Unity.Mathematics.math; [MessagePackObject, JsonObject(MemberSerialization.OptIn), EntityTypeRestriction(HullType.Ship)] public class PatrolControllerData : ControllerData { - [InspectableField, JsonProperty("targetDistance"), Key(4)] + [InspectableField, JsonProperty("targetDistance"), Key(5)] public float TargetDistance = 10; public override IBehavior CreateInstance(GameContext context, Entity entity, Gear item) @@ -47,13 +47,15 @@ public class PatrolController : IBehavior, IController RandomTarget(); } - public void Update(float delta) + public bool Update(float delta) { _locomotion.Objective = _context.GetOrbitPosition(_targetOrbit); _locomotion.Update(delta); if(length(_entity.Position - _locomotion.Objective) < _data.TargetDistance) RandomTarget(); + + return true; } public void AssignTask(Guid task, List path) @@ -66,4 +68,8 @@ public class PatrolController : IBehavior, IController var entities = _context.ZonePlanets[_entity.Zone]; _targetOrbit = _context.Cache.Get(entities[_context.Random.NextInt(entities.Length)]).Orbit; } + + public void Remove() + { + } } diff --git a/Assets/Scripts/ServerShared/Behaviors/ProjectileWeapon.cs b/Assets/Scripts/ServerShared/Behaviors/ProjectileWeapon.cs index 221a88d83..8393065bd 100644 --- a/Assets/Scripts/ServerShared/Behaviors/ProjectileWeapon.cs +++ b/Assets/Scripts/ServerShared/Behaviors/ProjectileWeapon.cs @@ -24,26 +24,32 @@ public class ProjectileWeaponData : WeaponData public class ProjectileWeapon : IActivatedBehavior { + private ProjectileWeaponData _data; + private Entity Entity { get; } + private Gear Item { get; } + private GameContext Context { get; } + private float _firingVisibility; private bool _firing; private float _cooldown; // normalized - private ProjectileWeaponData _projectileWeapon; - public Entity Entity { get; } - public Gear Item { get; } - public GameContext Context { get; } - private float _firingVisibility; + private float _burstCooldown; + private float _burstCooldownTime; + private int _burstRemaining; + + public BehaviorData Data => _data; public ProjectileWeapon(GameContext context, ProjectileWeaponData c, Entity entity, Gear item) { Context = context; - _projectileWeapon = c; + _data = c; Entity = entity; Item = item; } - public void Activate() + public bool Activate() { + _firing = true; + return true; // Debug.Log("Activating ProjectileWeapon"); -// _firing = true; // Observable.EveryUpdate().TakeWhile(_ => _firing).Subscribe(_ => // { // // Debug.Log($"Updating Observable {_cooldown}"); @@ -58,9 +64,11 @@ public class ProjectileWeapon : IActivatedBehavior // }); } - private void Fire(long b) + private void Fire() { - // _firingVisibility += _projectileWeapon.Visibility.Evaluate(Hardpoint); + _burstRemaining--; + _firingVisibility += Context.Evaluate(_data.Visibility, Item, Entity); + Entity.AddHeat(Context.Evaluate(_data.Heat, Item, Entity)); // Hardpoint.Temperature += _projectileWeapon.Heat.Evaluate(Hardpoint) / Hardpoint.HeatCapacity; // var inst = GameObject.Instantiate(_projectileWeapon.BulletPrefab).transform; // Physics.IgnoreCollision(Hardpoint.Ship.Ship.GetComponent(), inst.GetComponent()); @@ -85,17 +93,38 @@ public class ProjectileWeapon : IActivatedBehavior { } - public void Update(float delta) + public bool Update(float delta) { - _cooldown -= delta / Context.Evaluate(_projectileWeapon.Cooldown, Item, Entity); + _cooldown -= delta / Context.Evaluate(_data.Cooldown, Item, Entity); + if (_cooldown < 0) + { + _cooldown = 1; + _burstRemaining = _data.BurstCount; + _burstCooldownTime = Context.Evaluate(_data.BurstTime, Item, Entity) / _data.BurstCount; + _burstCooldown = -1; + Fire(); + } + + if (_burstRemaining > 0) + { + _burstCooldown -= delta / _burstCooldownTime; + if (_burstCooldown < 0) + { + _burstCooldown = 1; + Fire(); + } + } - _firingVisibility *= Context.Evaluate(_projectileWeapon.VisibilityDecay, Item, Entity); + _firingVisibility *= Context.Evaluate(_data.VisibilityDecay, Item, Entity); if (_firingVisibility < 0.01f) { Entity.VisibilitySources.Remove(this); } + return true; + } + + public void Remove() + { } - - public BehaviorData Data => _projectileWeapon; } \ No newline at end of file diff --git a/Assets/Scripts/ServerShared/Behaviors/Radiator.cs b/Assets/Scripts/ServerShared/Behaviors/Radiator.cs index f2af749f1..2ad67a42e 100644 --- a/Assets/Scripts/ServerShared/Behaviors/Radiator.cs +++ b/Assets/Scripts/ServerShared/Behaviors/Radiator.cs @@ -6,7 +6,7 @@ using static Unity.Mathematics.math; [MessagePackObject, JsonObject(MemberSerialization.OptIn)] public class RadiatorData : BehaviorData { - [InspectableField, JsonProperty("emissivity"), Key(0)] + [InspectableField, JsonProperty("emissivity"), Key(1)] public PerformanceStat Emissivity = new PerformanceStat(); public override IBehavior CreateInstance(GameContext context, Entity entity, Gear item) @@ -37,10 +37,15 @@ public class Radiator : IBehavior { } - public void Update(float delta) + public bool Update(float delta) { var rad = pow(Entity.Temperature, Context.GlobalData.HeatRadiationPower) * Context.GlobalData.HeatRadiationMultiplier; Entity.Temperature -= rad * delta; Entity.VisibilitySources[this] = rad; + return true; + } + + public void Remove() + { } } \ No newline at end of file diff --git a/Assets/Scripts/ServerShared/Behaviors/Reactor.cs b/Assets/Scripts/ServerShared/Behaviors/Reactor.cs index f5db66cf4..ec0204758 100644 --- a/Assets/Scripts/ServerShared/Behaviors/Reactor.cs +++ b/Assets/Scripts/ServerShared/Behaviors/Reactor.cs @@ -4,19 +4,19 @@ using Newtonsoft.Json; [MessagePackObject, JsonObject(MemberSerialization.OptIn)] public class ReactorData : BehaviorData { - [InspectableField, JsonProperty("charge"), Key(0)] + [InspectableField, JsonProperty("charge"), Key(1)] public PerformanceStat Charge = new PerformanceStat(); - [InspectableField, JsonProperty("capacitance"), Key(1)] + [InspectableField, JsonProperty("capacitance"), Key(2)] public PerformanceStat Capacitance = new PerformanceStat(); - [InspectableField, JsonProperty("efficiency"), Key(2)] + [InspectableField, JsonProperty("efficiency"), Key(3)] public PerformanceStat Efficiency = new PerformanceStat(); - [InspectableField, JsonProperty("overload"), Key(3)] + [InspectableField, JsonProperty("overload"), Key(4)] public PerformanceStat OverloadEfficiency = new PerformanceStat(); - [InspectableField, JsonProperty("underload"), Key(4)] + [InspectableField, JsonProperty("underload"), Key(5)] public PerformanceStat UnderloadRecovery = new PerformanceStat(); public override IBehavior CreateInstance(GameContext context, Entity entity, Gear item) @@ -47,7 +47,7 @@ public class Reactor : IBehavior { } - public void Update(float delta) + public bool Update(float delta) { var cap = Context.Evaluate(_data.Capacitance, Item, Entity); var charge = Context.Evaluate(_data.Charge, Item, Entity) * delta; @@ -67,5 +67,10 @@ public class Reactor : IBehavior Entity.AddHeat( -Entity.Energy / Context.Evaluate(_data.OverloadEfficiency, Item, Entity)); Entity.Energy = 0; } + return true; + } + + public void Remove() + { } } \ No newline at end of file diff --git a/Assets/Scripts/ServerShared/Behaviors/Reflector.cs b/Assets/Scripts/ServerShared/Behaviors/Reflector.cs index c9b6d900b..8229d601b 100644 --- a/Assets/Scripts/ServerShared/Behaviors/Reflector.cs +++ b/Assets/Scripts/ServerShared/Behaviors/Reflector.cs @@ -6,7 +6,7 @@ using static Unity.Mathematics.math; [MessagePackObject, JsonObject(MemberSerialization.OptIn)] public class ReflectorData : BehaviorData { - [InspectableField, JsonProperty("crossSection"), Key(0)] + [InspectableField, JsonProperty("crossSection"), Key(1)] public PerformanceStat CrossSection = new PerformanceStat(); // [InspectableAnimationCurve, JsonProperty("visibility"), Key(1)] @@ -40,7 +40,12 @@ public class Reflector : IBehavior { } - public void Update(float delta) + public bool Update(float delta) + { + return true; + } + + public void Remove() { } } \ No newline at end of file diff --git a/Assets/Scripts/ServerShared/Behaviors/Sensor.cs b/Assets/Scripts/ServerShared/Behaviors/Sensor.cs index 06801bd62..e156d968d 100644 --- a/Assets/Scripts/ServerShared/Behaviors/Sensor.cs +++ b/Assets/Scripts/ServerShared/Behaviors/Sensor.cs @@ -5,16 +5,16 @@ using Newtonsoft.Json; [InspectableField, MessagePackObject, JsonObject(MemberSerialization.OptIn)] public class SensorData : BehaviorData { - [InspectableField, JsonProperty("radiance"), Key(0)] + [InspectableField, JsonProperty("radiance"), Key(1)] public PerformanceStat Radiance = new PerformanceStat(); - [InspectableField, JsonProperty("masking"), Key(1)] + [InspectableField, JsonProperty("masking"), Key(2)] public PerformanceStat RadianceMasking = new PerformanceStat(); - [InspectableField, JsonProperty("sensitivity"), Key(2)] + [InspectableField, JsonProperty("sensitivity"), Key(3)] public PerformanceStat Sensitivity = new PerformanceStat(); - [InspectableField, JsonProperty("range"), Key(3)] + [InspectableField, JsonProperty("range"), Key(4)] public PerformanceStat Range = new PerformanceStat(); public override IBehavior CreateInstance(GameContext context, Entity entity, Gear item) @@ -27,9 +27,9 @@ public class Sensor : IBehavior { private SensorData _data; - public Entity Entity { get; } - public Gear Item { get; } - public GameContext Context { get; } + private Entity Entity { get; } + private Gear Item { get; } + private GameContext Context { get; } public BehaviorData Data => _data; @@ -45,8 +45,9 @@ public class Sensor : IBehavior { } - public void Update(float delta) + public bool Update(float delta) { + return true; // var ship = Hardpoint.Ship.Ship.transform; // var contacts = // Physics.OverlapSphere(ship.position, _data.Range.Evaluate(Hardpoint)).Where(c=>c.attachedRigidbody?.GetComponent()!=null).Select(c=>c.attachedRigidbody.GetComponent()); @@ -65,4 +66,8 @@ public class Sensor : IBehavior // Hardpoint.Ship.VisibilitySources[this] = _data.Radiance.Evaluate(Hardpoint) / _data.RadianceMasking.Evaluate(Hardpoint); // TODO: Handle Active Detection / Visibility From Reflected Radiance } + + public void Remove() + { + } } \ No newline at end of file diff --git a/Assets/Scripts/ServerShared/Behaviors/Shield.cs b/Assets/Scripts/ServerShared/Behaviors/Shield.cs index 600b8224e..2f42461d5 100644 --- a/Assets/Scripts/ServerShared/Behaviors/Shield.cs +++ b/Assets/Scripts/ServerShared/Behaviors/Shield.cs @@ -6,10 +6,10 @@ using static Unity.Mathematics.math; [MessagePackObject, JsonObject(MemberSerialization.OptIn)] public class ShieldData : BehaviorData { - [InspectableField, JsonProperty("efficiency"), Key(0)] + [InspectableField, JsonProperty("efficiency"), Key(1)] public PerformanceStat Efficiency = new PerformanceStat(); - [InspectableField, JsonProperty("shielding"), Key(1)] + [InspectableField, JsonProperty("shielding"), Key(2)] public PerformanceStat Shielding = new PerformanceStat(); public override IBehavior CreateInstance(GameContext context, Entity entity, Gear item) @@ -42,7 +42,12 @@ public class Shield : IBehavior { } - public void Update(float delta) + public bool Update(float delta) + { + return true; + } + + public void Remove() { } } \ No newline at end of file diff --git a/Assets/Scripts/ServerShared/Behaviors/Thruster.cs b/Assets/Scripts/ServerShared/Behaviors/Thruster.cs index 000cc3b0b..4cd1d2458 100644 --- a/Assets/Scripts/ServerShared/Behaviors/Thruster.cs +++ b/Assets/Scripts/ServerShared/Behaviors/Thruster.cs @@ -6,13 +6,13 @@ using static Unity.Mathematics.math; [MessagePackObject, JsonObject(MemberSerialization.OptIn), EntityTypeRestriction(HullType.Ship)] public class ThrusterData : BehaviorData { - [InspectableField, JsonProperty("thrust"), Key(0)] + [InspectableField, JsonProperty("thrust"), Key(1)] public PerformanceStat Thrust = new PerformanceStat(); - [InspectableField, JsonProperty("visibility"), Key(1)] + [InspectableField, JsonProperty("visibility"), Key(2)] public PerformanceStat Visibility = new PerformanceStat(); - [InspectableField, JsonProperty("heat"), Key(2)] + [InspectableField, JsonProperty("heat"), Key(3)] public PerformanceStat Heat = new PerformanceStat(); public override IBehavior CreateInstance(GameContext context, Entity entity, Gear item) @@ -52,11 +52,16 @@ public class Thruster : IAnalogBehavior { } - public void Update(float delta) + public bool Update(float delta) { Thrust = Context.Evaluate(_data.Thrust, Item, Entity); Entity.Velocity += Entity.Direction * _input * Thrust / Entity.Mass * delta; Entity.AddHeat(_input * Context.Evaluate(_data.Heat, Item, Entity) * delta); Entity.VisibilitySources[this] = _input * Context.Evaluate(_data.Visibility, Item, Entity); + return true; + } + + public void Remove() + { } } \ No newline at end of file diff --git a/Assets/Scripts/ServerShared/Behaviors/TowingController.cs b/Assets/Scripts/ServerShared/Behaviors/TowingController.cs index 1e60bc02c..b11a42bb5 100644 --- a/Assets/Scripts/ServerShared/Behaviors/TowingController.cs +++ b/Assets/Scripts/ServerShared/Behaviors/TowingController.cs @@ -48,7 +48,7 @@ public class TowingController : IBehavior, IPersistentBehavior, IController _velocityMatch = new VelocityMatch(_context, _entity, _data); } - public void Update(float delta) + public bool Update(float delta) { if (_path.Any()) { @@ -119,6 +119,7 @@ public class TowingController : IBehavior, IPersistentBehavior, IController _velocityMatch.Update(delta); } } + return true; } public PersistentBehaviorData Store() @@ -148,6 +149,10 @@ public class TowingController : IBehavior, IPersistentBehavior, IController { _locomotion.Objective = _context.WormholePosition(_entity.Zone, _path[0].ZoneID); } + + public void Remove() + { + } } public class TowingControllerPersistence : PersistentBehaviorData diff --git a/Assets/Scripts/ServerShared/Behaviors/Turning.cs b/Assets/Scripts/ServerShared/Behaviors/Turning.cs index da86a40a4..974d1a8e7 100644 --- a/Assets/Scripts/ServerShared/Behaviors/Turning.cs +++ b/Assets/Scripts/ServerShared/Behaviors/Turning.cs @@ -6,13 +6,13 @@ using static Unity.Mathematics.math; [MessagePackObject, JsonObject(MemberSerialization.OptIn)] public class TurningData : BehaviorData { - [InspectableField, JsonProperty("torque"), Key(0)] + [InspectableField, JsonProperty("torque"), Key(1)] public PerformanceStat Torque = new PerformanceStat(); - [InspectableField, JsonProperty("visibility"), Key(1)] + [InspectableField, JsonProperty("visibility"), Key(2)] public PerformanceStat Visibility = new PerformanceStat(); - [InspectableField, JsonProperty("heat"), Key(2)] + [InspectableField, JsonProperty("heat"), Key(3)] public PerformanceStat Heat = new PerformanceStat(); public override IBehavior CreateInstance(GameContext context, Entity entity, Gear item) @@ -52,11 +52,16 @@ public class Turning : IAnalogBehavior { } - public void Update(float delta) + public bool Update(float delta) { Torque = Context.Evaluate(_data.Torque, Item, Entity); Entity.Direction = mul(Entity.Direction, Unity.Mathematics.float2x2.Rotate(_input * Torque / Entity.Mass * delta)); Entity.AddHeat(abs(_input) * Context.Evaluate(_data.Heat, Item, Entity) * delta); Entity.VisibilitySources[this] = abs(_input) * Context.Evaluate(_data.Visibility, Item, Entity); + return true; + } + + public void Remove() + { } } \ No newline at end of file diff --git a/Assets/Scripts/ServerShared/Behaviors/VelocityConversion.cs b/Assets/Scripts/ServerShared/Behaviors/VelocityConversion.cs index 4acda6667..c884986ad 100644 --- a/Assets/Scripts/ServerShared/Behaviors/VelocityConversion.cs +++ b/Assets/Scripts/ServerShared/Behaviors/VelocityConversion.cs @@ -6,7 +6,7 @@ using static Unity.Mathematics.math; [MessagePackObject, JsonObject(MemberSerialization.OptIn)] public class VelocityConversionData : BehaviorData { - [InspectableField, JsonProperty("traction"), Key(0)] + [InspectableField, JsonProperty("traction"), Key(1)] public PerformanceStat Traction = new PerformanceStat(); public override IBehavior CreateInstance(GameContext context, Entity entity, Gear item) @@ -37,7 +37,12 @@ public class VelocityConversion : IBehavior { } - public void Update(float delta) + public bool Update(float delta) + { + return true; + } + + public void Remove() { } } \ No newline at end of file diff --git a/Assets/Scripts/ServerShared/Behaviors/VelocityLimit.cs b/Assets/Scripts/ServerShared/Behaviors/VelocityLimit.cs index 698df3f16..a9b6e48ee 100644 --- a/Assets/Scripts/ServerShared/Behaviors/VelocityLimit.cs +++ b/Assets/Scripts/ServerShared/Behaviors/VelocityLimit.cs @@ -6,7 +6,7 @@ using static Unity.Mathematics.math; [MessagePackObject, JsonObject(MemberSerialization.OptIn)] public class VelocityLimitData : BehaviorData { - [InspectableField, JsonProperty("topSpeed"), Key(0)] + [InspectableField, JsonProperty("topSpeed"), Key(1)] public PerformanceStat TopSpeed = new PerformanceStat(); public override IBehavior CreateInstance(GameContext context, Entity entity, Gear item) @@ -40,10 +40,15 @@ public class VelocityLimit : IBehavior { } - public void Update(float delta) + public bool Update(float delta) { Limit = Context.Evaluate(_data.TopSpeed, Item, Entity); if (length(Entity.Velocity) > Limit) Entity.Velocity = normalize(Entity.Velocity) * Limit; + return true; + } + + public void Remove() + { } } \ No newline at end of file diff --git a/Assets/Scripts/ServerShared/Behaviors/Weapon.cs b/Assets/Scripts/ServerShared/Behaviors/Weapon.cs index e80cdcedb..53ba24292 100644 --- a/Assets/Scripts/ServerShared/Behaviors/Weapon.cs +++ b/Assets/Scripts/ServerShared/Behaviors/Weapon.cs @@ -5,30 +5,30 @@ using Newtonsoft.Json; Union(1, typeof(LauncherData))] public abstract class WeaponData : BehaviorData { - [InspectableField, JsonProperty("damageType"), Key(0)] + [InspectableField, JsonProperty("damageType"), Key(1)] public DamageType DamageType; - [InspectableField, JsonProperty("damage"), Key(1)] + [InspectableField, JsonProperty("damage"), Key(2)] public PerformanceStat Damage = new PerformanceStat(); - [InspectableField, JsonProperty("range"), Key(2)] + [InspectableField, JsonProperty("range"), Key(3)] public PerformanceStat Range = new PerformanceStat(); - [InspectableField, JsonProperty("cooldown"), Key(3)] + [InspectableField, JsonProperty("cooldown"), Key(4)] public PerformanceStat Cooldown = new PerformanceStat(); - [InspectableField, JsonProperty("heat"), Key(4)] + [InspectableField, JsonProperty("heat"), Key(5)] public PerformanceStat Heat = new PerformanceStat(); - [InspectableField, JsonProperty("visibility"), Key(5)] + [InspectableField, JsonProperty("visibility"), Key(6)] public PerformanceStat Visibility = new PerformanceStat(); - [InspectableField, JsonProperty("visibilityDecay"), Key(6)] + [InspectableField, JsonProperty("visibilityDecay"), Key(7)] public PerformanceStat VisibilityDecay = new PerformanceStat(); - [InspectableField, JsonProperty("burstCount"), Key(7)] + [InspectableField, JsonProperty("burstCount"), Key(8)] public int BurstCount; - [InspectableField, JsonProperty("burstTime"), Key(8)] + [InspectableField, JsonProperty("burstTime"), Key(9)] public PerformanceStat BurstTime = new PerformanceStat(); } \ No newline at end of file diff --git a/Assets/Scripts/ServerShared/DatabaseItems.cs b/Assets/Scripts/ServerShared/DatabaseItems.cs index 23e9375aa..26cd686c0 100644 --- a/Assets/Scripts/ServerShared/DatabaseItems.cs +++ b/Assets/Scripts/ServerShared/DatabaseItems.cs @@ -87,15 +87,21 @@ public class MegaCorporation : DatabaseEntry, INamedEntry [InspectableField, JsonProperty("name"), Key(1)] public string Name; - [InspectableTexture, JsonProperty("logo"), Key(2)] + [InspectableText, JsonProperty("description"), Key(2)] + public string Description; + + [InspectableTexture, JsonProperty("logo"), Key(3)] public string Logo; - [InspectableDatabaseLink(typeof(PersonalityAttribute)), JsonProperty("initialFleet"), Key(3)] + [InspectableDatabaseLink(typeof(PersonalityAttribute)), JsonProperty("personality"), Key(4)] public Dictionary Personality = new Dictionary(); - [InspectableDatabaseLink(typeof(LoadoutData)), JsonProperty("initialFleet"), Key(4)] + [InspectableDatabaseLink(typeof(LoadoutData)), JsonProperty("initialFleet"), Key(5)] public Dictionary InitialFleet = new Dictionary(); + [InspectableDatabaseLink(typeof(BlueprintData)), JsonProperty("initialTechs"), Key(6)] + public List InitialTechnologies = new List(); + [IgnoreMember] public string EntryName { get => Name; diff --git a/Assets/Scripts/ServerShared/Entity.cs b/Assets/Scripts/ServerShared/Entity.cs index 763a53205..23ee022f5 100644 --- a/Assets/Scripts/ServerShared/Entity.cs +++ b/Assets/Scripts/ServerShared/Entity.cs @@ -30,13 +30,14 @@ public abstract class Entity : DatabaseEntry, IMessagePackSerializationCallbackR // [IgnoreMember] protected List Behaviors; [IgnoreMember] public Guid Zone; - [IgnoreMember] public Dictionary> ItemBehaviors; - [IgnoreMember] public Dictionary> Bindings; - [IgnoreMember] public Dictionary Axes; - [IgnoreMember] public Dictionary AxisOverrides = new Dictionary(); + [IgnoreMember] public Dictionary> Behaviors; + [IgnoreMember] public Dictionary> Bindings; + [IgnoreMember] public Dictionary Axes; + [IgnoreMember] public Dictionary AxisOverrides = new Dictionary(); [IgnoreMember] public readonly Dictionary VisibilitySources = new Dictionary(); private Dictionary GearData; + private Dictionary> ItemBehaviors; [IgnoreMember] public float Mass { get; private set; } [IgnoreMember] public float SpecificHeat { get; private set; } @@ -64,26 +65,44 @@ public abstract class Entity : DatabaseEntry, IMessagePackSerializationCallbackR var gearData = gear.ToDictionary(id => id, id => Context.Cache.Get(id)); GearData = gearData.Values.ToDictionary(g => g, g => g.ItemData); + // Associate items with behavior list, used only for persistence ItemBehaviors = gear .Where(i=> gearData[i].ItemData.Behaviors?.Any()??false) .ToDictionary(i=> gearData[i], i => gearData[i].ItemData.Behaviors .Select(bd => bd.CreateInstance(Context, this, gearData[i])) .OrderBy(b => b.GetType().GetCustomAttribute()?.Order ?? 0) .ToList()); + + var behaviorGroups = ItemBehaviors + .SelectMany(x => x.Value + .Select(b => new {item = x.Key, behavior = b}) + .OrderBy(ib => ib.behavior.GetType().GetCustomAttribute()?.Order ?? 0)) + .GroupBy(ib => new {ib.item, ib.behavior.Data.Group}); - Bindings = ItemBehaviors + Behaviors = behaviorGroups + .Select((grouping, i) => new {index = i, group = grouping}) + .ToDictionary(ig => ig.index, ig => ig.group.Select(x => x.behavior).ToList()); + + Bindings = Behaviors .Where(x => x.Value.Any(g => g is IActivatedBehavior)) - .Select(x=> new {gear = x.Key, behaviors = x.Value + .Select((x, i) => new {index = i, behaviors = x.Value .Where(g => g is IActivatedBehavior) .Cast().ToList()}) - .ToDictionary(x => x.gear, x => x.behaviors); + .ToDictionary(x => x.index, x => x.behaviors); - Axes = ItemBehaviors.Values - .SelectMany(behaviors=>behaviors) - .Where(b => b is IAnalogBehavior) - .ToDictionary(b => b as IAnalogBehavior, b => 0f); + Axes = Behaviors + .Where(x => x.Value.Any(g => g is IAnalogBehavior)) + .Select((x, i) => new {index = i, behaviors = x.Value + .Where(g => g is IAnalogBehavior) + .Cast().ToList()}) + .ToDictionary(x => x.index, x => new AxisSetting{Behaviors = x.behaviors, Value = 0f}); - foreach (var behavior in ItemBehaviors.Values + // Axes = Behaviors.Values + // .SelectMany(behaviors=>behaviors) + // .Where(b => b is IAnalogBehavior) + // .ToDictionary(b => b as IAnalogBehavior, b => 0f); + + foreach (var behavior in Behaviors.Values .SelectMany(behaviors=>behaviors)) behavior.Initialize(); } @@ -101,7 +120,7 @@ public abstract class Entity : DatabaseEntry, IMessagePackSerializationCallbackR public IEnumerable GetBehaviors() where T : class, IBehavior { - foreach (var behaviors in ItemBehaviors.Values) + foreach (var behaviors in Behaviors.Values) foreach (var behavior in behaviors) if (behavior is T b) yield return b; @@ -109,12 +128,29 @@ public abstract class Entity : DatabaseEntry, IMessagePackSerializationCallbackR public IEnumerable GetBehaviorData() where T : BehaviorData { - foreach (var behaviors in ItemBehaviors.Values) + foreach (var behaviors in Behaviors.Values) foreach (var behavior in behaviors) if (behavior.Data is T b) yield return b; } + public int GetAxis(IAnalogBehavior behavior) + { + var axis = Axes.FirstOrDefault(x => x.Value.Behaviors.Contains(behavior)); + if (axis.IsNull()) + return -1; + return axis.Key; + } + + public int GetAxis() where T : class, IBehavior + { + foreach (var axis in Axes) + foreach (var behavior in axis.Value.Behaviors) + if (behavior is T) + return axis.Key; + return -1; + } + public void AddHeat(float heat) { Temperature += heat / SpecificHeat; @@ -122,63 +158,14 @@ public abstract class Entity : DatabaseEntry, IMessagePackSerializationCallbackR public virtual void Update(float delta) { - foreach(var analogBehavior in Axes.Keys) - analogBehavior.SetAxis(AxisOverrides.ContainsKey(analogBehavior) ? AxisOverrides[analogBehavior] : Axes[analogBehavior]); + foreach(var axis in Axes) + foreach(var analogBehavior in axis.Value.Behaviors) + analogBehavior.SetAxis(AxisOverrides.ContainsKey(axis.Key) ? AxisOverrides[axis.Key] : Axes[axis.Key].Value); - foreach (var behavior in ItemBehaviors.SelectMany(item => item.Value)) - behavior.Update(delta); - - foreach (var staleItem in GearData - .Where(kvp=>kvp.Key.ItemData!=kvp.Value) - .Select(kvp=>kvp.Key)) - { - // Item is stale, there's an updated one in the database cache - //var actualItem = Context.Cache.Get(staleItem.ID); - var staleBehaviors = ItemBehaviors[staleItem]; - ItemBehaviors.Remove(staleItem); - - // We'll be reloading the item along with its behaviors, some data must be persisted - var persistentData = staleBehaviors - .Where(b => b is IPersistentBehavior) - .Cast() - .Select(b => b.Store()); - - // Regenerate the behavior list for the updated item - ItemBehaviors[staleItem] = staleItem.ItemData.Behaviors - .Select(bd => bd.CreateInstance(Context, this, staleItem)) - .OrderBy(b => b.GetType().GetCustomAttribute()?.Order ?? 0).ToList(); - - foreach(var behavior in ItemBehaviors[staleItem]) - behavior.Initialize(); - - // Restore state for persisted behaviors - foreach(var persistentBehaviorData in ItemBehaviors[staleItem] - .Where(b => b is IPersistentBehavior) - .Cast() - .Zip(persistentData, (behavior, data) => new {behavior, data})) - persistentBehaviorData.behavior.Restore(persistentBehaviorData.data); - - // Regenerate activated behavior bindings - if (Bindings.ContainsKey(staleItem)) - { - Bindings.Remove(staleItem); - Bindings[staleItem] = ItemBehaviors[staleItem] - .Where(b => b is IActivatedBehavior) - .Cast().ToList(); - } - - // Remove axis bindings and overrides for analog behaviors - foreach (var axis in staleBehaviors.Where(b => b is IAnalogBehavior).Cast()) - { - Axes.Remove(axis); - if (AxisOverrides.ContainsKey(axis)) - AxisOverrides.Remove(axis); - } - - // Regenerate axis bindings for analog behaviors - foreach (var axis in ItemBehaviors[staleItem].Where(b => b is IAnalogBehavior).Cast()) - Axes[axis] = 0; - } + foreach (var group in Behaviors.Values) + foreach(var behavior in group) + if(!behavior.Update(delta)) + break; foreach (var item in EquippedItems .Select(item => Context.Cache.Get(item)) @@ -191,6 +178,66 @@ public abstract class Entity : DatabaseEntry, IMessagePackSerializationCallbackR Position = parent.Position; Velocity = parent.Velocity; } + + // Processing stale item data properly was a pain in my ass, so the shortcut is just persisting and restoring everything + if (GearData.Any(kvp => kvp.Key.ItemData != kvp.Value)) + { + OnBeforeSerialize(); + OnAfterDeserialize(); + } + + // Here's the PITA way + // foreach (var staleItem in GearData + // .Where(kvp=>kvp.Key.ItemData!=kvp.Value) + // .Select(kvp=>kvp.Key)) + // { + // // Item is stale, there's an updated one in the database cache + // //var actualItem = Context.Cache.Get(staleItem.ID); + // var staleBehaviors = ItemBehaviors[staleItem]; + // ItemBehaviors.Remove(staleItem); + // + // // We'll be reloading the item along with its behaviors, some data must be persisted + // var persistentData = staleBehaviors + // .Where(b => b is IPersistentBehavior) + // .Cast() + // .Select(b => b.Store()); + // + // // Regenerate the behavior list for the updated item + // ItemBehaviors[staleItem] = staleItem.ItemData.Behaviors + // .Select(bd => bd.CreateInstance(Context, this, staleItem)) + // .OrderBy(b => b.GetType().GetCustomAttribute()?.Order ?? 0).ToList(); + // + // foreach(var behavior in ItemBehaviors[staleItem]) + // behavior.Initialize(); + // + // // Restore state for persisted behaviors + // foreach(var persistentBehaviorData in ItemBehaviors[staleItem] + // .Where(b => b is IPersistentBehavior) + // .Cast() + // .Zip(persistentData, (behavior, data) => new {behavior, data})) + // persistentBehaviorData.behavior.Restore(persistentBehaviorData.data); + // + // // Regenerate activated behavior bindings + // if (Bindings.ContainsKey(staleItem)) + // { + // Bindings.Remove(staleItem); + // Bindings[staleItem] = ItemBehaviors[staleItem] + // .Where(b => b is IActivatedBehavior) + // .Cast().ToList(); + // } + // + // // Remove axis bindings and overrides for analog behaviors + // foreach (var axis in staleBehaviors.Where(b => b is IAnalogBehavior).Cast()) + // { + // Axes.Remove(axis); + // if (AxisOverrides.ContainsKey(axis)) + // AxisOverrides.Remove(axis); + // } + // + // // Regenerate axis bindings for analog behaviors + // foreach (var axis in ItemBehaviors[staleItem].Where(b => b is IAnalogBehavior).Cast()) + // Axes[axis] = 0; + // } } public void OnBeforeSerialize() @@ -223,4 +270,10 @@ public abstract class Entity : DatabaseEntry, IMessagePackSerializationCallbackR .Zip(PersistedBehaviors[itemBehaviors.Key.ID], (behavior, data) => new{behavior, data}))) persistentBehaviorData.behavior.Restore(persistentBehaviorData.data); } +} + +public class AxisSetting +{ + public float Value; + public List Behaviors; } \ No newline at end of file diff --git a/Assets/Scripts/ServerShared/Extensions.cs b/Assets/Scripts/ServerShared/Extensions.cs index 93c81d85a..b61ef016e 100644 --- a/Assets/Scripts/ServerShared/Extensions.cs +++ b/Assets/Scripts/ServerShared/Extensions.cs @@ -130,6 +130,11 @@ public static class Extensions return isDefault; } + + public static bool IsNull(this KeyValuePair pair) + { + return pair.Equals(new KeyValuePair()); + } private static Dictionary _cachedIndices = new Dictionary(); public static float Evaluate(this float4[] curve, float time) diff --git a/Assets/Scripts/ServerShared/GlobalData.cs b/Assets/Scripts/ServerShared/GlobalData.cs index bbbdd2efa..2ddcc799a 100644 --- a/Assets/Scripts/ServerShared/GlobalData.cs +++ b/Assets/Scripts/ServerShared/GlobalData.cs @@ -14,6 +14,9 @@ public class GlobalData : DatabaseEntry [InspectableField, JsonProperty("galaxyName"), Key(1)] public string GalaxyName; + [InspectableDatabaseLink(typeof(ZoneData)), JsonProperty("arrivalZone"), Key(2)] + public Guid ArrivalZone; + [InspectableField, JsonProperty("galaxyArms"), Key(4)] public int Arms = 4; diff --git a/Assets/Scripts/ServerShared/ItemData.cs b/Assets/Scripts/ServerShared/ItemData.cs index 596f839af..d039a4d15 100644 --- a/Assets/Scripts/ServerShared/ItemData.cs +++ b/Assets/Scripts/ServerShared/ItemData.cs @@ -83,28 +83,25 @@ public class CompoundCommodityData : CraftedItemData JsonObject(MemberSerialization.OptIn), JsonConverter(typeof(JsonKnownTypesConverter))] public abstract class EquippableItemData : CraftedItemData { - [InspectableField, JsonProperty("draw"), Key(6)] - public PerformanceStat EnergyDraw = new PerformanceStat(); - - [InspectableAnimationCurve, JsonProperty("performanceCurve"), Key(7)] + [InspectableAnimationCurve, JsonProperty("performanceCurve"), Key(6)] public float4[] HeatPerformanceCurve; - [TemperatureInspectable, JsonProperty("minTemp"), Key(8)] + [TemperatureInspectable, JsonProperty("minTemp"), Key(7)] public float MinimumTemperature; - [TemperatureInspectable, JsonProperty("maxTemp"), Key(9)] + [TemperatureInspectable, JsonProperty("maxTemp"), Key(8)] public float MaximumTemperature; - [InspectableField, JsonProperty("durabilityStat"), Key(10)] + [InspectableField, JsonProperty("durabilityStat"), Key(9)] public PerformanceStat Durability = new PerformanceStat(); - [InspectableField, JsonProperty("durabilityExponent"), Key(11)] + [InspectableField, JsonProperty("durabilityExponent"), Key(10)] public PerformanceStat DurabilityExponent = new PerformanceStat(); - [InspectableField, JsonProperty("heatExponent"), Key(12)] + [InspectableField, JsonProperty("heatExponent"), Key(11)] public PerformanceStat HeatExponent = new PerformanceStat(); - [InspectableField, JsonProperty("behaviors"), Key(13)] + [InspectableField, JsonProperty("behaviors"), Key(12)] public List Behaviors = new List(); [IgnoreMember] @@ -142,7 +139,7 @@ public abstract class EquippableItemData : CraftedItemData [RethinkTable("Items"), Inspectable, MessagePackObject, JsonObject(MemberSerialization.OptIn)] public class GearData : EquippableItemData { - [InspectableField, JsonProperty("hardpointType"), Key(14)] + [InspectableField, JsonProperty("hardpointType"), Key(13)] public HardpointType Hardpoint; [IgnoreMember] public override HardpointType HardpointType => Hardpoint; @@ -151,16 +148,16 @@ public class GearData : EquippableItemData [RethinkTable("Items"), Inspectable, MessagePackObject, JsonObject(MemberSerialization.OptIn)] public class HullData : EquippableItemData { - [InspectableField, JsonProperty("hullCapacity"), Key(14)] + [InspectableField, JsonProperty("hullCapacity"), Key(13)] public PerformanceStat Capacity = new PerformanceStat(); - [InspectableField, JsonProperty("hardpoints"), Key(15)] + [InspectableField, JsonProperty("hardpoints"), Key(14)] public List Hardpoints = new List(); - [InspectablePrefab, JsonProperty("prefab"), Key(16)] + [InspectablePrefab, JsonProperty("prefab"), Key(15)] public string Prefab; - [InspectableField, JsonProperty("hullType"), Key(17)] + [InspectableField, JsonProperty("hullType"), Key(16)] public HullType HullType; [IgnoreMember] public override HardpointType HardpointType => HardpointType.Hull; diff --git a/Assets/Scripts/StrategyGameManager.cs b/Assets/Scripts/StrategyGameManager.cs index 341f76e5d..3b86609da 100644 --- a/Assets/Scripts/StrategyGameManager.cs +++ b/Assets/Scripts/StrategyGameManager.cs @@ -244,7 +244,7 @@ public class StrategyGameManager : MonoBehaviour ship.Value.transform.position = float3(ship.Key.Position * ZoneSizeScale, .05f); ship.Value.Icon.transform.up = (Vector2) ship.Key.Direction; var thrusterScale = ship.Value.Thruster.localScale; - thrusterScale.x = ship.Key.Axes.First(kvp => kvp.Key is Thruster).Value; + thrusterScale.x = ship.Key.Axes[ship.Key.GetAxis()].Value; ship.Value.Thruster.localScale = thrusterScale; } } diff --git a/Economy.Shared.Unity.csproj b/Economy.Shared.Unity.csproj index b4d713656..19667b939 100644 --- a/Economy.Shared.Unity.csproj +++ b/Economy.Shared.Unity.csproj @@ -69,10 +69,13 @@ + + +