Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
ComponentFourLeggedModel.cs
浏览该文件的文档.
1using Engine;
5
6namespace Game {
8 public enum Gait {
12 }
13
15
17
19
21
23
25
27
29
31
33
35
37
38 public float m_walkHindLegsAngle;
39
40 public float m_walkBobHeight;
41
43
44 public bool m_canCanter;
45
46 public bool m_canTrot;
47
48 public bool m_useCanterSound;
49
50 public Gait m_gait;
51
52 public float m_feedFactor;
53
54 public float m_buttFactor;
55
56 public float m_buttPhase;
57
58 public float m_footstepsPhase;
59
60 public float m_legAngle1;
61
62 public float m_legAngle2;
63
64 public float m_legAngle3;
65
66 public float m_legAngle4;
67
68 public float m_headAngleY;
69
70 public override float AttackPhase {
71 get => m_buttPhase;
72 set => m_buttPhase = value;
73 }
74
75 public override float AttackFactor {
76 get => m_buttFactor;
77 set => m_buttFactor = value;
78 }
79
80 public override void Update(float dt) {
81 float footstepsPhase = m_footstepsPhase;
82 float num = m_componentCreature.ComponentLocomotion.SlipSpeed
83 ?? Vector3.Dot(m_componentCreature.ComponentBody.Velocity, m_componentCreature.ComponentBody.Matrix.Forward);
84 if (m_canCanter && num > 0.7f * m_componentCreature.ComponentLocomotion.WalkSpeed) {
85 m_gait = Gait.Canter;
87 m_footstepsPhase += 0.7f * m_walkAnimationSpeed * num * dt;
88 }
89 else if (m_canTrot && num > 0.5f * m_componentCreature.ComponentLocomotion.WalkSpeed) {
90 m_gait = Gait.Trot;
92 m_footstepsPhase += 1.25f * m_walkAnimationSpeed * num * dt;
93 }
94 else if (MathF.Abs(num) > 0.2f) {
95 m_gait = Gait.Walk;
97 m_footstepsPhase += 1.25f * m_walkAnimationSpeed * num * dt;
98 }
99 else {
100 m_gait = Gait.Walk;
102 m_footstepsPhase = 0f;
103 }
104 float num2 = 0f;
105 if (m_gait == Gait.Canter) {
106 num2 = (0f - m_walkBobHeight) * 1.5f * MathF.Sin((float)Math.PI * 2f * MovementAnimationPhase);
107 }
108 else if (m_gait == Gait.Trot) {
109 num2 = m_walkBobHeight * 1.5f * MathUtils.Sqr(MathF.Sin((float)Math.PI * 2f * MovementAnimationPhase));
110 }
111 else if (m_gait == Gait.Walk) {
112 num2 = (0f - m_walkBobHeight) * MathUtils.Sqr(MathF.Sin((float)Math.PI * 2f * MovementAnimationPhase));
113 }
114 float num3 = MathUtils.Min(12f * m_subsystemTime.GameTimeDelta, 1f);
115 Bob += num3 * (num2 - Bob);
116 if (m_gait == Gait.Canter && m_useCanterSound) {
117 float num4 = MathF.Floor(m_footstepsPhase);
118 if (m_footstepsPhase > num4
119 && footstepsPhase <= num4) {
120 string footstepSoundMaterialName = m_subsystemSoundMaterials.GetFootstepSoundMaterialName(m_componentCreature);
121 if (!string.IsNullOrEmpty(footstepSoundMaterialName)
122 && footstepSoundMaterialName != "Water") {
123 m_subsystemAudio.PlayRandomSound(
124 "Audio/Footsteps/CanterDirt",
125 0.75f,
126 m_random.Float(-0.25f, 0f),
127 m_componentCreature.ComponentBody.Position,
128 3f,
129 true
130 );
131 }
132 }
133 }
134 else {
135 float num5 = MathF.Floor(m_footstepsPhase);
136 if (m_footstepsPhase > num5
137 && footstepsPhase <= num5) {
138 m_componentCreature.ComponentCreatureSounds.PlayFootstepSound(1f);
139 }
140 }
141 m_feedFactor = FeedOrder ? MathUtils.Min(m_feedFactor + 2f * dt, 1f) : MathUtils.Max(m_feedFactor - 2f * dt, 0f);
142 IsAttackHitMoment = false;
143 if (AttackOrder) {
144 m_buttFactor = MathUtils.Min(m_buttFactor + 4f * dt, 1f);
145 float buttPhase = m_buttPhase;
147 if (buttPhase < 0.5f
148 && m_buttPhase >= 0.5f) {
149 IsAttackHitMoment = true;
150 }
151 }
152 else {
153 m_buttFactor = MathUtils.Max(m_buttFactor - 4f * dt, 0f);
154 if (m_buttPhase != 0f) {
155 if (m_buttPhase > 0.5f) {
157 }
158 else if (m_buttPhase > 0f) {
159 m_buttPhase = MathUtils.Max(m_buttPhase - dt * 2f, 0f);
160 }
161 }
162 }
163 FeedOrder = false;
164 AttackOrder = false;
165 base.Update(dt);
166 }
167
168 public override void AnimateCreature() {
169 Vector3 position = m_componentCreature.ComponentBody.Position;
170 Vector3 vector = m_componentCreature.ComponentBody.Rotation.ToYawPitchRoll();
171 if (m_componentCreature.ComponentHealth.Health > 0f) {
172 float num = 0f;
173 float num2 = 0f;
174 float num3 = 0f;
175 float num4 = 0f;
176 float num5 = 0f;
177 if (MovementAnimationPhase != 0f
178 && (m_componentCreature.ComponentBody.StandingOnValue.HasValue || m_componentCreature.ComponentBody.ImmersionFactor > 0f)) {
179 if (m_gait == Gait.Canter) {
180 float num6 = MathF.Sin((float)Math.PI * 2f * (MovementAnimationPhase + 0f));
181 float num7 = MathF.Sin((float)Math.PI * 2f * (MovementAnimationPhase + 0.25f));
182 float num8 = MathF.Sin((float)Math.PI * 2f * (MovementAnimationPhase + 0.15f));
183 float num9 = MathF.Sin((float)Math.PI * 2f * (MovementAnimationPhase + 0.4f));
188 num5 = MathUtils.DegToRad(8f) * MathF.Sin((float)Math.PI * 2f * MovementAnimationPhase);
189 }
190 else if (m_gait == Gait.Trot) {
191 float num10 = MathF.Sin((float)Math.PI * 2f * (MovementAnimationPhase + 0f));
192 float num11 = MathF.Sin((float)Math.PI * 2f * (MovementAnimationPhase + 0.5f));
193 float num12 = MathF.Sin((float)Math.PI * 2f * (MovementAnimationPhase + 0.5f));
194 float num13 = MathF.Sin((float)Math.PI * 2f * (MovementAnimationPhase + 0f));
195 num = m_walkFrontLegsAngle * num10;
196 num2 = m_walkFrontLegsAngle * num11;
197 num3 = m_walkHindLegsAngle * num12;
198 num4 = m_walkHindLegsAngle * num13;
199 num5 = MathUtils.DegToRad(3f) * MathF.Sin((float)Math.PI * 4f * MovementAnimationPhase);
200 }
201 else {
202 float num14 = MathF.Sin((float)Math.PI * 2f * (MovementAnimationPhase + 0f));
203 float num15 = MathF.Sin((float)Math.PI * 2f * (MovementAnimationPhase + 0.5f));
204 float num16 = MathF.Sin((float)Math.PI * 2f * (MovementAnimationPhase + 0.25f));
205 float num17 = MathF.Sin((float)Math.PI * 2f * (MovementAnimationPhase + 0.75f));
206 num = m_walkFrontLegsAngle * num14;
207 num2 = m_walkFrontLegsAngle * num15;
208 num3 = m_walkHindLegsAngle * num16;
209 num4 = m_walkHindLegsAngle * num17;
210 num5 = MathUtils.DegToRad(3f) * MathF.Sin((float)Math.PI * 4f * MovementAnimationPhase);
211 }
212 }
213 float num18 = MathUtils.Min(12f * m_subsystemTime.GameTimeDelta, 1f);
214 m_legAngle1 += num18 * (num - m_legAngle1);
215 m_legAngle2 += num18 * (num2 - m_legAngle2);
216 m_legAngle3 += num18 * (num3 - m_legAngle3);
217 m_legAngle4 += num18 * (num4 - m_legAngle4);
218 m_headAngleY += num18 * (num5 - m_headAngleY);
219 Vector2 vector2 = m_componentCreature.ComponentLocomotion.LookAngles;
220 vector2.Y += m_headAngleY;
221 vector2.X = Math.Clamp(vector2.X, 0f - MathUtils.DegToRad(65f), MathUtils.DegToRad(65f));
222 vector2.Y = Math.Clamp(vector2.Y, 0f - MathUtils.DegToRad(55f), MathUtils.DegToRad(55f));
223 Vector2 vector3 = Vector2.Zero;
224 if (m_neckBone != null) {
225 vector3 = 0.6f * vector2;
226 vector2 = 0.4f * vector2;
227 }
228 if (m_feedFactor > 0f) {
229 float y = 0f - MathUtils.DegToRad(25f + 45f * SimplexNoise.OctavedNoise((float)m_subsystemTime.GameTime, 3f, 2, 2f, 0.75f));
230 vector2 = Vector2.Lerp(v2: new Vector2(0f, y), v1: vector2, f: m_feedFactor);
231 //if (m_moveLegWhenFeeding)
232 //{
233 //float x = MathUtils.DegToRad(20f) + (MathUtils.PowSign(SimplexNoise.OctavedNoise((float)m_subsystemTime.GameTime, 1f, 1, 1f, 1f) - 0.5f, 0.33f) / 0.5f * MathUtils.DegToRad(25f) * (float)Math.Sin(17.0 * m_subsystemTime.GameTime));
234 //num2 = MathUtils.Lerp(num2, x, m_feedFactor);
235 //}
236 }
237 if (m_buttFactor != 0f) {
238 float y2 = (0f - MathUtils.DegToRad(40f)) * MathF.Sin((float)Math.PI * 2f * MathUtils.Sigmoid(m_buttPhase, 4f));
239 vector2 = Vector2.Lerp(v2: new Vector2(0f, y2), v1: vector2, f: m_buttFactor);
240 }
242 m_bodyBone.Index,
243 Matrix.CreateRotationY(vector.X) * Matrix.CreateTranslation(position.X, position.Y + Bob, position.Z)
244 );
245 SetBoneTransform(m_headBone.Index, Matrix.CreateRotationX(vector2.Y) * Matrix.CreateRotationZ(0f - vector2.X));
246 if (m_neckBone != null) {
248 }
253 }
254 else {
255 float num19 = 1f - DeathPhase;
256 float num20 = Vector3.Dot(m_componentFrame.Matrix.Right, DeathCauseOffset) > 0f ? 1 : -1;
257 float num21 = m_componentCreature.ComponentBody.BoundingBox.Max.Y - m_componentCreature.ComponentBody.BoundingBox.Min.Y;
259 m_bodyBone.Index,
261 * Matrix.CreateFromYawPitchRoll(vector.X, 0f, (float)Math.PI / 2f * DeathPhase * num20)
263 * Matrix.CreateTranslation(position)
264 );
266 if (m_neckBone != null) {
268 }
273 }
274 }
275
276 public override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap) {
277 base.Load(valuesDictionary, idToEntityMap);
278 m_subsystemAudio = Project.FindSubsystem<SubsystemAudio>(true);
280 m_walkAnimationSpeed = valuesDictionary.GetValue<float>("WalkAnimationSpeed");
281 m_walkFrontLegsAngle = valuesDictionary.GetValue<float>("WalkFrontLegsAngle");
282 m_walkHindLegsAngle = valuesDictionary.GetValue<float>("WalkHindLegsAngle");
283 m_canterLegsAngleFactor = valuesDictionary.GetValue<float>("CanterLegsAngleFactor");
284 m_walkBobHeight = valuesDictionary.GetValue<float>("WalkBobHeight");
285 m_moveLegWhenFeeding = valuesDictionary.GetValue<bool>("MoveLegWhenFeeding");
286 m_canCanter = valuesDictionary.GetValue<bool>("CanCanter");
287 m_canTrot = valuesDictionary.GetValue<bool>("CanTrot");
288 m_useCanterSound = valuesDictionary.GetValue<bool>("UseCanterSound");
289 }
290
291 public override void SetModel(Model model) {
292 base.SetModel(model);
293 if (IsSet) {
294 return;
295 }
296 if (Model != null) {
297 m_bodyBone = Model.FindBone("Body");
298 m_neckBone = Model.FindBone("Neck", false);
299 m_headBone = Model.FindBone("Head");
300 m_leg1Bone = Model.FindBone("Leg1");
301 m_leg2Bone = Model.FindBone("Leg2");
302 m_leg3Bone = Model.FindBone("Leg3");
303 m_leg4Bone = Model.FindBone("Leg4");
304 }
305 else {
306 m_bodyBone = null;
307 m_neckBone = null;
308 m_headBone = null;
309 m_leg1Bone = null;
310 m_leg2Bone = null;
311 m_leg3Bone = null;
312 m_leg4Bone = null;
313 }
314 }
315 }
316}
static float Remainder(float x, float y)
static int Min(int x1, int x2)
static float Sigmoid(float x, float steepness)
static int Max(int x1, int x2)
static int Sqr(int x)
static float DegToRad(float degrees)
override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap)
virtual void SetBoneTransform(int boneIndex, Matrix? transformation)
ComponentFrame m_componentFrame
static float OctavedNoise(float x, float frequency, int octaves, float frequencyStep, float amplitudeStep, bool ridged=false)
ValuesDictionary ValuesDictionary
static Matrix CreateRotationX(float radians)
static Matrix CreateTranslation(float x, float y, float z)
static Matrix CreateRotationZ(float radians)
static readonly Matrix Identity
static Matrix CreateFromYawPitchRoll(float yaw, float pitch, float roll)
static Matrix CreateRotationY(float radians)
static readonly Vector2 Zero
static Vector2 Lerp(Vector2 v1, Vector2 v2, float f)
static readonly Vector3 UnitY
static float Dot(Vector3 v1, Vector3 v2)