aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Assets/Scripts
diff options
context:
space:
mode:
authorCarson Katri <carson.katri@gmail.com>2021-06-12 12:52:30 -0400
committerCarson Katri <carson.katri@gmail.com>2021-06-12 12:52:30 -0400
commit00f001ab1df95c96d5d148133bf1dbd790baae05 (patch)
tree6036134f9c3239fc71677142986c951bba332ffd /Assets/Scripts
parentff111d95db1878f9c35d99e7d4fa0b4d9a0de37c (diff)
downloadgmtk-gamejam-00f001ab1df95c96d5d148133bf1dbd790baae05.tar.gz
gmtk-gamejam-00f001ab1df95c96d5d148133bf1dbd790baae05.tar.zst
gmtk-gamejam-00f001ab1df95c96d5d148133bf1dbd790baae05.zip
Add player graphics
Diffstat (limited to 'Assets/Scripts')
-rw-r--r--Assets/Scripts/Controllers/Ball.cs24
-rw-r--r--Assets/Scripts/Controllers/Ball.cs.meta3
-rw-r--r--Assets/Scripts/Controllers/GameController.cs113
-rw-r--r--Assets/Scripts/Controllers/GameController.cs.meta3
-rw-r--r--Assets/Scripts/Controllers/Player/PlayerController.cs67
-rw-r--r--Assets/Scripts/Controllers/Player/PlayerSegment.cs8
6 files changed, 176 insertions, 42 deletions
diff --git a/Assets/Scripts/Controllers/Ball.cs b/Assets/Scripts/Controllers/Ball.cs
new file mode 100644
index 0000000..b63c928
--- /dev/null
+++ b/Assets/Scripts/Controllers/Ball.cs
@@ -0,0 +1,24 @@
+using System;
+using UnityEngine;
+
+namespace Controllers
+{
+ [RequireComponent(typeof(Rigidbody2D))]
+ [RequireComponent(typeof(CircleCollider2D))]
+ public class Ball : MonoBehaviour
+ {
+ internal GameController controller;
+
+ [SerializeField] private float shotForce;
+
+ private void OnCollisionEnter2D(Collision2D other)
+ {
+ controller.BallDropped();
+ }
+
+ public void Shoot(Transform playerTransform)
+ {
+ GetComponent<Rigidbody2D>().AddForce((Vector2)(playerTransform.forward + playerTransform.up) * shotForce);
+ }
+ }
+}
diff --git a/Assets/Scripts/Controllers/Ball.cs.meta b/Assets/Scripts/Controllers/Ball.cs.meta
new file mode 100644
index 0000000..d33ab19
--- /dev/null
+++ b/Assets/Scripts/Controllers/Ball.cs.meta
@@ -0,0 +1,3 @@
+fileFormatVersion: 2
+guid: 71c18c0662ec4384bf64aae1dc016093
+timeCreated: 1623503837 \ No newline at end of file
diff --git a/Assets/Scripts/Controllers/GameController.cs b/Assets/Scripts/Controllers/GameController.cs
new file mode 100644
index 0000000..2ae5f21
--- /dev/null
+++ b/Assets/Scripts/Controllers/GameController.cs
@@ -0,0 +1,113 @@
+using System;
+using UnityEngine;
+
+namespace Controllers
+{
+ /// <summary>
+ /// The controller for the basketball-game logic.
+ /// </summary>
+ public class GameController : MonoBehaviour
+ {
+ private State state = State.JumpBall; // A basketball game always starts with a jump ball.
+
+ public Player player;
+ public Player enemy;
+
+ /// <summary>
+ /// The single ball for the game.
+ /// </summary>
+ [SerializeField] private Ball ball;
+
+ private void Awake()
+ {
+ player = new Player { isEnemy = false, controller = this };
+ enemy = new Player { isEnemy = true, controller = this };
+ ball.controller = this;
+ }
+
+ public struct Player
+ {
+ /// <summary>
+ /// Whether this player is the AI-enemy.
+ /// </summary>
+ internal bool isEnemy;
+
+ /// <summary>
+ /// A back-reference to the containing GameController.
+ /// </summary>
+ internal GameController controller;
+
+ private State dribble => isEnemy ? State.EnemyDribble : State.PlayerDribble;
+ private State shoot => isEnemy ? State.EnemyShoot : State.PlayerShoot;
+
+ /// <summary>
+ /// When dribbling, move the ball with the player.
+ /// </summary>
+ /// <param name="handPosition">The position of the hand dribbling the ball.</param>
+ public void Move(Vector2 handPosition)
+ {
+ if (controller.state == (isEnemy ? State.EnemyDribble : State.PlayerDribble)) // Make sure they're dribbling.
+ controller.ball.transform.position = handPosition; // TODO: Make this perform a dribbling motion, otherwise it looks like they're travelling.
+ }
+
+ /// <summary>
+ /// Grab the ball if possible given the current game state.
+ /// </summary>
+ /// <param name="handPosition">The position of the hand to attempt grabbing from.</param>
+ /// <returns>Whether or not the ball was able to be picked up.</returns>
+ 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;
+
+ // Make sure its within their grab area.
+ if (Vector2.Distance(controller.ball.transform.position, handPosition) > 0.75f) return false;
+
+ controller.state = dribble;
+ Move(handPosition);
+ return true;
+ }
+
+ /// <summary>
+ /// Shoot the ball if possible.
+ /// </summary>
+ /// <param name="playerTransform"></param>
+ /// <returns>Whether or not the ball was shot</returns>
+ public bool Shoot(Transform playerTransform)
+ {
+ if (controller.state != dribble) return false; // We must be dribbling the ball to shoot it.
+ controller.state = shoot;
+ controller.ball.Shoot(playerTransform);
+ return true;
+ }
+ }
+
+ internal void BallDropped()
+ {
+ state = State.Idle;
+ }
+
+ internal enum State
+ {
+ Idle,
+ JumpBall,
+ PlayerDribble,
+ PlayerShoot,
+ EnemyDribble,
+ EnemyShoot,
+ }
+ }
+
+ internal static class GameControllerStateExtensions
+ {
+ internal static bool IsShot(this GameController.State state)
+ {
+ return state == GameController.State.EnemyShoot || state == GameController.State.PlayerShoot;
+ }
+
+ internal static bool IsDribble(this GameController.State state)
+ {
+ return state == GameController.State.EnemyDribble || state == GameController.State.PlayerDribble;
+ }
+ }
+}
diff --git a/Assets/Scripts/Controllers/GameController.cs.meta b/Assets/Scripts/Controllers/GameController.cs.meta
new file mode 100644
index 0000000..90c96aa
--- /dev/null
+++ b/Assets/Scripts/Controllers/GameController.cs.meta
@@ -0,0 +1,3 @@
+fileFormatVersion: 2
+guid: 6639705c6da14aab845bd87d3d8fbf38
+timeCreated: 1623502380 \ No newline at end of file
diff --git a/Assets/Scripts/Controllers/Player/PlayerController.cs b/Assets/Scripts/Controllers/Player/PlayerController.cs
index c5cfc3d..e0de738 100644
--- a/Assets/Scripts/Controllers/Player/PlayerController.cs
+++ b/Assets/Scripts/Controllers/Player/PlayerController.cs
@@ -1,3 +1,4 @@
+using System;
using System.Linq;
using Controllers.Player;
using UnityEngine;
@@ -10,15 +11,18 @@ namespace Controllers.Player
[SerializeField] private float sensitivity;
[SerializeField] private float verticalSensitivity;
- [SerializeField] private Rigidbody2D ballPrefab;
- [SerializeField] private float ballSpeed;
+ [SerializeField] private GameController game;
+ [SerializeField] private Controls controls;
+
private void Update()
{
+ // Loop over each body segment and control it.
for (var i = 0; i < segments.Length; i++)
{
var segment = segments[i];
var isRoot = i == 0;
+ var isTop = i == segments.Length - 1;
// Update icons
foreach (var control in new[] {segment.left, segment.right})
@@ -31,45 +35,34 @@ namespace Controllers.Player
// Add forces
if (Input.GetKey(segment.left.keyCode))
- segment.Rigidbody.AddForceAtPosition(Vector2.left * (sensitivity * (isRoot ? 2f : 1f)) + Vector2.up * (verticalSensitivity * (isRoot ? -1f : 1f)), segment.transform.position + (segment.Sprite.bounds.size.y * segment.forceOrigin));
+ segment.Rigidbody.AddForceAtPosition(Vector2.left * (sensitivity * (isRoot ? 2f : 1f)) + Vector2.up * (verticalSensitivity * (isRoot ? -1f : 1f)), segment.transform.position + (segment.height * segment.forceOrigin));
if (Input.GetKey(segment.right.keyCode))
- segment.Rigidbody.AddForceAtPosition(Vector2.right * (sensitivity * (isRoot ? 2f : 1f)) + Vector2.up * (verticalSensitivity * (isRoot ? -1f : 1f)), segment.transform.position + (segment.Sprite.bounds.size.y * segment.forceOrigin));
- }
+ segment.Rigidbody.AddForceAtPosition(Vector2.right * (sensitivity * (isRoot ? 2f : 1f)) + Vector2.up * (verticalSensitivity * (isRoot ? -1f : 1f)), segment.transform.position + (segment.height * segment.forceOrigin));
- // if (Input.GetKey(KeyCode.D))
- // {
- // segments[0].AddForceAtPosition(Vector2.right * sensitivity, (Vector2)segments[0].transform.position - Vector2.up * 0.5f);
- // }
- // else if (Input.GetKey(KeyCode.A))
- // {
- // segments[0].AddForceAtPosition(Vector2.left * sensitivity, (Vector2)segments[0].transform.position - Vector2.up * 0.5f);
- // }
- //
- // if (Input.GetKey(KeyCode.L))
- // {
- // segments[1].AddForceAtPosition(Vector2.right * sensitivity, (Vector2)segments[1].transform.position + Vector2.up * 0.5f);
- // }
- // else if (Input.GetKey(KeyCode.J))
- // {
- // segments[1].AddForceAtPosition(Vector2.left * sensitivity, (Vector2)segments[1].transform.position + Vector2.up * 0.5f);
- // }
- //
- // if (Input.GetKey(KeyCode.RightArrow))
- // {
- // segments[2].AddForceAtPosition(Vector2.right * sensitivity, (Vector2)segments[2].transform.position + Vector2.up * 0.5f);
- // }
- // else if (Input.GetKey(KeyCode.LeftArrow))
- // {
- // segments[2].AddForceAtPosition(Vector2.left * sensitivity, (Vector2)segments[2].transform.position + Vector2.up * 0.5f);
- // }
- //
- if (Input.GetKeyDown(KeyCode.Space))
+ if (isTop)
+ game.player.Move(segment.transform.position);
+ }
+
+ // Try grabbing the ball if pressed.
+ if (Input.GetKeyDown(controls.grab))
{
- var ball = Instantiate(ballPrefab);
- var lastSegment = segments.Last();
- ball.transform.position = lastSegment.transform.position;
- ball.AddForce((lastSegment.transform.right + lastSegment.transform.up) * ballSpeed);
+ // Try grabbing at each segment, and then the segment can pass it up to the top guy.
+ foreach (var segment in segments)
+ {
+ if (game.player.GrabBall(segment.transform.position)) break; // Stop after we successfully grab.
+ }
}
+
+ // Try shooting the ball if pressed.
+ if (Input.GetKeyDown(controls.shoot))
+ game.player.Shoot(segments.Last().transform);
+ }
+
+ [Serializable]
+ private struct Controls
+ {
+ [SerializeField] internal KeyCode grab;
+ [SerializeField] internal KeyCode shoot;
}
}
}
diff --git a/Assets/Scripts/Controllers/Player/PlayerSegment.cs b/Assets/Scripts/Controllers/Player/PlayerSegment.cs
index 6306201..b4b3267 100644
--- a/Assets/Scripts/Controllers/Player/PlayerSegment.cs
+++ b/Assets/Scripts/Controllers/Player/PlayerSegment.cs
@@ -4,19 +4,17 @@ using UnityEngine;
namespace Controllers.Player
{
[RequireComponent(typeof(Rigidbody2D))]
- [RequireComponent(typeof(SpriteRenderer))]
public class PlayerSegment : MonoBehaviour
{
[SerializeField] public Control left;
[SerializeField] public Control right;
- [SerializeField] public Vector3 forceOrigin;
+ [SerializeField] public Vector3 forceOrigin;
+
+ [SerializeField] public float height;
public Rigidbody2D Rigidbody => _rigidbody != null ? _rigidbody : _rigidbody = GetComponent<Rigidbody2D>();
private Rigidbody2D _rigidbody;
-
- public SpriteRenderer Sprite => _sprite != null ? _sprite : _sprite = GetComponent<SpriteRenderer>();
- private SpriteRenderer _sprite;
}
[Serializable]