|
@ -7,7 +7,7 @@ using UnityEngine; |
|
|
namespace GWConquest |
|
|
namespace GWConquest |
|
|
{ |
|
|
{ |
|
|
[System.Serializable] |
|
|
[System.Serializable] |
|
|
public class ItemStack : IIconObject, IProtocolToken |
|
|
|
|
|
|
|
|
public struct ItemStack : IIconObject, IProtocolToken |
|
|
{ |
|
|
{ |
|
|
public static int MaxStackSize { |
|
|
public static int MaxStackSize { |
|
|
get => GameManager.Instance.MaxStackSize; |
|
|
get => GameManager.Instance.MaxStackSize; |
|
@ -15,7 +15,12 @@ namespace GWConquest |
|
|
|
|
|
|
|
|
public bool IsUnit; |
|
|
public bool IsUnit; |
|
|
public string ItemName; |
|
|
public string ItemName; |
|
|
public Unit Unit; |
|
|
|
|
|
|
|
|
public NetworkId UnitId; |
|
|
|
|
|
|
|
|
|
|
|
public Unit Unit |
|
|
|
|
|
{ |
|
|
|
|
|
get => BoltNetwork.FindEntity(UnitId)?.GetComponent<Unit>(); |
|
|
|
|
|
} |
|
|
public bool Stackable; |
|
|
public bool Stackable; |
|
|
public int Amount; |
|
|
public int Amount; |
|
|
|
|
|
|
|
@ -74,7 +79,7 @@ namespace GWConquest |
|
|
{ |
|
|
{ |
|
|
return new ItemStack { |
|
|
return new ItemStack { |
|
|
IsUnit = true, |
|
|
IsUnit = true, |
|
|
Unit = unit, |
|
|
|
|
|
|
|
|
UnitId = unit.entity.NetworkId, |
|
|
Stackable = false, |
|
|
Stackable = false, |
|
|
Amount = 1 |
|
|
Amount = 1 |
|
|
}; |
|
|
}; |
|
@ -83,7 +88,7 @@ namespace GWConquest |
|
|
public ItemStack Copy() { |
|
|
public ItemStack Copy() { |
|
|
return new ItemStack() { |
|
|
return new ItemStack() { |
|
|
IsUnit = IsUnit, |
|
|
IsUnit = IsUnit, |
|
|
Unit = Unit, |
|
|
|
|
|
|
|
|
UnitId = UnitId, |
|
|
ItemName = ItemName, |
|
|
ItemName = ItemName, |
|
|
Stackable = Stackable, |
|
|
Stackable = Stackable, |
|
|
Amount = Amount |
|
|
Amount = Amount |
|
@ -103,7 +108,7 @@ namespace GWConquest |
|
|
packet.WriteBool(IsUnit); |
|
|
packet.WriteBool(IsUnit); |
|
|
if(IsUnit) |
|
|
if(IsUnit) |
|
|
{ |
|
|
{ |
|
|
packet.WriteULong(Unit.entity.NetworkId.PackedValue); |
|
|
|
|
|
|
|
|
packet.WriteULong(UnitId.PackedValue); |
|
|
} |
|
|
} |
|
|
else { |
|
|
else { |
|
|
packet.WriteString(ItemName); |
|
|
packet.WriteString(ItemName); |
|
@ -118,7 +123,7 @@ namespace GWConquest |
|
|
IsUnit = packet.ReadBool(); |
|
|
IsUnit = packet.ReadBool(); |
|
|
if(IsUnit) |
|
|
if(IsUnit) |
|
|
{ |
|
|
{ |
|
|
Unit = BoltNetwork.FindEntity(new NetworkId(packet.ReadULong()))?.GetComponent<Unit>(); |
|
|
|
|
|
|
|
|
UnitId = new NetworkId(packet.ReadULong()); |
|
|
} |
|
|
} |
|
|
else { |
|
|
else { |
|
|
ItemName = packet.ReadString(); |
|
|
ItemName = packet.ReadString(); |
|
@ -176,20 +181,24 @@ namespace GWConquest |
|
|
public bool AddItem(string item, int amount) |
|
|
public bool AddItem(string item, int amount) |
|
|
{ |
|
|
{ |
|
|
if(DoesItemFit(item, amount)) { |
|
|
if(DoesItemFit(item, amount)) { |
|
|
foreach(var stack in this) |
|
|
|
|
|
|
|
|
for(int i = 0; i < Count; i++) |
|
|
{ |
|
|
{ |
|
|
|
|
|
var stack = this[i]; |
|
|
if(!stack.IsUnit && stack.ItemName == item) |
|
|
if(!stack.IsUnit && stack.ItemName == item) |
|
|
{ |
|
|
{ |
|
|
int remainingRoom = ItemStack.MaxStackSize - stack.Amount; |
|
|
int remainingRoom = ItemStack.MaxStackSize - stack.Amount; |
|
|
if(remainingRoom >= amount) |
|
|
if(remainingRoom >= amount) |
|
|
{ |
|
|
{ |
|
|
stack.Amount += amount; |
|
|
stack.Amount += amount; |
|
|
|
|
|
this[i] = stack; |
|
|
return true; |
|
|
return true; |
|
|
} |
|
|
} |
|
|
else { |
|
|
else { |
|
|
amount -= remainingRoom; |
|
|
amount -= remainingRoom; |
|
|
stack.Amount = ItemStack.MaxStackSize; |
|
|
stack.Amount = ItemStack.MaxStackSize; |
|
|
|
|
|
this[i] = stack; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -220,7 +229,9 @@ namespace GWConquest |
|
|
|
|
|
|
|
|
if(dict.ContainsKey(stack.ItemName)) |
|
|
if(dict.ContainsKey(stack.ItemName)) |
|
|
{ |
|
|
{ |
|
|
dict[stack.ItemName].Amount += stack.Amount; |
|
|
|
|
|
|
|
|
var s = dict[stack.ItemName]; |
|
|
|
|
|
s.Amount += stack.Amount; |
|
|
|
|
|
dict[stack.ItemName] = s; |
|
|
} |
|
|
} |
|
|
else { |
|
|
else { |
|
|
dict[stack.ItemName] = stack.Copy(); |
|
|
dict[stack.ItemName] = stack.Copy(); |
|
|