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