Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
ComponentCetaceanBreatheBehavior.cs
浏览该文件的文档.
1using Engine;
4
5namespace Game {
8
10
12
14
16
18
19 public Random m_random = new();
20
22
23 public float m_importanceLevel;
24
26
27 public override float ImportanceLevel => m_importanceLevel;
28
29 public virtual void Update(float dt) {
30 if (!IsActive) {
31 m_stateMachine.TransitionTo("Inactive");
32 }
33 m_stateMachine.Update();
34 }
35
36 public override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap) {
37 m_subsystemParticles = Project.FindSubsystem<SubsystemParticles>(true);
38 m_subsystemAudio = Project.FindSubsystem<SubsystemAudio>(true);
39 m_subsystemTerrain = Project.FindSubsystem<SubsystemTerrain>(true);
40 m_componentCreature = Entity.FindComponent<ComponentCreature>(true);
42 m_stateMachine.AddState(
43 "Inactive",
44 null,
45 delegate {
46 m_importanceLevel = MathUtils.Lerp(0f, 400f, MathUtils.Saturate((0.75f - m_componentCreature.ComponentHealth.Air) / 0.75f));
47 if (IsActive) {
48 m_stateMachine.TransitionTo("Surface");
49 }
50 },
51 null
52 );
53 m_stateMachine.AddState(
54 "Surface",
55 delegate { m_componentPathfinding.Stop(); },
56 delegate {
57 _ = m_componentCreature.ComponentBody.Position;
58 if (!m_componentPathfinding.Destination.HasValue) {
59 Vector3? destination = FindSurfaceDestination();
60 if (destination.HasValue) {
61 float speed = m_componentCreature.ComponentHealth.Air < 0.25f ? 1f : m_random.Float(0.4f, 0.6f);
62 m_componentPathfinding.SetDestination(
63 destination,
64 speed,
65 1f,
66 0,
67 false,
68 false,
69 false,
70 null
71 );
72 }
73 }
74 else if (m_componentPathfinding.IsStuck) {
76 }
77 if (m_componentCreature.ComponentHealth.Air > 0.9f) {
78 m_stateMachine.TransitionTo("Breathe");
79 }
80 },
81 null
82 );
83 m_stateMachine.AddState(
84 "Breathe",
85 delegate {
86 Vector3 forward = m_componentCreature.ComponentBody.Matrix.Forward;
87 Vector3 value = m_componentCreature.ComponentBody.Matrix.Translation + 10f * forward + new Vector3(0f, 2f, 0f);
88 m_componentPathfinding.SetDestination(
89 value,
90 0.6f,
91 1f,
92 0,
93 false,
94 false,
95 false,
96 null
97 );
98 m_particleSystem = new WhalePlumeParticleSystem(m_subsystemTerrain, m_random.Float(0.8f, 1.1f), m_random.Float(1f, 1.3f));
99 m_subsystemParticles.AddParticleSystem(m_particleSystem);
100 m_subsystemAudio.PlayRandomSound(
101 "Audio/Creatures/WhaleBlow",
102 1f,
103 m_random.Float(-0.2f, 0.2f),
104 m_componentCreature.ComponentBody.Position,
105 10f,
106 true
107 );
108 },
109 delegate {
110 m_particleSystem.Position = m_componentCreature.ComponentBody.Position
111 + new Vector3(0f, 0.8f * m_componentCreature.ComponentBody.BoxSize.Y, 0f);
112 if (!m_subsystemParticles.ContainsParticleSystem(m_particleSystem)) {
114 }
115 },
116 delegate {
117 m_particleSystem.IsStopped = true;
118 m_particleSystem = null;
119 }
120 );
121 }
122
123 public virtual Vector3? FindSurfaceDestination() {
124 Vector3 vector = 0.5f * (m_componentCreature.ComponentBody.BoundingBox.Min + m_componentCreature.ComponentBody.BoundingBox.Max);
125 Vector3 forward = m_componentCreature.ComponentBody.Matrix.Forward;
126 float s = 2f * m_componentCreature.ComponentBody.ImmersionDepth;
127 for (int i = 0; i < 16; i++) {
128 Vector2 vector2 = i < 4 ? new Vector2(forward.X, forward.Z) + m_random.Vector2(0f, 0.25f) : m_random.Vector2(0.5f, 1f);
129 Vector3 v = Vector3.Normalize(new Vector3(vector2.X, 1f, vector2.Y));
130 Vector3 end = vector + s * v;
131 TerrainRaycastResult? terrainRaycastResult = m_subsystemTerrain.Raycast(
132 vector,
133 end,
134 false,
135 false,
136 (value, _) => Terrain.ExtractContents(value) != 18
137 );
138 if (terrainRaycastResult.HasValue
139 && Terrain.ExtractContents(terrainRaycastResult.Value.Value) == 0) {
140 return new Vector3(
141 terrainRaycastResult.Value.CellFace.X + 0.5f,
142 terrainRaycastResult.Value.CellFace.Y,
143 terrainRaycastResult.Value.CellFace.Z + 0.5f
144 );
145 }
146 }
147 return null;
148 }
149 }
150}
Engine.Vector3 Vector3
static float Saturate(float x)
static float Lerp(float x1, float x2, float f)
override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap)
static int ExtractContents(int value)
ValuesDictionary ValuesDictionary
static Vector3 Normalize(Vector3 v)