]> git.cameronkatri.com Git - gmtk-gamejam.git/blob - Assets/Scripts/Controllers/GameController.cs
Resolve conflicts
[gmtk-gamejam.git] / Assets / Scripts / Controllers / GameController.cs
1 using System;
2 using UnityEngine;
3
4 namespace Controllers
5 {
6 /// <summary>
7 /// The controller for the basketball-game logic.
8 /// </summary>
9 public class GameController : MonoBehaviour
10 {
11 private State state = State.JumpBall; // A basketball game always starts with a jump ball.
12
13 public Player player;
14 public Player enemy;
15
16 /// <summary>
17 /// The single ball for the game.
18 /// </summary>
19 [SerializeField] public Ball ball;
20
21 [SerializeField] public Hoop PlayerHoop;
22 [SerializeField] public Hoop EnemyHoop;
23
24 private void Awake()
25 {
26 player = new Player { isEnemy = false, controller = this };
27 enemy = new Player { isEnemy = true, controller = this };
28 ball.controller = this;
29 }
30
31 public struct Player
32 {
33 /// <summary>
34 /// Whether this player is the AI-enemy.
35 /// </summary>
36 internal bool isEnemy;
37
38 /// <summary>
39 /// A back-reference to the containing GameController.
40 /// </summary>
41 internal GameController controller;
42
43 internal int score;
44
45 private Vector2 lastShotPosition;
46 public void Score(Vector2 Rim) {
47 if (Vector2.Distance(lastShotPosition, Rim) >= 1)
48 {
49 score += 3;
50 Debug.Log("Three point");
51 } else
52 score += 2;
53 Debug.Log("Two point");
54 }
55
56 private State dribble => isEnemy ? State.EnemyDribble : State.PlayerDribble;
57 private State shoot => isEnemy ? State.EnemyShoot : State.PlayerShoot;
58
59 public bool HasBall => controller.state == dribble;
60
61 /// <summary>
62 /// When dribbling, move the ball with the player.
63 /// </summary>
64 /// <param name="handPosition">The position of the hand dribbling the ball.</param>
65 public void Move(Vector2 handPosition)
66 {
67 if (controller.state == (isEnemy ? State.EnemyDribble : State.PlayerDribble)) // Make sure they're dribbling.
68 controller.ball.transform.position = handPosition; // TODO: Make this perform a dribbling motion, otherwise it looks like they're travelling.
69 }
70
71 /// <summary>
72 /// Grab the ball if possible given the current game state.
73 /// </summary>
74 /// <param name="handPosition">The position of the hand to attempt grabbing from.</param>
75 /// <returns>Whether or not the ball was able to be picked up.</returns>
76 public bool GrabBall(Vector2 handPosition)
77 {
78 // Don't allow the ball to be picked up if someone shot it. Also don't try picking it up if we're already holding it.
79 if (controller.state.IsShot() || controller.state == dribble) return false;
80
81 // Make sure its within their grab area.
82 if (Vector2.Distance(controller.ball.transform.position, handPosition) > 0.75f) return false;
83
84 controller.state = dribble;
85 Move(handPosition);
86 return true;
87 }
88
89 /// <summary>
90 /// Shoot the ball if possible.
91 /// </summary>
92 /// <param name="playerTransform"></param>
93 /// <returns>Whether or not the ball was shot</returns>
94 public bool Shoot(Transform playerTransform)
95 {
96 if (controller.state != dribble) return false; // We must be dribbling the ball to shoot it.
97 controller.state = shoot;
98 controller.ball.Shoot(playerTransform);
99 lastShotPosition = playerTransform.position;
100 return true;
101 }
102 }
103
104 internal void BallDropped()
105 {
106 state = State.Idle;
107 }
108
109 internal enum State
110 {
111 Idle,
112 JumpBall,
113 PlayerDribble,
114 PlayerShoot,
115 EnemyDribble,
116 EnemyShoot,
117 }
118 }
119
120 internal static class GameControllerStateExtensions
121 {
122 internal static bool IsShot(this GameController.State state)
123 {
124 return state == GameController.State.EnemyShoot || state == GameController.State.PlayerShoot;
125 }
126
127 internal static bool IsDribble(this GameController.State state)
128 {
129 return state == GameController.State.EnemyDribble || state == GameController.State.PlayerDribble;
130 }
131 }
132 }