3 using Controllers.Player;
5 using Random = UnityEngine.Random;
7 namespace Controllers.Player
9 public class PlayerController : MonoBehaviour
11 [SerializeField] private PlayerSegment[] segments;
12 [SerializeField] private float sensitivity;
13 [SerializeField] private float verticalSensitivity;
15 [SerializeField] private GameController game;
17 [SerializeField] private Controls controls;
19 [SerializeField] private float maxLegAngle;
20 [SerializeField] private float legSpeed;
22 [SerializeField] private GameObject progressBarContainer;
23 [SerializeField] private GameObject progressBar;
25 public PlayerSegment root => segments[0];
27 private float ShootStartTime;
31 foreach (var segment in segments) {
32 segment.sensitivity = sensitivity;
33 segment.verticalSensitivity = verticalSensitivity;
39 if (game.freezeMotion) return;
41 // Loop over each body segment and control it.
42 for (var i = 0; i < segments.Length; i++)
44 var segment = segments[i];
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);
50 foreach (var control in new[] {segment.left, segment.right})
52 if (Input.GetKeyDown(control.keyCode))
53 control.icon.SetActive(true);
54 else if (Input.GetKeyUp(control.keyCode))
55 control.icon.SetActive(false);
59 segment.moveleft = Input.GetKey(segment.left.keyCode);
60 segment.moveright = Input.GetKey(segment.right.keyCode);
62 // Rotate the legs if we move.
65 if (Input.GetKey(segment.left.keyCode) || Input.GetKey(segment.right.keyCode))
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))),
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))),
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);
87 game.player.Move(segment.transform.position + segment.transform.right * (0.5f * (root.Rigidbody.velocity.x > 0.01f ? 1 : -1)));
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);
95 // Try grabbing the ball if pressed.
96 if (Input.GetKeyDown(controls.grab))
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)
102 if (game.player.GrabBall(segment.transform.position)) break; // Stop after we successfully grab.
106 // Try shooting the ball if pressed.
107 if (Input.GetKeyDown(controls.shoot))
109 ShootStartTime = Time.time;
110 progressBarContainer.SetActive(true);
113 progressBar.transform.localScale = new Vector3(0.75f * Mathf.Clamp(Time.time - ShootStartTime, 0f, 1f), 0.05f, 1f);
114 // progressBar.transform.localPosition = new Vector3(0.75f * (1 - Mathf.Clamp(Time.time - ShootStartTime, 0f, 1f)), 0f, 0f);
116 if (Input.GetKeyUp(controls.shoot))
118 game.player.Shoot(segments.Last().transform, Time.time - ShootStartTime);
119 progressBarContainer.SetActive(false);
124 private struct Controls
126 [SerializeField] internal KeyCode grab;
127 [SerializeField] internal KeyCode shoot;