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];
26 foreach (var segment in segments) {
27 segment.sensitivity = sensitivity;
28 segment.verticalSensitivity = verticalSensitivity;
34 // Loop over each body segment and control it.
35 for (var i = 0; i < segments.Length; i++)
37 var segment = segments[i];
39 // Flip the player to face the right direction.
40 segment.flipper.transform.localScale = root.Rigidbody.velocity.x > 0.01f ? new Vector3(1f, 1f, 1f) : new Vector3(-1f, 1f, 1f);
43 foreach (var control in new[] {segment.left, segment.right})
45 if (Input.GetKeyDown(control.keyCode))
46 control.icon.SetActive(true);
47 else if (Input.GetKeyUp(control.keyCode))
48 control.icon.SetActive(false);
52 segment.moveleft = Input.GetKey(segment.left.keyCode);
53 segment.moveright = Input.GetKey(segment.right.keyCode);
55 // Rotate the legs if we move.
58 if (Input.GetKey(segment.left.keyCode) || Input.GetKey(segment.right.keyCode))
60 segment.left.leg.transform.localRotation = Quaternion.Slerp(
61 segment.left.leg.transform.localRotation,
62 Quaternion.Euler(0, 0, maxLegAngle * Mathf.Sin(Time.time * legSpeed * (segment.Rigidbody.velocity.magnitude / 5f))),
65 segment.right.leg.transform.localRotation = Quaternion.Slerp(
66 segment.right.leg.transform.localRotation,
67 Quaternion.Euler(0, 0, maxLegAngle * Mathf.Sin(Time.time * -legSpeed * (segment.Rigidbody.velocity.magnitude / 5f))),
73 segment.left.leg.transform.localRotation = Quaternion.Slerp(segment.left.leg.transform.localRotation, Quaternion.identity, Time.deltaTime * legSpeed);
74 segment.right.leg.transform.localRotation = Quaternion.Slerp(segment.right.leg.transform.localRotation, Quaternion.identity, Time.deltaTime * legSpeed);
80 game.player.Move(segment.transform.position);
82 // Point the arms at the ball.
83 segment.left.arm.transform.up = -(game.ball.transform.position - segment.left.arm.transform.position);
84 segment.right.arm.transform.up = -(game.ball.transform.position - segment.right.arm.transform.position);
88 // Try grabbing the ball if pressed.
89 if (Input.GetKeyDown(controls.grab))
91 // Try grabbing at each segment, and then the segment can pass it up to the top guy.
92 if (game.enemy.HasBall && Random.Range(0, 100) < 50) return; // 50% chance of stealing.
93 foreach (var segment in segments)
95 if (game.player.GrabBall(segment.transform.position)) break; // Stop after we successfully grab.
99 // Try shooting the ball if pressed.
100 if (Input.GetKeyDown(controls.shoot))
101 game.player.Shoot(segments.Last().transform);
105 private struct Controls
107 [SerializeField] internal KeyCode grab;
108 [SerializeField] internal KeyCode shoot;