Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
SubsystemItemsScanner.cs
浏览该文件的文档.
1using Engine;
4
5namespace Game {
7 public const float m_automaticScanPeriod = 60f;
8
10
11 public List<ScannedItemData> m_items = [];
12
14
15 public virtual Action<ReadOnlyList<ScannedItemData>> ItemsScanned { get; set; }
16
17 public ReadOnlyList<ScannedItemData> ScanItems() {
18 m_items.Clear();
19 foreach (Subsystem subsystem in Project.Subsystems) {
20 // ReSharper disable SuspiciousTypeConversion.Global
21 if (subsystem is IInventory inventory)
22 // ReSharper restore SuspiciousTypeConversion.Global
23 {
24 ScanInventory(inventory, m_items);
25 }
26 }
27 foreach (Entity entity in Project.Entities) {
28 foreach (Component component in entity.Components) {
29 if (component is IInventory inventory2) {
30 ScanInventory(inventory2, m_items);
31 }
32 }
33 }
34 ScannedItemData item;
35 foreach (Pickable pickable in Project.FindSubsystem<SubsystemPickables>(true).Pickables) {
36 if (pickable.Count > 0
37 && pickable.Value != 0) {
38 List<ScannedItemData> items = m_items;
39 item = new ScannedItemData { Container = pickable, Value = pickable.Value, Count = pickable.Count };
40 items.Add(item);
41 }
42 }
43 foreach (Projectile projectile in Project.FindSubsystem<SubsystemProjectiles>(true).Projectiles) {
44 if (projectile.Value != 0) {
45 List<ScannedItemData> items2 = m_items;
46 item = new ScannedItemData { Container = projectile, Value = projectile.Value, Count = 1 };
47 items2.Add(item);
48 }
49 }
50 foreach (IMovingBlockSet movingBlockSet in Project.FindSubsystem<SubsystemMovingBlocks>(true).MovingBlockSets) {
51 for (int i = 0; i < movingBlockSet.Blocks.Count; i++) {
52 List<ScannedItemData> items3 = m_items;
53 item = new ScannedItemData {
54 Container = movingBlockSet, Value = movingBlockSet.Blocks[i].Value, Count = 1, IndexInContainer = i
55 };
56 items3.Add(item);
57 }
58 }
59 return new ReadOnlyList<ScannedItemData>(m_items);
60 }
61
62 public bool TryModifyItem(ScannedItemData itemData, int newValue) {
63 if (itemData.Container is IInventory) {
64 IInventory obj = (IInventory)itemData.Container;
65 int slotCapacity = obj.GetSlotCapacity(itemData.IndexInContainer, newValue);
66 if (slotCapacity < itemData.Count) {
67 return false;
68 }
69 obj.RemoveSlotItems(itemData.IndexInContainer, itemData.Count);
70 obj.AddSlotItems(itemData.IndexInContainer, newValue, itemData.Count);
71 return true;
72 }
73 if (itemData.Container is WorldItem) {
74 ((WorldItem)itemData.Container).Value = newValue;
75 return true;
76 }
77 if (itemData.Container is IMovingBlockSet) {
79 MovingBlock movingBlock = obj2.Blocks.ElementAt(itemData.IndexInContainer);
80 obj2.SetBlock(movingBlock.Offset, newValue);
81 return true;
82 }
83 return false;
84 }
85
86 public virtual void Update(float dt) {
89 ItemsScanned?.Invoke(ScanItems());
90 }
91 }
92
93 public override void Load(ValuesDictionary valuesDictionary) {
95 }
96
97 public void ScanInventory(IInventory inventory, List<ScannedItemData> items) {
98 for (int i = 0; i < inventory.SlotsCount; i++) {
99 int slotCount = inventory.GetSlotCount(i);
100 if (slotCount > 0) {
101 int slotValue = inventory.GetSlotValue(i);
102 if (slotValue != 0) {
103 items.Add(new ScannedItemData { Container = inventory, IndexInContainer = i, Value = slotValue, Count = slotCount });
104 }
105 }
106 }
107 }
108 }
109}
static double FrameStartTime
定义 Time.cs:42
virtual Action< ReadOnlyList< ScannedItemData > > ItemsScanned
void ScanInventory(IInventory inventory, List< ScannedItemData > items)
bool TryModifyItem(ScannedItemData itemData, int newValue)
override void Load(ValuesDictionary valuesDictionary)
ReadOnlyList< ScannedItemData > ScanItems()
ReadOnlyList< Pickable > Pickables
ReadOnlyList< Projectile > Projectiles
List< Component > Components
ValuesDictionary ValuesDictionary
int RemoveSlotItems(int slotIndex, int count)
实际移除的数量
int GetSlotCount(int slotIndex)
int GetSlotCapacity(int slotIndex, int value)
void AddSlotItems(int slotIndex, int value, int count)
int GetSlotValue(int slotIndex)
List< MovingBlock > Blocks
void SetBlock(Point3 offset, int value)