namespace Controllers
{
+ [RequireComponent(typeof(Rigidbody2D))]
+ [RequireComponent(typeof(BoxCollider2D))]
public class AIController : MonoBehaviour
{
-
+ [SerializeField] private float speed;
+
+ [Header("Legs")]
+ [SerializeField] private Leg leftLeg;
+ [SerializeField] private Leg rightLeg;
+ [SerializeField] private float maxLegAngle;
+ [SerializeField] private float legSpeed;
+
+ [Header("References")]
+ [SerializeField] private GameController game;
+ [SerializeField] private PlayerController player;
+
+ public Rigidbody2D Rigidbody => _rigidbody != null ? _rigidbody : _rigidbody = GetComponent<Rigidbody2D>();
+ private Rigidbody2D _rigidbody;
+
+ 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 (Input.GetKey(KeyCode.H))
+ if (!game.enemy.HasBall) // Move towards the ball to grab it.
{
- transform.position += Vector3.right * 0.01f;
+ transform.position += new Vector3(game.ball.transform.position.x - transform.position.x, 0f, 0f).normalized * (Time.deltaTime * speed);
+
+ 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 if (Input.GetKey(KeyCode.F))
+ else // Otherwise, move toward the basket, and then once we get within range, take the shot.
{
- transform.position += Vector3.left * 0.01f;
}
+
+ game.enemy.Move(transform.position + new Vector3(0f, BoxCollider.size.y / 2, 0f));
+
+ leftLeg.top.transform.localRotation = Quaternion.Slerp(
+ leftLeg.top.transform.localRotation,
+ Quaternion.Euler(0, 0, maxLegAngle * Mathf.Sin(Time.time * legSpeed * (Rigidbody.velocity.magnitude / 5f))),
+ Time.deltaTime * 50f
+ );
+ leftLeg.bottom.transform.localRotation = Quaternion.Slerp(
+ leftLeg.bottom.transform.localRotation,
+ Quaternion.Euler(0, 0, maxLegAngle + maxLegAngle * Mathf.Sin(Time.time * legSpeed * (Rigidbody.velocity.magnitude / 5f))),
+ Time.deltaTime * 50f
+ );
+ rightLeg.top.transform.localRotation = Quaternion.Slerp(
+ rightLeg.top.transform.localRotation,
+ Quaternion.Euler(0, 0, maxLegAngle * Mathf.Sin(Time.time * -legSpeed * (Rigidbody.velocity.magnitude / 5f))),
+ Time.deltaTime * 50f
+ );
+ rightLeg.bottom.transform.localRotation = Quaternion.Slerp(
+ rightLeg.bottom.transform.localRotation,
+ Quaternion.Euler(0, 0, maxLegAngle + maxLegAngle * Mathf.Sin(Time.time * -legSpeed * (Rigidbody.velocity.magnitude / 5f))),
+ Time.deltaTime * 50f
+ );
+ }
+
+ [Serializable]
+ private struct Leg
+ {
+ public GameObject top;
+ public GameObject bottom;
}
}
}