From 1e2b474fc1eb5d95b29779f0562c05a8308ffd9d Mon Sep 17 00:00:00 2001 From: hyperion51 Date: Fri, 3 Jul 2020 01:33:42 +0100 Subject: [PATCH] Various Fixes Newly created drones now register their origin colony correctly Hauling behavior fixed for multiple items Buttons can't be dragged unless explicitly enabled Inspecting a colony with lots of compound commodities no longer sets your computer on fire --- .../ServerShared/Behaviors/Controller.cs | 19 +++-- .../Behaviors/HaulingController.cs | 3 +- Assets/Scripts/ServerShared/Entity.cs | 46 +++++++++--- Assets/Scripts/ServerShared/GameContext.cs | 7 +- Assets/Scripts/ServerShared/Ship.cs | 3 + Assets/Scripts/StrategyGameManager.cs | 5 +- Assets/Scripts/TechTreeMsagl.cs | 3 + Assets/Scripts/UI/ColonyTab.cs | 10 +-- Assets/Scripts/UI/FlatFlatButton.cs | 6 +- Assets/Scripts/UI/PropertiesList.cs | 6 +- Assets/Scripts/UI/PropertiesPanel.cs | 72 ++++++++++++++----- 11 files changed, 133 insertions(+), 47 deletions(-) diff --git a/Assets/Scripts/ServerShared/Behaviors/Controller.cs b/Assets/Scripts/ServerShared/Behaviors/Controller.cs index 67033d53a..87d735851 100644 --- a/Assets/Scripts/ServerShared/Behaviors/Controller.cs +++ b/Assets/Scripts/ServerShared/Behaviors/Controller.cs @@ -28,7 +28,7 @@ public abstract class ControllerData : BehaviorData public abstract class ControllerBase : IBehavior, IController, IInitializableBehavior { - public Guid HomeEntity; + protected Guid HomeEntity => (_entity as Ship)?.HomeEntity ?? Guid.Empty; public bool Available => _spaceworthy && Task == Guid.Empty; public abstract TaskType TaskType { get; } public Guid Zone => _entity.Zone; @@ -59,15 +59,24 @@ public abstract class ControllerBase : IBehavior, IController, IInitializableBeh _context = context; _controllerData = data; _entity = entity; - entity.GearEvent.OnChanged += () => _spaceworthy = entity.GetBehavior() != null && entity.GetBehavior() != null; + entity.GearEvent.OnChanged += () => + { + _spaceworthy = entity.GetBehavior() != null && entity.GetBehavior() != null; + if(_spaceworthy) + CreateBehaviors(); + }; } - - public void Initialize() + + public void CreateBehaviors() { Locomotion = new Locomotion(_context, _entity, _controllerData); VelocityMatch = new VelocityMatch(_context, _entity, _controllerData); Aim = new Aim(_context, _entity, _controllerData); - + } + + public void Initialize() + { + CreateBehaviors(); _context.CorporationControllers[_entity.Corporation].Add(this); } diff --git a/Assets/Scripts/ServerShared/Behaviors/HaulingController.cs b/Assets/Scripts/ServerShared/Behaviors/HaulingController.cs index 95adf87f6..8b6f0357c 100644 --- a/Assets/Scripts/ServerShared/Behaviors/HaulingController.cs +++ b/Assets/Scripts/ServerShared/Behaviors/HaulingController.cs @@ -84,7 +84,8 @@ public class HaulingController : ControllerBase, IBehavior, IPersistentBehavior { var itemMatches = target.Cargo .Select(id => _context.Cache.Get(id)) - .Where(ii => ii != null && ii.Data == haulingTask.ItemType); + .Where(ii => ii != null && ii.Data == haulingTask.ItemType) + .ToArray(); if(!itemMatches.Any()) FinishTask(); foreach (var match in itemMatches) diff --git a/Assets/Scripts/ServerShared/Entity.cs b/Assets/Scripts/ServerShared/Entity.cs index 61c95e30d..a308b3f53 100644 --- a/Assets/Scripts/ServerShared/Entity.cs +++ b/Assets/Scripts/ServerShared/Entity.cs @@ -68,9 +68,27 @@ public abstract class Entity : DatabaseEntry, IMessagePackSerializationCallbackR public Dictionary IncompleteCargo = new Dictionary(); [JsonProperty("active"), Key(18)] - public bool Active { get; set; } + private bool _active; + public bool Active + { + get => _active; + set + { + _active = value; + if (_active) + { + foreach (var hardpoint in Hardpoints) + if(hardpoint.Gear!=null) + foreach (var behavior in hardpoint.Behaviors) + { + if(behavior is IInitializableBehavior initializableBehavior) + initializableBehavior.Initialize(); + } + } + } + } //public List IncompleteCargo = new List(); - + [IgnoreMember] public Guid Zone; [IgnoreMember] public List Hardpoints; [IgnoreMember] public readonly Dictionary VisibilitySources = new Dictionary(); @@ -168,15 +186,16 @@ public abstract class Entity : DatabaseEntry, IMessagePackSerializationCallbackR Cargo.Add(remaining); } - foreach (var hardpoint in Hardpoints) - if(hardpoint.Gear!=null) - foreach (var behavior in hardpoint.Behaviors) - { - if(behavior is IPopulationAssignment populationAssignment) - PopulationAssignments.Add(populationAssignment); - if(behavior is IInitializableBehavior initializableBehavior) - initializableBehavior.Initialize(); - } + if (_active) + { + foreach (var hardpoint in Hardpoints) + if(hardpoint.Gear!=null) + foreach (var behavior in hardpoint.Behaviors) + { + if(behavior is IInitializableBehavior initializableBehavior) + initializableBehavior.Initialize(); + } + } RecalculateMass(); } @@ -206,6 +225,11 @@ public abstract class Entity : DatabaseEntry, IMessagePackSerializationCallbackR }) .ToArray(); + foreach (var behavior in hardpoint.Behaviors) + { + if(behavior is IPopulationAssignment populationAssignment) + PopulationAssignments.Add(populationAssignment); + } } public void Unequip(Hardpoint hardpoint) diff --git a/Assets/Scripts/ServerShared/GameContext.cs b/Assets/Scripts/ServerShared/GameContext.cs index 8ef462a3c..88e1712ae 100644 --- a/Assets/Scripts/ServerShared/GameContext.cs +++ b/Assets/Scripts/ServerShared/GameContext.cs @@ -710,7 +710,7 @@ public class GameContext if (hullData.HullType == HullType.Ship) { var entity = CreateShip(hull.ID, gear.Select(g => g.ID), - cargo.Select(c => c.ID).Concat(simpleCargo.Select(c => c.ID)), zone, corporation, loadoutData.Name); + cargo.Select(c => c.ID).Concat(simpleCargo.Select(c => c.ID)), zone, corporation, Guid.Empty, loadoutData.Name); entity.Active = true; return entity; } @@ -752,12 +752,13 @@ public class GameContext return null; } - public Ship CreateShip(Guid hull, IEnumerable gear, IEnumerable cargo, Guid zone, Guid corporation, string typeName) + public Ship CreateShip(Guid hull, IEnumerable gear, IEnumerable cargo, Guid zone, Guid corporation, Guid homeEntity, string typeName) { var ship = new Ship(this, hull, gear, cargo, zone, corporation) { - Name = $"{typeName} {Random.NextInt(1,255):X}" + Name = $"{typeName} {Random.NextInt(1,255):X}", + HomeEntity = homeEntity }; Cache.Add(ship); diff --git a/Assets/Scripts/ServerShared/Ship.cs b/Assets/Scripts/ServerShared/Ship.cs index 3b8ca2349..e7eb41042 100644 --- a/Assets/Scripts/ServerShared/Ship.cs +++ b/Assets/Scripts/ServerShared/Ship.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Linq; using System.Reflection; using MessagePack; +using Newtonsoft.Json; using Unity.Mathematics; using static Unity.Mathematics.math; @@ -16,6 +17,8 @@ public class Ship : Entity // [IgnoreMember] public Dictionary Contacts = new Dictionary(); // [IgnoreMember] public Targetable Target; + [JsonProperty("homeEntity"), Key(19)] + public Guid HomeEntity; public Ship(GameContext context, Guid hull, IEnumerable gear, IEnumerable cargo, Guid zone, Guid corporation) : base(context, hull, gear, cargo, zone, corporation) { diff --git a/Assets/Scripts/StrategyGameManager.cs b/Assets/Scripts/StrategyGameManager.cs index b3ec81b2e..0cfb5bbe4 100644 --- a/Assets/Scripts/StrategyGameManager.cs +++ b/Assets/Scripts/StrategyGameManager.cs @@ -213,6 +213,7 @@ public class StrategyGameManager : MonoBehaviour ZoneTabButton.OnPointerClick(null); var entities = new List(); + foreach(var loadout in parentCorp.InitialFleet) for(int i=0; i e is Ship)) { - foreach (var controller in ship.GetBehaviors()) - controller.HomeEntity = colony.ID; + ((Ship)ship).HomeEntity = colony.ID; _context.SetParent(ship, colony); } @@ -719,6 +719,7 @@ public class StrategyGameManager : MonoBehaviour var itemData = _context.Cache.Get(resource.Key); list.AddProperty(itemData.Name, () => $"{resource.Value:0}"); } + list.SetExpanded(true, true); } } } diff --git a/Assets/Scripts/TechTreeMsagl.cs b/Assets/Scripts/TechTreeMsagl.cs index be3769bbe..11db11faa 100644 --- a/Assets/Scripts/TechTreeMsagl.cs +++ b/Assets/Scripts/TechTreeMsagl.cs @@ -344,6 +344,7 @@ public class TechTreeMsagl : MonoBehaviour var ingredientData = Context.Cache.Get(ingredient.Key); ingredientsList.AddProperty(ingredientData.Name, () => ingredient.Value.ToString()); } + ingredientsList.SetExpanded(true, true); var dependenciesList = PropertiesPanel.AddList("Dependencies"); foreach (var dependency in blueprint.Dependencies) @@ -351,12 +352,14 @@ public class TechTreeMsagl : MonoBehaviour var dependencyBlueprint = Context.Cache.Get(dependency); dependenciesList.AddProperty(dependencyBlueprint.Name); } + dependenciesList.SetExpanded(true, true); var descendantsList = PropertiesPanel.AddList("Descendants"); foreach (var descendant in Blueprints.Where(bp => bp.Dependencies.Any(dep => blueprint.ID == dep))) { descendantsList.AddProperty(descendant.Name); } + descendantsList.SetExpanded(true, true); }; _techInstances.Add(tech.GetComponent()); } diff --git a/Assets/Scripts/UI/ColonyTab.cs b/Assets/Scripts/UI/ColonyTab.cs index cf24d4652..b89420711 100644 --- a/Assets/Scripts/UI/ColonyTab.cs +++ b/Assets/Scripts/UI/ColonyTab.cs @@ -88,7 +88,7 @@ public class ColonyTab : MonoBehaviour foreach (var attribute in entity.Personality.Keys) personalityList.AddPersonalityProperty(Context.Cache.Get(attribute), () => entity.Personality[attribute]); - personalityList.SetExpanded(false, true); + // personalityList.SetExpanded(false, true); var childList = General.AddList("Children"); childList.InspectChildren(entity, child => @@ -96,7 +96,7 @@ public class ColonyTab : MonoBehaviour _selectedChild = child; UpdateInventory(child, ChildInventory); }); - childList.SetExpanded(false, true); + // childList.SetExpanded(false, true); General.RefreshValues(); } @@ -357,7 +357,7 @@ public class ColonyTab : MonoBehaviour { entity.RemoveCargo(gear); var ship = Context.CreateShip(gear.ID, Enumerable.Empty(), - Enumerable.Empty(), entity.Zone, entity.Corporation, gear.Name); + Enumerable.Empty(), entity.Zone, entity.Corporation, _parentEntity.ID, gear.Name); _context.SetParent(ship, entity); } else // Equip Item @@ -412,7 +412,7 @@ public class ColonyTab : MonoBehaviour button.DisableClickWhenSelected = false; button.DragSuccess = data => dragSuccess(data, item); } - instanceList.SetExpanded(false, true); + // instanceList.SetExpanded(false, true); } } } @@ -443,7 +443,7 @@ public class ColonyTab : MonoBehaviour }); blueprintList.RefreshValues(); - blueprintList.SetExpanded(false, true); + // blueprintList.SetExpanded(false, true); } Details.RefreshValues(); } diff --git a/Assets/Scripts/UI/FlatFlatButton.cs b/Assets/Scripts/UI/FlatFlatButton.cs index 3d9a174b7..1b99045c4 100644 --- a/Assets/Scripts/UI/FlatFlatButton.cs +++ b/Assets/Scripts/UI/FlatFlatButton.cs @@ -56,7 +56,11 @@ public class FlatFlatButton : MonoBehaviour, IPointerEnterHandler, IPointerExitH public void OnBeginDrag(PointerEventData eventData) { - if (DragSuccess == null || DragObject == null) return; + if (DragSuccess == null || DragObject == null) + { + eventData.pointerDrag = null; + return; + } var canvas = gameObject.FindInParents(); if (canvas == null) diff --git a/Assets/Scripts/UI/PropertiesList.cs b/Assets/Scripts/UI/PropertiesList.cs index c240c77fd..198341450 100644 --- a/Assets/Scripts/UI/PropertiesList.cs +++ b/Assets/Scripts/UI/PropertiesList.cs @@ -14,12 +14,15 @@ public class PropertiesList : PropertiesPanel public VerticalLayoutGroup LayoutGroup; public int FoldedPadding = -8; public int ExpandedPadding = 8; + public event Action OnExpand; private bool _expanded = false; private float _targetFoldoutRotation = 0; private float _foldoutRotation = 0; - public void Start() + public bool Expanded => _expanded; + + public new void Awake() { Button.OnClick += _ => ToggleExpand(); OnPropertyAdded += go => go.SetActive(_expanded); @@ -49,6 +52,7 @@ public class PropertiesList : PropertiesPanel _foldoutRotation = _targetFoldoutRotation; FoldoutIcon.transform.localRotation = Quaternion.Euler(0,0, _foldoutRotation); } + OnExpand?.Invoke(_expanded); } // public override PropertyLabel AddProperty(string name, Func read = null, Action onClick = null, bool radio = false) diff --git a/Assets/Scripts/UI/PropertiesPanel.cs b/Assets/Scripts/UI/PropertiesPanel.cs index aa117ad56..d177a9375 100644 --- a/Assets/Scripts/UI/PropertiesPanel.cs +++ b/Assets/Scripts/UI/PropertiesPanel.cs @@ -40,8 +40,10 @@ public class PropertiesPanel : MonoBehaviour protected event Action OnPropertyAdded; protected IChangeSource ChangeSource; protected Action OnPropertiesChanged; + + public int Children => Properties.Count + Buttons.Count; - public void Start() + public void Awake() { OnPropertyAdded += go => go.SetActive(true); } @@ -357,11 +359,11 @@ public class PropertiesPanel : MonoBehaviour var hardpointList = AddList("Gear"); hardpointList.InspectHardpoints(entity); - hardpointList.SetExpanded(false,true); + //hardpointList.SetExpanded(false,true); var cargoList = AddList("Cargo"); cargoList.InspectCargo(entity); - cargoList.SetExpanded(false,true); + //cargoList.SetExpanded(false,true); RefreshValues(); } @@ -373,7 +375,18 @@ public class PropertiesPanel : MonoBehaviour ChangeSource = entity.GearEvent; OnPropertiesChanged = InspectGearInternal; ChangeSource.OnChanged += OnPropertiesChanged; - InspectGearInternal(); + if (this is PropertiesList list) + { + if (list.Expanded) + InspectGearInternal(); + else + list.OnExpand += b => + { + if (b) InspectGearInternal(); + }; + } + else + InspectGearInternal(); void InspectGearInternal() { @@ -383,7 +396,7 @@ public class PropertiesPanel : MonoBehaviour { var propertyList = AddList(hardpoint.Gear.Name); propertyList.Inspect(entity, hardpoint); - propertyList.SetExpanded(false,true); + //propertyList.SetExpanded(false,true); } RefreshValues(); } @@ -396,7 +409,18 @@ public class PropertiesPanel : MonoBehaviour ChangeSource = entity.CargoEvent; OnPropertiesChanged = InspectCargoInternal; ChangeSource.OnChanged += OnPropertiesChanged; - InspectCargoInternal(); + if (this is PropertiesList list) + { + if (list.Expanded) + InspectCargoInternal(); + else + list.OnExpand += b => + { + if (b) InspectCargoInternal(); + }; + } + else + InspectCargoInternal(); void InspectCargoInternal() { @@ -408,7 +432,7 @@ public class PropertiesPanel : MonoBehaviour var propertiesList = AddList($"{simpleCommodity.Quantity.ToString()} {data.Name}"); propertiesList.AddItemProperties(entity, simpleCommodity); propertiesList.RefreshValues(); - propertiesList.SetExpanded(false, true); + //propertiesList.SetExpanded(false, true); } foreach (var group in entity.Cargo @@ -429,19 +453,21 @@ public class PropertiesPanel : MonoBehaviour var propertiesList = AddList(item.Name); propertiesList.AddItemProperties(entity, item); propertiesList.RefreshValues(); - propertiesList.SetExpanded(false, true); + //propertiesList.SetExpanded(false, true); } else { var instanceList = AddList($"{group.Count().ToString()} {group.Key.Name}"); - foreach (var item in group) + instanceList.OnExpand += b => { - var propertiesList = instanceList.AddList(item.Name); - propertiesList.AddItemProperties(entity, item); - propertiesList.RefreshValues(); - propertiesList.SetExpanded(false, true); - } - instanceList.SetExpanded(false, true); + if (!b || instanceList.Children > 0) return; + foreach (var item in group) + { + var propertiesList = instanceList.AddList(item.Name); + propertiesList.AddItemProperties(entity, item); + propertiesList.RefreshValues(); + } + }; } } @@ -527,7 +553,6 @@ public class PropertiesPanel : MonoBehaviour AddProperty(childEntity.Name, null, data => onSelect(childEntity), true); } } - } public void Inspect(Entity entity, Hardpoint hardpoint) @@ -539,7 +564,18 @@ public class PropertiesPanel : MonoBehaviour return; OnPropertiesChanged = InspectGearInternal; ChangeSource.OnChanged += OnPropertiesChanged; - InspectGearInternal(); + if (this is PropertiesList list) + { + if (list.Expanded) + InspectGearInternal(); + else + list.OnExpand += b => + { + if (b) InspectGearInternal(); + }; + } + else + InspectGearInternal(); void InspectGearInternal() { @@ -598,7 +634,7 @@ public class PropertiesPanel : MonoBehaviour var itemData = Context.Cache.Get(ingredient.Key); ingredientsList.AddProperty(itemData.Name, () => ingredient.Value.ToString()); } - ingredientsList.SetExpanded(false, true); + // ingredientsList.SetExpanded(false, true); ingredientsList.RefreshValues(); } }