Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
ComponentSwimAwayBehavior.cs
浏览该文件的文档.
1using Engine;
4
5namespace Game {
8
10
12
14
16
18
19 public Random m_random = new();
20
21 public float m_importanceLevel;
22
24
26 public float LowHealthToEscape { get; set; }
27
29
30 public override float ImportanceLevel => m_importanceLevel;
31
32 public virtual void SwimAwayFrom(ComponentBody attacker) {
33 m_attacker = attacker;
34 m_timeToForgetAttacker = m_random.Float(10f, 20f);
35 }
36
37 public virtual void Update(float dt) {
38 m_stateMachine.Update();
39 }
40
41 public override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap) {
42 m_subsystemTerrain = Project.FindSubsystem<SubsystemTerrain>(true);
43 m_subsystemTime = Project.FindSubsystem<SubsystemTime>(true);
44 m_componentCreature = Entity.FindComponent<ComponentCreature>(true);
47 LowHealthToEscape = valuesDictionary.GetValue<float>("LowHealthToEscape");
48 m_componentCreature.ComponentHealth.Injured += delegate(Injury injury) {
49 ComponentCreature attacker = injury.Attacker;
50 SwimAwayFrom(attacker?.ComponentBody);
51 };
52 m_stateMachine.AddState(
53 "Inactive",
54 delegate {
56 m_attacker = null;
57 },
58 delegate {
59 if (m_attacker != null) {
61 if (m_timeToForgetAttacker <= 0f) {
62 m_attacker = null;
63 }
64 }
65 if (m_componentCreature.ComponentHealth.HealthChange < 0f) {
66 m_importanceLevel = m_componentCreature.ComponentHealth.Health < LowHealthToEscape ? 300 : 100;
67 }
68 else if (m_attacker != null
69 && Vector3.DistanceSquared(m_attacker.Position, m_componentCreature.ComponentBody.Position) < 25f) {
70 m_importanceLevel = 100f;
71 }
72 if (IsActive) {
73 m_stateMachine.TransitionTo("SwimmingAway");
74 }
75 },
76 null
77 );
78 m_stateMachine.AddState(
79 "SwimmingAway",
80 delegate {
81 m_componentPathfinding.SetDestination(
83 1f,
84 1f,
85 0,
86 false,
87 true,
88 false,
89 null
90 );
91 },
92 delegate {
93 if (!IsActive
94 || !m_componentPathfinding.Destination.HasValue
95 || m_componentPathfinding.IsStuck) {
96 m_stateMachine.TransitionTo("Inactive");
97 }
98 },
99 null
100 );
101 m_stateMachine.TransitionTo("Inactive");
102 }
103
104 public virtual Vector3 FindSafePlace() {
105 Vector3 vector = 0.5f * (m_componentCreature.ComponentBody.BoundingBox.Min + m_componentCreature.ComponentBody.BoundingBox.Max);
106 Vector3? herdPosition = m_componentHerdBehavior?.FindHerdCenter();
107 float num = float.NegativeInfinity;
108 Vector3 result = vector;
109 for (int i = 0; i < 40; i++) {
110 Vector2 vector2 = m_random.Vector2(1f, 1f);
111 float y = 0.4f * m_random.Float(-1f, 1f);
112 Vector3 v = Vector3.Normalize(new Vector3(vector2.X, y, vector2.Y));
113 Vector3 vector3 = vector + m_random.Float(10f, 20f) * v;
114 TerrainRaycastResult? terrainRaycastResult = m_subsystemTerrain.Raycast(
115 vector,
116 vector3,
117 false,
118 false,
119 delegate(int value, float _) {
120 int num3 = Terrain.ExtractContents(value);
121 return !(BlocksManager.Blocks[num3] is WaterBlock);
122 }
123 );
124 Vector3 vector4 = terrainRaycastResult.HasValue ? vector + v * terrainRaycastResult.Value.Distance : vector3;
125 float num2 = ScoreSafePlace(vector, vector4, herdPosition);
126 if (num2 > num) {
127 num = num2;
128 result = vector4;
129 }
130 }
131 return result;
132 }
133
134 public virtual float ScoreSafePlace(Vector3 currentPosition, Vector3 safePosition, Vector3? herdPosition) {
135 Vector2 vector = new(currentPosition.X, currentPosition.Z);
136 Vector2 vector2 = new(safePosition.X, safePosition.Z);
137 Vector2? vector3 = herdPosition.HasValue ? new Vector2?(new Vector2(herdPosition.Value.X, herdPosition.Value.Z)) : null;
138 Segment2 s = new(vector, vector2);
139 float num = vector3.HasValue ? Segment2.Distance(s, vector3.Value) : 0f;
140 if (m_attacker != null) {
141 Vector3 position = m_attacker.Position;
142 Vector2 vector4 = new(position.X, position.Z);
143 float num2 = Vector2.Distance(vector4, vector2);
144 float num3 = Segment2.Distance(s, vector4);
145 return num2 + 1.5f * num3 - num;
146 }
147 return 1.5f * Vector2.Distance(vector, vector2) - num;
148 }
149 }
150}
Engine.Vector3 Vector3
override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap)
virtual float ScoreSafePlace(Vector3 currentPosition, Vector3 safePosition, Vector3? herdPosition)
virtual void SwimAwayFrom(ComponentBody attacker)
virtual ComponentCreature Attacker
static int ExtractContents(int value)
ValuesDictionary ValuesDictionary
static float Distance(Vector2 v1, Vector2 v2)
static float DistanceSquared(Vector3 v1, Vector3 v2)
static Vector3 Normalize(Vector3 v)
static float Distance(Segment2 s, Vector2 p)