Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
ComponentMoveAwayBehavior.cs
浏览该文件的文档.
1using Engine;
4
5namespace Game {
8
10
12
13 public Random m_random = new();
14
15 public float m_importanceLevel;
16
18
19 public bool m_isFast;
20
22
23 public override float ImportanceLevel => m_importanceLevel;
24
25 public virtual void Update(float dt) {
26 m_stateMachine.Update();
27 }
28
29 public override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap) {
30 m_componentCreature = Entity.FindComponent<ComponentCreature>(true);
32 m_componentCreature.ComponentBody.CollidedWithBody += delegate(ComponentBody body) {
33 m_target = body;
34 m_isFast = MathUtils.Max(body.Velocity.Length(), m_componentCreature.ComponentBody.Velocity.Length()) > 3f;
35 };
36 m_stateMachine.AddState(
37 "Inactive",
38 delegate {
40 m_target = null;
41 },
42 delegate {
43 if (IsActive) {
44 m_stateMachine.TransitionTo("Move");
45 }
46 if (m_target != null) {
48 }
49 },
50 null
51 );
52 m_stateMachine.AddState(
53 "Move",
54 delegate {
55 if (m_random.Float(0f, 1f) < 0.5f) {
56 m_componentCreature.ComponentCreatureSounds.PlayIdleSound(true);
57 }
58 if (m_target != null) {
59 Vector3 vector = m_target.Position + 0.5f * m_target.Velocity;
60 Vector2 v = Vector2.Normalize(m_componentCreature.ComponentBody.Position.XZ - vector.XZ);
61 Vector2 vector2 = Vector2.Zero;
62 float num = float.MinValue;
63 for (float num2 = 0f; num2 < (float)Math.PI * 2f; num2 += 0.1f) {
64 Vector2 vector3 = Vector2.CreateFromAngle(num2);
65 if (Vector2.Dot(vector3, v) > 0.2f) {
66 float num3 = Vector2.Dot(m_componentCreature.ComponentBody.Matrix.Forward.XZ, vector3);
67 if (num3 > num) {
68 vector2 = vector3;
69 num = num3;
70 }
71 }
72 }
73 float s = m_random.Float(1.5f, 2f);
74 float speed = m_isFast ? 0.7f : 0.35f;
75 m_componentPathfinding.SetDestination(
76 m_componentCreature.ComponentBody.Position + s * new Vector3(vector2.X, 0f, vector2.Y),
77 speed,
78 1f,
79 0,
80 false,
81 true,
82 false,
83 null
84 );
85 }
86 },
87 delegate {
88 if (!IsActive) {
89 m_stateMachine.TransitionTo("Inactive");
90 }
91 else if (m_componentPathfinding.IsStuck
92 || !m_componentPathfinding.Destination.HasValue) {
94 }
95 m_componentCreature.ComponentCreatureModel.LookRandomOrder = true;
96 },
97 null
98 );
99 m_stateMachine.TransitionTo("Inactive");
100 }
101 }
102}
static int Max(int x1, int x2)
override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap)
ValuesDictionary ValuesDictionary
static float Dot(Vector2 v1, Vector2 v2)
static readonly Vector2 Zero
static Vector2 CreateFromAngle(float angle)
static Vector2 Normalize(Vector2 v)