Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
SwitchBlock.cs
浏览该文件的文档.
1using Engine;
3
4namespace Game {
6 public static int Index = 141;
7
9
11
13
14 public override void Initialize() {
15 Model model = ContentManager.Get<Model>("Models/Switch");
16 Matrix boneAbsoluteTransform = BlockMesh.GetBoneAbsoluteTransform(model.FindMesh("Body").ParentBone);
17 Matrix boneAbsoluteTransform2 = BlockMesh.GetBoneAbsoluteTransform(model.FindMesh("Lever").ParentBone);
18 for (int i = 0; i < 6; i++) {
19 for (int j = 0; j < 2; j++) {
20 int num = (i << 1) | j;
21 Matrix matrix = i >= 4
22 ? i != 4
23 ? Matrix.CreateRotationX((float)Math.PI) * Matrix.CreateTranslation(0.5f, 1f, 0.5f)
24 : Matrix.CreateTranslation(0.5f, 0f, 0.5f)
25 : Matrix.CreateRotationX((float)Math.PI / 2f)
26 * Matrix.CreateTranslation(0f, 0f, -0.5f)
27 * Matrix.CreateRotationY(i * (float)Math.PI / 2f)
28 * Matrix.CreateTranslation(0.5f, 0.5f, 0.5f);
29 Matrix matrix2 = Matrix.CreateRotationX(j == 0 ? MathUtils.DegToRad(30f) : MathUtils.DegToRad(-30f));
32 .AppendModelMeshPart(
33 model.FindMesh("Body").MeshParts[0],
34 boneAbsoluteTransform * matrix,
35 false,
36 false,
37 false,
38 false,
40 );
42 .AppendModelMeshPart(
43 model.FindMesh("Lever").MeshParts[0],
44 boneAbsoluteTransform2 * matrix2 * matrix,
45 false,
46 false,
47 false,
48 false,
50 );
51 m_collisionBoxesByIndex[num] = [m_blockMeshesByIndex[num].CalculateBoundingBox()];
52 }
53 }
54 Matrix matrix3 = Matrix.CreateRotationY(-(float)Math.PI / 2f) * Matrix.CreateRotationZ((float)Math.PI / 2f);
55 m_standaloneBlockMesh.AppendModelMeshPart(
56 model.FindMesh("Body").MeshParts[0],
57 boneAbsoluteTransform * matrix3,
58 false,
59 false,
60 false,
61 false,
63 );
64 m_standaloneBlockMesh.AppendModelMeshPart(
65 model.FindMesh("Lever").MeshParts[0],
66 boneAbsoluteTransform2 * matrix3,
67 false,
68 false,
69 false,
70 false,
72 );
73 }
74
75 public static bool GetLeverState(int value) => (Terrain.ExtractData(value) & 1) != 0;
76
77 public static int SetLeverState(int value, bool state) => Terrain.ReplaceData(
78 value,
79 state ? Terrain.ExtractData(value) | 1 : Terrain.ExtractData(value) & -2
80 );
81
82 public static int GetVoltageLevel(int data) => 15 - ((data >> 4) & 0xF);
83
84 public static int SetVoltageLevel(int data, int voltageLevel) => (data & -241) | ((15 - (voltageLevel & 0xF)) << 4);
85
86 public override int GetFace(int value) => (Terrain.ExtractData(value) >> 1) & 7;
87
88 public override BlockPlacementData GetPlacementValue(SubsystemTerrain subsystemTerrain,
89 ComponentMiner componentMiner,
90 int value,
91 TerrainRaycastResult raycastResult) {
92 BlockPlacementData result = default;
93 result.Value = Terrain.ReplaceData(value, raycastResult.CellFace.Face << 1);
95 result.Value = Terrain.ReplaceData(value, data);
96 result.CellFace = raycastResult.CellFace;
97 return result;
98 }
99
100 public override BoundingBox[] GetCustomCollisionBoxes(SubsystemTerrain terrain, int value) {
101 int num = CalculateIndex(value);
102 if (num >= m_collisionBoxesByIndex.Length) {
103 return null;
104 }
105 return m_collisionBoxesByIndex[num];
106 }
107
108 public override void GenerateTerrainVertices(BlockGeometryGenerator generator, TerrainGeometry geometry, int value, int x, int y, int z) {
109 int num = CalculateIndex(value);
110 if (num < m_blockMeshesByIndex.Length) {
111 generator.GenerateMeshVertices(
112 this,
113 x,
114 y,
115 z,
117 Color.White,
118 null,
119 geometry.SubsetOpaque
120 );
121 generator.GenerateWireVertices(
122 value,
123 x,
124 y,
125 z,
126 GetFace(value),
127 0.25f,
129 geometry.SubsetOpaque
130 );
131 }
132 }
133
134 public override void DrawBlock(PrimitivesRenderer3D primitivesRenderer,
135 int value,
136 Color color,
137 float size,
138 ref Matrix matrix,
139 DrawBlockEnvironmentData environmentData) {
140 BlocksManager.DrawMeshBlock(primitivesRenderer, m_standaloneBlockMesh, color, 2f * size, ref matrix, environmentData);
141 }
142
143 public override ElectricElement CreateElectricElement(SubsystemElectricity subsystemElectricity, int value, int x, int y, int z) =>
144 new SwitchElectricElement(subsystemElectricity, new CellFace(x, y, z, GetFace(value)), value);
145
147 int value,
148 int face,
149 int connectorFace,
150 int x,
151 int y,
152 int z) {
153 int face2 = GetFace(value);
154 if (face == face2
155 && SubsystemElectricity.GetConnectorDirection(face2, 0, connectorFace).HasValue) {
156 return ElectricConnectorType.Output;
157 }
158 return null;
159 }
160
161 public int CalculateIndex(int value) {
162 int face = GetFace(value);
163 bool leverState = GetLeverState(value);
164 return (face << 1) | (leverState ? 1 : 0);
165 }
166 }
167}
ModelMesh FindMesh(string name, bool throwIfNotFound=true)
ReadOnlyList< ModelMeshPart > MeshParts
static float DegToRad(float degrees)
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 ? ElectricConnectorDirection GetConnectorDirection(int mountingFace, int rotation, int connectorFace)
BoundingBox[][] m_collisionBoxesByIndex
override void GenerateTerrainVertices(BlockGeometryGenerator generator, TerrainGeometry geometry, int value, int x, int y, int z)
override int GetFace(int value)
override ElectricElement CreateElectricElement(SubsystemElectricity subsystemElectricity, int value, int x, int y, int z)
override? ElectricConnectorType GetConnectorType(SubsystemTerrain terrain, int value, int face, int connectorFace, int x, int y, int z)
static int SetLeverState(int value, bool state)
BlockMesh m_standaloneBlockMesh
static int SetVoltageLevel(int data, int voltageLevel)
override BlockPlacementData GetPlacementValue(SubsystemTerrain subsystemTerrain, ComponentMiner componentMiner, int value, TerrainRaycastResult raycastResult)
方块放置方向
override void DrawBlock(PrimitivesRenderer3D primitivesRenderer, int value, Color color, float size, ref Matrix matrix, DrawBlockEnvironmentData environmentData)
BlockMesh[] m_blockMeshesByIndex
int CalculateIndex(int value)
static int GetVoltageLevel(int data)
static bool GetLeverState(int value)
override BoundingBox[] GetCustomCollisionBoxes(SubsystemTerrain terrain, int value)
override void Initialize()
TerrainGeometrySubset SubsetOpaque
static int ReplaceData(int value, int data)
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