Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
ComponentInventoryBase.cs
浏览该文件的文档.
1using System.Globalization;
2using Engine;
5
6namespace Game {
7 public abstract class ComponentInventoryBase : Component, IInventory {
8 public class Slot {
9 public int Value;
10
11 public int Count;
12 }
13
14 public List<Slot> m_slots = [];
15
16 public Random m_random = new();
17
19
20 public virtual int SlotsCount => m_slots.Count;
21
22 public virtual int VisibleSlotsCount {
23 get => SlotsCount;
24 set { }
25 }
26
27 public virtual int ActiveSlotIndex {
28 get => -1;
29 set { }
30 }
31
32 public static int FindAcquireSlotForItem(IInventory inventory, int value) {
33 for (int i = 0; i < inventory.SlotsCount; i++) {
34 if (inventory.GetSlotCount(i) > 0
35 && inventory.GetSlotValue(i) == value
36 && inventory.GetSlotCount(i) < inventory.GetSlotCapacity(i, value)) {
37 return i;
38 }
39 }
40 for (int j = 0; j < inventory.SlotsCount; j++) {
41 if (inventory.GetSlotCount(j) == 0
42 && inventory.GetSlotCapacity(j, value) > 0) {
43 return j;
44 }
45 }
46 return -1;
47 }
48
49 public static int AcquireItems(IInventory inventory, int value, int count) {
50 while (count > 0) {
51 int num = FindAcquireSlotForItem(inventory, value);
52 if (num < 0) {
53 break;
54 }
55 inventory.AddSlotItems(num, value, 1);
56 count--;
57 }
58 return count;
59 }
60
62 ComponentPlayer componentPlayer = Entity.FindComponent<ComponentPlayer>();
63 if (componentPlayer == null) {
64 ComponentBlockEntity componentBlockEntity = Entity.FindComponent<ComponentBlockEntity>();
65 if (componentBlockEntity != null) {
66 Vector3 position = new(componentBlockEntity.Coordinates);
67 componentPlayer = Project.FindSubsystem<SubsystemPlayers>(true).FindNearestPlayer(position);
68 }
69 }
70 return componentPlayer;
71 }
72
73 public static void DropSlotItems(IInventory inventory, int slotIndex, Vector3 position, Vector3 velocity) {
74 int slotCount = inventory.GetSlotCount(slotIndex);
75 if (slotCount > 0) {
76 int slotValue = inventory.GetSlotValue(slotIndex);
77 int num = inventory.RemoveSlotItems(slotIndex, slotCount);
78 if (num > 0) {
79 Entity entity = null;
80 if (inventory is Component component) {
81 entity = component.Entity;
82 }
83 inventory.Project.FindSubsystem<SubsystemPickables>(true).AddPickable(slotValue, num, position, velocity, null, entity);
84 }
85 }
86 }
87
88 public override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap) {
89 int value = valuesDictionary.GetValue<int>("SlotsCount");
90 for (int i = 0; i < value; i++) {
91 m_slots.Add(new Slot());
92 }
93 ValuesDictionary value2 = valuesDictionary.GetValue<ValuesDictionary>("Slots");
94 for (int j = 0; j < m_slots.Count; j++) {
95 ValuesDictionary value3 = value2.GetValue<ValuesDictionary>($"Slot{j.ToString(CultureInfo.InvariantCulture)}", null);
96 if (value3 != null) {
97 Slot slot = m_slots[j];
98 slot.Value = value3.GetValue<int>("Contents");
99 slot.Count = value3.GetValue<int>("Count");
100 }
101 }
102 }
103
104 public override void Save(ValuesDictionary valuesDictionary, EntityToIdMap entityToIdMap) {
105 ValuesDictionary valuesDictionary2 = new();
106 valuesDictionary.SetValue("Slots", valuesDictionary2);
107 for (int i = 0; i < m_slots.Count; i++) {
108 Slot slot = m_slots[i];
109 if (slot.Count > 0) {
110 ValuesDictionary valuesDictionary3 = new();
111 valuesDictionary2.SetValue($"Slot{i.ToString(CultureInfo.InvariantCulture)}", valuesDictionary3);
112 valuesDictionary3.SetValue("Contents", slot.Value);
113 valuesDictionary3.SetValue("Count", slot.Count);
114 }
115 }
116 }
117
118 public virtual int GetSlotValue(int slotIndex) {
119 if (slotIndex >= 0
120 && slotIndex < m_slots.Count) {
121 if (m_slots[slotIndex].Count <= 0) {
122 return 0;
123 }
124 return m_slots[slotIndex].Value;
125 }
126 return 0;
127 }
128
129 public virtual int GetSlotCount(int slotIndex) {
130 if (slotIndex >= 0
131 && slotIndex < m_slots.Count) {
132 return m_slots[slotIndex].Count;
133 }
134 return 0;
135 }
136
137 public virtual int GetSlotCapacity(int slotIndex, int value) {
138 if (slotIndex >= 0
139 && slotIndex < m_slots.Count) {
141 }
142 return 0;
143 }
144
145 public virtual int GetSlotProcessCapacity(int slotIndex, int value) {
146 int slotCount = GetSlotCount(slotIndex);
147 int slotValue = GetSlotValue(slotIndex);
148 if (slotCount > 0
149 && slotValue != 0) {
150 SubsystemBlockBehavior[] blockBehaviors = Project.FindSubsystem<SubsystemBlockBehaviors>(true)
151 .GetBlockBehaviors(Terrain.ExtractContents(slotValue));
152 for (int i = 0; i < blockBehaviors.Length; i++) {
153 int processInventoryItemCapacity = blockBehaviors[i].GetProcessInventoryItemCapacity(this, slotIndex, value);
154 if (processInventoryItemCapacity > 0) {
155 return processInventoryItemCapacity;
156 }
157 }
158 }
159 return 0;
160 }
161
162 public virtual void AddSlotItems(int slotIndex, int value, int count) {
163 if (count > 0
164 && slotIndex >= 0
165 && slotIndex < m_slots.Count) {
166 Slot slot = m_slots[slotIndex];
167 int slotValue = GetSlotValue(slotIndex);
168 int slotCount = GetSlotCount(slotIndex);
169 int slotCapacity = GetSlotCapacity(slotIndex, value);
170 if (slotCount != 0
171 && slotValue != value) {
172 throw new InvalidOperationException(
173 $"Cannot add slot items because items are different. Slot {slotIndex} Contains BlockValue {slotValue} with count {slotCount}. The value to add is {value} with count {count}. Slot capacity is {slotCapacity}"
174 );
175 }
176 if (GetSlotCount(slotIndex) + count > slotCapacity) {
177 throw new InvalidOperationException(
178 $"Cannot add slot items because it exceeded capacity. Slot {slotIndex} Contains BlockValue {slotValue} with count {slotCount}. The value to add is {value} with count {count}. Slot capacity is {slotCapacity}"
179 );
180 }
181 slot.Value = value;
182 slot.Count += count;
183 }
184 }
185
186 public virtual void ProcessSlotItems(int slotIndex, int value, int count, int processCount, out int processedValue, out int processedCount) {
187 int slotCount = GetSlotCount(slotIndex);
188 int slotValue = GetSlotValue(slotIndex);
189 if (slotCount > 0
190 && slotValue != 0) {
191 SubsystemBlockBehavior[] blockBehaviors = Project.FindSubsystem<SubsystemBlockBehaviors>(true)
192 .GetBlockBehaviors(Terrain.ExtractContents(slotValue));
193 foreach (SubsystemBlockBehavior subsystemBlockBehavior in blockBehaviors) {
194 int processInventoryItemCapacity = subsystemBlockBehavior.GetProcessInventoryItemCapacity(this, slotIndex, value);
195 if (processInventoryItemCapacity > 0) {
196 subsystemBlockBehavior.ProcessInventoryItem(
197 this,
198 slotIndex,
199 value,
200 count,
201 MathUtils.Min(processInventoryItemCapacity, processCount),
202 out processedValue,
203 out processedCount
204 );
205 return;
206 }
207 }
208 }
209 processedValue = value;
210 processedCount = count;
211 }
212
213 public virtual int RemoveSlotItems(int slotIndex, int count) {
214 if (slotIndex >= 0
215 && slotIndex < m_slots.Count) {
216 Slot slot = m_slots[slotIndex];
217 count = MathUtils.Min(count, GetSlotCount(slotIndex));
218 slot.Count -= count;
219 return count;
220 }
221 return 0;
222 }
223
224 public virtual void DropAllItems(Vector3 position) {
225 for (int i = 0; i < SlotsCount; i++) {
227 this,
228 i,
229 position,
230 m_random.Float(5f, 10f) * Vector3.Normalize(new Vector3(m_random.Float(-1f, 1f), m_random.Float(1f, 2f), m_random.Float(-1f, 1f)))
231 );
232 }
233 }
234 }
235}
static int Min(int x1, int x2)
virtual int GetMaxStacking(int value)
virtual int GetSlotCount(int slotIndex)
virtual int GetSlotCapacity(int slotIndex, int value)
virtual void DropAllItems(Vector3 position)
virtual void AddSlotItems(int slotIndex, int value, int count)
static void DropSlotItems(IInventory inventory, int slotIndex, Vector3 position, Vector3 velocity)
virtual int GetSlotValue(int slotIndex)
override void Save(ValuesDictionary valuesDictionary, EntityToIdMap entityToIdMap)
static int FindAcquireSlotForItem(IInventory inventory, int value)
virtual int GetSlotProcessCapacity(int slotIndex, int value)
static int AcquireItems(IInventory inventory, int value, int count)
virtual int RemoveSlotItems(int slotIndex, int count)
实际移除的数量
override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap)
virtual void ProcessSlotItems(int slotIndex, int value, int count, int processCount, out int processedValue, out int processedCount)
virtual void ProcessInventoryItem(IInventory inventory, int slotIndex, int value, int count, int processCount, out int processedValue, out int processedCount)
virtual int GetProcessInventoryItemCapacity(IInventory inventory, int slotIndex, int value)
static int ExtractContents(int value)
ValuesDictionary ValuesDictionary
Entity(Project project, ValuesDictionary valuesDictionary)
virtual Subsystem FindSubsystem(Type type, string name, bool throwOnError)
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)
static Vector3 Normalize(Vector3 v)