Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
ComponentRunAwayBehavior.cs
浏览该文件的文档.
1using Engine;
4
5namespace Game {
8
10
12
14
16
18
19 public Random m_random = new();
20
22
23 public float m_importanceLevel;
24
26
28
29 public bool m_heardNoise;
30
32 public float LowHealthToEscape { get; set; }
33
35
36 public override float ImportanceLevel => m_importanceLevel;
37
38 public virtual void RunAwayFrom(ComponentBody componentBody) {
39 m_attacker = componentBody;
40 m_timeToForgetAttacker = m_random.Float(10f, 20f);
41 }
42
43 public virtual void Update(float dt) {
44 m_stateMachine.Update();
45 m_heardNoise = false;
46 }
47
48 public virtual void HearNoise(ComponentBody sourceBody, Vector3 sourcePosition, float loudness) {
49 if (loudness >= 1f) {
50 m_heardNoise = true;
51 m_lastNoiseSourcePosition = sourcePosition;
52 }
53 }
54
55 public override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap) {
56 m_subsystemTerrain = Project.FindSubsystem<SubsystemTerrain>(true);
57 m_subsystemTime = Project.FindSubsystem<SubsystemTime>(true);
58 m_subsystemNoise = Project.FindSubsystem<SubsystemNoise>(true);
59 m_componentCreature = Entity.FindComponent<ComponentCreature>(true);
62 LowHealthToEscape = valuesDictionary.GetValue<float>("LowHealthToEscape");
63 m_componentCreature.ComponentHealth.Injured += delegate(Injury injury) {
64 ComponentCreature attacker = injury.Attacker;
65 RunAwayFrom(attacker?.ComponentBody);
66 };
67 m_stateMachine.AddState(
68 "Inactive",
69 delegate {
72 },
73 delegate {
74 if (m_attacker != null) {
76 if (m_timeToForgetAttacker <= 0f) {
77 m_attacker = null;
78 }
79 }
80 if (m_componentCreature.ComponentHealth.HealthChange < 0f
81 || (m_attacker != null && Vector3.DistanceSquared(m_attacker.Position, m_componentCreature.ComponentBody.Position) < 36f)) {
84 m_componentCreature.ComponentHealth.Health < LowHealthToEscape ? 300 : 100
85 );
86 }
87 else if (m_heardNoise) {
89 }
90 else if (!IsActive) {
92 }
93 if (IsActive) {
94 m_stateMachine.TransitionTo("RunningAway");
95 }
96 },
97 null
98 );
99 m_stateMachine.AddState(
100 "RunningAway",
101 delegate {
102 Vector3 value = FindSafePlace();
103 m_componentPathfinding.SetDestination(
104 value,
105 1f,
106 1f,
107 0,
108 false,
109 true,
110 false,
111 null
112 );
113 m_componentCreature.ComponentCreatureSounds.PlayPainSound();
114 m_subsystemNoise.MakeNoise(m_componentCreature.ComponentBody, 0.25f, 6f);
115 },
116 delegate {
117 if (!IsActive) {
118 m_stateMachine.TransitionTo("Inactive");
119 }
120 else if (!m_componentPathfinding.Destination.HasValue
121 || m_componentPathfinding.IsStuck) {
123 }
124 else if (m_attacker != null) {
125 if (!m_attacker.IsAddedToProject) {
127 m_attacker = null;
128 }
129 else {
130 ComponentHealth componentHealth = m_attacker.Entity.FindComponent<ComponentHealth>();
131 if (componentHealth != null
132 && componentHealth.Health == 0f) {
134 m_attacker = null;
135 }
136 }
137 }
138 },
139 null
140 );
141 m_stateMachine.TransitionTo("Inactive");
142 }
143
144 public virtual Vector3 FindSafePlace() {
145 Vector3 position = m_componentCreature.ComponentBody.Position;
146 Vector3? herdPosition = m_componentHerdBehavior?.FindHerdCenter();
147 if (herdPosition.HasValue
148 && Vector3.DistanceSquared(position, herdPosition.Value) < 144f) {
149 herdPosition = null;
150 }
151 float num = float.NegativeInfinity;
152 Vector3 result = position;
153 for (int i = 0; i < 30; i++) {
154 int num2 = Terrain.ToCell(position.X + m_random.Float(-25f, 25f));
155 int num3 = Terrain.ToCell(position.Z + m_random.Float(-25f, 25f));
156 for (int num4 = 255; num4 >= 0; num4--) {
157 int cellValue = m_subsystemTerrain.Terrain.GetCellValue(num2, num4, num3);
158 if (BlocksManager.Blocks[Terrain.ExtractContents(cellValue)].IsCollidable_(cellValue)
159 || Terrain.ExtractContents(cellValue) == 18) {
160 Vector3 vector = new(num2 + 0.5f, num4 + 1.1f, num3 + 0.5f);
161 float num5 = ScoreSafePlace(position, vector, herdPosition, m_lastNoiseSourcePosition, Terrain.ExtractContents(cellValue));
162 if (num5 > num) {
163 num = num5;
164 result = vector;
165 }
166 break;
167 }
168 }
169 }
170 return result;
171 }
172
173 public virtual float ScoreSafePlace(Vector3 currentPosition,
174 Vector3 safePosition,
175 Vector3? herdPosition,
176 Vector3? noiseSourcePosition,
177 int contents) {
178 float num = 0f;
179 Vector2 vector = new(currentPosition.X, currentPosition.Z);
180 Vector2 vector2 = new(safePosition.X, safePosition.Z);
181 Segment2 s = new(vector, vector2);
182 if (m_attacker != null) {
183 Vector3 position = m_attacker.Position;
184 Vector2 vector3 = new(position.X, position.Z);
185 float num2 = Vector2.Distance(vector3, vector2);
186 float num3 = Segment2.Distance(s, vector3);
187 num += num2 + 3f * num3;
188 }
189 else {
190 num += 2f * Vector2.Distance(vector, vector2);
191 }
192 Vector2? vector4 = herdPosition.HasValue ? new Vector2?(new Vector2(herdPosition.Value.X, herdPosition.Value.Z)) : null;
193 float num4 = vector4.HasValue ? Segment2.Distance(s, vector4.Value) : 0f;
194 num -= num4;
195 Vector2? vector5 = noiseSourcePosition.HasValue
196 ? new Vector2?(new Vector2(noiseSourcePosition.Value.X, noiseSourcePosition.Value.Z))
197 : null;
198 float num5 = vector5.HasValue ? Segment2.Distance(s, vector5.Value) : 0f;
199 num += 1.5f * num5;
200 if (contents == 18) {
201 num -= 4f;
202 }
203 return num;
204 }
205 }
206}
static int Max(int x1, int x2)
virtual bool IsCollidable_(int value)
virtual float ScoreSafePlace(Vector3 currentPosition, Vector3 safePosition, Vector3? herdPosition, Vector3? noiseSourcePosition, int contents)
virtual void HearNoise(ComponentBody sourceBody, Vector3 sourcePosition, float loudness)
virtual void RunAwayFrom(ComponentBody componentBody)
override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap)
virtual ComponentCreature Attacker
static int ExtractContents(int value)
static int ToCell(float x)
ValuesDictionary ValuesDictionary
static float Distance(Vector2 v1, Vector2 v2)
static float DistanceSquared(Vector3 v1, Vector3 v2)
static float Distance(Segment2 s, Vector2 p)