]> git.cameronkatri.com Git - gmtk-gamejam.git/blob - Assets/Scripts/Controllers/Player/PlayerController.cs
Hold ball away from face
[gmtk-gamejam.git] / Assets / Scripts / Controllers / Player / PlayerController.cs
1 using System;
2 using System.Linq;
3 using Controllers.Player;
4 using UnityEngine;
5 using Random = UnityEngine.Random;
6
7 namespace Controllers.Player
8 {
9 public class PlayerController : MonoBehaviour
10 {
11 [SerializeField] private PlayerSegment[] segments;
12 [SerializeField] private float sensitivity;
13 [SerializeField] private float verticalSensitivity;
14
15 [SerializeField] private GameController game;
16
17 [SerializeField] private Controls controls;
18
19 [SerializeField] private float maxLegAngle;
20 [SerializeField] private float legSpeed;
21
22 public PlayerSegment root => segments[0];
23
24 private float ShootStartTime;
25
26 private void Awake()
27 {
28 foreach (var segment in segments) {
29 segment.sensitivity = sensitivity;
30 segment.verticalSensitivity = verticalSensitivity;
31 }
32 }
33
34 private void Update()
35 {
36 if (game.freezeMotion) return;
37
38 // Loop over each body segment and control it.
39 for (var i = 0; i < segments.Length; i++)
40 {
41 var segment = segments[i];
42
43 // Flip the player to face the right direction.
44 segment.flipper.transform.localScale = root.Rigidbody.velocity.x > 0.01f ? new Vector3(1f, 1f, 1f) : new Vector3(-1f, 1f, 1f);
45
46 // Update icons
47 foreach (var control in new[] {segment.left, segment.right})
48 {
49 if (Input.GetKeyDown(control.keyCode))
50 control.icon.SetActive(true);
51 else if (Input.GetKeyUp(control.keyCode))
52 control.icon.SetActive(false);
53 }
54
55 // Add forces
56 segment.moveleft = Input.GetKey(segment.left.keyCode);
57 segment.moveright = Input.GetKey(segment.right.keyCode);
58
59 // Rotate the legs if we move.
60 if (segment.isRoot)
61 {
62 if (Input.GetKey(segment.left.keyCode) || Input.GetKey(segment.right.keyCode))
63 {
64 segment.left.leg.transform.localRotation = Quaternion.Slerp(
65 segment.left.leg.transform.localRotation,
66 Quaternion.Euler(0, 0, maxLegAngle * Mathf.Sin(Time.time * legSpeed * (segment.Rigidbody.velocity.magnitude / 5f))),
67 Time.deltaTime * 50f
68 );
69 segment.right.leg.transform.localRotation = Quaternion.Slerp(
70 segment.right.leg.transform.localRotation,
71 Quaternion.Euler(0, 0, maxLegAngle * Mathf.Sin(Time.time * -legSpeed * (segment.Rigidbody.velocity.magnitude / 5f))),
72 Time.deltaTime * 50f
73 );
74 }
75 else
76 {
77 segment.left.leg.transform.localRotation = Quaternion.Slerp(segment.left.leg.transform.localRotation, Quaternion.identity, Time.deltaTime * legSpeed);
78 segment.right.leg.transform.localRotation = Quaternion.Slerp(segment.right.leg.transform.localRotation, Quaternion.identity, Time.deltaTime * legSpeed);
79 }
80 }
81
82 if (segment.isTop)
83 {
84 game.player.Move(segment.transform.position + segment.transform.right * (0.5f * (root.Rigidbody.velocity.x > 0.01f ? 1 : -1)));
85
86 // Point the arms at the ball.
87 segment.left.arm.transform.up = -(game.ball.transform.position - segment.left.arm.transform.position);
88 segment.right.arm.transform.up = -(game.ball.transform.position - segment.right.arm.transform.position);
89 }
90 }
91
92 // Try grabbing the ball if pressed.
93 if (Input.GetKeyDown(controls.grab))
94 {
95 // Try grabbing at each segment, and then the segment can pass it up to the top guy.
96 if (game.enemy.HasBall && Random.Range(0, 100) < 50) return; // 50% chance of stealing.
97 foreach (var segment in segments)
98 {
99 if (game.player.GrabBall(segment.transform.position)) break; // Stop after we successfully grab.
100 }
101 }
102
103 // Try shooting the ball if pressed.
104 if (Input.GetKeyDown(controls.shoot))
105 ShootStartTime = Time.time;
106
107 if (Input.GetKeyUp(controls.shoot))
108 game.player.Shoot(segments.Last().transform, Time.time - ShootStartTime);
109 }
110
111 [Serializable]
112 private struct Controls
113 {
114 [SerializeField] internal KeyCode grab;
115 [SerializeField] internal KeyCode shoot;
116 }
117 }
118 }