7 /// The controller for the basketball-game logic.
9 public class GameController : MonoBehaviour
11 private State state = State.JumpBall; // A basketball game always starts with a jump ball.
17 /// The single ball for the game.
19 [SerializeField] public Ball ball;
21 [SerializeField] public Hoop PlayerHoop;
22 [SerializeField] public Hoop EnemyHoop;
26 player = new Player { isEnemy = false, controller = this };
27 enemy = new Player { isEnemy = true, controller = this };
28 ball.controller = this;
34 /// Whether this player is the AI-enemy.
36 internal bool isEnemy;
39 /// A back-reference to the containing GameController.
41 internal GameController controller;
45 private Vector2 lastShotPosition;
46 public void Score(Vector2 Rim)
48 if (Vector2.Distance(lastShotPosition, Rim) >= 1)
51 Debug.Log("Three point");
56 Debug.Log("Two point");
60 private State dribble => isEnemy ? State.EnemyDribble : State.PlayerDribble;
61 private State shoot => isEnemy ? State.EnemyShoot : State.PlayerShoot;
63 private Hoop hoop => isEnemy ? controller.EnemyHoop : controller.PlayerHoop;
65 public bool HasBall => controller.state == dribble;
68 /// When dribbling, move the ball with the player.
70 /// <param name="handPosition">The position of the hand dribbling the ball.</param>
71 public void Move(Vector2 handPosition)
73 if (controller.state == (isEnemy ? State.EnemyDribble : State.PlayerDribble)) // Make sure they're dribbling.
74 controller.ball.transform.position = handPosition; // TODO: Make this perform a dribbling motion, otherwise it looks like they're travelling.
78 /// Grab the ball if possible given the current game state.
80 /// <param name="handPosition">The position of the hand to attempt grabbing from.</param>
81 /// <returns>Whether or not the ball was able to be picked up.</returns>
82 public bool GrabBall(Vector2 handPosition)
84 // 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.
85 if (controller.state == shoot || controller.state == dribble) return false;
87 // Make sure its within their grab area.
88 if (Vector2.Distance(controller.ball.transform.position, handPosition) >= 1f) return false;
90 controller.state = dribble;
96 /// Shoot the ball if possible.
98 /// <param name="playerTransform"></param>
99 /// <returns>Whether or not the ball was shot</returns>
100 public bool Shoot(Transform playerTransform)
102 if (controller.state != dribble) return false; // We must be dribbling the ball to shoot it.
103 controller.state = shoot;
104 controller.ball.Shoot(hoop.transform.position);
105 lastShotPosition = playerTransform.position;
110 internal void BallDropped()
126 internal static class GameControllerStateExtensions
128 internal static bool IsShot(this GameController.State state)
130 return state == GameController.State.EnemyShoot || state == GameController.State.PlayerShoot;
133 internal static bool IsDribble(this GameController.State state)
135 return state == GameController.State.EnemyDribble || state == GameController.State.PlayerDribble;