Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
EggBlock.cs
浏览该文件的文档.
1using System.Globalization;
2using Engine;
5
6namespace Game {
7 public class EggBlock : Block {
8 public class EggType {
9 public int EggTypeIndex;
10
11 public bool ShowEgg;
12
13 public string DisplayName;
14
15 public string TemplateName;
16
17 public float NutritionalValue;
18
19 public int TextureSlot;
20
21 public Color Color;
22
24
25 public bool SwapUV;
26
27 public float Scale;
28
30 }
31
32 public static string fName = "EggBlock";
33 public static int Index = 118;
34 public Dictionary<int, EggType> m_eggTypes = [];
35 public ReadOnlyList<EggType> EggTypes => new(m_eggTypes.Values.ToList());
36
37 public override void Initialize() {
38 m_eggTypes.Clear();
39 DatabaseObjectType parameterSetType = DatabaseManager.GameDatabase.ParameterSetType;
40 Guid eggParameterSetGuid = new("300ff557-775f-4c7c-a88a-26655369f00b");
41 foreach (DatabaseObject item in from o in DatabaseManager.GameDatabase.Database.Root.GetExplicitNestingChildren(parameterSetType, false)
42 where o.EffectiveInheritanceRoot.Guid == eggParameterSetGuid
43 select o) {
44 int nestedValue = item.GetNestedValue<int>("EggTypeIndex");
45 if (nestedValue >= 0) {
46 nestedValue &= 0xFFF;
47 string value = item.GetNestedValue<string>("DisplayName");
48 if (value.StartsWith('[')
49 && value.EndsWith(']')) {
50 string[] lp = value.Substring(1, value.Length - 2).Split([":"], StringSplitOptions.RemoveEmptyEntries);
51 value = LanguageControl.GetDatabase("DisplayName", lp[1]);
52 }
53 m_eggTypes[nestedValue] = new EggType {
54 EggTypeIndex = nestedValue,
55 ShowEgg = item.GetNestedValue<bool>("ShowEgg"),
56 DisplayName = value,
57 TemplateName = item.NestingParent.Name,
58 NutritionalValue = item.GetNestedValue<float>("NutritionalValue"),
59 Color = item.GetNestedValue<Color>("Color"),
60 ScaleUV = item.GetNestedValue<Vector2>("ScaleUV"),
61 SwapUV = item.GetNestedValue<bool>("SwapUV"),
62 Scale = item.GetNestedValue<float>("Scale"),
63 TextureSlot = item.GetNestedValue<int>("TextureSlot")
64 };
65 }
66 }
67 Model model = ContentManager.Get<Model>("Models/Egg");
68 Matrix boneAbsoluteTransform = BlockMesh.GetBoneAbsoluteTransform(model.FindMesh("Egg").ParentBone);
69 foreach (EggType eggType in m_eggTypes.Values) {
70 if (eggType == null) {
71 continue;
72 }
73 eggType.BlockMesh = new BlockMesh();
75 model.FindMesh("Egg").MeshParts[0],
76 boneAbsoluteTransform,
77 false,
78 false,
79 false,
80 false,
81 eggType.Color
82 );
83 Matrix identity = Matrix.Identity;
84 if (eggType.SwapUV) {
85 identity.M11 = 0f;
86 identity.M12 = 1f;
87 identity.M21 = 1f;
88 identity.M22 = 0f;
89 }
90 identity *= Matrix.CreateScale(0.0625f * eggType.ScaleUV.X, 0.0625f * eggType.ScaleUV.Y, 1f);
91 identity *= Matrix.CreateTranslation(eggType.TextureSlot % 16 / 16f, eggType.TextureSlot / 16 / 16f, 0f);
92 eggType.BlockMesh.TransformTextureCoordinates(identity);
93 }
94 base.Initialize();
95 }
96
97 public override string GetDisplayName(SubsystemTerrain subsystemTerrain, int value) {
98 EggType eggType = GetEggType(Terrain.ExtractData(value));
99 int data = Terrain.ExtractData(value);
100 bool isCooked = GetIsCooked(data);
101 bool isLaid = GetIsLaid(data);
102 if (isCooked) {
103 return LanguageControl.Get(fName, 1) + eggType.DisplayName;
104 }
105 if (!isLaid) {
106 return eggType.DisplayName;
107 }
108 return LanguageControl.Get(fName, 2) + eggType.DisplayName;
109 }
110
111 public override string GetCategory(int value) => "Spawner Eggs";
112
113 public override float GetNutritionalValue(int value) {
114 EggType eggType = GetEggType(Terrain.ExtractData(value));
115 if (!GetIsCooked(Terrain.ExtractData(value))) {
116 return eggType.NutritionalValue;
117 }
118 return 1.5f * eggType.NutritionalValue;
119 }
120
121 public override float GetSicknessProbability(int value) {
122 if (!GetIsCooked(Terrain.ExtractData(value))) {
124 }
125 return 0f;
126 }
127
128 public override int GetRotPeriod(int value) {
129 if (GetNutritionalValue(value) > 0f) {
130 return base.GetRotPeriod(value);
131 }
132 return 0;
133 }
134
135 public override int GetDamage(int value) => (Terrain.ExtractData(value) >> 16) & 1;
136
137 public override int SetDamage(int value, int damage) {
138 int num = Terrain.ExtractData(value);
139 num = (num & -65537) | ((damage & 1) << 16);
140 return Terrain.ReplaceData(value, num);
141 }
142
143 public override int GetDamageDestructionValue(int value) => 246;
144
145 public override IEnumerable<int> GetCreativeValues() {
146 EggType[] eggs = m_eggTypes.OrderBy(pair => pair.Key).Select(pair => pair.Value).ToArray();
147 foreach (EggType eggType in eggs) {
148 if (eggType == null) {
149 continue;
150 }
151 if (eggType.ShowEgg) {
152 yield return Terrain.MakeBlockValue(118, 0, SetEggType(0, eggType.EggTypeIndex));
153 if (eggType.NutritionalValue > 0f) {
154 yield return Terrain.MakeBlockValue(118, 0, SetIsCooked(SetEggType(0, eggType.EggTypeIndex), true));
155 }
156 }
157 }
158 }
159
160 public override IEnumerable<CraftingRecipe> GetProceduralCraftingRecipes() {
161 string description = LanguageControl.Get(fName, 4);
162 foreach (EggType eggType in EggTypes) {
163 if (eggType == null) {
164 continue;
165 }
166 if (eggType.NutritionalValue > 0f) {
167 int rot = 0;
168 while (rot <= 1) {
169 CraftingRecipe craftingRecipe = new() {
170 ResultCount = 1,
171 ResultValue = Terrain.MakeBlockValue(118, 0, SetEggType(SetIsCooked(0, true), eggType.EggTypeIndex)),
172 RemainsCount = 1,
173 RemainsValue = Terrain.MakeBlockValue(91),
174 RequiredHeatLevel = 1f,
175 Description = description
176 };
177 int data = SetEggType(SetIsLaid(0, true), eggType.EggTypeIndex);
178 int value = SetDamage(Terrain.MakeBlockValue(118, 0, data), rot);
179 craftingRecipe.Ingredients[0] = $"egg:{Terrain.ExtractData(value).ToString(CultureInfo.InvariantCulture)}";
180 craftingRecipe.Ingredients[1] = "waterbucket";
181 yield return craftingRecipe;
182 rot++;
183 }
184 }
185 }
186 }
187
188 public override void GenerateTerrainVertices(BlockGeometryGenerator generator, TerrainGeometry geometry, int value, int x, int y, int z) { }
189
190 public override void DrawBlock(PrimitivesRenderer3D primitivesRenderer,
191 int value,
192 Color color,
193 float size,
194 ref Matrix matrix,
195 DrawBlockEnvironmentData environmentData) {
196 int data = Terrain.ExtractData(value);
197 EggType eggType = GetEggType(data);
198 BlocksManager.DrawMeshBlock(primitivesRenderer, eggType.BlockMesh, color, eggType.Scale * size, ref matrix, environmentData);
199 }
200
201 public EggType GetEggType(int data) {
202 int index = (data >> 4) & 0xFFF;
203 bool found = m_eggTypes.TryGetValue(index, out EggType eggType);
204 if (found) {
205 return eggType;
206 }
207 return m_eggTypes[0];
208 }
209
210 public EggType GetEggTypeByCreatureTemplateName(string templateName) {
211 return m_eggTypes.FirstOrDefault(pair => pair.Value.TemplateName == templateName).Value;
212 }
213
214 public static bool GetIsCooked(int data) => (data & 1) != 0;
215
216 public static int SetIsCooked(int data, bool isCooked) {
217 if (!isCooked) {
218 return data & -2;
219 }
220 return data | 1;
221 }
222
223 public static bool GetIsLaid(int data) => (data & 2) != 0;
224
225 public static int SetIsLaid(int data, bool isLaid) {
226 if (!isLaid) {
227 return data & -3;
228 }
229 return data | 2;
230 }
231
232 public static int SetEggType(int data, int eggTypeIndex) {
233 data &= -65521;
234 data |= (eggTypeIndex & 0xFFF) << 4;
235 return data;
236 }
237 }
238}
ModelMesh FindMesh(string name, bool throwIfNotFound=true)
float DefaultSicknessProbability
virtual void TransformTextureCoordinates(Matrix matrix, int facesMask=-1)
static Matrix GetBoneAbsoluteTransform(ModelBone modelBone)
virtual void AppendModelMeshPart(ModelMeshPart meshPart, Matrix matrix, bool makeEmissive, bool flipWindingOrder, bool doubleSided, bool flipNormals, Color color)
static void DrawMeshBlock(PrimitivesRenderer3D primitivesRenderer, BlockMesh blockMesh, float size, ref Matrix matrix, DrawBlockEnvironmentData environmentData)
static object Get(Type type, string name)
static GameDatabase GameDatabase
override int GetDamageDestructionValue(int value)
override void GenerateTerrainVertices(BlockGeometryGenerator generator, TerrainGeometry geometry, int value, int x, int y, int z)
static int SetIsLaid(int data, bool isLaid)
override string GetCategory(int value)
override string GetDisplayName(SubsystemTerrain subsystemTerrain, int value)
static string fName
override int GetDamage(int value)
static int Index
Dictionary< int, EggType > m_eggTypes
override void DrawBlock(PrimitivesRenderer3D primitivesRenderer, int value, Color color, float size, ref Matrix matrix, DrawBlockEnvironmentData environmentData)
override float GetSicknessProbability(int value)
override float GetNutritionalValue(int value)
EggType GetEggTypeByCreatureTemplateName(string templateName)
override void Initialize()
override int GetRotPeriod(int value)
ReadOnlyList< EggType > EggTypes
EggType GetEggType(int data)
static int SetEggType(int data, int eggTypeIndex)
override IEnumerable< int > GetCreativeValues()
static int SetIsCooked(int data, bool isCooked)
override int SetDamage(int value, int damage)
static bool GetIsLaid(int data)
override IEnumerable< CraftingRecipe > GetProceduralCraftingRecipes()
static bool GetIsCooked(int data)
static string GetDatabase(string name, string prop)
static string Get(string className, int key)
获取在当前语言类名键对应的字符串
static int ReplaceData(int value, int data)
static int MakeBlockValue(int contents)
static int ExtractData(int value)
static Matrix CreateTranslation(float x, float y, float z)
static readonly Matrix Identity
static Matrix CreateScale(float scale)