using System;
using UnityEngine;
+using UnityEngine.UI;
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 };
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
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;
}
/// 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;
}
internal void BallDropped()
{
+ BallAnimation.enabled = true;
dribbleSound.Stop();
+ ball.Rigidbody.bodyType = RigidbodyType2D.Dynamic;
state = State.Idle;
}