aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Assets/Scripts
diff options
context:
space:
mode:
authorCarson Katri <carson.katri@gmail.com>2021-06-12 18:06:04 -0400
committerCarson Katri <carson.katri@gmail.com>2021-06-12 18:06:04 -0400
commit67c86ed960cc5feaedd06569ad62a340cc0d013f (patch)
treec5408bb4dd3b6df4a6b6e88fa41ee9875d3201e4 /Assets/Scripts
parent71e603101369cf4a144904a2fc0e45ad3f862296 (diff)
downloadgmtk-gamejam-67c86ed960cc5feaedd06569ad62a340cc0d013f.tar.gz
gmtk-gamejam-67c86ed960cc5feaedd06569ad62a340cc0d013f.tar.zst
gmtk-gamejam-67c86ed960cc5feaedd06569ad62a340cc0d013f.zip
Functional AI that can dribble, steal, and shoot
Diffstat (limited to 'Assets/Scripts')
-rw-r--r--Assets/Scripts/Controllers/AIController.cs11
-rw-r--r--Assets/Scripts/Controllers/Ball.cs10
-rw-r--r--Assets/Scripts/Controllers/CameraController.cs3
-rw-r--r--Assets/Scripts/Controllers/GameController.cs6
-rw-r--r--Assets/Scripts/Controllers/Player/PlayerController.cs5
-rw-r--r--Assets/Scripts/Controllers/Player/PlayerSegment.cs2
6 files changed, 28 insertions, 9 deletions
diff --git a/Assets/Scripts/Controllers/AIController.cs b/Assets/Scripts/Controllers/AIController.cs
index 0edffb7..f897f42 100644
--- a/Assets/Scripts/Controllers/AIController.cs
+++ b/Assets/Scripts/Controllers/AIController.cs
@@ -10,6 +10,8 @@ namespace Controllers
public class AIController : MonoBehaviour
{
[SerializeField] private float speed;
+
+ [SerializeField] private Transform flipper;
[Header("Legs")]
[SerializeField] private Leg leftLeg;
@@ -59,11 +61,18 @@ namespace Controllers
else
{
// transform.position += new Vector3(game.ball.transform.position.x - transform.position.x, 0f, 0f).normalized * (Time.deltaTime * speed);
- transform.position = new Vector2(Vector2.MoveTowards(transform.position, game.ball.transform.position, speed * Time.deltaTime).x, transform.position.y);
+ var direction = Vector2.MoveTowards(transform.position, game.ball.transform.position, speed * Time.deltaTime);
+ transform.position = new Vector2(direction.x, transform.position.y);
+ flipper.localScale = new Vector3((Vector2.Distance(transform.position, direction) > 0 ? -1f : 1f), 1, 1);
}
}
else // Otherwise, move toward the basket, and then once we get within range, take the shot.
{
+ transform.position = new Vector2(Vector2.MoveTowards(transform.position, game.EnemyHoop.transform.position, speed * Time.deltaTime).x, transform.position.y);
+ if (Mathf.Abs(transform.position.x - game.EnemyHoop.transform.position.x) < 5f) // Take the shot.
+ {
+ game.enemy.Shoot(transform);
+ }
}
movement -= transform.position;
diff --git a/Assets/Scripts/Controllers/Ball.cs b/Assets/Scripts/Controllers/Ball.cs
index fa9c251..45e607b 100644
--- a/Assets/Scripts/Controllers/Ball.cs
+++ b/Assets/Scripts/Controllers/Ball.cs
@@ -17,12 +17,12 @@ namespace Controllers
controller.BallDropped();
}
- public void Shoot(Transform playerTransform)
+ public void Shoot(Vector3 target)
{
- transform.right = (controller.PlayerHoop.transform.position - transform.position);
- var rigidbody = GetComponent<Rigidbody2D>();
- rigidbody.velocity = Vector2.zero;
- rigidbody.AddForce(transform.right * shotForce);
+ transform.right = (target - transform.position);
+ var rb = GetComponent<Rigidbody2D>();
+ rb.velocity = Vector2.zero;
+ rb.AddForce((transform.right + (transform.up * 0.5f)) * shotForce);
}
}
}
diff --git a/Assets/Scripts/Controllers/CameraController.cs b/Assets/Scripts/Controllers/CameraController.cs
index a72b2f4..faffbe5 100644
--- a/Assets/Scripts/Controllers/CameraController.cs
+++ b/Assets/Scripts/Controllers/CameraController.cs
@@ -5,10 +5,11 @@ namespace Controllers
public class CameraController : MonoBehaviour
{
[SerializeField] private Transform target;
+ [SerializeField] private float speed;
private void Update()
{
- transform.position = new Vector3(target.transform.position.x, target.transform.position.y, -10f);
+ transform.position = Vector3.MoveTowards(transform.position, new Vector3(target.transform.position.x, target.transform.position.y, -10f), Time.deltaTime * speed);
}
}
}
diff --git a/Assets/Scripts/Controllers/GameController.cs b/Assets/Scripts/Controllers/GameController.cs
index 646b750..1573405 100644
--- a/Assets/Scripts/Controllers/GameController.cs
+++ b/Assets/Scripts/Controllers/GameController.cs
@@ -60,6 +60,8 @@ namespace Controllers
private State dribble => isEnemy ? State.EnemyDribble : State.PlayerDribble;
private State shoot => isEnemy ? State.EnemyShoot : State.PlayerShoot;
+ private Hoop hoop => isEnemy ? controller.EnemyHoop : controller.PlayerHoop;
+
public bool HasBall => controller.state == dribble;
/// <summary>
@@ -80,7 +82,7 @@ namespace Controllers
public bool GrabBall(Vector2 handPosition)
{
// Don't allow the ball to be picked up if someone shot it. Also don't try picking it up if we're already holding it.
- //if (controller.state.IsShot() || controller.state == dribble) return false;
+ if (controller.state == shoot || controller.state == dribble) return false;
// Make sure its within their grab area.
if (Vector2.Distance(controller.ball.transform.position, handPosition) > 0.75f) return false;
@@ -99,7 +101,7 @@ namespace Controllers
{
if (controller.state != dribble) return false; // We must be dribbling the ball to shoot it.
controller.state = shoot;
- controller.ball.Shoot(playerTransform);
+ controller.ball.Shoot(hoop.transform.position);
lastShotPosition = playerTransform.position;
return true;
}
diff --git a/Assets/Scripts/Controllers/Player/PlayerController.cs b/Assets/Scripts/Controllers/Player/PlayerController.cs
index 6436235..c789ccb 100644
--- a/Assets/Scripts/Controllers/Player/PlayerController.cs
+++ b/Assets/Scripts/Controllers/Player/PlayerController.cs
@@ -2,6 +2,7 @@ using System;
using System.Linq;
using Controllers.Player;
using UnityEngine;
+using Random = UnityEngine.Random;
namespace Controllers.Player
{
@@ -35,6 +36,9 @@ namespace Controllers.Player
{
var segment = segments[i];
+ // Flip the player to face the right direction.
+ segment.transform.localScale = root.Rigidbody.velocity.x > 0.01f ? new Vector3(1f, 1f, 1f) : new Vector3(-1f, 1f, 1f);
+
// Update icons
foreach (var control in new[] {segment.left, segment.right})
{
@@ -85,6 +89,7 @@ namespace Controllers.Player
if (Input.GetKeyDown(controls.grab))
{
// Try grabbing at each segment, and then the segment can pass it up to the top guy.
+ if (game.enemy.HasBall && Random.Range(0, 100) < 50) return; // 50% chance of stealing.
foreach (var segment in segments)
{
if (game.player.GrabBall(segment.transform.position)) break; // Stop after we successfully grab.
diff --git a/Assets/Scripts/Controllers/Player/PlayerSegment.cs b/Assets/Scripts/Controllers/Player/PlayerSegment.cs
index 4d0fb4f..0e8a969 100644
--- a/Assets/Scripts/Controllers/Player/PlayerSegment.cs
+++ b/Assets/Scripts/Controllers/Player/PlayerSegment.cs
@@ -21,6 +21,8 @@ namespace Controllers.Player
[SerializeField] public bool isRoot;
[SerializeField] public bool isTop;
+ [SerializeField] public Transform flipper;
+
public Rigidbody2D Rigidbody => _rigidbody != null ? _rigidbody : _rigidbody = GetComponent<Rigidbody2D>();
private Rigidbody2D _rigidbody;