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 public PlayerSegment root => segments[0];
24 private float ShootStartTime;
28 foreach (var segment in segments) {
29 segment.sensitivity = sensitivity;
30 segment.verticalSensitivity = verticalSensitivity;
36 if (game.freezeMotion) return;
38 // Loop over each body segment and control it.
39 for (var i = 0; i < segments.Length; i++)
41 var segment = segments[i];
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);
47 foreach (var control in new[] {segment.left, segment.right})
49 if (Input.GetKeyDown(control.keyCode))
50 control.icon.SetActive(true);
51 else if (Input.GetKeyUp(control.keyCode))
52 control.icon.SetActive(false);
56 segment.moveleft = Input.GetKey(segment.left.keyCode);
57 segment.moveright = Input.GetKey(segment.right.keyCode);
59 // Rotate the legs if we move.
62 if (Input.GetKey(segment.left.keyCode) || Input.GetKey(segment.right.keyCode))
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))),
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))),
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);
84 game.player.Move(segment.transform.position + segment.transform.right * (0.5f * (root.Rigidbody.velocity.x > 0.01f ? 1 : -1)));
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);
92 // Try grabbing the ball if pressed.
93 if (Input.GetKeyDown(controls.grab))
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)
99 if (game.player.GrabBall(segment.transform.position)) break; // Stop after we successfully grab.
103 // Try shooting the ball if pressed.
104 if (Input.GetKeyDown(controls.shoot))
105 ShootStartTime = Time.time;
107 if (Input.GetKeyUp(controls.shoot))
108 game.player.Shoot(segments.Last().transform, Time.time - ShootStartTime);
112 private struct Controls
114 [SerializeField] internal KeyCode grab;
115 [SerializeField] internal KeyCode shoot;