Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
MovingBlock.cs
浏览该文件的文档.
1using Engine;
5
6namespace Game {
7 public class MovingBlock {
8 public static bool IsNullOrStopped(MovingBlock movingBlock) {
9 if (movingBlock == null) {
10 return true;
11 }
12 IMovingBlockSet movingBlockSet = movingBlock.MovingBlockSet;
13 if (movingBlockSet == null) {
14 return true;
15 }
16 if (movingBlockSet.Stopped) {
17 return true;
18 }
19 return false;
20 }
21
22 public Vector3 Position => MovingBlockSet.Position + new Vector3(Offset);
23
24 public static MovingBlock LoadFromPositionAndOffset(Project project, Vector3 movingBlocksPosition, Point3 offset, bool throwOnError = true) {
25 SubsystemMovingBlocks subsystemMovingBlocks = project.FindSubsystem<SubsystemMovingBlocks>();
26 IMovingBlockSet movingBlockSet = subsystemMovingBlocks.MovingBlockSets.FirstOrDefault(
27 set => set.Position.ToString() == movingBlocksPosition.ToString(),
28 null
29 );
30 if (movingBlockSet != null) {
31 MovingBlock movingBlock = movingBlockSet.Blocks.FirstOrDefault(block => block.Offset == offset, null);
32 if (!IsNullOrStopped(movingBlock)) {
33 return movingBlock;
34 }
35 if (throwOnError) {
36 throw new Exception($"Required moving block offset {offset} is not found in MovingBlockSet {movingBlocksPosition}");
37 }
38 return null;
39 }
40 if (throwOnError) {
41 throw new Exception($"Required moving block set {movingBlocksPosition} is not found.");
42 }
43 return null;
44 }
45
47 ValuesDictionary valuesDictionary,
48 bool throwOnError = true,
49 bool throwIfNotFound = false) {
50 Vector3? movingBlocksPosition = valuesDictionary.GetValue<Vector3?>("MovingBlockSetPosition", null);
51 Point3? point = valuesDictionary.GetValue<Point3?>("MovingBlockOffset", null);
52 if (movingBlocksPosition.HasValue
53 && point.HasValue) {
54 return LoadFromPositionAndOffset(project, movingBlocksPosition.Value, point.Value);
55 }
56 if (throwIfNotFound) {
57 throw new Exception("MovingBlockSetPosition is not contained in valuesDictionary.");
58 }
59 return null;
60 }
61
62 public static MovingBlock LoadFromString(Project project, string movingBlockInfo, out Exception exception) {
63 exception = null;
64 try {
65 string[] str1 = movingBlockInfo.Split(';');
66 if (str1.Length == 0
67 || !str1[0].Contains("MovingBlock")) {
68 exception = new InvalidDataException($"String \"{movingBlockInfo}\" is not valid for moving block load.");
69 return null;
70 }
71 Vector3 movingBlockSetPosition = HumanReadableConverter.ConvertFromString<Vector3>(str1[1]);
72 Point3 movingBlockOffset = HumanReadableConverter.ConvertFromString<Point3>(str1[2]);
73 return LoadFromPositionAndOffset(project, movingBlockSetPosition, movingBlockOffset);
74 }
75 catch (Exception e) {
76 exception = e;
77 return null;
78 }
79 }
80
81 public void SetValuesDicionary(ValuesDictionary valuesDictionary, bool saveWhenStopped = false) {
82 if (!IsNullOrStopped(this) || saveWhenStopped) {
83 valuesDictionary.SetValue("MovingBlockSetPosition", MovingBlockSet.Position);
84 valuesDictionary.SetValue("MovingBlockOffset", Offset);
85 }
86 }
87
88 public override string ToString() =>
89 $"MovingBlock;{HumanReadableConverter.ConvertToString(MovingBlockSet.Position)};{HumanReadableConverter.ConvertToString(Offset)}";
90
91 public Point3 Offset;
92
93 public int Value;
94
96 }
97}
Engine.Vector3 Vector3
static object ConvertFromString(Type type, string data)
static bool IsNullOrStopped(MovingBlock movingBlock)
void SetValuesDicionary(ValuesDictionary valuesDictionary, bool saveWhenStopped=false)
static MovingBlock LoadFromValuesDictionary(Project project, ValuesDictionary valuesDictionary, bool throwOnError=true, bool throwIfNotFound=false)
override string ToString()
static MovingBlock LoadFromString(Project project, string movingBlockInfo, out Exception exception)
IMovingBlockSet MovingBlockSet
static MovingBlock LoadFromPositionAndOffset(Project project, Vector3 movingBlocksPosition, Point3 offset, bool throwOnError=true)
virtual Subsystem FindSubsystem(Type type, string name, bool throwOnError)
override string ToString()