Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
ComponentStareBehavior.cs
浏览该文件的文档.
1using Engine;
4
5namespace Game {
8
10
12
14
15 public DynamicArray<ComponentBody> m_componentBodies = [];
16
18
19 public Random m_random = new();
20
21 public float m_importanceLevel;
22
23 public float m_stareRange;
24
25 public double m_stareEndTime;
26
28
30
31 public override float ImportanceLevel => m_importanceLevel;
32
33 public virtual void Update(float dt) {
34 m_stateMachine.Update();
35 }
36
37 public override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap) {
38 m_subsystemTime = Project.FindSubsystem<SubsystemTime>(true);
39 m_subsystemBodies = Project.FindSubsystem<SubsystemBodies>(true);
40 m_componentCreature = Entity.FindComponent<ComponentCreature>(true);
42 m_stareRange = valuesDictionary.GetValue<float>("StareRange");
43 m_stateMachine.AddState(
44 "Inactive",
45 delegate { m_importanceLevel = 0f; },
46 delegate {
47 if (m_subsystemTime.GameTime > m_stareEndTime + 8.0
48 && m_random.Float(0f, 1f) < 1f * m_subsystemTime.GameTimeDelta) {
50 if (m_target != null) {
51 float probability = m_target.Entity.FindComponent<ComponentPlayer>() != null ? 1f : 0.25f;
52 if (m_random.Bool(probability)) {
53 m_importanceLevel = m_random.Float(3f, 5f);
54 }
55 }
56 }
57 if (IsActive) {
58 m_stateMachine.TransitionTo("Stare");
59 }
60 },
61 null
62 );
63 m_stateMachine.AddState(
64 "Stare",
65 delegate {
66 m_stareEndTime = m_subsystemTime.GameTime + m_random.Float(6f, 12f);
67 if (m_target != null) {
68 Vector3 position = m_componentCreature.ComponentBody.Position;
69 Vector3 v = Vector3.Normalize(m_target.ComponentBody.Position - position);
70 m_componentPathfinding.SetDestination(
71 position + 1.1f * v,
72 m_random.Float(0.3f, 0.4f),
73 1f,
74 0,
75 false,
76 true,
77 false,
78 null
79 );
80 if (m_random.Float(0f, 1f) < 0.5f) {
81 m_componentCreature.ComponentCreatureSounds.PlayIdleSound(false);
82 }
83 }
84 },
85 delegate {
86 if (!IsActive
87 || m_target == null
88 || m_componentPathfinding.IsStuck
89 || m_subsystemTime.GameTime > m_stareEndTime) {
90 m_stateMachine.TransitionTo("Inactive");
91 }
92 else if (m_random.Float(0f, 1f) < 1f * m_subsystemTime.GameTimeDelta
93 && ScoreTarget(m_target) <= 0f) {
94 m_stateMachine.TransitionTo("Inactive");
95 }
96 else {
97 m_componentCreature.ComponentCreatureModel.LookAtOrder = m_target.ComponentCreatureModel.EyePosition;
98 }
99 },
100 null
101 );
102 m_stateMachine.TransitionTo("Inactive");
103 }
104
105 public virtual ComponentCreature FindTarget() {
106 Vector3 position = m_componentCreature.ComponentBody.Position;
107 m_componentBodies.Clear();
108 m_subsystemBodies.FindBodiesAroundPoint(new Vector2(position.X, position.Z), m_stareRange, m_componentBodies);
109 ComponentCreature result = null;
110 float num = 0f;
111 for (int i = 0; i < m_componentBodies.Count; i++) {
112 ComponentCreature componentCreature = m_componentBodies.Array[i].Entity.FindComponent<ComponentCreature>();
113 if (componentCreature != null) {
114 float num2 = ScoreTarget(componentCreature);
115 if (num2 > num) {
116 result = componentCreature;
117 num = num2;
118 }
119 }
120 }
121 return result;
122 }
123
124 public virtual float ScoreTarget(ComponentCreature componentCreature) {
125 if (componentCreature != m_componentCreature
126 && componentCreature.Entity.IsAddedToProject) {
127 float num = Vector3.Distance(m_componentCreature.ComponentBody.Position, componentCreature.ComponentBody.Position);
128 float num2 = m_stareRange - num;
129 if (m_random.Float(0f, 1f) < 0.66f
130 && componentCreature.Entity.FindComponent<ComponentPlayer>() != null) {
131 num2 *= 100f;
132 }
133 return num2;
134 }
135 return 0f;
136 }
137 }
138}
virtual ComponentCreature FindTarget()
override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap)
virtual float ScoreTarget(ComponentCreature componentCreature)
DynamicArray< ComponentBody > m_componentBodies
ValuesDictionary ValuesDictionary
Component FindComponent(Type type, string name, bool throwOnError)
static Vector3 Normalize(Vector3 v)
static float Distance(Vector3 v1, Vector3 v2)