Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
LedBlock.cs
浏览该文件的文档.
1using System.Globalization;
2using Engine;
4
5namespace Game {
7 public static int Index = 152;
8 public static readonly Color[] LedColors = [
9 new(255, 255, 255),
10 new(0, 255, 255),
11 new(255, 0, 0),
12 new(0, 0, 255),
13 new(255, 240, 0),
14 new(0, 255, 0),
15 new(255, 120, 0),
16 new(255, 0, 255)
17 ];
18
22 public const string fName = "LedBlock";
23
24 public override void Initialize() {
25 Model model = ContentManager.Get<Model>("Models/Leds");
26 ModelMesh modelMesh = model.FindMesh("Led");
27 ModelMesh modelMesh2 = model.FindMesh("LedBulb");
28 Matrix boneAbsoluteTransform = BlockMesh.GetBoneAbsoluteTransform(modelMesh.ParentBone);
29 Matrix boneAbsoluteTransform2 = BlockMesh.GetBoneAbsoluteTransform(modelMesh2.ParentBone);
30 for (int i = 0; i < 8; i++) {
31 Color color = LedColors[i];
32 color *= 0.5f;
33 color.A = byte.MaxValue;
34 Matrix m = Matrix.CreateRotationY(-(float)Math.PI / 2f) * Matrix.CreateRotationZ((float)Math.PI / 2f);
37 .AppendModelMeshPart(
38 modelMesh.MeshParts[0],
39 boneAbsoluteTransform * m,
40 false,
41 false,
42 false,
43 false,
45 );
47 .AppendModelMeshPart(
48 modelMesh2.MeshParts[0],
49 boneAbsoluteTransform2 * m,
50 false,
51 false,
52 false,
53 false,
54 color
55 );
56 for (int j = 0; j < 6; j++) {
57 int num = SetMountingFace(SetColor(0, i), j);
58 Matrix m2 = j >= 4
59 ? j != 4
60 ? Matrix.CreateRotationX((float)Math.PI) * Matrix.CreateTranslation(0.5f, 1f, 0.5f)
61 : Matrix.CreateTranslation(0.5f, 0f, 0.5f)
62 : Matrix.CreateRotationX((float)Math.PI / 2f)
63 * Matrix.CreateTranslation(0f, 0f, -0.5f)
64 * Matrix.CreateRotationY(j * (float)Math.PI / 2f)
65 * Matrix.CreateTranslation(0.5f, 0.5f, 0.5f);
66 m_blockMeshesByData[num] = new BlockMesh();
68 .AppendModelMeshPart(
69 modelMesh.MeshParts[0],
70 boneAbsoluteTransform * m2,
71 false,
72 false,
73 false,
74 false,
76 );
78 .AppendModelMeshPart(
79 modelMesh2.MeshParts[0],
80 boneAbsoluteTransform2 * m2,
81 false,
82 false,
83 false,
84 false,
85 color
86 );
87 m_collisionBoxesByData[num] = [m_blockMeshesByData[num].CalculateBoundingBox()];
88 }
89 }
90 }
91
92 public override IEnumerable<CraftingRecipe> GetProceduralCraftingRecipes() {
93 int color = 0;
94 while (color < 8) {
95 CraftingRecipe craftingRecipe = new() {
96 ResultCount = 4,
97 ResultValue = Terrain.MakeBlockValue(152, 0, SetColor(0, color)),
98 RemainsCount = 1,
99 RemainsValue = Terrain.MakeBlockValue(90),
100 RequiredHeatLevel = 0f,
101 Description = LanguageControl.Get(fName, "CRDescription"),
102 Ingredients = {
103 [1] = "glass",
104 [4] = $"paintbucket:{color.ToString(CultureInfo.InvariantCulture)}",
105 [6] = "copperingot",
106 [7] = "copperingot",
107 [8] = "copperingot"
108 }
109 };
110 yield return craftingRecipe;
111 int num = color + 1;
112 color = num;
113 }
114 }
115
116 public override int GetFace(int value) => GetMountingFace(Terrain.ExtractData(value));
117
118 public override string GetDisplayName(SubsystemTerrain subsystemTerrain, int value) {
119 int data = Terrain.ExtractData(value);
120 int color = GetColor(data);
121 return string.Format(
122 LanguageControl.Get(fName, "Format"),
123 LanguageControl.Get(fName, color),
124 LanguageControl.GetBlock($"{fName}:{data.ToString()}", "DisplayName")
125 );
126 }
127
128 public override IEnumerable<int> GetCreativeValues() {
129 int i = 0;
130 while (i < 8) {
131 yield return Terrain.MakeBlockValue(152, 0, SetColor(0, i));
132 int num = i + 1;
133 i = num;
134 }
135 }
136
137 public override BlockPlacementData GetPlacementValue(SubsystemTerrain subsystemTerrain,
138 ComponentMiner componentMiner,
139 int value,
140 TerrainRaycastResult raycastResult) {
141 int data = SetMountingFace(Terrain.ExtractData(value), raycastResult.CellFace.Face);
142 int value2 = Terrain.ReplaceData(value, data);
143 BlockPlacementData result = default;
144 result.Value = value2;
145 result.CellFace = raycastResult.CellFace;
146 return result;
147 }
148
149 public override void GetDropValues(SubsystemTerrain subsystemTerrain,
150 int oldValue,
151 int newValue,
152 int toolLevel,
153 List<BlockDropValue> dropValues,
154 out bool showDebris) {
155 int color = GetColor(Terrain.ExtractData(oldValue));
156 dropValues.Add(new BlockDropValue { Value = Terrain.MakeBlockValue(152, 0, SetColor(0, color)), Count = 1 });
157 showDebris = true;
158 }
159
160 public override BoundingBox[] GetCustomCollisionBoxes(SubsystemTerrain terrain, int value) {
161 int num = Terrain.ExtractData(value);
162 if (num >= m_collisionBoxesByData.Length) {
163 return null;
164 }
165 return m_collisionBoxesByData[num];
166 }
167
168 public override void GenerateTerrainVertices(BlockGeometryGenerator generator, TerrainGeometry geometry, int value, int x, int y, int z) {
169 int num = Terrain.ExtractData(value);
170 if (num < m_blockMeshesByData.Length) {
171 generator.GenerateMeshVertices(
172 this,
173 x,
174 y,
175 z,
177 Color.White,
178 null,
179 geometry.SubsetOpaque
180 );
181 generator.GenerateWireVertices(
182 value,
183 x,
184 y,
185 z,
186 GetFace(value),
187 0.25f,
189 geometry.SubsetOpaque
190 );
191 }
192 }
193
194 public override void DrawBlock(PrimitivesRenderer3D primitivesRenderer,
195 int value,
196 Color color,
197 float size,
198 ref Matrix matrix,
199 DrawBlockEnvironmentData environmentData) {
200 int color2 = GetColor(Terrain.ExtractData(value));
201 BlocksManager.DrawMeshBlock(primitivesRenderer, m_standaloneBlockMeshesByColor[color2], color, 2f * size, ref matrix, environmentData);
202 }
203
204 public override ElectricElement CreateElectricElement(SubsystemElectricity subsystemElectricity, int value, int x, int y, int z) =>
205 new LedElectricElement(subsystemElectricity, new CellFace(x, y, z, GetFace(value)));
206
208 int value,
209 int face,
210 int connectorFace,
211 int x,
212 int y,
213 int z) {
214 int face2 = GetFace(value);
215 if (face == face2
216 && SubsystemElectricity.GetConnectorDirection(face2, 0, connectorFace).HasValue) {
217 return ElectricConnectorType.Input;
218 }
219 return null;
220 }
221
222 public static int GetMountingFace(int data) => data & 7;
223
224 public static int SetMountingFace(int data, int face) => (data & -8) | (face & 7);
225
226 public static int GetColor(int data) => (data >> 3) & 7;
227
228 public static int SetColor(int data, int color) => (data & -57) | ((color & 7) << 3);
229 }
230}
ModelMesh FindMesh(string name, bool throwIfNotFound=true)
ReadOnlyList< ModelMeshPart > MeshParts
virtual void GenerateMeshVertices(Block block, int x, int y, int z, BlockMesh blockMesh, Color color, Matrix? matrix, TerrainGeometrySubset subset)
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)
static string GetBlock(string blockName, string prop)
static string Get(string className, int key)
获取在当前语言类名键对应的字符串
override? ElectricConnectorType GetConnectorType(SubsystemTerrain terrain, int value, int face, int connectorFace, int x, int y, int z)
override IEnumerable< int > GetCreativeValues()
override void DrawBlock(PrimitivesRenderer3D primitivesRenderer, int value, Color color, float size, ref Matrix matrix, DrawBlockEnvironmentData environmentData)
override void GetDropValues(SubsystemTerrain subsystemTerrain, int oldValue, int newValue, int toolLevel, List< BlockDropValue > dropValues, out bool showDebris)
BlockMesh[] m_standaloneBlockMeshesByColor
override void GenerateTerrainVertices(BlockGeometryGenerator generator, TerrainGeometry geometry, int value, int x, int y, int z)
override IEnumerable< CraftingRecipe > GetProceduralCraftingRecipes()
static int Index
override void Initialize()
override BoundingBox[] GetCustomCollisionBoxes(SubsystemTerrain terrain, int value)
override string GetDisplayName(SubsystemTerrain subsystemTerrain, int value)
static readonly Color[] LedColors
static int SetColor(int data, int color)
static int SetMountingFace(int data, int face)
static int GetMountingFace(int data)
override ElectricElement CreateElectricElement(SubsystemElectricity subsystemElectricity, int value, int x, int y, int z)
override BlockPlacementData GetPlacementValue(SubsystemTerrain subsystemTerrain, ComponentMiner componentMiner, int value, TerrainRaycastResult raycastResult)
方块放置方向
override int GetFace(int value)
const string fName
BlockMesh[] m_blockMeshesByData
static int GetColor(int data)
BoundingBox[][] m_collisionBoxesByData
static ? ElectricConnectorDirection GetConnectorDirection(int mountingFace, int rotation, int connectorFace)
TerrainGeometrySubset SubsetOpaque
static int ReplaceData(int value, int data)
static int MakeBlockValue(int contents)
static int ExtractData(int value)
static Color White
static Matrix CreateRotationX(float radians)
static Matrix CreateTranslation(float x, float y, float z)
static Matrix CreateRotationZ(float radians)
static Matrix CreateRotationY(float radians)
static readonly Vector2 Zero