Campofinale/Campofinale/Game/Factory/Components/FComponentCache.cs

101 lines
2.7 KiB
C#
Raw Normal View History

2025-08-06 23:45:49 +00:00
using Campofinale.Resource;
using static Campofinale.Game.Factory.FactoryNode;
2025-08-08 00:04:38 +00:00
using static Campofinale.Resource.ResourceManager;
2025-08-06 23:45:49 +00:00
namespace Campofinale.Game.Factory.Components
{
public class FComponentCache : FComponent
{
2025-08-08 00:04:38 +00:00
public List<ItemCount> items = new();
public FComponentCache(uint id,FCComponentPos pos) : base(id, FCComponentType.Cache,pos)
2025-08-06 23:45:49 +00:00
{
}
2025-08-08 00:04:38 +00:00
public int GetItemCount(string id)
{
int count = 0;
ItemCount item = items.Find(i=>i.id == id);
if (item != null)
{
count += item.count;
}
return count;
}
2025-08-06 23:45:49 +00:00
public override void SetComponentInfo(ScdFacCom proto)
{
2025-08-08 00:04:38 +00:00
if(items.Count < 1)
{
//Add 1 empty item as default
items.Add(new ItemCount());
}
2025-08-06 23:45:49 +00:00
proto.Cache = new()
{
2025-08-08 00:04:38 +00:00
Items =
{
},
Size=items.Count,
2025-08-06 23:45:49 +00:00
};
2025-08-08 00:04:38 +00:00
items.ForEach(item =>
{
proto.Cache.Items.Add(item.ToFactoryItemProto());
});
}
public void ConsumeItems(List<ItemCount> toConsume)
{
foreach (var consume in toConsume)
{
ItemCount item = items.Find(i => i.id == consume.id);
if (item != null)
{
item.count-=consume.count;
if(item.count < 1)
{
item.count = 0;
item.id = "";
}
}
}
}
public bool IsFull()
{
int maxItems = items.Count * 100;
int count = 0;
foreach (var item in items)
{
count += item.count;
};
return count>=maxItems;
}
public void AddItem(string id, int count)
{
int remaining = count;
foreach (var item in items)
{
int space = 100-item.count;
if (item.id==id)
{
if (space >= remaining)
{
item.count +=remaining;
remaining = 0;
}
else
{
item.count += space;
remaining -= space;
}
}
else if(item.id.Length < 1)
{
item.id = id;
item.count = remaining;
}
}
2025-08-06 23:45:49 +00:00
}
}
}