Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
ComponentAvoidPlayerBehavior.cs
浏览该文件的文档.
1using Engine;
4
5namespace Game {
8
10
12
14
16
18
19 public DynamicArray<ComponentBody> m_componentBodies = [];
20
21 public Random m_random = new();
22
23 public float m_importanceLevel;
24
25 public float m_dayRange;
26
27 public float m_nightRange;
28
29 public float m_dt;
30
32
33 public double m_nextUpdateTime;
34
36
37 public override float ImportanceLevel => m_importanceLevel;
38
39 public virtual void Update(float dt) {
40 if (m_subsystemTime.GameTime >= m_nextUpdateTime) {
41 m_dt = m_random.Float(0.4f, 0.6f) + MathUtils.Min((float)(m_subsystemTime.GameTime - m_nextUpdateTime), 0.1f);
42 m_nextUpdateTime = m_subsystemTime.GameTime + m_dt;
43 m_stateMachine.Update();
44 }
45 }
46
47 public override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap) {
48 m_subsystemTime = Project.FindSubsystem<SubsystemTime>(true);
49 m_subsystemSky = Project.FindSubsystem<SubsystemSky>(true);
50 m_subsystemBodies = Project.FindSubsystem<SubsystemBodies>(true);
51 m_componentCreature = Entity.FindComponent<ComponentCreature>(true);
53 m_dayRange = valuesDictionary.GetValue<float>("DayRange");
54 m_nightRange = valuesDictionary.GetValue<float>("NightRange");
55 m_stateMachine.AddState(
56 "Inactive",
57 delegate {
59 m_target = null;
60 },
61 delegate {
62 if (IsActive) {
63 m_stateMachine.TransitionTo("Move");
64 }
65 m_target = FindTarget(out float targetScore);
66 if (m_target != null) {
67 Vector3.Distance(m_target.ComponentBody.Position, m_componentCreature.ComponentBody.Position);
68 SetImportanceLevel(targetScore);
69 }
70 else {
72 }
73 },
74 null
75 );
76 m_stateMachine.AddState(
77 "Move",
78 null,
79 delegate {
80 if (!IsActive) {
81 m_stateMachine.TransitionTo("Inactive");
82 }
83 else if (m_target == null
84 || m_componentPathfinding.IsStuck
85 || !m_componentPathfinding.Destination.HasValue) {
87 }
88 else {
89 float num = ScoreTarget(m_target);
91 Vector3 vector = Vector3.Normalize(m_componentCreature.ComponentBody.Position - m_target.ComponentBody.Position);
92 Vector3 value = m_componentCreature.ComponentBody.Position + 10f * Vector3.Normalize(new Vector3(vector.X, 0f, vector.Z));
93 m_componentPathfinding.SetDestination(
94 value,
95 MathUtils.Lerp(0.6f, 1f, num),
96 1f,
97 0,
98 false,
99 true,
100 false,
101 null
102 );
103 m_componentCreature.ComponentCreatureModel.LookRandomOrder = true;
104 if (m_random.Float(0f, 1f) < 0.1f * m_dt) {
105 m_componentCreature.ComponentCreatureSounds.PlayIdleSound(true);
106 }
107 }
108 },
109 null
110 );
111 m_stateMachine.TransitionTo("Inactive");
112 }
113
114 public virtual void SetImportanceLevel(float score) {
115 m_importanceLevel = MathUtils.Lerp(4f, 8f, MathF.Sqrt(score));
116 }
117
118 public virtual ComponentCreature FindTarget(out float targetScore) {
119 Vector3 position = m_componentCreature.ComponentBody.Position;
120 ComponentCreature result = null;
121 float num = 0f;
122 m_componentBodies.Clear();
123 m_subsystemBodies.FindBodiesAroundPoint(new Vector2(position.X, position.Z), MathUtils.Max(m_nightRange, m_dayRange), m_componentBodies);
124 for (int i = 0; i < m_componentBodies.Count; i++) {
125 ComponentCreature componentCreature = m_componentBodies.Array[i].Entity.FindComponent<ComponentCreature>();
126 if (componentCreature != null) {
127 float num2 = ScoreTarget(componentCreature);
128 if (num2 > num) {
129 num = num2;
130 result = componentCreature;
131 }
132 }
133 }
134 targetScore = num;
135 return result;
136 }
137
138 public virtual float ScoreTarget(ComponentCreature target) {
139 float num = m_subsystemSky.SkyLightIntensity > 0.2f ? m_dayRange : m_nightRange;
140 if (num > 0f) {
141 if (!target.IsAddedToProject
142 || target.ComponentHealth.Health <= 0f
143 || target.Entity.FindComponent<ComponentPlayer>() == null) {
144 return 0f;
145 }
146 float num2 = Vector3.Distance(target.ComponentBody.Position, m_componentCreature.ComponentBody.Position);
147 return MathUtils.Saturate(1f - num2 / num);
148 }
149 return 0f;
150 }
151 }
152}
Engine.Vector3 Vector3
static int Min(int x1, int x2)
static float Saturate(float x)
static int Max(int x1, int x2)
static float Lerp(float x1, float x2, float f)
override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap)
virtual float ScoreTarget(ComponentCreature target)
virtual ComponentCreature FindTarget(out float targetScore)
ValuesDictionary ValuesDictionary
Component FindComponent(Type type, string name, bool throwOnError)
static Vector3 Normalize(Vector3 v)
static float Distance(Vector3 v1, Vector3 v2)