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