aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Assets/Scripts/Controllers/AIController.cs
blob: 19a7d3e5cf9e386ed46083809137bd222779b090 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System;
using Controllers.Player;
using UnityEngine;
using Random = UnityEngine.Random;

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 Update()
    {
      if (!game.enemy.HasBall) // Move towards the ball to grab it.
      {
        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));
        }
      }
      else // Otherwise, move toward the basket, and then once we get within range, take the shot.
      {
      }

      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;
    }
  }
}