From 2fd846835d2460a12c321c460e77fd6026eb07a0 Mon Sep 17 00:00:00 2001 From: Carson Katri Date: Sun, 13 Jun 2021 00:06:15 -0400 Subject: Add respawning after each point --- Assets/Scripts/Controllers/AIController.cs | 2 + Assets/Scripts/Controllers/GameController.cs | 64 ++++++++++++++++++++++ .../Scripts/Controllers/Player/PlayerController.cs | 2 + 3 files changed, 68 insertions(+) (limited to 'Assets/Scripts') diff --git a/Assets/Scripts/Controllers/AIController.cs b/Assets/Scripts/Controllers/AIController.cs index 9d3d77b..ad258f3 100644 --- a/Assets/Scripts/Controllers/AIController.cs +++ b/Assets/Scripts/Controllers/AIController.cs @@ -61,6 +61,8 @@ namespace Controllers private void Update() { + if (game.freezeMotion) return; + var movement = transform.position; if (!game.enemy.HasBall) // Move towards the ball to grab it. diff --git a/Assets/Scripts/Controllers/GameController.cs b/Assets/Scripts/Controllers/GameController.cs index 6ebcabe..303b5a0 100644 --- a/Assets/Scripts/Controllers/GameController.cs +++ b/Assets/Scripts/Controllers/GameController.cs @@ -1,4 +1,6 @@ using System; +using System.Collections; +using JetBrains.Annotations; using UnityEngine; using UnityEngine.UI; @@ -14,6 +16,8 @@ namespace Controllers public Player player; public Player enemy; + + public bool freezeMotion; private float startTime; [SerializeField] private float timeLimit; @@ -104,6 +108,9 @@ namespace Controllers { score += 2; } + + // They made a shot! Now respawn the players and give possession to the opposite player. + controller.Respawn(isEnemy ? Possession.Player : Possession.Enemy); } private State dribble => isEnemy ? State.EnemyDribble : State.PlayerDribble; @@ -173,6 +180,54 @@ namespace Controllers state = State.Idle; } + private void Respawn(Possession possession) + { + PlayerSpawnPoints.body.transform.position = new Vector3(PlayerSpawnPoints.character.position.x, PlayerSpawnPoints.character.position.y, PlayerSpawnPoints.body.transform.position.y); + PlayerSpawnPoints.body.GetComponent().velocity = Vector2.zero; + + if (PlayerSpawnPoints.secondBody is { }) + { + if (PlayerSpawnPoints.secondCharacter is { }) + PlayerSpawnPoints.secondBody.transform.position = new Vector3(PlayerSpawnPoints.secondCharacter.position.x, PlayerSpawnPoints.secondCharacter.position.y, PlayerSpawnPoints.body.transform.position.y); + PlayerSpawnPoints.secondBody.GetComponent().velocity = Vector2.zero; + } + + PlayerSpawnPoints.body.transform.localRotation = Quaternion.identity; + EnemySpawnPoints.body.transform.position = new Vector3(EnemySpawnPoints.character.position.x, EnemySpawnPoints.character.position.y, EnemySpawnPoints.body.transform.position.y); + ball.transform.position = possession switch + { + Possession.Player => new Vector3(PlayerSpawnPoints.ball.position.x, PlayerSpawnPoints.ball.position.y, ball.transform.position.y), + Possession.Enemy => new Vector3(EnemySpawnPoints.ball.position.x, EnemySpawnPoints.ball.position.y, ball.transform.position.y), + _ => ball.transform.position + }; + + // Set a cooldown so they can stop trying to wrangle the player while it respawns. + StartCoroutine(RespawnCooldown(possession)); + } + + private IEnumerator RespawnCooldown(Possession possession) + { + // Show the new score. + var possessionText = possession == Possession.Player ? "HOME" : "AWAY"; + ShowModal($"{player.score}-{enemy.score}\n{possessionText}'S POSSESSION"); + + freezeMotion = true; + + yield return new WaitForSeconds(1f); + + HideModal(); + + freezeMotion = false; + } + + private void ShowModal(string text) + { + resultOverlay.SetActive(true); + resultText.text = text; + } + + private void HideModal() => resultOverlay.SetActive(false); + internal enum State { Idle, @@ -186,8 +241,17 @@ namespace Controllers [Serializable] private struct SpawnPoints { + [SerializeField] internal Transform body; + [SerializeField] [CanBeNull] internal Transform secondBody; [SerializeField] internal Transform ball; [SerializeField] internal Transform character; + [SerializeField] [CanBeNull] internal Transform secondCharacter; + } + + private enum Possession + { + Player, + Enemy } } diff --git a/Assets/Scripts/Controllers/Player/PlayerController.cs b/Assets/Scripts/Controllers/Player/PlayerController.cs index d72b077..9df8c0a 100644 --- a/Assets/Scripts/Controllers/Player/PlayerController.cs +++ b/Assets/Scripts/Controllers/Player/PlayerController.cs @@ -33,6 +33,8 @@ namespace Controllers.Player private void Update() { + if (game.freezeMotion) return; + // Loop over each body segment and control it. for (var i = 0; i < segments.Length; i++) { -- cgit v1.2.3-56-ge451