]> git.cameronkatri.com Git - gmtk-gamejam.git/blob - Assets/Scripts/Controllers/AIController.cs
bd5fee1165aa96ccae8544d59a124c94d9551f4c
[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 [SerializeField] private Transform flipper;
15
16 [Header("Legs")]
17 [SerializeField] private Leg leftLeg;
18 [SerializeField] private Leg rightLeg;
19 [SerializeField] private float maxLegAngle;
20 [SerializeField] private float legSpeed;
21
22 [Header("References")]
23 [SerializeField] private GameController game;
24 [SerializeField] private PlayerController player;
25
26 public Rigidbody2D Rigidbody => _rigidbody != null ? _rigidbody : _rigidbody = GetComponent<Rigidbody2D>();
27 private Rigidbody2D _rigidbody;
28
29 private float animationStep;
30
31 public BoxCollider2D BoxCollider => _boxCollider != null ? _boxCollider : _boxCollider = GetComponent<BoxCollider2D>();
32 private BoxCollider2D _boxCollider;
33
34 private void Start()
35 {
36 // Try to grab (steal) the ball every n seconds.
37 InvokeRepeating(nameof(GrabBall), 0, 0.5f);
38 InvokeRepeating(nameof(ShootBall), 0, 2f);
39 }
40
41 private void GrabBall()
42 {
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.
45 {
46 game.enemy.GrabBall(transform.position);
47 game.enemy.GrabBall(transform.position + new Vector3(0f, BoxCollider.size.y / 2, 0f));
48 }
49 }
50
51 private void ShootBall()
52 {
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 < 6f && 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.
57 {
58 game.enemy.Shoot(transform, 0f);
59 }
60 }
61
62 private void Update()
63 {
64 var movement = transform.position;
65
66 if (!game.enemy.HasBall) // Move towards the ball to grab it.
67 {
68 if (Vector2.Distance(player.root.transform.position, transform.position) < 0.8f) // Repel from the player if they get too close.
69 {
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);
72 }
73 else
74 {
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);
79 }
80 }
81 else // Otherwise, move toward the basket, and then once we get within range, take the shot.
82 {
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 ? 5f : 3f;
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);
87 }
88
89 movement -= transform.position;
90
91 game.enemy.Move(transform.position + new Vector3(0f, BoxCollider.size.y * 3/4, 0f));
92
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));
97
98 animationStep += Time.deltaTime * Mathf.Abs(movement.x);
99 }
100
101 [Serializable]
102 private struct Leg
103 {
104 public GameObject top;
105 public GameObject bottom;
106 }
107 }
108 }