diff options
Diffstat (limited to 'Assets/Scripts')
-rw-r--r-- | Assets/Scripts/Controllers/AIController.cs | 27 | ||||
-rw-r--r-- | Assets/Scripts/Controllers/GameController.cs | 17 | ||||
-rw-r--r-- | Assets/Scripts/Controllers/Hoop.cs | 19 | ||||
-rw-r--r-- | Assets/Scripts/Controllers/Hoop.cs.meta | 3 |
4 files changed, 57 insertions, 9 deletions
diff --git a/Assets/Scripts/Controllers/AIController.cs b/Assets/Scripts/Controllers/AIController.cs index 19a7d3e..1b7fba8 100644 --- a/Assets/Scripts/Controllers/AIController.cs +++ b/Assets/Scripts/Controllers/AIController.cs @@ -26,7 +26,23 @@ namespace Controllers public BoxCollider2D BoxCollider => _boxCollider != null ? _boxCollider : _boxCollider = GetComponent<BoxCollider2D>(); private BoxCollider2D _boxCollider; - + + private void Start() + { + // Try to grab (steal) the ball every n seconds. + InvokeRepeating(nameof(GrabBall), 0, 0.5f); + } + + private void GrabBall() + { + // Grab from the middle, and the bottom. + if (game.player.HasBall && Random.Range(0, 100) > 95) // 5% chance of them grabbing the ball from the player. + { + game.enemy.GrabBall(transform.position); + game.enemy.GrabBall(transform.position + new Vector3(0f, BoxCollider.size.y / 2, 0f)); + } + } + private void Update() { if (!game.enemy.HasBall) // Move towards the ball to grab it. @@ -35,14 +51,7 @@ namespace Controllers if (Vector2.Distance(player.transform.position, transform.position) < 1f) // Repel from the player if they get too close. { - transform.position -= (player.transform.position - transform.position).normalized * (Time.deltaTime * speed / 2f); - } - - // Grab from the middle, and the bottom. - if (game.player.HasBall && Random.Range(0, 100) > 50) // 50% chance of them grabbing the ball from the player. - { - game.enemy.GrabBall(transform.position); - game.enemy.GrabBall(transform.position + new Vector3(0f, BoxCollider.size.y / 2, 0f)); + transform.position += (player.transform.position - transform.position).normalized * (Time.deltaTime * speed / 2f); } } else // Otherwise, move toward the basket, and then once we get within range, take the shot. diff --git a/Assets/Scripts/Controllers/GameController.cs b/Assets/Scripts/Controllers/GameController.cs index 9ae0ce8..b9f65f2 100644 --- a/Assets/Scripts/Controllers/GameController.cs +++ b/Assets/Scripts/Controllers/GameController.cs @@ -18,6 +18,9 @@ namespace Controllers /// </summary> [SerializeField] public Ball ball; + [SerializeField] public Hoop PlayerHoop; + [SerializeField] public Hoop EnemyHoop; + private void Awake() { player = new Player { isEnemy = false, controller = this }; @@ -37,6 +40,19 @@ namespace Controllers /// </summary> internal GameController controller; + internal int score; + + private Vector2 lastShotPosition; + public void Score(Vector2 Rim) { + if (Vector2.Distance(lastShotPosition, Rim) >= 1) + { + score += 3; + Debug.Log("Three point"); + } else + score += 2; + Debug.Log("Two point"); + } + private State dribble => isEnemy ? State.EnemyDribble : State.PlayerDribble; private State shoot => isEnemy ? State.EnemyShoot : State.PlayerShoot; @@ -80,6 +96,7 @@ namespace Controllers if (controller.state != dribble) return false; // We must be dribbling the ball to shoot it. controller.state = shoot; controller.ball.Shoot(playerTransform); + lastShotPosition = playerTransform.position; return true; } } diff --git a/Assets/Scripts/Controllers/Hoop.cs b/Assets/Scripts/Controllers/Hoop.cs new file mode 100644 index 0000000..58f7130 --- /dev/null +++ b/Assets/Scripts/Controllers/Hoop.cs @@ -0,0 +1,19 @@ +using System; +using UnityEngine; + +namespace Controllers +{ + public class Hoop : MonoBehaviour + { + [SerializeField] private BoxCollider2D Rim; + [SerializeField] private BoxCollider2D Net; + + private void OnTriggerEnter2D(Collider2D other) + { + if (other.GetComponent<Ball>() == null) return; + + if (Rim.IsTouching(other) && Net.IsTouching(other)) + Debug.Log("Swish!"); + } + } +}
\ No newline at end of file diff --git a/Assets/Scripts/Controllers/Hoop.cs.meta b/Assets/Scripts/Controllers/Hoop.cs.meta new file mode 100644 index 0000000..29125c3 --- /dev/null +++ b/Assets/Scripts/Controllers/Hoop.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 40e5d6a883d14c7fb60913ec2fc3009c +timeCreated: 1623524972
\ No newline at end of file |