-
Notifications
You must be signed in to change notification settings - Fork 0
/
PipeMazePlayerMachine.cs
executable file
·355 lines (291 loc) · 9.1 KB
/
PipeMazePlayerMachine.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
using UnityEngine;
using System.Collections;
/*
* Example implementation of the SuperStateMachine and SuperCharacterController
*/
[RequireComponent(typeof(SuperCharacterController))]
[RequireComponent(typeof(PipeMazePlayerInput))]
public class PipeMazePlayerMachine : SuperStateMachine
{
public Transform AnimatedMesh;
public Rigidbody rb;
private Animator Animator;
public float WalkSpeed = 4.0f;
public float WalkAcceleration = 30.0f;
public float RunSpeed = 4.0f;
public float RunAcceleration = 8.0f;
public float JumpAcceleration = 5.0f;
public float JumpHeight = 0.5f;
public float Gravity = 25.0f;
// Add more states by comma separating them
enum PlayerStates { Idle, Walk, Run, Dive, Jump, Fall, Float }
public SuperCharacterController controller;
// current velocity
private Vector3 moveDirection;
// current direction our character's art is facing
public Vector3 lookDirection { get; private set; }
private PipeMazePlayerInput input;
void Awake()
{
this.rb = this.GetComponent<Rigidbody>();
this.Animator = this.GetComponent<Animator>();
}
void Start()
{
// Put any code here you want to run ONCE, when the object is initialized
input = gameObject.GetComponent<PipeMazePlayerInput>();
// Grab the controller object from our object
controller = gameObject.GetComponent<SuperCharacterController>();
// Our character's current facing direction, planar to the ground
lookDirection = transform.forward;
// Set our currentState to idle on startup
currentState = PlayerStates.Idle;
}
protected override void EarlyGlobalSuperUpdate()
{
// Rotate out facing direction horizontally based on mouse input
// Put any code in here you want to run BEFORE the state's update function.
// This is run regardless of what state you're in
//lookDirection = Quaternion.AngleAxis(input.Current.MouseInput.x, controller.up) * lookDirection;
}
protected override void LateGlobalSuperUpdate()
{
// Put any code in here you want to run AFTER the state's update function.
// This is run regardless of what state you're in
// Move the player by our velocity every frame
transform.position += moveDirection * controller.deltaTime;
// Rotate our mesh to face where we are "looking"
AnimatedMesh.rotation = Quaternion.LookRotation(lookDirection, controller.up);
}
public bool AcquiringGround()
{
return controller.currentGround.IsGrounded(false, 0.01f);
}
public bool MaintainingGround()
{
return controller.currentGround.IsGrounded(true, 0.2f);
}
public void RotateGravity(Vector3 up)
{
lookDirection = Quaternion.FromToRotation(transform.up, up) * lookDirection;
}
/// <summary>
/// Constructs a vector representing our movement local to our lookDirection, which is
/// controlled by the camera
/// </summary>
private Vector3 LocalMovement()
{
Vector3 right = Vector3.Cross(controller.up, lookDirection);
Vector3 local = Vector3.zero;
if (input.Current.Run)
{
local += lookDirection * 1.0f;
}
return local.normalized;
}
// Calculate the initial velocity of a jump based off gravity and desired maximum height attained
private float CalculateJumpSpeed(float jumpHeight, float gravity)
{
return Mathf.Sqrt(2 * jumpHeight * gravity);
}
private IEnumerator EnableBool( string key, float time, System.Action act=null )
{
yield return new WaitForSeconds(time);
this.Animator.SetBool(key, true);
if (act != null)
act();
}
private IEnumerator DisableBool( string key, float time, System.Action act=null )
{
yield return new WaitForSeconds(time);
this.Animator.SetBool(key, false);
if (act != null)
act();
}
/*void Update () {
* Update is normally run once on every frame update. We won't be using it
* in this case, since the SuperCharacterController component sends a callback Update
* called SuperUpdate. SuperUpdate is recieved by the SuperStateMachine, and then fires
* further callbacks depending on the state
}*/
// Below are the three state functions. Each one is called based on the name of the state,
// so when currentState = Idle, we call Idle_EnterState. If currentState = Jump, we call
// Jump_SuperUpdate()
void Idle_EnterState()
{
controller.EnableSlopeLimit();
controller.EnableClamping();
}
void Idle_SuperUpdate()
{
// Run every frame we are in the idle state
if (input.Current.Jump)
{
currentState = PlayerStates.Jump;
return;
}
if (input.Current.Dive)
{
currentState = PlayerStates.Dive;
return;
}
if (!MaintainingGround())
{
currentState = PlayerStates.Float;
return;
}
if (input.Current.Walk)
{
currentState = PlayerStates.Walk;
return;
}
if (input.Current.Run)
{
currentState = PlayerStates.Run;
return;
}
// Apply friction to slow us to a halt
moveDirection = Vector3.MoveTowards(moveDirection, Vector3.zero, 20.0f * controller.deltaTime);
}
void Idle_ExitState()
{
// Run once when we exit the idle state
}
void Walk_EnterState()
{
this.Animator.SetBool("Walk", true);
}
void Walk_ExitState()
{
this.Animator.SetBool("Walk", false);
}
void Walk_SuperUpdate()
{
if (input.Current.Jump)
{
currentState = PlayerStates.Jump;
return;
}
if (input.Current.Dive)
{
currentState = PlayerStates.Dive;
return;
}
if (!MaintainingGround())
{
currentState = PlayerStates.Float;
return;
}
if (input.Current.Walk)
{
moveDirection = Vector3.MoveTowards(moveDirection, LocalMovement() * WalkSpeed, WalkAcceleration * controller.deltaTime);
}
else
{
currentState = PlayerStates.Idle;
return;
}
}
void Run_EnterState()
{
this.Animator.SetBool("Run", true);
}
void Run_ExitState()
{
this.Animator.SetBool("Run", false);
}
void Run_SuperUpdate()
{
if (input.Current.Jump)
{
currentState = PlayerStates.Jump;
return;
}
if (input.Current.Dive)
{
currentState = PlayerStates.Dive;
return;
}
if (!MaintainingGround())
{
currentState = PlayerStates.Fall;
return;
}
if (input.Current.Run)
{
if (this.Animator.GetCurrentAnimatorStateInfo(0).IsName("Run"))
{
moveDirection = Vector3.MoveTowards(moveDirection, LocalMovement() * RunSpeed, RunAcceleration * controller.deltaTime);
}
}
else
{
currentState = PlayerStates.Idle;
return;
}
}
void Jump_EnterState()
{
controller.DisableClamping();
controller.DisableSlopeLimit();
this.Animator.SetBool("Jump", true);
this.StartCoroutine(DisableBool("Jump", 0.1f));
}
void Jump_SuperUpdate()
{
if (!this.Animator.GetCurrentAnimatorStateInfo(0).IsName("Jump"))
//if (Animator.GetCurrentAnimatorStateInfo(0).normalizedTime > 1 && !Animator.IsInTransition(0))
{
currentState = PlayerStates.Idle;
}
// Apply friction to slow us to a halt
moveDirection = Vector3.MoveTowards(moveDirection, Vector3.zero, 5.0f * controller.deltaTime);
}
void Dive_EnterState()
{
controller.DisableClamping();
controller.DisableSlopeLimit();
this.Animator.SetBool("Dive", true);
this.StartCoroutine(DisableBool("Dive", 0.1f));
}
void Dive_SuperUpdate()
{
if (!this.Animator.GetCurrentAnimatorStateInfo(0).IsName("Dive"))
//if (Animator.GetCurrentAnimatorStateInfo(0).normalizedTime > 1 && !Animator.IsInTransition(0))
{
currentState = PlayerStates.Idle;
}
// Apply friction to slow us to a halt
moveDirection = Vector3.MoveTowards(moveDirection, Vector3.zero, 5.0f * controller.deltaTime);
}
void Float_EnterState()
{
controller.DisableClamping();
controller.DisableSlopeLimit();
}
void Float_SuperUpdate()
{
if (AcquiringGround())
{
moveDirection = Math3d.ProjectVectorOnPlane(controller.up, moveDirection);
currentState = PlayerStates.Idle;
return;
}
}
void Fall_EnterState()
{
controller.DisableClamping();
controller.DisableSlopeLimit();
// moveDirection = trueVelocity;
}
void Fall_SuperUpdate()
{
if (AcquiringGround())
{
moveDirection = Math3d.ProjectVectorOnPlane(controller.up, moveDirection);
currentState = PlayerStates.Idle;
return;
}
moveDirection -= controller.up * Gravity * controller.deltaTime;
}
}