Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
SubsystemPickables.cs
浏览该文件的文档.
1using Engine;
6
7namespace Game {
10
12
14
16
18
20
22
24
26
28
30
31 [Obsolete("该字段已弃用,掉落物被玩家的拾取逻辑被转移到ComponentPickableGathererPlayer中")]
32 public List<ComponentPlayer> m_tmpPlayers = [];
33
34 public List<Pickable> m_pickables = [];
35
36 public List<Pickable> m_pickablesToRemove = [];
37
39
40 public Random m_random = new();
41
43
44 public static int[] m_drawOrders = [10];
45
46 public ReadOnlyList<Pickable> Pickables {
47 get {
48 lock (m_lock) {
49 return new ReadOnlyList<Pickable>(m_pickables);
50 }
51 }
52 }
53
54 public int[] DrawOrders => m_drawOrders;
55
56 public virtual Action<Pickable> PickableAdded { get; set; }
57 public virtual Action<Pickable> PickableRemoved { get; set; }
59
60 readonly Lock m_lock = new();
61
62 public virtual Pickable AddPickable(Pickable pickable) {
63 if (pickable == null) {
64 return null;
65 }
66 //如果掉落物创建时间没有初始化,就初始化一下
67 if (pickable.CreationTime == 0) {
68 pickable.CreationTime = m_subsystemGameInfo.TotalElapsedGameTime;
69 }
71 "OnPickableAdded",
72 loader => {
73 loader.OnPickableAdded(this, ref pickable, null);
74 return false;
75 }
76 );
77 lock (m_lock) {
78 m_pickables.Add(pickable);
79 }
80 PickableAdded?.Invoke(pickable);
81 return pickable;
82 }
83
84 public virtual Pickable AddPickable(int value, int count, Vector3 position, Vector3? velocity, Matrix? stuckMatrix) =>
85 AddPickable(value, count, position, velocity, stuckMatrix, null);
86
87 public virtual Pickable AddPickable(int value, int count, Vector3 position, Vector3? velocity, Matrix? stuckMatrix, Entity owner) =>
88 AddPickable<Pickable>(value, count, position, velocity, stuckMatrix, owner);
89
90 public virtual Pickable CreatePickable(int value, int count, Vector3 position, Vector3? velocity, Matrix? stuckMatrix, Entity owner) =>
91 CreatePickable<Pickable>(value, count, position, velocity, stuckMatrix, owner);
92
93 public virtual T CreatePickable<T>(int value, int count, Vector3 position, Vector3? velocity, Matrix? stuckMatrix, Entity owner)
94 where T : Pickable, new() {
95 try {
96 T pickable = new();
97 pickable.InitializeData(
98 () => m_subsystemTerrain.Terrain,
101 () => m_subsystemSky.CalculateFog,
103 );
104 pickable.Initialize(value, count, position, velocity, stuckMatrix, owner);
105 pickable.CreationTime = m_subsystemGameInfo.TotalElapsedGameTime;
106 return pickable;
107 }
108 catch (Exception e) {
109 Log.Error($"Pickable create error: {e}");
110 return null;
111 }
112 }
113
114 public virtual T AddPickable<T>(int value, int count, Vector3 position, Vector3? velocity, Matrix? stuckMatrix, Entity owner)
115 where T : Pickable, new() {
116 try {
117 T pickable = CreatePickable<T>(value, count, position, velocity, stuckMatrix, owner);
118 Pickable pickable2 = AddPickable(pickable);
119 return pickable2 as T;
120 }
121 catch (Exception e) {
122 Log.Error($"Pickable add error: {e}");
123 return null;
124 }
125 }
126
127 public virtual void Draw(Camera camera, int drawOrder) {
128 double totalElapsedGameTime = m_subsystemGameInfo.TotalElapsedGameTime;
129 m_drawBlockEnvironmentData.SubsystemTerrain = m_subsystemTerrain;
130 Matrix matrix = Matrix.CreateRotationY((float)MathUtils.Remainder(totalElapsedGameTime, 6.2831854820251465));
131 lock (m_lock) {
132 foreach (Pickable pickable in m_pickables) {
133 try {
134 pickable.Project = Project;
135 pickable.Draw(camera, drawOrder, totalElapsedGameTime, matrix);
136 }
137 catch (Exception e) {
138 if (pickable.LogDrawError) {
139 Log.Error($"Pickable draw error: {e}");
140 pickable.LogDrawError = false;
141 }
142 }
143 }
144 }
146 }
147
148 public virtual void Update(float dt) {
149 lock (m_lock) {
150 foreach (Pickable pickable in m_pickables) {
151 if (pickable.ToRemove) {
152 m_pickablesToRemove.Add(pickable);
153 }
154 else {
155 try {
156 pickable.Project = Project;
157 pickable.Update(dt);
158 }
159 catch (Exception e) {
160 Log.Error($"Pickable update error: {e}");
161 pickable.ToRemove = true;
162 }
163 }
164 }
165 foreach (Pickable item in m_pickablesToRemove) {
166 m_pickables.Remove(item);
167 PickableRemoved?.Invoke(item);
168 }
169 m_pickablesToRemove.Clear();
170 }
171 }
172
173 public override void Load(ValuesDictionary valuesDictionary) {
174 m_subsystemAudio = Project.FindSubsystem<SubsystemAudio>(true);
175 m_subsystemPlayers = Project.FindSubsystem<SubsystemPlayers>(true);
176 m_subsystemTerrain = Project.FindSubsystem<SubsystemTerrain>(true);
177 m_subsystemSky = Project.FindSubsystem<SubsystemSky>(true);
178 m_subsystemTime = Project.FindSubsystem<SubsystemTime>(true);
179 m_subsystemGameInfo = Project.FindSubsystem<SubsystemGameInfo>(true);
180 m_subsystemParticles = Project.FindSubsystem<SubsystemParticles>(true);
185 foreach (ValuesDictionary item in valuesDictionary.GetValue<ValuesDictionary>("Pickables").Values.Where(v => v is ValuesDictionary)) {
186 try {
187 string className = item.GetValue("Class", typeof(Pickable).FullName);
188 Type type = TypeCache.FindType(className, false, true);
189#pragma warning disable IL2072
190 if (Activator.CreateInstance(type) is Pickable pickable) {
191#pragma warning restore IL2072
192 pickable.Project = Project;
193 pickable.InitializeData(
194 () => m_subsystemTerrain.Terrain,
197 () => m_subsystemSky.CalculateFog,
199 );
200 pickable.Load(item);
202 "OnPickableAdded",
203 loader => {
204 loader.OnPickableAdded(this, ref pickable, item);
205 return false;
206 }
207 );
208 lock (m_pickables) {
209 m_pickables.Add(pickable);
210 }
211 }
212 }
213 catch (Exception ex) {
214 Log.Error("Pickable Loaded Error");
215 Log.Error(ex);
216 }
217 }
218 }
219
220 public override void Save(ValuesDictionary valuesDictionary) {
221 ValuesDictionary valuesDictionary2 = new();
222 valuesDictionary.SetValue("Pickables", valuesDictionary2);
223 int num = 0;
224 lock (m_lock) {
225 foreach (Pickable pickable in m_pickables) {
226 ValuesDictionary valuesDictionary3 = new();
227 pickable.Save(valuesDictionary3);
229 "SavePickable",
230 loader => {
231 loader.SavePickable(this, pickable, ref valuesDictionary3);
232 return false;
233 }
234 );
235 valuesDictionary2.SetValue(num.ToString(), valuesDictionary3);
236 num++;
237 }
238 }
239 }
240
241 public float DefaultCalcVisibilityRange() => Math.Min(m_subsystemSky.VisibilityRange, 30);
242 }
243}
static void Error(object message)
定义 Log.cs:80
static float Remainder(float x, float y)
static Type FindType(string typeName, bool skipSystemAssemblies, bool throwIfNotFound)
Matrix ViewProjectionMatrix
virtual void Update(float dt)
virtual void Draw(Camera camera, int drawOrder, double totalElapsedGameTime, Matrix rotationMatrix)
virtual void Save(ValuesDictionary valuesDictionary)
virtual Pickable AddPickable(int value, int count, Vector3 position, Vector3? velocity, Matrix? stuckMatrix)
PrimitivesRenderer3D m_primitivesRenderer
virtual Pickable AddPickable(Pickable pickable)
virtual Action< Pickable > PickableRemoved
ReadOnlyList< Pickable > Pickables
List< ComponentPlayer > m_tmpPlayers
virtual Action< Pickable > PickableAdded
SubsystemParticles m_subsystemParticles
virtual void Draw(Camera camera, int drawOrder)
SubsystemBlockBehaviors m_subsystemBlockBehaviors
SubsystemFireBlockBehavior m_subsystemFireBlockBehavior
SubsystemFluidBlockBehavior m_subsystemFluidBlockBehavior
virtual Pickable AddPickable(int value, int count, Vector3 position, Vector3? velocity, Matrix? stuckMatrix, Entity owner)
virtual Pickable CreatePickable(int value, int count, Vector3 position, Vector3? velocity, Matrix? stuckMatrix, Entity owner)
virtual T CreatePickable< T >(int value, int count, Vector3 position, Vector3? velocity, Matrix? stuckMatrix, Entity owner)
virtual T AddPickable< T >(int value, int count, Vector3 position, Vector3? velocity, Matrix? stuckMatrix, Entity owner)
override void Save(ValuesDictionary valuesDictionary)
SubsystemExplosions m_subsystemExplosions
override void Load(ValuesDictionary valuesDictionary)
DrawBlockEnvironmentData m_drawBlockEnvironmentData
ValuesDictionary ValuesDictionary
static void HookAction(string HookName, Func< ModLoader, bool > action)
执行Hook
static Matrix CreateRotationY(float radians)