aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Assets/Scripts
diff options
context:
space:
mode:
authorCarson Katri <carson.katri@gmail.com>2021-06-12 16:55:43 -0400
committerCarson Katri <carson.katri@gmail.com>2021-06-12 16:55:43 -0400
commit939170f625e5928d5ca86a57796aaa23f25a4fa7 (patch)
treedb7d4672a634a38bccbb448563f4387daef4beab /Assets/Scripts
parent6bd59f3f902328c5c2ac19f0a03abb1194c113c9 (diff)
downloadgmtk-gamejam-939170f625e5928d5ca86a57796aaa23f25a4fa7.tar.gz
gmtk-gamejam-939170f625e5928d5ca86a57796aaa23f25a4fa7.tar.zst
gmtk-gamejam-939170f625e5928d5ca86a57796aaa23f25a4fa7.zip
Smooth leg animation
Diffstat (limited to 'Assets/Scripts')
-rw-r--r--Assets/Scripts/Controllers/AIController.cs72
-rw-r--r--Assets/Scripts/Controllers/GameController.cs2
2 files changed, 70 insertions, 4 deletions
diff --git a/Assets/Scripts/Controllers/AIController.cs b/Assets/Scripts/Controllers/AIController.cs
index 9f115c2..19a7d3e 100644
--- a/Assets/Scripts/Controllers/AIController.cs
+++ b/Assets/Scripts/Controllers/AIController.cs
@@ -1,19 +1,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 (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));
+ }
}
- 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;
}
}
}
diff --git a/Assets/Scripts/Controllers/GameController.cs b/Assets/Scripts/Controllers/GameController.cs
index c326c4e..9ae0ce8 100644
--- a/Assets/Scripts/Controllers/GameController.cs
+++ b/Assets/Scripts/Controllers/GameController.cs
@@ -40,6 +40,8 @@ namespace Controllers
private State dribble => isEnemy ? State.EnemyDribble : State.PlayerDribble;
private State shoot => isEnemy ? State.EnemyShoot : State.PlayerShoot;
+ public bool HasBall => controller.state == dribble;
+
/// <summary>
/// When dribbling, move the ball with the player.
/// </summary>