Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
WireBlock.cs
浏览该文件的文档.
1using Engine;
3
4namespace Game {
6 public static int Index = 133;
7
9
11
12 public static readonly Color WireColor = new(79, 36, 21);
13
14 public override void Initialize() {
15 Model model = ContentManager.Get<Model>("Models/Wire");
16 Matrix boneAbsoluteTransform = BlockMesh.GetBoneAbsoluteTransform(model.FindMesh("Wire").ParentBone);
17 m_standaloneBlockMesh.AppendModelMeshPart(
18 model.FindMesh("Wire").MeshParts[0],
19 boneAbsoluteTransform * Matrix.CreateTranslation(0f, -0.5f, 0f),
20 false,
21 false,
22 false,
23 false,
25 );
26 m_standaloneBlockMesh.TransformTextureCoordinates(Matrix.CreateTranslation(0.9375f, 0f, 0f));
27 for (int i = 0; i < 6; i++) {
29 Vector3 v2 = new Vector3(0.5f, 0.5f, 0.5f) - 0.5f * v;
30 Vector3 v3;
31 Vector3 v4;
32 if (v.X != 0f) {
33 v3 = new Vector3(0f, 1f, 0f);
34 v4 = new Vector3(0f, 0f, 1f);
35 }
36 else if (v.Y != 0f) {
37 v3 = new Vector3(1f, 0f, 0f);
38 v4 = new Vector3(0f, 0f, 1f);
39 }
40 else {
41 v3 = new Vector3(1f, 0f, 0f);
42 v4 = new Vector3(0f, 1f, 0f);
43 }
44 Vector3 v5 = v2 - 0.5f * v3 - 0.5f * v4;
45 Vector3 v6 = v2 + 0.5f * v3 + 0.5f * v4 + 0.05f * v;
46 m_collisionBoxesByFace[i] = new BoundingBox(Vector3.Min(v5, v6), Vector3.Max(v5, v6));
47 }
48 }
49
50 public ElectricElement CreateElectricElement(SubsystemElectricity subsystemElectricity, int value, int x, int y, int z) => null;
51
53 int value,
54 int face,
55 int connectorFace,
56 int x,
57 int y,
58 int z) {
59 if (!WireExistsOnFace(value, face)) {
60 return null;
61 }
62 return ElectricConnectorType.InputOutput;
63 }
64
65 public int GetConnectionMask(int value) {
66 int? color = GetColor(Terrain.ExtractData(value));
67 if (!color.HasValue) {
68 return int.MaxValue;
69 }
70 return 1 << color.Value;
71 }
72
73 public int GetConnectedWireFacesMask(int value, int face) {
74 int num = 0;
75 if (WireExistsOnFace(value, face)) {
76 int num2 = CellFace.OppositeFace(face);
77 bool flag = false;
78 for (int i = 0; i < 6; i++) {
79 if (i == face) {
80 num |= 1 << i;
81 }
82 else if (i != num2
83 && WireExistsOnFace(value, i)) {
84 num |= 1 << i;
85 flag = true;
86 }
87 }
88 if (flag && WireExistsOnFace(value, num2)) {
89 num |= 1 << num2;
90 }
91 }
92 return num;
93 }
94
95 public override BoundingBox[] GetCustomCollisionBoxes(SubsystemTerrain terrain, int value) {
96 BoundingBox[] array = new BoundingBox[6];
97 for (int i = 0; i < 6; i++) {
98 array[i] = WireExistsOnFace(value, i) ? m_collisionBoxesByFace[i] : default;
99 }
100 return array;
101 }
102
103 public override void GenerateTerrainVertices(BlockGeometryGenerator generator, TerrainGeometry geometry, int value, int x, int y, int z) {
104 for (int i = 0; i < 6; i++) {
105 if (WireExistsOnFace(value, i)) {
106 generator.GenerateWireVertices(
107 value,
108 x,
109 y,
110 z,
111 i,
112 0f,
114 geometry.SubsetOpaque
115 );
116 }
117 }
118 }
119
120 public override void DrawBlock(PrimitivesRenderer3D primitivesRenderer,
121 int value,
122 Color color,
123 float size,
124 ref Matrix matrix,
125 DrawBlockEnvironmentData environmentData) {
126 int? paintColor = GetPaintColor(value);
127 Color color2 = paintColor.HasValue ? color * SubsystemPalette.GetColor(environmentData, paintColor) : 1.25f * WireColor * color;
128 BlocksManager.DrawMeshBlock(primitivesRenderer, m_standaloneBlockMesh, color2, 2f * size, ref matrix, environmentData);
129 }
130
131 public override BlockPlacementData GetPlacementValue(SubsystemTerrain subsystemTerrain,
132 ComponentMiner componentMiner,
133 int value,
134 TerrainRaycastResult raycastResult) {
135 Point3 point = CellFace.FaceToPoint3(raycastResult.CellFace.Face);
136 int cellValue = subsystemTerrain.Terrain.GetCellValue(
137 raycastResult.CellFace.X + point.X,
138 raycastResult.CellFace.Y + point.Y,
139 raycastResult.CellFace.Z + point.Z
140 );
141 int num = Terrain.ExtractContents(cellValue);
142 Block block = BlocksManager.Blocks[num];
143 int wireFacesBitmask = GetWireFacesBitmask(cellValue);
144 int num2 = wireFacesBitmask | (1 << raycastResult.CellFace.Face);
145 BlockPlacementData result;
146 if (num2 != wireFacesBitmask
147 || !(block is WireBlock)) {
148 result = default;
149 result.Value = SetWireFacesBitmask(value, num2);
150 result.CellFace = raycastResult.CellFace;
151 return result;
152 }
153 result = default;
154 return result;
155 }
156
157 public override BlockPlacementData GetDigValue(SubsystemTerrain subsystemTerrain,
158 ComponentMiner componentMiner,
159 int value,
160 int toolValue,
161 TerrainRaycastResult raycastResult) {
162 int wireFacesBitmask = GetWireFacesBitmask(value);
163 wireFacesBitmask &= ~(1 << raycastResult.CollisionBoxIndex);
164 BlockPlacementData result = default;
165 result.Value = SetWireFacesBitmask(value, wireFacesBitmask);
166 result.CellFace = raycastResult.CellFace;
167 return result;
168 }
169
170 public override void GetDropValues(SubsystemTerrain subsystemTerrain,
171 int oldValue,
172 int newValue,
173 int toolLevel,
174 List<BlockDropValue> dropValues,
175 out bool showDebris) {
176 int? paintColor = GetPaintColor(oldValue);
177 for (int i = 0; i < 6; i++) {
178 if (WireExistsOnFace(oldValue, i)
179 && !WireExistsOnFace(newValue, i)) {
180 dropValues.Add(new BlockDropValue { Value = Terrain.MakeBlockValue(133, 0, SetColor(0, paintColor)), Count = 1 });
181 }
182 }
183 showDebris = dropValues.Count > 0;
184 }
185
186 public override IEnumerable<int> GetCreativeValues() {
187 yield return Terrain.MakeBlockValue(133);
188 yield return Terrain.MakeBlockValue(133, 0, SetColor(0, 0));
189 yield return Terrain.MakeBlockValue(133, 0, SetColor(0, 8));
190 yield return Terrain.MakeBlockValue(133, 0, SetColor(0, 15));
191 yield return Terrain.MakeBlockValue(133, 0, SetColor(0, 11));
192 yield return Terrain.MakeBlockValue(133, 0, SetColor(0, 12));
193 yield return Terrain.MakeBlockValue(133, 0, SetColor(0, 13));
194 yield return Terrain.MakeBlockValue(133, 0, SetColor(0, 14));
195 }
196
197 public override string GetDisplayName(SubsystemTerrain subsystemTerrain, int value) {
198 int? paintColor = GetPaintColor(value);
199 return SubsystemPalette.GetName(subsystemTerrain, paintColor, base.GetDisplayName(subsystemTerrain, value));
200 }
201
202 public int? GetPaintColor(int value) => GetColor(Terrain.ExtractData(value));
203
204 public int Paint(SubsystemTerrain subsystemTerrain, int value, int? color) {
205 int data = Terrain.ExtractData(value);
206 return Terrain.ReplaceData(value, SetColor(data, color));
207 }
208
209 public static bool WireExistsOnFace(int value, int face) => (GetWireFacesBitmask(value) & (1 << face)) != 0;
210
211 public static int GetWireFacesBitmask(int value) {
212 if (Terrain.ExtractContents(value) == 133) {
213 return Terrain.ExtractData(value) & 0x3F;
214 }
215 return 0;
216 }
217
218 public static int SetWireFacesBitmask(int value, int bitmask) {
219 int num = Terrain.ExtractData(value);
220 num &= -64;
221 num |= bitmask & 0x3F;
222 return Terrain.ReplaceData(Terrain.ReplaceContents(value, 133), num);
223 }
224
225 public static int? GetColor(int data) {
226 if ((data & 0x40) != 0) {
227 return (data >> 7) & 0xF;
228 }
229 return null;
230 }
231
232 public static int SetColor(int data, int? color) {
233 if (color.HasValue) {
234 return (data & -1985) | 0x40 | ((color.Value & 0xF) << 7);
235 }
236 return data & -1985;
237 }
238 }
239}
Engine.Vector3 Vector3
ModelMesh FindMesh(string name, bool throwIfNotFound=true)
ReadOnlyList< ModelMeshPart > MeshParts
virtual void GenerateWireVertices(int value, int x, int y, int z, int mountingFace, float centerBoxSize, Vector2 centerOffset, TerrainGeometrySubset subset)
static Matrix GetBoneAbsoluteTransform(ModelBone modelBone)
static void DrawMeshBlock(PrimitivesRenderer3D primitivesRenderer, BlockMesh blockMesh, float size, ref Matrix matrix, DrawBlockEnvironmentData environmentData)
static object Get(Type type, string name)
TerrainGeometrySubset SubsetOpaque
static int ExtractContents(int value)
static int ReplaceData(int value, int data)
virtual int GetCellValue(int x, int y, int z)
static int ReplaceContents(int value, int contents)
方块值的最低10位,替换为目标Content
static int MakeBlockValue(int contents)
static int ExtractData(int value)
int? GetPaintColor(int value)
override void GetDropValues(SubsystemTerrain subsystemTerrain, int oldValue, int newValue, int toolLevel, List< BlockDropValue > dropValues, out bool showDebris)
ElectricElement CreateElectricElement(SubsystemElectricity subsystemElectricity, int value, int x, int y, int z)
static ? int GetColor(int data)
ElectricConnectorType? GetConnectorType(SubsystemTerrain terrain, int value, int face, int connectorFace, int x, int y, int z)
override BlockPlacementData GetDigValue(SubsystemTerrain subsystemTerrain, ComponentMiner componentMiner, int value, int toolValue, TerrainRaycastResult raycastResult)
static bool WireExistsOnFace(int value, int face)
override void DrawBlock(PrimitivesRenderer3D primitivesRenderer, int value, Color color, float size, ref Matrix matrix, DrawBlockEnvironmentData environmentData)
override IEnumerable< int > GetCreativeValues()
static int SetWireFacesBitmask(int value, int bitmask)
override string GetDisplayName(SubsystemTerrain subsystemTerrain, int value)
static readonly Color WireColor
int GetConnectedWireFacesMask(int value, int face)
static int SetColor(int data, int? color)
override BoundingBox[] GetCustomCollisionBoxes(SubsystemTerrain terrain, int value)
static int Index
override BlockPlacementData GetPlacementValue(SubsystemTerrain subsystemTerrain, ComponentMiner componentMiner, int value, TerrainRaycastResult raycastResult)
方块放置方向
static int GetWireFacesBitmask(int value)
BoundingBox[] m_collisionBoxesByFace
int Paint(SubsystemTerrain subsystemTerrain, int value, int? color)
int GetConnectionMask(int value)
override void Initialize()
override void GenerateTerrainVertices(BlockGeometryGenerator generator, TerrainGeometry geometry, int value, int x, int y, int z)
BlockMesh m_standaloneBlockMesh
static Color White
static Matrix CreateTranslation(float x, float y, float z)
static readonly Vector2 Zero
static Vector3 Max(Vector3 v, float f)
static Vector3 Min(Vector3 v, float f)
static int OppositeFace(int face)
static Vector3 FaceToVector3(int face)
static Point3 FaceToPoint3(int face)