]> git.cameronkatri.com Git - gmtk-gamejam.git/blobdiff - Assets/Scripts/Controllers/GameController.cs
Merge branch 'master' of git.cameronkatri.com:gmtk-gamejam
[gmtk-gamejam.git] / Assets / Scripts / Controllers / GameController.cs
index de3b00c5479281f2570d782190d821062a657645..ce6ea7ea2a2becf7a9404e4adef7b964e2b2fa66 100644 (file)
@@ -1,5 +1,6 @@
 using System;
 using UnityEngine;
+using UnityEngine.UI;
 
 namespace Controllers
 {
@@ -9,20 +10,31 @@ namespace Controllers
   public class GameController : MonoBehaviour
   {
     private State state = State.JumpBall; // A basketball game always starts with a jump ball.
+    internal Animator BallAnimation;
 
     public Player player;
     public Player enemy;
+
+    private float startTime;
+    [SerializeField] private float timeLimit;
     
     /// <summary>
     /// The single ball for the game.
     /// </summary>
     [SerializeField] public Ball ball;
 
+    [Header("Hoops")]
     [SerializeField] public Hoop PlayerHoop;
     [SerializeField] public Hoop EnemyHoop;
 
+    [Header("SFX")]
     [SerializeField] public AudioSource dribbleSound;
 
+    [Header("UI")]
+    [SerializeField] private Text playerScoreText;
+    [SerializeField] private Text enemyScoreText;
+    [SerializeField] private Text timerText;
+
     private void Awake()
     {
       player = new Player { isEnemy = false, controller = this };
@@ -30,6 +42,26 @@ namespace Controllers
       ball.controller = this;
       PlayerHoop.controller = this;
       EnemyHoop.controller = this;
+      BallAnimation = ball.GetComponentInChildren<Animator>();
+    }
+
+    private void Start()
+    {
+      startTime = Time.time;
+    }
+
+    private void Update()
+    {
+      UpdateUI();
+    }
+
+    private void UpdateUI()
+    {
+      playerScoreText.text = $"{player.score}";
+      enemyScoreText.text = $"{enemy.score}";
+
+      var remaining = TimeSpan.FromSeconds(timeLimit - (Time.time - startTime));
+      timerText.text = $"{remaining.Minutes:00}:{remaining.Seconds:00}";
     }
     
     public struct Player
@@ -90,8 +122,12 @@ namespace Controllers
         if (Vector2.Distance(controller.ball.transform.position, handPosition) >= 1f) return false;
         
         controller.state = dribble;
+        controller.BallAnimation.enabled = false;
         controller.dribbleSound.Play();
         Move(handPosition);
+
+        controller.ball.Rigidbody.bodyType = RigidbodyType2D.Kinematic;
+        
         return true;
       }
 
@@ -99,13 +135,16 @@ namespace Controllers
       /// Shoot the ball if possible.
       /// </summary>
       /// <param name="playerTransform"></param>
+      /// <param name="time"></param>
       /// <returns>Whether or not the ball was shot</returns>
-      public bool Shoot(Transform playerTransform)
+      public bool Shoot(Transform playerTransform, float time)
       {
         if (controller.state != dribble) return false; // We must be dribbling the ball to shoot it.
+        controller.BallAnimation.enabled = true;
         controller.dribbleSound.Stop();
         controller.state = shoot;
-        controller.ball.Shoot(hoop.transform.position);
+        controller.ball.Rigidbody.bodyType = RigidbodyType2D.Dynamic;
+        controller.ball.Shoot(hoop.transform.position, time);
         lastShotPosition = playerTransform.position;
         return true;
       }
@@ -113,7 +152,9 @@ namespace Controllers
 
     internal void BallDropped()
     {
+      BallAnimation.enabled = true;
       dribbleSound.Stop();
+      ball.Rigidbody.bodyType = RigidbodyType2D.Dynamic;
       state = State.Idle;
     }