diff --git a/Assets/GWConquest/Scripts/Battle.cs b/Assets/GWConquest/Scripts/Battle.cs index a1dd5c5..1b60bdf 100644 --- a/Assets/GWConquest/Scripts/Battle.cs +++ b/Assets/GWConquest/Scripts/Battle.cs @@ -9,11 +9,11 @@ namespace GWConquest public EntityList FormationList; public IEnumerable AllPlayers { - get => FormationList.Select((f,i) => f.GetComponent().Player).Distinct(); + get => FormationList.Select((f,i) => f.Entity.GetComponent().Player).Distinct(); } public IEnumerable AllUnits { - get => FormationList.SelectMany(e => e.GetComponent().Units); + get => FormationList.SelectMany(e => e.Entity.GetComponent().Units); } private List UnitActions = new List(); diff --git a/Assets/GWConquest/Scripts/BoltList.cs b/Assets/GWConquest/Scripts/BoltList.cs index 1713bea..16741a5 100644 --- a/Assets/GWConquest/Scripts/BoltList.cs +++ b/Assets/GWConquest/Scripts/BoltList.cs @@ -12,9 +12,6 @@ namespace GWConquest private IState state; private string propertyName; - private Func readMethod; - private Action writeMethod; - public BoltList(IState _state, string _propertyName) { state = _state; diff --git a/Assets/GWConquest/Scripts/District.cs b/Assets/GWConquest/Scripts/District.cs index cff2bf1..1c60f04 100644 --- a/Assets/GWConquest/Scripts/District.cs +++ b/Assets/GWConquest/Scripts/District.cs @@ -22,13 +22,19 @@ namespace GWConquest public Player ControllingPlayer { get { - if(State.ControllingPlayerId == -1) + var id = State.ControllingPlayerId; + if(id < 0) { return null; } + else if(id >= Player.PlayerList.Count) + { + BoltLog.Warn("Tried accessing player {0} in player list of length {1}", id, Player.PlayerList.Count); + return null; + } else { - return Player.PlayerList[State.ControllingPlayerId]; + return Player.GetPlayerById(State.ControllingPlayerId); } } @@ -52,10 +58,15 @@ namespace GWConquest public override void Attached() { - StorageCapacity = GameManager.Instance.DefaultStorageCapacity; + if(entity.IsOwner) + { + StorageCapacity = GameManager.Instance.DefaultStorageCapacity; + State.ControllingPlayerId = -1; + } + Inventory = new Inventory(State, "Inventory"); Inventory.StorageCapacity = StorageCapacity; - State.ControllingPlayerId = -1; + BoltLog.Info("Setting storage capacity to {0}", StorageCapacity); AllDistricts.Add(this); } diff --git a/Assets/GWConquest/Scripts/EntityList.cs b/Assets/GWConquest/Scripts/EntityList.cs index 7dcadf6..6bb4afb 100644 --- a/Assets/GWConquest/Scripts/EntityList.cs +++ b/Assets/GWConquest/Scripts/EntityList.cs @@ -35,15 +35,66 @@ namespace GWConquest } } - public class EntityListToken : BoltListToken + public class CachedBoltEntity { - public override BoltEntity ReadEntry(UdpPacket packet) + public NetworkId NetworkId { get; private set; } + private BoltEntity cachedEntity; + + public CachedBoltEntity(NetworkId id) + { + NetworkId = id; + } + + public CachedBoltEntity(BoltEntity e) + { + NetworkId = e.NetworkId; + cachedEntity = e; + } + + public BoltEntity Entity + { + get + { + if (cachedEntity == null) + { + cachedEntity = BoltNetwork.FindEntity(NetworkId); + } + return cachedEntity; + } + } + + public override bool Equals(object obj) + { + if (obj == null || GetType() != obj.GetType()) + { + return false; + } + + var other = obj as CachedBoltEntity; + + return NetworkId == other.NetworkId; + } + + public override int GetHashCode() + { + return NetworkId.PackedValue.GetHashCode(); + } + + public override string ToString() + { + return Entity?.ToString(); + } + } + + public class EntityListToken : BoltListToken + { + public override CachedBoltEntity ReadEntry(UdpPacket packet) { -var id = new NetworkId(packet.ReadULong()); - return BoltNetwork.FindEntity(id); + var id = new NetworkId(packet.ReadULong()); + return new CachedBoltEntity(id); } -public override void WriteEntry(UdpPacket packet, BoltEntity entry) + public override void WriteEntry(UdpPacket packet, CachedBoltEntity entry) { packet.WriteULong(entry.NetworkId.PackedValue); } @@ -51,8 +102,18 @@ public override void WriteEntry(UdpPacket packet, BoltEntity entry) } -public class EntityList : BoltList + public class EntityList : BoltList { - public EntityList(IState _state, string _propertyName) : base(_state, _propertyName) {} + public EntityList(IState _state, string _propertyName) : base(_state, _propertyName) { } + + public void Add(BoltEntity e) + { + Add(new CachedBoltEntity(e)); + } + + public bool Remove(BoltEntity e) + { + return Remove(new CachedBoltEntity(e)); + } } } \ No newline at end of file diff --git a/Assets/GWConquest/Scripts/Formation.cs b/Assets/GWConquest/Scripts/Formation.cs index 8a01011..dbb30fa 100644 --- a/Assets/GWConquest/Scripts/Formation.cs +++ b/Assets/GWConquest/Scripts/Formation.cs @@ -77,7 +77,7 @@ namespace GWConquest public IEnumerable Units { - get => UnitEntities.Select((e,i) => e.GetComponent()); + get => UnitEntities.Select((e,i) => e.Entity.GetComponent()); } public Unit HeroUnit @@ -93,7 +93,7 @@ namespace GWConquest public IEnumerable SubFormations { - get => SubFormationEntities.Select((e,i) => e.GetComponent()); + get => SubFormationEntities.Select((e,i) => e.Entity.GetComponent()); } public ZoneList PathQueue; @@ -240,15 +240,11 @@ namespace GWConquest movingArmyIcon.transform.SetParent(FindObjectOfType().PlanetView.DistrictIcons); movingArmyIcon.transform.localRotation = Quaternion.identity; var iconComp = movingArmyIcon.GetComponent(); + iconComp.Arrow.gameObject.SetActive(true); iconComp.UpdateDisplay(this); iconComp.CanBeDragged = false; var stick = movingArmyIcon.AddComponent(); stick.target = transform; - //movingArmyIcon.GetComponent().target = transform; - //movingArmyIcon.GetComponentInChildren().color = Player.Color; - //movingArmyIcon.GetComponentInChildren().color = Player.Color; - iconComp.Arrow.gameObject.SetActive(true); - //Transform circleTransform = movingArmyIcon.GetComponentInChildren().transform; Camera cam = Camera.main; Vector3 originPoint = RectTransformUtility.WorldToScreenPoint(cam, originZone.transform.position); Vector3 targetPoint = RectTransformUtility.WorldToScreenPoint(cam, targetZone.transform.position); @@ -425,7 +421,13 @@ namespace GWConquest public float GetFormationStrength() { - return UnitEntities.Sum(unit => unit.GetComponent().Class.UnitStrength); + if(entity.IsAttached) + { + return UnitEntities.Sum(unit => unit.Entity.GetComponent().Class.UnitStrength); + } + else { + return 0f; + } } @@ -436,20 +438,26 @@ namespace GWConquest { currentZone.OnFormationChanged(this); } - if(!keepFormationName) + if(entity.IsOwner) { - ConstructName(); - } + if(!keepFormationName) + { + ConstructName(); + } + } OnUnitsChanged?.Invoke(); } public void OnUnitRemoved(Unit unit) { OnUnitsChanged?.Invoke(); - if(!keepFormationName) + if(entity.IsOwner) { - ConstructName(); - } + if(!keepFormationName) + { + ConstructName(); + } + } } public void AddSubFormation(Formation f) diff --git a/Assets/GWConquest/Scripts/Inventory.cs b/Assets/GWConquest/Scripts/Inventory.cs index 7582444..ae264d6 100644 --- a/Assets/GWConquest/Scripts/Inventory.cs +++ b/Assets/GWConquest/Scripts/Inventory.cs @@ -7,7 +7,7 @@ using UnityEngine; namespace GWConquest { [System.Serializable] - public class ItemStack : IIconObject + public class ItemStack : IIconObject, IProtocolToken { public static int MaxStackSize { get => GameManager.Instance.MaxStackSize; @@ -98,27 +98,48 @@ namespace GWConquest (stack1.ItemName == stack2.ItemName)); } + public void Write(UdpPacket packet) + { + packet.WriteBool(IsUnit); + if(IsUnit) + { + packet.WriteULong(Unit.entity.NetworkId.PackedValue); + } + else { + packet.WriteString(ItemName); + } + + packet.WriteBool(Stackable); + packet.WriteInt(Amount); + } + + public void Read(UdpPacket packet) + { + IsUnit = packet.ReadBool(); + if(IsUnit) + { + Unit = BoltNetwork.FindEntity(new NetworkId(packet.ReadULong()))?.GetComponent(); + } + else { + ItemName = packet.ReadString(); + } + Stackable = packet.ReadBool(); + Amount = packet.ReadInt(); + } + } public class InventoryToken : BoltListToken { - public override void WriteEntry(UdpPacket packet, ItemStack entry) + public override void WriteEntry(UdpPacket packet, ItemStack item) { - packet.WriteBool(entry.IsUnit); - packet.WriteString(entry.ItemName); - packet.WriteULong(entry.Unit.entity.NetworkId.PackedValue); - packet.WriteBool(entry.Stackable); - packet.WriteInt(entry.Amount); + item.Write(packet); } public override ItemStack ReadEntry(UdpPacket packet) { - ItemStack item = new ItemStack(); - item.IsUnit = packet.ReadBool(); - item.ItemName = packet.ReadString(); - item.Unit = BoltNetwork.FindEntity(new NetworkId(packet.ReadULong()))?.GetComponent(); - item.Stackable = packet.ReadBool(); - item.Amount = packet.ReadInt(); + var item = new ItemStack(); + item.Read(packet); return item; } } diff --git a/Assets/GWConquest/Scripts/Player.cs b/Assets/GWConquest/Scripts/Player.cs index 392ee7e..f96e72f 100644 --- a/Assets/GWConquest/Scripts/Player.cs +++ b/Assets/GWConquest/Scripts/Player.cs @@ -10,6 +10,11 @@ namespace GWConquest public static Player CurrentPlayer; + public static Player GetPlayerById(int id) + { + return PlayerList.Where(p => p.PlayerId == id).FirstOrDefault(); + } + public Faction Faction { get => GameManager.Instance.Factions[State.FactionIndex]; diff --git a/Assets/GWConquest/Scripts/SpawnAIUnits.cs b/Assets/GWConquest/Scripts/SpawnAIUnits.cs index 46fec24..c902575 100644 --- a/Assets/GWConquest/Scripts/SpawnAIUnits.cs +++ b/Assets/GWConquest/Scripts/SpawnAIUnits.cs @@ -16,7 +16,7 @@ namespace GWConquest { { BoltLog.Info("Spawning AI units"); - Player player = Player.PlayerList[playerId]; + Player player = Player.GetPlayerById(playerId); if(player != null) { for(int i = 0; i < unitCount; i++) diff --git a/Assets/GWConquest/Scripts/UI/BattleArmyPanel.cs b/Assets/GWConquest/Scripts/UI/BattleArmyPanel.cs index a65ad3c..ad80666 100644 --- a/Assets/GWConquest/Scripts/UI/BattleArmyPanel.cs +++ b/Assets/GWConquest/Scripts/UI/BattleArmyPanel.cs @@ -34,12 +34,12 @@ namespace GWConquest { if(IsOwnPlayer) { - return Battle.FormationList.Select((e,i) => e.GetComponent()).Where(f => f.Player == Player.CurrentPlayer ); + return Battle.FormationList.Select((e,i) => e.Entity.GetComponent()).Where(f => f.Player == Player.CurrentPlayer ); } else { - return Battle.FormationList.Select((e,i) => e.GetComponent()).Where(f => f.Player != Player.CurrentPlayer ); + return Battle.FormationList.Select((e,i) => e.Entity.GetComponent()).Where(f => f.Player != Player.CurrentPlayer ); } } diff --git a/Assets/GWConquest/Scripts/UI/DebugUI.cs b/Assets/GWConquest/Scripts/UI/DebugUI.cs index b11fb1f..5d02dd9 100644 --- a/Assets/GWConquest/Scripts/UI/DebugUI.cs +++ b/Assets/GWConquest/Scripts/UI/DebugUI.cs @@ -24,7 +24,7 @@ namespace GWConquest if(playerID < 0 || playerID >= Player.PlayerList.Count) throw new ArgumentException("Player not found"); - Player player = Player.PlayerList[playerID]; + Player player = Player.GetPlayerById(playerID); UnitClass uc = UnitClass.FromName(UnitNameText.text); diff --git a/Assets/GWConquest/Scripts/Unit.cs b/Assets/GWConquest/Scripts/Unit.cs index 7f7c468..43aa356 100644 --- a/Assets/GWConquest/Scripts/Unit.cs +++ b/Assets/GWConquest/Scripts/Unit.cs @@ -33,7 +33,7 @@ namespace GWConquest public Formation Formation { - get => state.Formation.GetComponent(); + get => state.Formation?.GetComponent(); set => state.Formation = value.entity; } diff --git a/Assets/GWConquest/Scripts/Util.cs b/Assets/GWConquest/Scripts/Util.cs index 9f7a5d9..af6ac37 100644 --- a/Assets/GWConquest/Scripts/Util.cs +++ b/Assets/GWConquest/Scripts/Util.cs @@ -42,7 +42,7 @@ namespace GWConquest str += "["; foreach (T o in e) { - str += o.ToString(); + str += o == null ? "null" : o.ToString(); str += "; "; } str += "]";