2 using Controllers.Player;
4 using Random = UnityEngine.Random;
8 [RequireComponent(typeof(Rigidbody2D))]
9 [RequireComponent(typeof(BoxCollider2D))]
10 public class AIController : MonoBehaviour
12 [SerializeField] private float speed;
14 [SerializeField] private Transform flipper;
17 [SerializeField] private Leg leftLeg;
18 [SerializeField] private Leg rightLeg;
19 [SerializeField] private float maxLegAngle;
20 [SerializeField] private float legSpeed;
22 [Header("References")]
23 [SerializeField] private GameController game;
24 [SerializeField] private PlayerController player;
26 public Rigidbody2D Rigidbody => _rigidbody != null ? _rigidbody : _rigidbody = GetComponent<Rigidbody2D>();
27 private Rigidbody2D _rigidbody;
29 private float animationStep;
31 public BoxCollider2D BoxCollider => _boxCollider != null ? _boxCollider : _boxCollider = GetComponent<BoxCollider2D>();
32 private BoxCollider2D _boxCollider;
36 // Try to grab (steal) the ball every n seconds.
37 InvokeRepeating(nameof(GrabBall), 0, 0.5f);
38 InvokeRepeating(nameof(ShootBall), 0, 2f);
41 private void GrabBall()
43 // Grab from the middle, and the bottom.
44 if (!game.player.HasBall || Random.Range(0, 100) > 70) // 30% chance of them grabbing the ball from the player.
46 game.enemy.GrabBall(transform.position);
47 game.enemy.GrabBall(transform.position + new Vector3(0f, BoxCollider.size.y / 2, 0f));
51 private void ShootBall()
53 if (!game.enemy.HasBall) return;
54 var hoopDistance = Mathf.Abs(transform.position.x - game.EnemyHoop.transform.position.x);
55 var playerHoopDistance = Mathf.Abs(player.root.transform.position.x - game.EnemyHoop.transform.position.x);
56 if (hoopDistance < 10.5f && hoopDistance > 2.5f && (Random.Range(0, 100) > 30 || playerHoopDistance > hoopDistance)) // Take the shot when in range. 70% chance he decides to if the player is standing between him and the basket.
58 game.enemy.Shoot(transform, Random.Range(0.5f, 1f));
64 var movement = transform.position;
66 if (!game.enemy.HasBall) // Move towards the ball to grab it.
68 if (Vector2.Distance(player.root.transform.position, transform.position) < 0.8f) // Repel from the player if they get too close.
70 // transform.position += new Vector3(-(player.transform.position - transform.position).normalized.x, 0, 0) * (Time.deltaTime * speed / 2f);
71 transform.position = new Vector2(Vector2.MoveTowards(transform.position, player.root.transform.position, -1.5f * speed * Time.deltaTime).x, transform.position.y);
75 // transform.position += new Vector3(game.ball.transform.position.x - transform.position.x, 0f, 0f).normalized * (Time.deltaTime * speed);
76 var direction = Vector2.MoveTowards(transform.position, game.ball.transform.position, speed * Time.deltaTime);
77 transform.position = new Vector2(direction.x, transform.position.y);
78 flipper.localScale = new Vector3((Vector2.Distance(transform.position, direction) > 0 ? -1f : 1f), 1, 1);
81 else // Otherwise, move toward the basket, and then once we get within range, take the shot.
83 // How far the enemy tries to get from the hoop before shooting.
84 // Currently, it will try to shoot a 3pt. shot if down, but a 2pt. shot if up or tied.
85 var targetDistance = game.enemy.score < game.player.score ? 10f : 4f;
86 transform.position = new Vector2(Vector2.MoveTowards(transform.position, game.EnemyHoop.transform.position + new Vector3(targetDistance, 0, 0), speed * Time.deltaTime).x, transform.position.y);
89 movement -= transform.position;
91 game.enemy.Move(transform.position + new Vector3(0f, BoxCollider.size.y * 3/4, 0f));
93 leftLeg.top.transform.localRotation = Quaternion.Euler(0, 0, maxLegAngle * Mathf.Sin(animationStep * legSpeed));
94 leftLeg.bottom.transform.localRotation = Quaternion.Euler(0, 0, maxLegAngle + maxLegAngle * Mathf.Sin(animationStep * legSpeed));
95 rightLeg.top.transform.localRotation = Quaternion.Euler(0, 0, maxLegAngle * Mathf.Sin(animationStep * -legSpeed));
96 rightLeg.bottom.transform.localRotation = Quaternion.Euler(0, 0, maxLegAngle + maxLegAngle * Mathf.Sin(animationStep * -legSpeed));
98 animationStep += Time.deltaTime * Mathf.Abs(movement.x);
104 public GameObject top;
105 public GameObject bottom;