diff options
Diffstat (limited to 'Assets/Scripts/Controllers/AIController.cs')
-rw-r--r-- | Assets/Scripts/Controllers/AIController.cs | 27 |
1 files changed, 18 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. |