Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
ComponentHerdBehavior.cs
浏览该文件的文档.
1using Engine;
4
5namespace Game {
8
10
12
14
16
17 public float m_dt;
18
19 public float m_importanceLevel;
20
21 public Random m_random = new();
22
24
25 public float m_herdingRange;
26
28
29 public string HerdName { get; set; }
30
32
33 public override float ImportanceLevel => m_importanceLevel;
34
35 public void CallNearbyCreaturesHelp(ComponentCreature target, float maxRange, float maxChaseTime, bool isPersistent) {
36 bool skipVanilla = false;
38 "CallNearbyCreaturesHelp",
39 modLoader => {
40 modLoader.CallNearbyCreaturesHelp(this, target, maxRange, maxChaseTime, isPersistent, out skipVanilla);
41 return false;
42 }
43 );
44 if (skipVanilla || target == null) {
45 return;
46 }
47 Vector3 position = target.ComponentBody.Position;
48 foreach (ComponentCreature creature in m_subsystemCreatureSpawn.Creatures) {
49 if (Vector3.DistanceSquared(position, creature.ComponentBody.Position) < 256f) {
50 ComponentHerdBehavior componentHerdBehavior = creature.Entity.FindComponent<ComponentHerdBehavior>();
51 if (componentHerdBehavior != null
52 && !string.IsNullOrEmpty(componentHerdBehavior.HerdName)
53 && componentHerdBehavior.HerdName == HerdName
54 && componentHerdBehavior.m_autoNearbyCreaturesHelp) {
55 ComponentChaseBehavior componentChaseBehavior = creature.Entity.FindComponent<ComponentChaseBehavior>();
56 if (componentChaseBehavior != null
57 && componentChaseBehavior.Target == null) {
58 componentChaseBehavior.Attack(target, maxRange, maxChaseTime, isPersistent);
59 }
60 }
61 }
62 }
63 }
64
66 bool skipVanilla = false;
67 Vector3? herdCenterFromMod = null;
69 "FindHerdCenter",
70 modLoader => {
71 modLoader.FindHerdCenter(m_componentCreature, out herdCenterFromMod, out skipVanilla);
72 return false;
73 }
74 );
75 if (skipVanilla) {
76 return herdCenterFromMod;
77 }
78 if (string.IsNullOrEmpty(HerdName)) {
79 return null;
80 }
81 Vector3 position = m_componentCreature.ComponentBody.Position;
82 int num = 0;
83 Vector3 zero = Vector3.Zero;
84 foreach (ComponentCreature creature in m_subsystemCreatureSpawn.Creatures) {
85 if (creature.ComponentHealth.Health > 0f) {
86 ComponentHerdBehavior componentHerdBehavior = creature.Entity.FindComponent<ComponentHerdBehavior>();
87 if (componentHerdBehavior != null
88 && componentHerdBehavior.HerdName == HerdName) {
89 Vector3 position2 = creature.ComponentBody.Position;
90 if (Vector3.DistanceSquared(position, position2) < m_herdingRange * m_herdingRange) {
91 zero += position2;
92 num++;
93 }
94 }
95 }
96 }
97 if (num > 0) {
98 return zero / num;
99 }
100 return null;
101 }
102
103 public virtual void Update(float dt) {
104 if (string.IsNullOrEmpty(m_stateMachine.CurrentState)
105 || !IsActive) {
106 m_stateMachine.TransitionTo("Inactive");
107 }
108 m_dt = dt;
109 m_stateMachine.Update();
110 }
111
112 public override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap) {
113 m_subsystemTime = Project.FindSubsystem<SubsystemTime>(true);
115 m_componentCreature = Entity.FindComponent<ComponentCreature>(true);
117 HerdName = valuesDictionary.GetValue<string>("HerdName");
118 m_herdingRange = valuesDictionary.GetValue<float>("HerdingRange");
119 m_autoNearbyCreaturesHelp = valuesDictionary.GetValue<bool>("AutoNearbyCreaturesHelp");
120 m_componentCreature.ComponentHealth.Injured += delegate(Injury injury) {
121 ComponentCreature attacker = injury.Attacker;
122 CallNearbyCreaturesHelp(attacker, 20f, 30f, false);
123 };
124 m_stateMachine.AddState(
125 "Inactive",
126 null,
127 delegate {
128 if (m_subsystemTime.PeriodicGameTimeEvent(1.0, 1f * (GetHashCode() % 256 / 256f))) {
129 Vector3? vector2 = FindHerdCenter();
130 if (vector2.HasValue) {
131 float num = Vector3.Distance(vector2.Value, m_componentCreature.ComponentBody.Position);
132 if (num > 10f) {
134 }
135 if (num > 12f) {
137 }
138 if (num > 16f) {
139 m_importanceLevel = 50f;
140 }
141 if (num > 20f) {
142 m_importanceLevel = 250f;
143 }
144 }
145 }
146 if (IsActive) {
147 m_stateMachine.TransitionTo("Herd");
148 }
149 },
150 null
151 );
152 m_stateMachine.AddState(
153 "Stuck",
154 delegate {
155 m_stateMachine.TransitionTo("Herd");
156 if (m_random.Bool(0.5f)) {
157 m_componentCreature.ComponentCreatureSounds.PlayIdleSound(false);
159 }
160 },
161 null,
162 null
163 );
164 m_stateMachine.AddState(
165 "Herd",
166 delegate {
167 Vector3? vector = FindHerdCenter();
168 if (vector.HasValue
169 && Vector3.Distance(m_componentCreature.ComponentBody.Position, vector.Value) > 6f) {
170 float speed = m_importanceLevel > 10f ? m_random.Float(0.9f, 1f) : m_random.Float(0.25f, 0.35f);
171 int maxPathfindingPositions = m_importanceLevel > 200f ? 100 : 0;
172 m_componentPathfinding.SetDestination(
173 vector.Value,
174 speed,
175 7f,
176 maxPathfindingPositions,
177 false,
178 true,
179 false,
180 null
181 );
182 }
183 else {
185 }
186 },
187 delegate {
188 m_componentCreature.ComponentLocomotion.LookOrder = m_look - m_componentCreature.ComponentLocomotion.LookAngles;
189 if (m_componentPathfinding.IsStuck) {
190 m_stateMachine.TransitionTo("Stuck");
191 }
192 if (!m_componentPathfinding.Destination.HasValue) {
194 }
195 if (m_random.Float(0f, 1f) < 0.05f * m_dt) {
196 m_componentCreature.ComponentCreatureSounds.PlayIdleSound(false);
197 }
198 if (m_random.Float(0f, 1f) < 1.5f * m_dt) {
199 m_look = new Vector2(MathUtils.DegToRad(45f) * m_random.Float(-1f, 1f), MathUtils.DegToRad(10f) * m_random.Float(-1f, 1f));
200 }
201 },
202 null
203 );
204 }
205 }
206}
static float DegToRad(float degrees)
virtual void Attack(ComponentCreature componentCreature, float maxRange, float maxChaseTime, bool isPersistent)
override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap)
void CallNearbyCreaturesHelp(ComponentCreature target, float maxRange, float maxChaseTime, bool isPersistent)
SubsystemCreatureSpawn m_subsystemCreatureSpawn
virtual ComponentCreature Attacker
ValuesDictionary ValuesDictionary
Component FindComponent(Type type, string name, bool throwOnError)
static void HookAction(string HookName, Func< ModLoader, bool > action)
执行Hook
static float DistanceSquared(Vector3 v1, Vector3 v2)
static readonly Vector3 Zero
static float Distance(Vector3 v1, Vector3 v2)