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;
32 public BoxCollider2D BoxCollider => _boxCollider != null ? _boxCollider : _boxCollider = GetComponent<BoxCollider2D>();
33 private BoxCollider2D _boxCollider;
37 // Try to grab (steal) the ball every n seconds.
38 InvokeRepeating(nameof(GrabBall), 0, 0.5f);
39 InvokeRepeating(nameof(ShootBall), 0, 2f);
42 private void GrabBall()
44 // Grab from the middle, and the bottom.
45 if (!game.player.HasBall || Random.Range(0, 100) > 70) // 30% chance of them grabbing the ball from the player.
47 game.enemy.GrabBall(transform.position);
48 game.enemy.GrabBall(transform.position + new Vector3(0f, BoxCollider.size.y / 2, 0f));
52 private void ShootBall()
54 if (!game.enemy.HasBall) return;
55 var hoopDistance = Mathf.Abs(transform.position.x - game.EnemyHoop.transform.position.x);
56 var playerHoopDistance = Mathf.Abs(player.root.transform.position.x - game.EnemyHoop.transform.position.x);
57 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.
59 game.enemy.Shoot(transform, Random.Range(0.5f, 1f));
65 if (game.freezeMotion) return;
67 var movement = transform.position;
69 if (!game.enemy.HasBall) // Move towards the ball to grab it.
71 if (Vector2.Distance(player.root.transform.position, transform.position) < 0.8f) // Repel from the player if they get too close.
73 // transform.position += new Vector3(-(player.transform.position - transform.position).normalized.x, 0, 0) * (Time.deltaTime * speed / 2f);
74 transform.position = new Vector2(Vector2.MoveTowards(transform.position, player.root.transform.position, -1.5f * speed * Time.deltaTime).x, transform.position.y);
78 // transform.position += new Vector3(game.ball.transform.position.x - transform.position.x, 0f, 0f).normalized * (Time.deltaTime * speed);
79 var direction = Vector2.MoveTowards(transform.position, game.ball.transform.position, speed * Time.deltaTime);
80 transform.position = new Vector2(direction.x, transform.position.y);
83 else // Otherwise, move toward the basket, and then once we get within range, take the shot.
85 // How far the enemy tries to get from the hoop before shooting.
86 // Currently, it will try to shoot a 3pt. shot if down, but a 2pt. shot if up or tied.
87 var targetDistance = game.enemy.score < game.player.score ? 10f : 4f;
88 transform.position = new Vector2(Vector2.MoveTowards(transform.position, game.EnemyHoop.transform.position + new Vector3(targetDistance, 0, 0), speed * Time.deltaTime).x, transform.position.y);
91 movement -= transform.position;
93 bool currentTurn = movement.x > 0;
94 if (turn && currentTurn)
95 flipper.transform.localScale = new Vector3(1f, 1f, 1f);
97 flipper.transform.localScale = new Vector3(-1f, 1f, 1f);
99 game.enemy.Move(transform.position + new Vector3(0f, BoxCollider.size.y * 3/4, 0f) + (transform.right * (0.5f * (turn && currentTurn ? -1 : 1))));
102 leftLeg.top.transform.localRotation = Quaternion.Euler(0, 0, maxLegAngle * Mathf.Sin(animationStep * legSpeed));
103 leftLeg.bottom.transform.localRotation = Quaternion.Euler(0, 0, maxLegAngle + maxLegAngle * Mathf.Sin(animationStep * legSpeed));
104 rightLeg.top.transform.localRotation = Quaternion.Euler(0, 0, maxLegAngle * Mathf.Sin(animationStep * -legSpeed));
105 rightLeg.bottom.transform.localRotation = Quaternion.Euler(0, 0, maxLegAngle + maxLegAngle * Mathf.Sin(animationStep * -legSpeed));
107 animationStep += Time.deltaTime * Mathf.Abs(movement.x);
113 public GameObject top;
114 public GameObject bottom;