Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
ComponentDigInMudBehavior.cs
浏览该文件的文档.
1using Engine;
4
5namespace Game {
8
10
12
14
16
18
20
22
23 public Random m_random = new();
24
25 public float m_importanceLevel;
26
27 public double m_sinkTime;
28
29 public double m_digInTime;
30
31 public double m_digOutTime = double.NegativeInfinity;
32
33 public float m_maxDigInDepth;
34
36
38
40
41 public override float ImportanceLevel => m_importanceLevel;
42
43 public virtual void Update(float dt) {
44 m_stateMachine.Update();
45 m_collidedWithBody = null;
46 }
47
48 public override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap) {
49 m_subsystemTerrain = Project.FindSubsystem<SubsystemTerrain>(true);
50 m_subsystemTime = Project.FindSubsystem<SubsystemTime>(true);
51 m_componentCreature = Entity.FindComponent<ComponentCreature>(true);
53 m_componentMiner = Entity.FindComponent<ComponentMiner>(true);
54 m_componentFishModel = Entity.FindComponent<ComponentFishModel>(true);
56 string digInBlockName = valuesDictionary.GetValue<string>("DigInBlockName");
57 m_digInBlockIndex = !string.IsNullOrEmpty(digInBlockName)
58 ? BlocksManager.Blocks.First(b => b.GetType().Name == digInBlockName).BlockIndex
59 : 0;
60 m_maxDigInDepth = valuesDictionary.GetValue<float>("MaxDigInDepth");
61 m_componentCreature.ComponentBody.CollidedWithBody += delegate(ComponentBody b) { m_collidedWithBody = b; };
62 m_stateMachine.AddState(
63 "Inactive",
64 delegate { m_importanceLevel = 0f; },
65 delegate {
66 if (m_random.Float(0f, 1f) < 0.5f * m_subsystemTime.GameTimeDelta
67 && m_subsystemTime.GameTime > m_digOutTime + 15.0
68 && m_digInBlockIndex != 0) {
69 int x = Terrain.ToCell(m_componentCreature.ComponentBody.Position.X);
70 int y = Terrain.ToCell(m_componentCreature.ComponentBody.Position.Y - 0.9f);
71 int z = Terrain.ToCell(m_componentCreature.ComponentBody.Position.Z);
72 if (m_subsystemTerrain.Terrain.GetCellContents(x, y, z) == m_digInBlockIndex) {
73 m_importanceLevel = m_random.Float(1f, 3f);
74 }
75 }
76 if (IsActive) {
77 m_stateMachine.TransitionTo("Sink");
78 }
79 },
80 null
81 );
82 m_stateMachine.AddState(
83 "Sink",
84 delegate {
86 m_sinkTime = m_subsystemTime.GameTime;
88 },
89 delegate {
90 if (m_random.Float(0f, 1f) < 2f * m_subsystemTime.GameTimeDelta
91 && m_componentCreature.ComponentBody.StandingOnValue == m_digInBlockIndex
92 && m_componentCreature.ComponentBody.Velocity.LengthSquared() < 1f) {
93 m_stateMachine.TransitionTo("DigIn");
94 }
95 if (!IsActive
96 || m_subsystemTime.GameTime > m_sinkTime + 6.0) {
97 m_stateMachine.TransitionTo("Inactive");
98 }
99 },
100 null
101 );
102 m_stateMachine.AddState(
103 "DigIn",
104 delegate {
105 m_digInTime = m_subsystemTime.GameTime;
106 m_digOutTime = m_digInTime + m_random.Float(30f, 60f);
107 },
108 delegate {
109 m_componentFishModel.DigInOrder = m_maxDigInDepth;
110 if (m_collidedWithBody != null) {
111 if (m_subsystemTime.GameTime - m_digInTime > 2.0
112 && m_collidedWithBody.Density < 0.95f) {
115 m_collidedWithBody.Position,
116 Vector3.Normalize(m_collidedWithBody.Position - m_componentCreature.ComponentBody.Position)
117 );
118 }
120 m_stateMachine.TransitionTo("Inactive");
121 }
122 if (!IsActive
123 || m_subsystemTime.GameTime >= m_digOutTime
124 || m_componentCreature.ComponentBody.StandingOnValue != m_digInBlockIndex
125 || m_componentCreature.ComponentBody.Velocity.LengthSquared() > 1f) {
126 m_stateMachine.TransitionTo("Inactive");
127 }
128 },
129 null
130 );
131 m_stateMachine.TransitionTo("Inactive");
132 }
133
134 public virtual Vector3? FindDestination() {
135 for (int i = 0; i < 8; i++) {
136 Vector2 vector = m_random.Vector2(1f, 1f);
137 float y = 0.2f * m_random.Float(-0.8f, 1f);
138 Vector3 v = Vector3.Normalize(new Vector3(vector.X, y, vector.Y));
139 Vector3 vector2 = m_componentCreature.ComponentBody.Position + m_random.Float(8f, 16f) * v;
140 TerrainRaycastResult? terrainRaycastResult = m_subsystemTerrain.Raycast(
141 m_componentCreature.ComponentBody.Position,
142 vector2,
143 false,
144 false,
145 delegate(int value, float _) {
146 int num = Terrain.ExtractContents(value);
147 return !(BlocksManager.Blocks[num] is WaterBlock);
148 }
149 );
150 if (!terrainRaycastResult.HasValue) {
151 return vector2;
152 }
153 if (terrainRaycastResult.Value.Distance > 4f) {
154 return m_componentCreature.ComponentBody.Position + v * terrainRaycastResult.Value.Distance;
155 }
156 }
157 return null;
158 }
159 }
160}
Engine.Vector3 Vector3
int BlockIndex
定义 Block.cs:6
override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap)
ComponentSwimAwayBehavior m_componentSwimAwayBehavior
static int ExtractContents(int value)
static int ToCell(float x)
ValuesDictionary ValuesDictionary
static Vector3 Normalize(Vector3 v)