-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlayerController.cs
610 lines (512 loc) · 18.3 KB
/
PlayerController.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
using UnityEngine;
using System.Collections.Generic;
using System.Collections;
using Rewired;
public class PlayerController : MonoBehaviour
{
public Animator animator;
public GameObject hat, hammer;
public Transform hatParentTransform, neckRigTransform;
private PlayerPickUp playerPickUp;
private FacialAnimation facialAnimation;
private Player player; //rewired player
public float speed;
public float maxSpeed;
public int playerID;
public bool holdingSomething;
public float dizzyTime;
public float burningTime;
public bool slowedEffect = false;
public bool slipperyEffect = false;
public float frictionFactor = 0.85f;
public bool isJumping;
public bool longNeck;
private Rigidbody rb;
public const float STANDARD_TURN_SPEED = 13f;
public float turnSpeed = STANDARD_TURN_SPEED;
private float maxVelocityY = 2f;
private float jumpForce = 400f;
private float jumpTime = 1.5f;
private float jumpTimeLeft = -0.01f;
private float startMaxSpeed;
private float tauntTimeLeft = -0.01f;
private float distanceBetweenFootsteps = 100f;
private float distanceUntilNextFootstep = 100f;
//disabled time means the player can't move
private float disabledTime = 0f;
public List<GameObject> nearbyInteractableList = new List<GameObject>();
public KeyCode[] actionKey, throwKey, jumpKey, falconTauntKey, pauseKey;
void Start()
{
rb = GetComponent<Rigidbody>();
playerPickUp = GetComponent<PlayerPickUp>();
facialAnimation = GetComponent<FacialAnimation>();
startMaxSpeed = maxSpeed;
SetHammerVisibility(false);
if (gameObject.tag == "Player2")
{
playerID = 1;
}
else
{
playerID = 0;
}
player = ReInput.players.GetPlayer(playerID);
}
void Update()
{
#region _PAUSE
if (CustomInputManager.GetKeyPressed(pauseKey))
{
GrayscaleEffect.Instance.FadeToAndFromGray(Time.timeScale == 1);
//show menu
if (UISettings.Instance != null)
{
UISettings.Instance.SetPauseScreenVisibility(Time.timeScale == 1);
}
Time.timeScale = 1 - Time.timeScale;
}
#endregion
#region _MOVEMENT
float moveHorizontal = 0;
float moveVertical = 0;
float lookHorizontal = 0;
float lookVertical = 0;
if (dizzyTime > 0)
{
dizzyTime -= Time.deltaTime;
animator.SetFloat("dizzyTime", dizzyTime);
}
if (burningTime > 0)
{
burningTime -= Time.deltaTime;
animator.SetFloat("burnTime", burningTime);
}
//Player can only move if not dizzy and not jumping (also when not carried)
if (burningTime > 0.1)
{
moveHorizontal = 0.5f * Mathf.Sin(5f * burningTime);
moveVertical = 0.5f * Mathf.Cos(5f * burningTime);
}
else
if (disabledTime < 0.2f && !playerPickUp.isPickedUp)
{
if (playerID == 0) //Player 1
{
moveHorizontal = player.GetAxis("Move Horizontal");
moveVertical = player.GetAxis("Move Vertical");
lookHorizontal = Input.GetAxis("P1HorizontalLook");
lookVertical = Input.GetAxis("P1VerticalLook");
}
else //Player 2
{
moveHorizontal = player.GetAxis("Move Horizontal");
moveVertical = player.GetAxis("Move Vertical");
lookHorizontal = Input.GetAxis("P2HorizontalLook");
lookVertical = Input.GetAxis("P2VerticalLook");
}
}
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
Vector3 lookDirection = new Vector3(lookHorizontal, 0.0f, lookVertical);
//normalize vector if it the magnitude is > 1 to prevent diagonal movement from being faster
if (movement.magnitude > 1)
{
movement /= movement.magnitude;
}
if (slowedEffect)
{
movement *= 0.5f;
maxSpeed = 0.5f * startMaxSpeed;
}
else
{
maxSpeed = startMaxSpeed;
}
//check if it is free in front with a raycast
float distanceToCheck = holdingSomething ? 2f : 0.1f;
var hitInFront = Physics.RaycastAll(transform.position, transform.forward, distanceToCheck * movement.magnitude);
Debug.DrawRay(transform.position, transform.forward);
//any hits with objects which are not pickups will slow down the player
bool nonPickupHit = false;
if (!holdingSomething)
{
foreach (var hit in hitInFront)
{
if (hit.transform.GetComponent<Ingredient>() == null)
{
nonPickupHit = true;
}
}
}
if (hitInFront.Length <= 1 || !nonPickupHit)
{
float timeFactor = 60f * Time.deltaTime;
rb.AddForce(timeFactor * movement * speed);
//footstep sounds
distanceUntilNextFootstep -= rb.velocity.magnitude;
if (distanceUntilNextFootstep < 0)
{
//audio
AudioManager.instance.PlayEvent("Play_footstep");
distanceUntilNextFootstep = distanceBetweenFootsteps;
GameManager.instance.globalSaveData.stats.stepsTaken++;
}
}
//limit the velocity
if (rb.velocity.magnitude > maxSpeed)
{
rb.velocity = new Vector3(rb.velocity.normalized.x * maxSpeed, rb.velocity.y, rb.velocity.normalized.z * maxSpeed);
}
//look in the direction of movement over time
if (movement.magnitude > 0.1f)
{
RotateTowardsTarget(transform.position + movement, turnSpeed);
}
//look direction override
if (lookDirection.magnitude > 0.3f && movement.magnitude < 0.2f)
{
RotateTowardsTarget(transform.position + lookDirection, turnSpeed);
}
if (rb.velocity.magnitude > 4f)
{
if (Random.Range(0, 100) > 30)
{
ParticleManager.Instance.CreateWalkParticles(transform);
}
}
//animation
if (animator != null)
{
animator.SetFloat("walkSpeed", movement.magnitude);
if (movement.magnitude > 0.1f)
{
animator.speed = 1.2f * (rb.velocity.magnitude / maxSpeed);
}
else
animator.speed = 1f;
}
//turn upright if skewed
float yRot = transform.eulerAngles.y;
Quaternion desired = Quaternion.Euler(0f, yRot, 0f);
transform.rotation = desired;
//friction
CheckIfOnIce(transform.position, 1);
float frictionAdjust = frictionFactor;
if (slipperyEffect) frictionAdjust += 0.3f;
//apply friction and prevent flying up
rb.velocity = new Vector3(frictionAdjust * rb.velocity.x, Mathf.Min(maxVelocityY, rb.velocity.y), frictionAdjust * rb.velocity.z);
#endregion
#region _INTERACTION
if (player.GetButtonDown("Action") && dizzyTime < 0.1f)
{
if (holdingSomething) //drop old interactable
{
DropObject();
}
else
if (nearbyInteractableList.Count > 0) //interactable nearby
{
//interact with objects
for (int i = 0; i < nearbyInteractableList.Count; i++)
{
//skip the element if it is null
if (nearbyInteractableList[i] != null)
{
if (nearbyInteractableList[i].GetComponent<Interactable>().interactable)
{
Interactable target = nearbyInteractableList[i].GetComponent<Interactable>();
//delete uihint
target.DeleteUIHint();
target.Interact(transform);
if (target is PickUp) {
//audio for pickups
AudioManager.instance.PlayEvent("Play_player_pickup");
}
break;
}
}
}
//pick up pickup objects
if (nearbyInteractableList[0].GetComponent<PickUp>() != null)
{
animator.SetTrigger("pickUp");
//definitely a pickup object
holdingSomething = true;
IncreaseColliderSize();
}
}
}
#endregion
#region _THROWING
if (player.GetButtonDown("Throw") && dizzyTime < 0.1f)
{
Throw();
}
#endregion
#region _JUMP
if (player.GetButtonDown("Leap") && dizzyTime < 0.1f && jumpTimeLeft < 0)
{
Jump();
}
//jump physics
if (jumpTimeLeft > 0)
{
jumpTimeLeft -= Time.deltaTime;
if (jumpTimeLeft > 1f)
{
Vector3 jumpVector;
float jumpFactor = jumpTimeLeft / jumpTime;
jumpVector = jumpFactor * jumpForce * transform.forward;
rb.AddForce(Time.timeScale * jumpVector);
facialAnimation.FlickerMaterial(jumpTimeLeft);
}
}
#endregion
#region _TAUNTS
//falcon salute
if (player.GetButtonDown("FalconTaunt") && disabledTime < 0.1f && !holdingSomething)
{
//audio
AudioManager.instance.PlayEvent("Play_player_taunt");
tauntTimeLeft = 1f;
animator.SetTrigger("falconTaunt");
}
if (tauntTimeLeft > 0)
{
//look towards world z-
RotateTowardsTarget(new Vector3(0, 0, -100), 10f);
tauntTimeLeft -= Time.deltaTime;
}
//dance
if (player.GetButton("Dance") && disabledTime < 0.1f && !holdingSomething)
{
animator.SetBool("dancing", true);
}
if (player.GetButtonUp("Dance"))
{
animator.SetBool("dancing", false);
}
#endregion
#region _IDLEBREAKUPS
bool animatorIsBusy = !animator.GetCurrentAnimatorStateInfo(4).IsName("Empty State");
if (disabledTime < 0.1f && movement.magnitude < 0.1f && !holdingSomething && !animatorIsBusy)
{
//if the player is not busy play an idle animation
if (Random.Range(0, 1000) > 994 && !animatorIsBusy)
{
//StartCoroutine(PlayIdleBreakup());
int targetIdle = Random.Range(1, 4);
animator.SetInteger("playBreakupIdle", targetIdle);
}
}
else if (animatorIsBusy && movement.magnitude > 0.5f || holdingSomething)
{
//breaking out of the idle when moving (and animator is busy with idle animation)
animator.SetInteger("playBreakupIdle", 0);
}
else if (animator.GetCurrentAnimatorStateInfo(4).normalizedTime > 0.9f)
{
//manually reset the idle animation int after normal time transition
animator.SetInteger("playBreakupIdle", 0);
}
#endregion
//calculate disabled time
disabledTime = dizzyTime + jumpTimeLeft + tauntTimeLeft;
}
//find new interactables nearby
private void OnTriggerEnter(Collider other)
{
if (other.GetComponent<Interactable>() != null)
{
if (other.GetComponent<Interactable>().interactable)
{
HandleNewInteractable(other.gameObject);
}
}
}
void HandleNewInteractable(GameObject other)
{
//add to nearby interactables list
if (!nearbyInteractableList.Contains(other.gameObject))
{
foreach (GameObject item in nearbyInteractableList)
{
if (item.GetComponent<Interactable>().uiHint != null)
{
item.GetComponent<Interactable>().uiHint.DestroySelf();
}
}
nearbyInteractableList.Clear();
nearbyInteractableList.Add(other.gameObject);
}
//create a UI hint only if the first item in the nearbyList
if (nearbyInteractableList.Count == 1)
{
if (UIHintManager.instance.ShouldCreateUIHint(other.GetComponent<Interactable>()))
{
UIHintManager.instance.CreateUIHint(transform, other.transform);
}
}
}
//clear old ones when leaving trigger
void OnTriggerExit(Collider other)
{
if (nearbyInteractableList.Contains(other.gameObject))
{
nearbyInteractableList.Remove(other.gameObject);
//turn off the uihint
if (!CheckOtherPlayerHasInteractableNearby(other.gameObject))
{
other.GetComponent<Interactable>().DeleteUIHint();
}
}
}
public void RemoveFromInteractableList(GameObject go)
{
nearbyInteractableList.Remove(go);
}
public static void RemoveFromBothInteractableLists(GameObject go)
{
GameController.instance.player1.RemoveFromInteractableList(go);
GameController.instance.player2.RemoveFromInteractableList(go);
}
public void AddDizzyTime(float timeToAdd)
{
if (dizzyTime < 0.1f && jumpTimeLeft < 0.1f)
{
Debug.Log("Added Dizzy Time");
dizzyTime += timeToAdd;
ParticleManager.Instance.CreateStunParticle(transform); //stun particles
GetComponent<FacialAnimation>().ChangeFaceToHit(); //Facial animation
ForceFeedback.Vibrate(player); //vibrate
if (CreditsController.Instance != null)
{
CreditsController.Instance.UpdatePlayerScore(1 - playerID);
}
//drop the held object too
DropObject();
//drop self if held
playerPickUp.ReverToState();
//audio
AudioManager.instance.PlayEvent("Play_dizzy");
}
}
public void AddBurnTime(float timeToAdd)
{
if (burningTime < 0.1f && jumpTimeLeft < 0.1f)
{
Debug.Log("Added Burning Time");
burningTime += timeToAdd;
ParticleManager.Instance.CreateStunParticle(transform); //stun particles
GetComponent<FacialAnimation>().ChangeFaceToHit(); //Facial animation
ForceFeedback.Vibrate(player); //vibrate
if (CreditsController.Instance != null)
{
CreditsController.Instance.UpdatePlayerScore(1 - playerID);
}
//drop the held object too
DropObject();
//drop self if held
playerPickUp.ReverToState();
//audio
AudioManager.instance.PlayEvent("Play_dizzy");
}
}
public void DropObject()
{
//audio
AudioManager.instance.PlayEvent("Play_player_drop");
animator.SetTrigger("putDown");
PickUp[] interactables = GameObjectExtensions.GetComponentsInDirectChildren<PickUp>(gameObject);
foreach (PickUp pickUp in interactables)
{
pickUp.PutDown();
}
holdingSomething = false;
ResetColliderSize();
}
private void Throw()
{
if (holdingSomething)
{
//audio
AudioManager.instance.PlayEvent("Play_player_throw");
animator.SetTrigger("throw");
List<PickUp> interactables = new List<PickUp>(GetComponentsInChildren<PickUp>());
interactables.Remove(GetComponent<PlayerPickUp>()); //remove player pickup
foreach (PickUp pickUp in interactables)
pickUp.Throw();
holdingSomething = false;
ResetColliderSize();
}
}
public void IncreaseColliderSize()
{
GetComponent<CapsuleCollider>().radius = 1;
}
public void ResetColliderSize()
{
GetComponent<CapsuleCollider>().radius = 0.5f;
}
public bool CheckOtherPlayerHasInteractableNearby(GameObject goToCheck)
{
//does not work, refactor.
PlayerController otherPlayer;
if (GameController.instance != null)
{
otherPlayer = (playerID == 0) ? GameController.instance.player2 : GameController.instance.player1;
}
else if (TrainStation.instance != null)
{
otherPlayer = (playerID == 0) ? TrainStation.instance.player2 : TrainStation.instance.player1;
}
else
{
otherPlayer = (playerID == 0) ? CreditsController.Instance.player2 : CreditsController.Instance.player1;
}
return otherPlayer.nearbyInteractableList.Contains(goToCheck);
}
private void Jump()
{
if (!playerPickUp.isPickedUp)
{
//audio
AudioManager.instance.PlayEvent("Play_player_leap");
animator.SetTrigger("jump");
jumpTimeLeft = jumpTime;
Throw();
}
}
private void RotateTowardsTarget(Vector3 target, float speed)
{
Vector3 targetPos = target;
Vector3 relativePos = targetPos - transform.position;
Quaternion desiredRot = Quaternion.LookRotation(relativePos, Vector3.up);
Quaternion newRot = Quaternion.Lerp(transform.rotation, desiredRot, speed * Time.deltaTime);
transform.rotation = newRot;
}
public void SetHammerVisibility(bool visible)
{
hammer.SetActive(visible);
}
//figure out if on ice
void CheckIfOnIce(Vector3 center, float radius)
{
Collider[] hitColliders = Physics.OverlapSphere(center, radius);
int i = 0;
while (i < hitColliders.Length)
{
if (hitColliders[i].GetComponent<Ice>())
{
slipperyEffect = true;
return;
}
i++;
}
slipperyEffect = false;
}
public void UpdateBeingCarriedAnimation(bool value)
{
animator.SetBool("isGrabbed", value);
}
}