]> git.cameronkatri.com Git - gmtk-gamejam.git/blob - Assets/Scripts/Controllers/Player/PlayerController.cs
8aa0f2523d40cc3c5b2785a9d5cad48ebfab8d51
[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 public PlayerSegment root => segments[0];
23
24 private void Awake()
25 {
26 foreach (var segment in segments) {
27 segment.sensitivity = sensitivity;
28 segment.verticalSensitivity = verticalSensitivity;
29 }
30 }
31
32 private void Update()
33 {
34 // Loop over each body segment and control it.
35 for (var i = 0; i < segments.Length; i++)
36 {
37 var segment = segments[i];
38
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);
41
42 // Update icons
43 foreach (var control in new[] {segment.left, segment.right})
44 {
45 if (Input.GetKeyDown(control.keyCode))
46 control.icon.SetActive(true);
47 else if (Input.GetKeyUp(control.keyCode))
48 control.icon.SetActive(false);
49 }
50
51 // Add forces
52 segment.moveleft = Input.GetKey(segment.left.keyCode);
53 segment.moveright = Input.GetKey(segment.right.keyCode);
54
55 // Rotate the legs if we move.
56 if (segment.isRoot)
57 {
58 if (Input.GetKey(segment.left.keyCode) || Input.GetKey(segment.right.keyCode))
59 {
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))),
63 Time.deltaTime * 50f
64 );
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))),
68 Time.deltaTime * 50f
69 );
70 }
71 else
72 {
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);
75 }
76 }
77
78 if (segment.isTop)
79 {
80 game.player.Move(segment.transform.position);
81
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);
85 }
86 }
87
88 // Try grabbing the ball if pressed.
89 if (Input.GetKeyDown(controls.grab))
90 {
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)
94 {
95 if (game.player.GrabBall(segment.transform.position)) break; // Stop after we successfully grab.
96 }
97 }
98
99 // Try shooting the ball if pressed.
100 if (Input.GetKeyDown(controls.shoot))
101 game.player.Shoot(segments.Last().transform);
102 }
103
104 [Serializable]
105 private struct Controls
106 {
107 [SerializeField] internal KeyCode grab;
108 [SerializeField] internal KeyCode shoot;
109 }
110 }
111 }