Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
FluidBlock.cs
浏览该文件的文档.
1using System.Reflection;
2using Engine;
3
4namespace Game {
5 public abstract class FluidBlock : CubeBlock {
6 public float[] m_heightByLevel = new float[16];
7
9
11
12 public readonly int MaxLevel;
13
14 public FluidBlock(int maxLevel) {
15 MaxLevel = maxLevel;
16 for (int i = 0; i < 16; i++) {
17 float num = 0.875f * MathUtils.Saturate(1f - i / (float)MaxLevel);
18 m_heightByLevel[i] = num;
19 m_boundingBoxesByLevel[i] = [new BoundingBox(new Vector3(0f, 0f, 0f), new Vector3(1f, num, 1f))];
20 }
21 }
22
23 public override void Initialize() {
24 base.Initialize();
25 TypeInfo typeInfo = null;
26 TypeInfo typeInfo2 = GetType().GetTypeInfo();
27 while (typeInfo2 != null) {
28 if (typeInfo2.BaseType == typeof(FluidBlock)) {
29 typeInfo = typeInfo2;
30 break;
31 }
32 typeInfo2 = typeInfo2.BaseType.GetTypeInfo();
33 }
34 if (typeInfo == null) {
35 throw new InvalidOperationException("Fluid type not found.");
36 }
38 for (int i = 0; i < BlocksManager.Blocks.Length; i++) {
39 Block block = BlocksManager.Blocks[i];
40 m_theSameFluidsByIndex[i] = block.GetType().GetTypeInfo() == typeInfo
41 || block.GetType().GetTypeInfo().IsSubclassOf(typeInfo.AsType());
42 }
43 }
44
47
48 public bool IsTheSameFluid(int contents) => m_theSameFluidsByIndex[contents];
49
50 public float GetLevelHeight(int level) => m_heightByLevel[level];
51
53 int value,
54 int x,
55 int y,
56 int z,
57 Color sideColor,
58 Color topColor,
59 TerrainGeometrySubset[] subset) {
60 int data = Terrain.ExtractData(value);
61 if (GetIsTop(data)) {
62 Terrain terrain = generator.Terrain;
63 int cellValueFast = terrain.GetCellValueFast(x - 1, y, z - 1);
64 int cellValueFast2 = terrain.GetCellValueFast(x, y, z - 1);
65 int cellValueFast3 = terrain.GetCellValueFast(x + 1, y, z - 1);
66 int cellValueFast4 = terrain.GetCellValueFast(x - 1, y, z);
67 int cellValueFast5 = terrain.GetCellValueFast(x + 1, y, z);
68 int cellValueFast6 = terrain.GetCellValueFast(x - 1, y, z + 1);
69 int cellValueFast7 = terrain.GetCellValueFast(x, y, z + 1);
70 int cellValueFast8 = terrain.GetCellValueFast(x + 1, y, z + 1);
71 float h = CalculateNeighborHeight(cellValueFast);
72 float num = CalculateNeighborHeight(cellValueFast2);
73 float h2 = CalculateNeighborHeight(cellValueFast3);
74 float num2 = CalculateNeighborHeight(cellValueFast4);
75 float num3 = CalculateNeighborHeight(cellValueFast5);
76 float h3 = CalculateNeighborHeight(cellValueFast6);
77 float num4 = CalculateNeighborHeight(cellValueFast7);
78 float h4 = CalculateNeighborHeight(cellValueFast8);
79 float levelHeight = GetLevelHeight(GetLevel(data));
80 float height = CalculateFluidVertexHeight(h, num, num2, levelHeight);
81 float height2 = CalculateFluidVertexHeight(num, h2, levelHeight, num3);
82 float height3 = CalculateFluidVertexHeight(levelHeight, num3, num4, h4);
83 float height4 = CalculateFluidVertexHeight(num2, levelHeight, h3, num4);
84 float x2 = ZeroSubst(num3, levelHeight) - ZeroSubst(num2, levelHeight);
85 float x3 = ZeroSubst(num4, levelHeight) - ZeroSubst(num, levelHeight);
86 int overrideTopTextureSlot = DefaultTextureSlot - MathF.Sign(x2) - 16 * Math.Sign(x3);
87 generator.GenerateCubeVertices(
88 this,
89 value,
90 x,
91 y,
92 z,
93 height,
94 height2,
95 height3,
96 height4,
97 sideColor,
98 topColor,
99 topColor,
100 topColor,
101 topColor,
102 overrideTopTextureSlot,
103 subset
104 );
105 }
106 else {
107 generator.GenerateCubeVertices(
108 this,
109 value,
110 x,
111 y,
112 z,
113 sideColor,
114 subset
115 );
116 }
117 }
118
119 public static float ZeroSubst(float v, float subst) {
120 if (v != 0f) {
121 return v;
122 }
123 return subst;
124 }
125
126 public static float CalculateFluidVertexHeight(float h1, float h2, float h3, float h4) {
127 float num = MathUtils.Max(h1, h2, h3, h4);
128 if (num < 1f) {
129 if (h1 == 0.01f
130 || h2 == 0.01f
131 || h3 == 0.01f
132 || h4 == 0.01f) {
133 return 0f;
134 }
135 return num;
136 }
137 return 1f;
138 }
139
140 public override BlockPlacementData GetPlacementValue(SubsystemTerrain subsystemTerrain,
141 ComponentMiner componentMiner,
142 int value,
143 TerrainRaycastResult raycastResult) {
144 BlockPlacementData result = default;
146 result.CellFace = raycastResult.CellFace;
147 return result;
148 }
149
150 public override int GetFaceTextureSlot(int face, int value) {
151 if (face >= 4) {
152 return DefaultTextureSlot;
153 }
154 return DefaultTextureSlot + 16;
155 }
156
157 public override bool ShouldGenerateFace(SubsystemTerrain subsystemTerrain,
158 int face,
159 int value,
160 int neighborValue,
161 int x,
162 int y,
163 int z) {
164 int contents = Terrain.ExtractContents(neighborValue);
165 if (IsTheSameFluid(contents)) {
166 return false;
167 }
168 return base.ShouldGenerateFace(
169 subsystemTerrain,
170 face,
171 value,
172 neighborValue,
173 x,
174 y,
175 z
176 );
177 }
178
179 public float CalculateNeighborHeight(int value) {
180 int num = Terrain.ExtractContents(value);
181 if (IsTheSameFluid(num)) {
182 int data = Terrain.ExtractData(value);
183 if (GetIsTop(data)) {
184 return GetLevelHeight(GetLevel(data));
185 }
186 return 1f;
187 }
188 if (num == 0) {
189 return 0.01f;
190 }
191 return 0f;
192 }
193
194 public override bool IsHeatBlocker(int value) => true;
195
196 public static int GetLevel(int data) => data & 0xF;
197
198 public static int SetLevel(int data, int level) => (data & -16) | (level & 0xF);
199
200 public static bool GetIsTop(int data) => (data & 0x10) != 0;
201
202 public static int SetIsTop(int data, bool isTop) {
203 if (!isTop) {
204 return data & -17;
205 }
206 return data | 0x10;
207 }
208
209 public override bool IsCollapseDestructibleBlock(int value) => false;
210 }
211}
Engine.Vector3 Vector3
static float Saturate(float x)
static int Max(int x1, int x2)
virtual void GenerateCubeVertices(Block block, int value, int x, int y, int z, Color color, TerrainGeometrySubset[] subsetsByFace)
int BlockIndex
定义 Block.cs:6
int DefaultTextureSlot
override BoundingBox[] GetCustomCollisionBoxes(SubsystemTerrain terrain, int value)
override bool IsHeatBlocker(int value)
static float ZeroSubst(float v, float subst)
float CalculateNeighborHeight(int value)
static float CalculateFluidVertexHeight(float h1, float h2, float h3, float h4)
static bool GetIsTop(int data)
override void Initialize()
bool[] m_theSameFluidsByIndex
readonly int MaxLevel
FluidBlock(int maxLevel)
override bool ShouldGenerateFace(SubsystemTerrain subsystemTerrain, int face, int value, int neighborValue, int x, int y, int z)
static int GetLevel(int data)
static int SetLevel(int data, int level)
bool IsTheSameFluid(int contents)
void GenerateFluidTerrainVertices(BlockGeometryGenerator generator, int value, int x, int y, int z, Color sideColor, Color topColor, TerrainGeometrySubset[] subset)
override int GetFaceTextureSlot(int face, int value)
override BlockPlacementData GetPlacementValue(SubsystemTerrain subsystemTerrain, ComponentMiner componentMiner, int value, TerrainRaycastResult raycastResult)
方块放置方向
BoundingBox[][] m_boundingBoxesByLevel
override bool IsCollapseDestructibleBlock(int value)
float GetLevelHeight(int level)
static int SetIsTop(int data, bool isTop)
static int ExtractContents(int value)
static int ReplaceData(int value, int data)
virtual int GetCellValueFast(int x, int y, int z)
static int ReplaceContents(int value, int contents)
方块值的最低10位,替换为目标Content
static int ExtractData(int value)