]> git.cameronkatri.com Git - gmtk-gamejam.git/blob - Assets/Scripts/Controllers/GameController.cs
Tweak AI
[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 {
48 if (Vector2.Distance(lastShotPosition, Rim) >= 1)
49 {
50 score += 3;
51 Debug.Log("Three point");
52 }
53 else
54 {
55 score += 2;
56 Debug.Log("Two point");
57 }
58 }
59
60 private State dribble => isEnemy ? State.EnemyDribble : State.PlayerDribble;
61 private State shoot => isEnemy ? State.EnemyShoot : State.PlayerShoot;
62
63 private Hoop hoop => isEnemy ? controller.EnemyHoop : controller.PlayerHoop;
64
65 public bool HasBall => controller.state == dribble;
66
67 /// <summary>
68 /// When dribbling, move the ball with the player.
69 /// </summary>
70 /// <param name="handPosition">The position of the hand dribbling the ball.</param>
71 public void Move(Vector2 handPosition)
72 {
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.
75 }
76
77 /// <summary>
78 /// Grab the ball if possible given the current game state.
79 /// </summary>
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)
83 {
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;
86
87 // Make sure its within their grab area.
88 if (Vector2.Distance(controller.ball.transform.position, handPosition) >= 1f) return false;
89
90 controller.state = dribble;
91 Move(handPosition);
92 return true;
93 }
94
95 /// <summary>
96 /// Shoot the ball if possible.
97 /// </summary>
98 /// <param name="playerTransform"></param>
99 /// <returns>Whether or not the ball was shot</returns>
100 public bool Shoot(Transform playerTransform)
101 {
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;
106 return true;
107 }
108 }
109
110 internal void BallDropped()
111 {
112 state = State.Idle;
113 }
114
115 internal enum State
116 {
117 Idle,
118 JumpBall,
119 PlayerDribble,
120 PlayerShoot,
121 EnemyDribble,
122 EnemyShoot,
123 }
124 }
125
126 internal static class GameControllerStateExtensions
127 {
128 internal static bool IsShot(this GameController.State state)
129 {
130 return state == GameController.State.EnemyShoot || state == GameController.State.PlayerShoot;
131 }
132
133 internal static bool IsDribble(this GameController.State state)
134 {
135 return state == GameController.State.EnemyDribble || state == GameController.State.PlayerDribble;
136 }
137 }
138 }