]> git.cameronkatri.com Git - gmtk-gamejam.git/blobdiff - Assets/Scripts/Controllers/GameController.cs
Add shot indicator
[gmtk-gamejam.git] / Assets / Scripts / Controllers / GameController.cs
index 4bdae387482415aa91d94d4655e09ec0313f814d..c9cb0ce53f9efac920661acc2ea20509dfb27300 100644 (file)
@@ -2,6 +2,7 @@ using System;
 using System.Collections;
 using JetBrains.Annotations;
 using UnityEngine;
+using UnityEngine.SceneManagement;
 using UnityEngine.UI;
 
 namespace Controllers
@@ -20,13 +21,19 @@ namespace Controllers
     public bool freezeMotion;
 
     private float startTime;
-    [SerializeField] private float timeLimit;
+    public static float timeLimit;
     
     /// <summary>
     /// The single ball for the game.
     /// </summary>
     [SerializeField] public Ball ball;
 
+    [SerializeField] private float dribbleHeight;
+    [SerializeField] private float dribbleSpeed;
+    private Vector3 ballTarget;
+    
+    [SerializeField] public SpriteRenderer[] trenchCoatSegments;
+
     [Header("Spawn Points")]
     [SerializeField] private SpawnPoints PlayerSpawnPoints;
     [SerializeField] private SpawnPoints EnemySpawnPoints;
@@ -37,6 +44,7 @@ namespace Controllers
 
     [Header("SFX")]
     [SerializeField] public AudioSource dribbleSound;
+    [SerializeField] public AudioSource airhornSound;
 
     [Header("UI")]
     [SerializeField] private Text playerScoreText;
@@ -45,6 +53,7 @@ namespace Controllers
     
     [SerializeField] private GameObject resultOverlay;
     [SerializeField] private Text resultText;
+    [SerializeField] private GameObject actionsUI;
 
     private void Awake()
     {
@@ -58,7 +67,30 @@ namespace Controllers
 
     private void Start()
     {
+      startTime = Time.time - 2f;
+      freezeMotion = true;
+      StartCoroutine(FadeInCoat());
+    }
+
+    private IEnumerator FadeInCoat()
+    {
+      foreach (var trenchCoat in trenchCoatSegments)
+        trenchCoat.material.color = new Color(1, 1, 1, 0);
+      
+      yield return new WaitForSeconds(1f);
+      
+      for (float t = 0; t < 1f; t += Time.deltaTime / 1)
+      {
+        foreach (var trenchCoat in trenchCoatSegments)
+          trenchCoat.material.color = new Color(1, 1, 1, Mathf.Lerp(0, 1, t));
+        yield return null;
+      }
+
+      yield return new WaitForSeconds(1f);
+
+      freezeMotion = false;
       startTime = Time.time;
+      ball.Rigidbody.velocity = new Vector2(0, 10f);
     }
 
     private void Update()
@@ -66,6 +98,15 @@ namespace Controllers
       UpdateUI();
     }
 
+    private void FixedUpdate()
+    {
+      if (player.HasBall || enemy.HasBall)
+      {
+        ball.transform.position = ballTarget - new Vector3(0, (Mathf.Sin(Time.time * dribbleSpeed) + 1f) * (ballTarget.y - 0.75f) * dribbleHeight, 0);
+      }
+    }
+
+    private bool gameover;
     private void UpdateUI()
     {
       playerScoreText.text = $"{player.score}";
@@ -75,12 +116,15 @@ namespace Controllers
       var remaining = TimeSpan.FromSeconds(Mathf.Clamp(remainingRaw, 0, float.MaxValue));
       timerText.text = $"{remaining.Minutes:00}:{remaining.Seconds:00}";
 
-      if (remainingRaw <= 0)
+      if (remainingRaw <= 0 && !gameover)
       {
+        airhornSound.Play();
         var outcome = player.score == enemy.score ? "TIE GAME" : player.score < enemy.score ? "AWAY TEAM WINS" : "HOME TEAM WINS";
+        actionsUI.SetActive(true);
         ShowModal($"{outcome}\n{player.score}-{enemy.score}");
 
         freezeMotion = true;
+        gameover = true;
       }
     }
 
@@ -128,7 +172,7 @@ namespace Controllers
       public void Move(Vector2 handPosition)
       {
         if (controller.state == (isEnemy ? State.EnemyDribble : State.PlayerDribble)) // Make sure they're dribbling.
-          controller.ball.transform.position = handPosition; // TODO: Make this perform a dribbling motion, otherwise it looks like they're travelling.
+          controller.ballTarget = handPosition;
       }
 
       /// <summary>
@@ -231,11 +275,20 @@ namespace Controllers
 
     private void ShowModal(string text)
     {
+      if (gameover) return;
       resultOverlay.SetActive(true);
       resultText.text = text;
     }
 
-    private void HideModal() => resultOverlay.SetActive(false);
+    private void HideModal()
+    {
+      if (gameover) return;
+      resultOverlay.SetActive(false);
+    }
+
+    public void Restart() => SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
+
+    public void MainMenu() => SceneManager.LoadScene("Menu");
 
     internal enum State
     {