Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
ComponentAvoidFireBehavior.cs
浏览该文件的文档.
1using Engine;
4
5namespace Game {
8
10
12
14
16
18
19 public Random m_random = new();
20
21 public float m_importanceLevel;
22
23 public float m_dayRange;
24
25 public float m_nightRange;
26
28
29 public float m_circlingDirection = 1f;
30
32
33 public double m_ignoreFireUntil;
34
36
37 public override float ImportanceLevel => m_importanceLevel;
38
39 public override string DebugInfo {
40 get {
41 if (!(m_ignoreFireUntil < m_subsystemTime.GameTime)) {
42 return string.Empty;
43 }
44 return $"ifu={m_ignoreFireUntil - m_subsystemTime.GameTime:0}";
45 }
46 }
47
48 public virtual void Update(float dt) {
49 m_stateMachine.Update();
50 }
51
52 public override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap) {
53 m_subsystemTime = Project.FindSubsystem<SubsystemTime>(true);
54 m_subsystemSky = Project.FindSubsystem<SubsystemSky>(true);
55 m_componentCreature = Entity.FindComponent<ComponentCreature>(true);
58 m_dayRange = valuesDictionary.GetValue<float>("DayRange");
59 m_nightRange = valuesDictionary.GetValue<float>("NightRange");
60 m_periodicEventOffset = m_random.Float(0f, 10f);
61 m_stateMachine.AddState(
62 "Inactive",
63 delegate {
65 m_target = null;
66 },
67 delegate {
68 if (IsActive) {
69 m_stateMachine.TransitionTo(m_importanceLevel < 10f ? "Circle" : "Move");
70 }
71 else if (m_subsystemTime.PeriodicGameTimeEvent(1.0, m_periodicEventOffset)) {
72 m_target = FindTarget(out float targetScore);
73 if (m_target.HasValue) {
74 if (m_random.Float(0f, 1f) < 0.015f) {
75 m_ignoreFireUntil = m_subsystemTime.GameTime + 20.0;
76 }
77 Vector3.Distance(m_target.Value, m_componentCreature.ComponentBody.Position);
78 m_importanceLevel = m_subsystemTime.GameTime < m_ignoreFireUntil ? 0f :
79 targetScore > 0.5f ? 250f : m_random.Float(1f, 5f);
80 }
81 else {
83 }
84 }
85 },
86 null
87 );
88 m_stateMachine.AddState(
89 "Move",
90 delegate {
91 if (m_target.HasValue) {
92 Vector3 vector2 = Vector3.Normalize(
93 Vector3.Normalize(m_componentCreature.ComponentBody.Position - m_target.Value) + m_random.Vector3(0.5f)
94 );
95 Vector3 value2 = m_componentCreature.ComponentBody.Position
96 + m_random.Float(6f, 8f) * Vector3.Normalize(new Vector3(vector2.X, 0f, vector2.Z));
97 m_componentPathfinding.SetDestination(
98 value2,
99 m_random.Float(0.6f, 0.8f),
100 1f,
101 0,
102 false,
103 true,
104 false,
105 null
106 );
107 }
108 },
109 delegate {
110 if (!IsActive) {
111 m_stateMachine.TransitionTo("Inactive");
112 }
113 else if (!m_target.HasValue
114 || m_componentPathfinding.IsStuck
115 || !m_componentPathfinding.Destination.HasValue
116 || ScoreTarget(m_target.Value) <= 0f) {
118 }
119 if (m_random.Float(0f, 1f) < 0.1f * m_subsystemTime.GameTimeDelta) {
120 m_componentCreature.ComponentCreatureSounds.PlayIdleSound(true);
121 }
122 m_componentCreature.ComponentCreatureModel.LookRandomOrder = true;
123 },
124 null
125 );
126 m_stateMachine.AddState(
127 "Circle",
128 delegate {
129 if (m_target.HasValue) {
130 Vector3 vector = Vector3.Cross(Vector3.Normalize(m_componentCreature.ComponentBody.Position - m_target.Value), Vector3.UnitY)
132 Vector3 value = m_componentCreature.ComponentBody.Position
133 + m_random.Float(6f, 8f) * Vector3.Normalize(new Vector3(vector.X, 0f, vector.Z));
134 m_componentPathfinding.SetDestination(
135 value,
136 m_random.Float(0.4f, 0.9f),
137 1f,
138 0,
139 false,
140 true,
141 false,
142 null
143 );
144 }
145 },
146 delegate {
147 if (!IsActive) {
148 m_stateMachine.TransitionTo("Inactive");
149 }
150 else if (m_componentPathfinding.IsStuck) {
153 }
154 else if (!m_target.HasValue
155 || !m_componentPathfinding.Destination.HasValue
156 || ScoreTarget(m_target.Value) <= 0f) {
158 }
159 if (m_random.Float(0f, 1f) < 0.1f * m_subsystemTime.GameTimeDelta) {
160 m_componentCreature.ComponentCreatureSounds.PlayIdleSound(true);
161 }
162 m_componentCreature.ComponentCreatureModel.LookAtOrder = m_target;
163 },
164 null
165 );
166 m_stateMachine.TransitionTo("Inactive");
167 }
168
169 public virtual Vector3? FindTarget(out float targetScore) {
170 _ = m_componentCreature.ComponentBody.Position;
171 Vector3? result = null;
172 float num = 0f;
173 foreach (Point3 campfire in m_subsystemCampfireBlockBehavior.Campfires) {
174 Vector3 vector = new(campfire.X, campfire.Y, campfire.Z);
175 float num2 = ScoreTarget(vector);
176 if (num2 > num) {
177 num = num2;
178 result = vector;
179 }
180 }
181 targetScore = num;
182 return result;
183 }
184
185 public virtual float ScoreTarget(Vector3 target) {
186 float num = m_subsystemSky.SkyLightIntensity > 0.2f ? m_dayRange : m_nightRange;
187 if (num > 0f) {
188 float num2 = Vector3.Distance(target, m_componentCreature.ComponentBody.Position);
189 return MathUtils.Saturate(1f - num2 / num);
190 }
191 return 0f;
192 }
193 }
194}
Engine.Vector3 Vector3
static float Saturate(float x)
virtual ? Vector3 FindTarget(out float targetScore)
SubsystemCampfireBlockBehavior m_subsystemCampfireBlockBehavior
override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap)
ValuesDictionary ValuesDictionary
static Vector3 Cross(Vector3 v1, Vector3 v2)
static Vector3 Normalize(Vector3 v)
static float Distance(Vector3 v1, Vector3 v2)
static readonly Vector3 UnitY