]> git.cameronkatri.com Git - gmtk-gamejam.git/blob - Assets/Scripts/Controllers/AIController.cs
0edffb7f82a8f178a9caff89ac290bd22a122742
[gmtk-gamejam.git] / Assets / Scripts / Controllers / AIController.cs
1 using System;
2 using Controllers.Player;
3 using UnityEngine;
4 using Random = UnityEngine.Random;
5
6 namespace Controllers
7 {
8 [RequireComponent(typeof(Rigidbody2D))]
9 [RequireComponent(typeof(BoxCollider2D))]
10 public class AIController : MonoBehaviour
11 {
12 [SerializeField] private float speed;
13
14 [Header("Legs")]
15 [SerializeField] private Leg leftLeg;
16 [SerializeField] private Leg rightLeg;
17 [SerializeField] private float maxLegAngle;
18 [SerializeField] private float legSpeed;
19
20 [Header("References")]
21 [SerializeField] private GameController game;
22 [SerializeField] private PlayerController player;
23
24 public Rigidbody2D Rigidbody => _rigidbody != null ? _rigidbody : _rigidbody = GetComponent<Rigidbody2D>();
25 private Rigidbody2D _rigidbody;
26
27 private float animationStep;
28
29 public BoxCollider2D BoxCollider => _boxCollider != null ? _boxCollider : _boxCollider = GetComponent<BoxCollider2D>();
30 private BoxCollider2D _boxCollider;
31
32 private void Start()
33 {
34 // Try to grab (steal) the ball every n seconds.
35 InvokeRepeating(nameof(GrabBall), 0, 0.5f);
36 }
37
38 private void GrabBall()
39 {
40 // Grab from the middle, and the bottom.
41 if (!game.player.HasBall || Random.Range(0, 100) > 70) // 30% chance of them grabbing the ball from the player.
42 {
43 game.enemy.GrabBall(transform.position);
44 game.enemy.GrabBall(transform.position + new Vector3(0f, BoxCollider.size.y / 2, 0f));
45 }
46 }
47
48 private void Update()
49 {
50 var movement = transform.position;
51
52 if (!game.enemy.HasBall) // Move towards the ball to grab it.
53 {
54 if (Vector2.Distance(player.root.transform.position, transform.position) < 2f) // Repel from the player if they get too close.
55 {
56 // transform.position += new Vector3(-(player.transform.position - transform.position).normalized.x, 0, 0) * (Time.deltaTime * speed / 2f);
57 transform.position = new Vector2(Vector2.MoveTowards(transform.position, player.root.transform.position, -1.5f * speed * Time.deltaTime).x, transform.position.y);
58 }
59 else
60 {
61 // transform.position += new Vector3(game.ball.transform.position.x - transform.position.x, 0f, 0f).normalized * (Time.deltaTime * speed);
62 transform.position = new Vector2(Vector2.MoveTowards(transform.position, game.ball.transform.position, speed * Time.deltaTime).x, transform.position.y);
63 }
64 }
65 else // Otherwise, move toward the basket, and then once we get within range, take the shot.
66 {
67 }
68
69 movement -= transform.position;
70
71 game.enemy.Move(transform.position + new Vector3(0f, BoxCollider.size.y / 2, 0f));
72
73 leftLeg.top.transform.localRotation = Quaternion.Euler(0, 0, maxLegAngle * Mathf.Sin(animationStep * legSpeed));
74 leftLeg.bottom.transform.localRotation = Quaternion.Euler(0, 0, maxLegAngle + maxLegAngle * Mathf.Sin(animationStep * legSpeed));
75 rightLeg.top.transform.localRotation = Quaternion.Euler(0, 0, maxLegAngle * Mathf.Sin(animationStep * -legSpeed));
76 rightLeg.bottom.transform.localRotation = Quaternion.Euler(0, 0, maxLegAngle + maxLegAngle * Mathf.Sin(animationStep * -legSpeed));
77
78 animationStep += Time.deltaTime * Mathf.Abs(movement.x);
79 }
80
81 [Serializable]
82 private struct Leg
83 {
84 public GameObject top;
85 public GameObject bottom;
86 }
87 }
88 }