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