Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
SubsystemCampfireBlockBehavior.cs
浏览该文件的文档.
1using Engine;
3
4namespace Game {
7
9
11
13
14 public Dictionary<Point3, FireParticleSystem> m_particleSystemsByCell = [];
15
16 public float m_fireSoundVolume;
17
18 public Random m_random = new();
19
20 public int m_updateIndex;
21
22 public List<Point3> m_toReduce = [];
23
24 public Dictionary<Point3, FireParticleSystem>.KeyCollection Campfires => m_particleSystemsByCell.Keys;
25
27
28 public override int[] HandledBlocks => [];
29
30 public virtual void Update(float dt) {
31 if (m_subsystemTime.PeriodicGameTimeEvent(5.0, 0.0)) {
33 foreach (Point3 key in m_particleSystemsByCell.Keys) {
34 PrecipitationShaftInfo precipitationShaftInfo = m_subsystemWeather.GetPrecipitationShaftInfo(key.X, key.Z);
35 if ((precipitationShaftInfo.Intensity > 0f && key.Y >= precipitationShaftInfo.YLimit - 1)
36 || m_updateIndex % 6 == 0) {
37 m_toReduce.Add(key);
38 }
39 }
40 foreach (Point3 item in m_toReduce) {
41 ResizeCampfire(item.X, item.Y, item.Z, -1, true);
42 }
43 m_toReduce.Clear();
44 }
45 if (Time.PeriodicEvent(0.5, 0.0)) {
46 float num = float.MaxValue;
47 foreach (Point3 key2 in m_particleSystemsByCell.Keys) {
48 float x = m_subsystemAmbientSounds.SubsystemAudio.CalculateListenerDistanceSquared(new Vector3(key2.X, key2.Y, key2.Z));
49 num = MathUtils.Min(num, x);
50 }
51 m_fireSoundVolume = m_subsystemAmbientSounds.SubsystemAudio.CalculateVolume(MathF.Sqrt(num), 2f);
52 }
53 m_subsystemAmbientSounds.FireSoundVolume = MathUtils.Max(m_subsystemAmbientSounds.FireSoundVolume, m_fireSoundVolume);
54 }
55
56 public override void OnNeighborBlockChanged(int x, int y, int z, int neighborX, int neighborY, int neighborZ) {
57 int cellValue = SubsystemTerrain.Terrain.GetCellValue(x, y - 1, z);
58 if (BlocksManager.Blocks[Terrain.ExtractContents(cellValue)].IsNonAttachable(cellValue)) {
59 SubsystemTerrain.DestroyCell(
60 0,
61 x,
62 y,
63 z,
64 0,
65 false,
66 false
67 );
68 }
69 }
70
71 public override void OnBlockAdded(int value, int oldValue, int x, int y, int z) {
72 AddCampfireParticleSystem(value, x, y, z);
73 }
74
75 public override void OnBlockRemoved(int value, int newValue, int x, int y, int z) {
77 }
78
79 public override void OnBlockModified(int value, int oldValue, int x, int y, int z) {
81 AddCampfireParticleSystem(value, x, y, z);
82 }
83
84 public override void OnBlockGenerated(int value, int x, int y, int z, bool isLoaded) {
85 AddCampfireParticleSystem(value, x, y, z);
86 }
87
88 public override void OnChunkDiscarding(TerrainChunk chunk) {
89 List<Point3> list = new();
90 foreach (Point3 key in m_particleSystemsByCell.Keys) {
91 if (key.X >= chunk.Origin.X
92 && key.X < chunk.Origin.X + 16
93 && key.Z >= chunk.Origin.Y
94 && key.Z < chunk.Origin.Y + 16) {
95 list.Add(key);
96 }
97 }
98 foreach (Point3 item in list) {
99 ResizeCampfire(item.X, item.Y, item.Z, -15, false);
100 RemoveCampfireParticleSystem(item.X, item.Y, item.Z);
101 }
102 }
103
104 public override void OnHitByProjectile(CellFace cellFace, WorldItem worldItem) {
105 if (!worldItem.ToRemove
106 && AddFuel(cellFace.X, cellFace.Y, cellFace.Z, worldItem.Value, (worldItem as Pickable)?.Count ?? 1)) {
107 worldItem.ToRemove = true;
108 }
109 }
110
111 public override bool OnInteract(TerrainRaycastResult raycastResult, ComponentMiner componentMiner) {
112 if (AddFuel(raycastResult.CellFace.X, raycastResult.CellFace.Y, raycastResult.CellFace.Z, componentMiner.ActiveBlockValue, 1)) {
113 componentMiner.RemoveActiveTool(1);
114 }
115 return true;
116 }
117
118 public override void Load(ValuesDictionary valuesDictionary) {
119 base.Load(valuesDictionary);
120 m_subsystemTime = Project.FindSubsystem<SubsystemTime>(true);
121 m_subsystemParticles = Project.FindSubsystem<SubsystemParticles>(true);
122 m_subsystemWeather = Project.FindSubsystem<SubsystemWeather>(true);
124 }
125
126 public void AddCampfireParticleSystem(int value, int x, int y, int z) {
127 int num = Terrain.ExtractData(value);
128 if (num > 0) {
129 Vector3 v = new(0.5f, 0.15f, 0.5f);
130 float size = MathUtils.Lerp(0.2f, 0.5f, num / 15f);
131 FireParticleSystem fireParticleSystem = new(new Vector3(x, y, z) + v, size, 256f);
132 m_subsystemParticles.AddParticleSystem(fireParticleSystem);
133 m_particleSystemsByCell[new Point3(x, y, z)] = fireParticleSystem;
134 }
135 }
136
137 public void RemoveCampfireParticleSystem(int x, int y, int z) {
138 Point3 key = new(x, y, z);
139 if (m_particleSystemsByCell.TryGetValue(key, out FireParticleSystem value)) {
140 value.IsStopped = true;
141 m_particleSystemsByCell.Remove(key);
142 }
143 }
144
145 public bool AddFuel(int x, int y, int z, int value, int count) {
146 if (Terrain.ExtractData(SubsystemTerrain.Terrain.GetCellValue(x, y, z)) > 0) {
147 int num = Terrain.ExtractContents(value);
148 Block block = BlocksManager.Blocks[num];
149 if (Project.FindSubsystem<SubsystemExplosions>(true).TryExplodeBlock(x, y, z, value)) {
150 return true;
151 }
152 if (block is SnowBlock
153 || block is SnowballBlock
154 || block is IceBlock) {
155 return ResizeCampfire(x, y, z, -1, true);
156 }
157 if (block.GetFuelHeatLevel(value) > 0f) {
158 float num2 = count * MathUtils.Min(block.GetFuelFireDuration(value), 20f) / 5f;
159 int num3 = (int)num2;
160 float num4 = num2 - num3;
161 if (m_random.Float(0f, 1f) < num4) {
162 num3++;
163 }
164 if (num3 > 0) {
165 return ResizeCampfire(x, y, z, num3, true);
166 }
167 return true;
168 }
169 }
170 return false;
171 }
172
173 public bool ResizeCampfire(int x, int y, int z, int steps, bool playSound) {
174 int cellValue = SubsystemTerrain.Terrain.GetCellValue(x, y, z);
175 int num = Terrain.ExtractData(cellValue);
176 if (num > 0) {
177 int num2 = Math.Clamp(num + steps, 0, 15);
178 if (num2 != num) {
179 int value = Terrain.ReplaceData(cellValue, num2);
180 SubsystemTerrain.ChangeCell(x, y, z, value);
181 if (playSound) {
182 if (steps >= 0) {
183 m_subsystemAmbientSounds.SubsystemAudio.PlaySound("Audio/BlockPlaced", 1f, 0f, new Vector3(x, y, z), 3f, false);
184 }
185 else {
186 m_subsystemAmbientSounds.SubsystemAudio.PlayRandomSound("Audio/Sizzles", 1f, 0f, new Vector3(x, y, z), 3f, true);
187 }
188 }
189 return true;
190 }
191 }
192 return false;
193 }
194 }
195}
Engine.Vector3 Vector3
static int Min(int x1, int x2)
static int Max(int x1, int x2)
static float Lerp(float x1, float x2, float f)
static bool PeriodicEvent(double period, double offset)
定义 Time.cs:63
virtual bool IsNonAttachable(int value)
virtual float GetFuelFireDuration(int value)
virtual float GetFuelHeatLevel(int value)
virtual void RemoveActiveTool(int removeCount)
override void OnNeighborBlockChanged(int x, int y, int z, int neighborX, int neighborY, int neighborZ)
Dictionary< Point3, FireParticleSystem > m_particleSystemsByCell
Dictionary< Point3, FireParticleSystem >.KeyCollection Campfires
override bool OnInteract(TerrainRaycastResult raycastResult, ComponentMiner componentMiner)
bool ResizeCampfire(int x, int y, int z, int steps, bool playSound)
void AddCampfireParticleSystem(int value, int x, int y, int z)
override void OnBlockGenerated(int value, int x, int y, int z, bool isLoaded)
override void OnHitByProjectile(CellFace cellFace, WorldItem worldItem)
override void Load(ValuesDictionary valuesDictionary)
override void OnBlockRemoved(int value, int newValue, int x, int y, int z)
bool AddFuel(int x, int y, int z, int value, int count)
override void OnBlockAdded(int value, int oldValue, int x, int y, int z)
override void OnBlockModified(int value, int oldValue, int x, int y, int z)
bool TryExplodeBlock(int x, int y, int z, int value)
static int ExtractContents(int value)
static int ReplaceData(int value, int data)
static int ExtractData(int value)
ValuesDictionary ValuesDictionary