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 // Loop over each body segment and control it.
37 for (var i = 0; i < segments.Length; i++)
39 var segment = segments[i];
41 // Flip the player to face the right direction.
42 segment.flipper.transform.localScale = root.Rigidbody.velocity.x > 0.01f ? new Vector3(1f, 1f, 1f) : new Vector3(-1f, 1f, 1f);
45 foreach (var control in new[] {segment.left, segment.right})
47 if (Input.GetKeyDown(control.keyCode))
48 control.icon.SetActive(true);
49 else if (Input.GetKeyUp(control.keyCode))
50 control.icon.SetActive(false);
54 segment.moveleft = Input.GetKey(segment.left.keyCode);
55 segment.moveright = Input.GetKey(segment.right.keyCode);
57 // Rotate the legs if we move.
60 if (Input.GetKey(segment.left.keyCode) || Input.GetKey(segment.right.keyCode))
62 segment.left.leg.transform.localRotation = Quaternion.Slerp(
63 segment.left.leg.transform.localRotation,
64 Quaternion.Euler(0, 0, maxLegAngle * Mathf.Sin(Time.time * legSpeed * (segment.Rigidbody.velocity.magnitude / 5f))),
67 segment.right.leg.transform.localRotation = Quaternion.Slerp(
68 segment.right.leg.transform.localRotation,
69 Quaternion.Euler(0, 0, maxLegAngle * Mathf.Sin(Time.time * -legSpeed * (segment.Rigidbody.velocity.magnitude / 5f))),
75 segment.left.leg.transform.localRotation = Quaternion.Slerp(segment.left.leg.transform.localRotation, Quaternion.identity, Time.deltaTime * legSpeed);
76 segment.right.leg.transform.localRotation = Quaternion.Slerp(segment.right.leg.transform.localRotation, Quaternion.identity, Time.deltaTime * legSpeed);
82 game.player.Move(segment.transform.position);
84 // Point the arms at the ball.
85 segment.left.arm.transform.up = -(game.ball.transform.position - segment.left.arm.transform.position);
86 segment.right.arm.transform.up = -(game.ball.transform.position - segment.right.arm.transform.position);
90 // Try grabbing the ball if pressed.
91 if (Input.GetKeyDown(controls.grab))
93 // Try grabbing at each segment, and then the segment can pass it up to the top guy.
94 if (game.enemy.HasBall && Random.Range(0, 100) < 50) return; // 50% chance of stealing.
95 foreach (var segment in segments)
97 if (game.player.GrabBall(segment.transform.position)) break; // Stop after we successfully grab.
101 // Try shooting the ball if pressed.
102 if (Input.GetKeyDown(controls.shoot))
103 ShootStartTime = Time.time;
105 if (Input.GetKeyUp(controls.shoot))
106 game.player.Shoot(segments.Last().transform, ShootStartTime - Time.time);
110 private struct Controls
112 [SerializeField] internal KeyCode grab;
113 [SerializeField] internal KeyCode shoot;