From ca2356eb3f3c88745d5ecc646383f49ff301be90 Mon Sep 17 00:00:00 2001 From: Carson Katri Date: Sat, 12 Jun 2021 19:50:29 -0400 Subject: Add scoreboard UI --- Assets/Scripts/Controllers/Ball.cs | 8 +++++--- Assets/Scripts/Controllers/GameController.cs | 25 +++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) (limited to 'Assets/Scripts') diff --git a/Assets/Scripts/Controllers/Ball.cs b/Assets/Scripts/Controllers/Ball.cs index 45e607b..d9c85c2 100644 --- a/Assets/Scripts/Controllers/Ball.cs +++ b/Assets/Scripts/Controllers/Ball.cs @@ -11,6 +11,9 @@ namespace Controllers internal GameController controller; [SerializeField] private float shotForce; + + public Rigidbody2D Rigidbody => _rigidbody != null ? _rigidbody : _rigidbody = GetComponent(); + private Rigidbody2D _rigidbody; private void OnCollisionEnter2D(Collision2D other) { @@ -20,9 +23,8 @@ namespace Controllers public void Shoot(Vector3 target) { transform.right = (target - transform.position); - var rb = GetComponent(); - rb.velocity = Vector2.zero; - rb.AddForce((transform.right + (transform.up * 0.5f)) * shotForce); + Rigidbody.velocity = Vector2.zero; + Rigidbody.AddForce((transform.right + (transform.up * 0.5f)) * shotForce); } } } diff --git a/Assets/Scripts/Controllers/GameController.cs b/Assets/Scripts/Controllers/GameController.cs index e1af95c..3260fb2 100644 --- a/Assets/Scripts/Controllers/GameController.cs +++ b/Assets/Scripts/Controllers/GameController.cs @@ -1,5 +1,6 @@ using System; using UnityEngine; +using UnityEngine.UI; namespace Controllers { @@ -19,11 +20,17 @@ namespace Controllers /// [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; + private void Awake() { player = new Player { isEnemy = false, controller = this }; @@ -33,6 +40,17 @@ namespace Controllers EnemyHoop.controller = this; BallAnimation = ball.GetComponentInChildren(); } + + private void Start() + { + UpdateUI(); + } + + private void UpdateUI() + { + playerScoreText.text = $"{player.score}"; + enemyScoreText.text = $"{enemy.score}"; + } public struct Player { @@ -59,6 +77,8 @@ namespace Controllers { score += 2; } + + controller.UpdateUI(); } private State dribble => isEnemy ? State.EnemyDribble : State.PlayerDribble; @@ -95,6 +115,9 @@ namespace Controllers controller.BallAnimation.enabled = false; controller.dribbleSound.Play(); Move(handPosition); + + controller.ball.Rigidbody.bodyType = RigidbodyType2D.Kinematic; + return true; } @@ -109,6 +132,7 @@ namespace Controllers controller.BallAnimation.enabled = true; controller.dribbleSound.Stop(); controller.state = shoot; + controller.ball.Rigidbody.bodyType = RigidbodyType2D.Dynamic; controller.ball.Shoot(hoop.transform.position); lastShotPosition = playerTransform.position; return true; @@ -119,6 +143,7 @@ namespace Controllers { BallAnimation.enabled = true; dribbleSound.Stop(); + ball.Rigidbody.bodyType = RigidbodyType2D.Dynamic; state = State.Idle; } -- cgit v1.2.3-56-ge451