]> git.cameronkatri.com Git - gmtk-gamejam.git/blob - Assets/Scripts/Controllers/AIController.cs
Hold ball away from face
[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 private bool turn;
31
32 public BoxCollider2D BoxCollider => _boxCollider != null ? _boxCollider : _boxCollider = GetComponent<BoxCollider2D>();
33 private BoxCollider2D _boxCollider;
34
35 private void Start()
36 {
37 // Try to grab (steal) the ball every n seconds.
38 InvokeRepeating(nameof(GrabBall), 0, 0.5f);
39 InvokeRepeating(nameof(ShootBall), 0, 2f);
40 }
41
42 private void GrabBall()
43 {
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.
46 {
47 game.enemy.GrabBall(transform.position);
48 game.enemy.GrabBall(transform.position + new Vector3(0f, BoxCollider.size.y / 2, 0f));
49 }
50 }
51
52 private void ShootBall()
53 {
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.
58 {
59 game.enemy.Shoot(transform, Random.Range(0.5f, 1f));
60 }
61 }
62
63 private void Update()
64 {
65 if (game.freezeMotion) return;
66
67 var movement = transform.position;
68
69 if (!game.enemy.HasBall) // Move towards the ball to grab it.
70 {
71 if (Vector2.Distance(player.root.transform.position, transform.position) < 0.8f) // Repel from the player if they get too close.
72 {
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);
75 }
76 else
77 {
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);
81 }
82 }
83 else // Otherwise, move toward the basket, and then once we get within range, take the shot.
84 {
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);
89 }
90
91 movement -= transform.position;
92
93 bool currentTurn = movement.x > 0;
94 if (turn && currentTurn)
95 flipper.transform.localScale = new Vector3(1f, 1f, 1f);
96 else
97 flipper.transform.localScale = new Vector3(-1f, 1f, 1f);
98
99 game.enemy.Move(transform.position + new Vector3(0f, BoxCollider.size.y * 3/4, 0f) + (transform.right * (0.5f * (turn && currentTurn ? -1 : 1))));
100 turn = currentTurn;
101
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));
106
107 animationStep += Time.deltaTime * Mathf.Abs(movement.x);
108 }
109
110 [Serializable]
111 private struct Leg
112 {
113 public GameObject top;
114 public GameObject bottom;
115 }
116 }
117 }