From fbae06233e6aa10cbf00056ceb557c38b0cfca13 Mon Sep 17 00:00:00 2001 From: Jason Durand 01 Date: Sat, 2 Apr 2022 10:44:39 -0400 Subject: [PATCH 1/4] Added NaughtyAttributes --- Assets/Scripts/Arena.cs | 10 +++++++--- Assets/Scripts/Entity.cs | 9 +++++++++ Assets/Scripts/EntityStats.cs | 6 ++++++ Assets/Scripts/EntityStats.cs.meta | 3 +++ Assets/Scripts/PlayerMovement.cs | 17 ++++++++++------- Assets/Scripts/SafeZone.cs | 7 +++++-- Assets/Scripts/VampireEntity.cs | 7 ++++--- Packages/manifest.json | 1 + Packages/packages-lock.json | 7 +++++++ 9 files changed, 52 insertions(+), 15 deletions(-) create mode 100644 Assets/Scripts/EntityStats.cs create mode 100644 Assets/Scripts/EntityStats.cs.meta diff --git a/Assets/Scripts/Arena.cs b/Assets/Scripts/Arena.cs index 78c76bd..d6a18ac 100644 --- a/Assets/Scripts/Arena.cs +++ b/Assets/Scripts/Arena.cs @@ -1,12 +1,16 @@ #nullable enable using System.Collections; +using NaughtyAttributes; using UnityEngine; public class Arena : MonoBehaviour { //TODO probably add initial direction too - [SerializeField] Vector3[] spawners = null!; - [SerializeField] ArenaStats stats = null!; - [SerializeField] GameObject monsterPrefab = null!; + [SerializeField] [Required] + Vector3[] spawners = null!; + [SerializeField] [Required] + ArenaStats stats = null!; + [SerializeField] [Required] + GameObject monsterPrefab = null!; SafeZone safeZone = null!; diff --git a/Assets/Scripts/Entity.cs b/Assets/Scripts/Entity.cs index bd6ea0d..77a143b 100644 --- a/Assets/Scripts/Entity.cs +++ b/Assets/Scripts/Entity.cs @@ -1,10 +1,13 @@ using System.Collections; using System.Collections.Generic; +using NaughtyAttributes; using UnityEngine; [RequireComponent(typeof(Rigidbody2D))] public class Entity : MonoBehaviour { + [field: SerializeField] [field: Required] + protected EntityStats stats { get; private set; } [field: SerializeField]protected float Health { get; private set; } [SerializeField]private float movementSpeed; [SerializeField]private float rotSpeed; @@ -18,6 +21,7 @@ public class Entity : MonoBehaviour private Collider atkCollider; private Vector3 direction; Rigidbody2D rb; + bool beingPushed; void Awake() => rb = GetComponent(); @@ -78,4 +82,9 @@ public class Entity : MonoBehaviour float angle = Vector2.SignedAngle(direction, (target.position - transform.position)); return angle >= -fov && angle <= fov; } + + protected void AddImpulse(Vector3 impulse) { + beingPushed = true; + rb.AddForce(impulse, ForceMode2D.Impulse); + } } diff --git a/Assets/Scripts/EntityStats.cs b/Assets/Scripts/EntityStats.cs new file mode 100644 index 0000000..654e200 --- /dev/null +++ b/Assets/Scripts/EntityStats.cs @@ -0,0 +1,6 @@ +using UnityEngine; + +public class EntityStats { + [field: SerializeField] [field: Min(0f)] + public float MinVelocityWhenPushed { get; private set; } = 5f; +} \ No newline at end of file diff --git a/Assets/Scripts/EntityStats.cs.meta b/Assets/Scripts/EntityStats.cs.meta new file mode 100644 index 0000000..0a1726f --- /dev/null +++ b/Assets/Scripts/EntityStats.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: f3b49b8d7dbc43dfbd074996aa811570 +timeCreated: 1648908811 \ No newline at end of file diff --git a/Assets/Scripts/PlayerMovement.cs b/Assets/Scripts/PlayerMovement.cs index 3a88661..4b623b0 100644 --- a/Assets/Scripts/PlayerMovement.cs +++ b/Assets/Scripts/PlayerMovement.cs @@ -1,10 +1,13 @@ #nullable enable +using NaughtyAttributes; using UnityEngine; using UnityEngine.InputSystem; [RequireComponent(typeof(PlayerInput), typeof(Rigidbody2D))] public class PlayerMovement : MonoBehaviour { - [SerializeField] PlayerStats stats = null!; + [SerializeField] [field: Required] + PlayerStats stats = null!; + [field: Required] Rigidbody2D rb = null!; Vector2 moveDirection; @@ -48,7 +51,7 @@ public class PlayerMovement : MonoBehaviour { return; if (safeZone.IsInSafeZone) { - if (moveDirection.magnitude >= safeZone.stats.MinJumpJoystickValue) + if (moveDirection.magnitude >= safeZone.Stats.MinJumpJoystickValue) SwitchState(new ExitSafeZoneMovementState(safeZone, moveDirection)); } else //TODO if (AngleBetween(moveDirection, toSafeZone) < 90) SwitchState(new EnterSafeZoneMovementState(safeZone)); @@ -139,7 +142,7 @@ public class PlayerMovement : MonoBehaviour { class EnterSafeZoneMovementState : JumpingMovementState { readonly SafeZone safeZone; - public EnterSafeZoneMovementState(SafeZone safeZone) : base(safeZone.stats.JumpDuration, safeZone.transform.position) { + public EnterSafeZoneMovementState(SafeZone safeZone) : base(safeZone.Stats.JumpDuration, safeZone.transform.position) { this.safeZone = safeZone; } @@ -152,12 +155,12 @@ public class PlayerMovement : MonoBehaviour { protected override BaseState Transition() => new ImmobileMovementState(); - protected override float ModifyLerpTime(float t) => safeZone.stats.JumpSpeedCurve.Evaluate(t); + protected override float ModifyLerpTime(float t) => safeZone.Stats.JumpSpeedCurve.Evaluate(t); } class ExitSafeZoneMovementState : JumpingMovementState { readonly SafeZone safeZone; - public ExitSafeZoneMovementState(SafeZone safeZone, Vector2 direction) : base(safeZone.stats.JumpDuration, safeZone.GetOutsidePosition(direction)) { + public ExitSafeZoneMovementState(SafeZone safeZone, Vector2 direction) : base(safeZone.Stats.JumpDuration, safeZone.GetOutsidePosition(direction)) { this.safeZone = safeZone; } @@ -168,7 +171,7 @@ public class PlayerMovement : MonoBehaviour { playerMovement.SetRigidbodyEnabled(true); } - protected override float ModifyLerpTime(float t) => safeZone.stats.JumpSpeedCurve.Evaluate(t); + protected override float ModifyLerpTime(float t) => safeZone.Stats.JumpSpeedCurve.Evaluate(t); } class ImmobileMovementState : BaseState { @@ -184,7 +187,7 @@ public class PlayerMovement : MonoBehaviour { return; Vector3 dropPosition = playerMovement.safeZone.GetOutsidePosition(playerMovement.moveDirection); - bool canJump = playerMovement.moveDirection.magnitude >= playerMovement.safeZone.stats.MinJumpJoystickValue; + bool canJump = playerMovement.moveDirection.magnitude >= playerMovement.safeZone.Stats.MinJumpJoystickValue; Gizmos.color = canJump ? Color.green : Color.red; Gizmos.DrawLine(playerMovement.transform.position, dropPosition); if (canJump) diff --git a/Assets/Scripts/SafeZone.cs b/Assets/Scripts/SafeZone.cs index bde3c07..bdcaa4d 100644 --- a/Assets/Scripts/SafeZone.cs +++ b/Assets/Scripts/SafeZone.cs @@ -1,7 +1,10 @@ +using NaughtyAttributes; using UnityEngine; public class SafeZone : MonoBehaviour { - public SafeZoneStats stats; + [field: SerializeField] [field: Required] + public SafeZoneStats Stats { get; private set; } + [SerializeField] CircleCollider2D moatCollider; public bool IsInSafeZone { get; private set; } = true; @@ -14,6 +17,6 @@ public class SafeZone : MonoBehaviour { } public Vector3 GetOutsidePosition(Vector2 direction) { - return transform.position + (moatCollider.radius + stats.JumpOffset) * (Vector3)direction; + return transform.position + (moatCollider.radius + Stats.JumpOffset) * (Vector3)direction; } } \ No newline at end of file diff --git a/Assets/Scripts/VampireEntity.cs b/Assets/Scripts/VampireEntity.cs index 51561e8..9af3ff2 100644 --- a/Assets/Scripts/VampireEntity.cs +++ b/Assets/Scripts/VampireEntity.cs @@ -1,9 +1,10 @@ -//TODO Replace with Damageable? - +using NaughtyAttributes; using UnityEngine; public class VampireEntity : Entity { - [SerializeField] HealthBar healthBar; + [SerializeField] [Required] + HealthBar healthBar; + [Min(10f)] float initialHealth; protected override void Start() { diff --git a/Packages/manifest.json b/Packages/manifest.json index 09feffa..de467d9 100644 --- a/Packages/manifest.json +++ b/Packages/manifest.json @@ -1,5 +1,6 @@ { "dependencies": { + "com.dbrizov.naughtyattributes": "https://github.com/dbrizov/NaughtyAttributes.git#upm", "com.unity.2d.animation": "5.1.1", "com.unity.2d.pixel-perfect": "4.0.1", "com.unity.2d.psdimporter": "4.2.0", diff --git a/Packages/packages-lock.json b/Packages/packages-lock.json index 0d61740..6e15d43 100644 --- a/Packages/packages-lock.json +++ b/Packages/packages-lock.json @@ -1,5 +1,12 @@ { "dependencies": { + "com.dbrizov.naughtyattributes": { + "version": "https://github.com/dbrizov/NaughtyAttributes.git#upm", + "depth": 0, + "source": "git", + "dependencies": {}, + "hash": "8a8fa5a9659a6d63f196391c71e06c4286c8acd7" + }, "com.unity.2d.animation": { "version": "5.1.1", "depth": 0, From fe334f8d1560833ee5eab58c94182addc248041a Mon Sep 17 00:00:00 2001 From: Jason Durand 01 Date: Sat, 2 Apr 2022 11:02:29 -0400 Subject: [PATCH 2/4] Added Entity.beingPushed() state without actually using it --- Assets/Scripts/Entity.cs | 14 +++++++++++--- Assets/Scripts/Monster.cs | 6 +++++- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/Assets/Scripts/Entity.cs b/Assets/Scripts/Entity.cs index 77a143b..891e2af 100644 --- a/Assets/Scripts/Entity.cs +++ b/Assets/Scripts/Entity.cs @@ -30,6 +30,14 @@ public class Entity : MonoBehaviour attackTimer = attackCooldown; } + protected virtual void FixedUpdate() { + //TODO sqrMagnitude? + if (beingPushed && rb.velocity.magnitude < stats.MinVelocityWhenPushed) { + rb.velocity = Vector2.zero; + beingPushed = false; + } + } + protected virtual void Attack(){ // jason: TODO Either have target be Entity instead of transform, or skip Attack when GetComponent() is null Entity targetEntity = target.GetComponent(); @@ -40,11 +48,11 @@ public class Entity : MonoBehaviour } - protected virtual void MoveToTarget(float deltaTime){ + protected virtual void MoveToTarget(){ - direction = Vector3.RotateTowards(direction, (target.position - transform.position), rotSpeed*deltaTime, 0.0f); + direction = Vector3.RotateTowards(direction, (target.position - transform.position), rotSpeed*Time.fixedDeltaTime, 0.0f); if(!IsInAttackRange()){ - rb.MovePosition(transform.position + direction * movementSpeed * deltaTime); + rb.MovePosition(transform.position + direction * movementSpeed * Time.fixedDeltaTime); } } diff --git a/Assets/Scripts/Monster.cs b/Assets/Scripts/Monster.cs index 77bd059..51e4937 100644 --- a/Assets/Scripts/Monster.cs +++ b/Assets/Scripts/Monster.cs @@ -11,7 +11,11 @@ public class Monster : Entity base.SetName("Monster"); } - void FixedUpdate() => MoveToTarget(Time.fixedDeltaTime); + protected override void FixedUpdate() { + base.FixedUpdate(); + + MoveToTarget(); + } // Update is called once per frame void Update() From 7ac89eb72f4a09589bdda2cbb39f829f6c952b3c Mon Sep 17 00:00:00 2001 From: Jason Durand 01 Date: Sat, 2 Apr 2022 12:04:56 -0400 Subject: [PATCH 3/4] Added rough paused state --- Assets/GameFlowManager.cs | 11 +++++++ Assets/GameFlowManager.cs.meta | 11 +++++++ Assets/Scenes/SampleScene.unity | 30 +++++++++++++++++ Assets/Scripts/Arena.cs | 12 +++++-- Assets/Scripts/Entity.cs | 3 ++ Assets/Scripts/PlayerMovement.cs | 55 ++++++++++++++++++++++++-------- 6 files changed, 105 insertions(+), 17 deletions(-) create mode 100644 Assets/GameFlowManager.cs create mode 100644 Assets/GameFlowManager.cs.meta diff --git a/Assets/GameFlowManager.cs b/Assets/GameFlowManager.cs new file mode 100644 index 0000000..792f185 --- /dev/null +++ b/Assets/GameFlowManager.cs @@ -0,0 +1,11 @@ +using UnityEngine; + +//Could be a singleton +public class GameFlowManager : MonoBehaviour { + [field: SerializeField] public bool Paused { get; private set; } = true; + + void SetPause(bool value) { + Paused = value; + Time.timeScale = value ? 0f : 1f; + } +} \ No newline at end of file diff --git a/Assets/GameFlowManager.cs.meta b/Assets/GameFlowManager.cs.meta new file mode 100644 index 0000000..17ff82d --- /dev/null +++ b/Assets/GameFlowManager.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: e3fecdc4a8b2cb4419ef9d03180d130d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 236943a..762f234 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -200,6 +200,10 @@ PrefabInstance: m_Modification: m_TransformParent: {fileID: 0} m_Modifications: + - target: {fileID: -7596782781093632548, guid: 581322f036f3ff1448d4d2ec70f295a4, type: 3} + propertyPath: gameFlowManager + value: + objectReference: {fileID: 1359990806} - target: {fileID: 9196727425507610130, guid: 581322f036f3ff1448d4d2ec70f295a4, type: 3} propertyPath: m_RootOrder value: 2 @@ -250,6 +254,24 @@ PrefabInstance: objectReference: {fileID: 0} m_RemovedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: 581322f036f3ff1448d4d2ec70f295a4, type: 3} +--- !u!1 &1359990805 stripped +GameObject: + m_CorrespondingSourceObject: {fileID: 9196727425507610131, guid: 581322f036f3ff1448d4d2ec70f295a4, type: 3} + m_PrefabInstance: {fileID: 1359990804} + m_PrefabAsset: {fileID: 0} +--- !u!114 &1359990806 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1359990805} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: e3fecdc4a8b2cb4419ef9d03180d130d, type: 3} + m_Name: + m_EditorClassIdentifier: + k__BackingField: 1 --- !u!114 &1464970062 stripped MonoBehaviour: m_CorrespondingSourceObject: {fileID: 1878107874314509256, guid: e1dac4f28fe75a547b919b7aa8240fed, type: 3} @@ -424,6 +446,10 @@ PrefabInstance: propertyPath: m_ActionEvents.Array.data[13].m_PersistentCalls.m_Calls.Array.data[0].m_Arguments.m_ObjectArgumentAssemblyTypeName value: UnityEngine.Object, UnityEngine objectReference: {fileID: 0} + - target: {fileID: 1214567908930553592, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3} + propertyPath: gameFlowManager + value: + objectReference: {fileID: 1359990806} - target: {fileID: 1214567908930553593, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3} propertyPath: m_Name value: Vampire @@ -480,6 +506,10 @@ PrefabInstance: propertyPath: healthBar value: objectReference: {fileID: 1464970062} + - target: {fileID: 3126145803593047825, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3} + propertyPath: gameFlowManager + value: + objectReference: {fileID: 1359990806} - target: {fileID: 3126145803593047825, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3} propertyPath: k__BackingField value: 100 diff --git a/Assets/Scripts/Arena.cs b/Assets/Scripts/Arena.cs index d6a18ac..da1bc49 100644 --- a/Assets/Scripts/Arena.cs +++ b/Assets/Scripts/Arena.cs @@ -4,8 +4,12 @@ using NaughtyAttributes; using UnityEngine; public class Arena : MonoBehaviour { - //TODO probably add initial direction too [SerializeField] [Required] + GameFlowManager gameFlowManager = null!; + + //TODO probably add initial direction too + //TODO Add some kind of "MinLength(1)" attribute + [SerializeField] Vector3[] spawners = null!; [SerializeField] [Required] ArenaStats stats = null!; @@ -19,6 +23,9 @@ public class Arena : MonoBehaviour { void Start() => StartCoroutine(SpawnEnemies()); void SpawnEnemy(int spawnerIndex) { + if (gameFlowManager.Paused) + return; + var monster = Instantiate(monsterPrefab, spawners[spawnerIndex], Quaternion.identity).GetComponent(); //TODO Replace hardcoded target with entity discovery monster.SetTarget(FindObjectOfType().transform); @@ -29,11 +36,10 @@ public class Arena : MonoBehaviour { int currentSpawner = 0; - //TODO Stop when pause/end game while (true) { SpawnEnemy(currentSpawner); currentSpawner = Random.Range(0, spawners.Length); - yield return new WaitForSeconds(stats.secondsBetweenSpawners); + yield return new WaitForSeconds(stats.secondsBetweenSpawners); } } diff --git a/Assets/Scripts/Entity.cs b/Assets/Scripts/Entity.cs index 891e2af..a6fec90 100644 --- a/Assets/Scripts/Entity.cs +++ b/Assets/Scripts/Entity.cs @@ -6,6 +6,9 @@ using UnityEngine; [RequireComponent(typeof(Rigidbody2D))] public class Entity : MonoBehaviour { + [SerializeField] [Required] + GameFlowManager gameFlowManager = null!; + [field: SerializeField] [field: Required] protected EntityStats stats { get; private set; } [field: SerializeField]protected float Health { get; private set; } diff --git a/Assets/Scripts/PlayerMovement.cs b/Assets/Scripts/PlayerMovement.cs index 4b623b0..84dd8f6 100644 --- a/Assets/Scripts/PlayerMovement.cs +++ b/Assets/Scripts/PlayerMovement.cs @@ -5,33 +5,48 @@ using UnityEngine.InputSystem; [RequireComponent(typeof(PlayerInput), typeof(Rigidbody2D))] public class PlayerMovement : MonoBehaviour { + [SerializeField] [Required] + GameFlowManager gameFlowManager = null!; + [SerializeField] [field: Required] PlayerStats stats = null!; + [field: Required] Rigidbody2D rb = null!; - + Vector2 moveDirection; BaseState currentState = new ImmobileMovementState(); SafeZone? safeZone; bool lastJumpButton; + #region Unity Messages void Awake() => rb = GetComponent(); void Start() => currentState.EnterState(this); void Update() { + if (gameFlowManager.Paused) + return; + if (currentState.UpdateState(this) is {} newState) SwitchState(newState); } void FixedUpdate() { + if (gameFlowManager.Paused) + return; + if (currentState.FixedUpdateState(this) is {} newState) SwitchState(newState); } void OnDrawGizmos() => currentState.OnDrawGizmos(this); + #endregion + + #region Inputs + public void OnMove(InputAction.CallbackContext ctx) { moveDirection = ctx.ReadValue(); if (moveDirection.sqrMagnitude > 1.0f) @@ -46,7 +61,7 @@ public class PlayerMovement : MonoBehaviour { if (!wasJustPressed) return; - + if (safeZone == null) return; @@ -57,11 +72,9 @@ public class PlayerMovement : MonoBehaviour { SwitchState(new EnterSafeZoneMovementState(safeZone)); } - void SwitchState(BaseState newState) { - currentState.LeaveState(this); - currentState = newState; - newState.EnterState(this); - } + #endregion + + #region Rigidbody void OnTriggerEnter2D(Collider2D other) { if (other.GetComponent() is {} triggeredSafeZone) @@ -85,6 +98,16 @@ public class PlayerMovement : MonoBehaviour { rb.isKinematic = !enabled; } + #endregion + + #region States + + void SwitchState(BaseState newState) { + currentState.LeaveState(this); + currentState = newState; + newState.EnterState(this); + } + class BaseState { public virtual void EnterState(PlayerMovement playerMovement) {} @@ -93,14 +116,14 @@ public class PlayerMovement : MonoBehaviour { public virtual BaseState? UpdateState(PlayerMovement playerMovement) => null; public virtual BaseState? FixedUpdateState(PlayerMovement playerMovement) => null; - + public virtual void OnDrawGizmos(PlayerMovement playerMovement) {} } class NormalMovementState : BaseState { public override BaseState? FixedUpdateState(PlayerMovement playerMovement) { playerMovement.rb.velocity = (Vector3)playerMovement.moveDirection * playerMovement.stats.movementSpeed; - + return null; } } @@ -126,7 +149,7 @@ public class PlayerMovement : MonoBehaviour { float currentTime = Time.time - startTime; if (currentTime >= duration) return Transition(); - + playerMovement.rb.MovePosition(Vector3.Lerp( startPosition, target, @@ -142,17 +165,18 @@ public class PlayerMovement : MonoBehaviour { class EnterSafeZoneMovementState : JumpingMovementState { readonly SafeZone safeZone; + public EnterSafeZoneMovementState(SafeZone safeZone) : base(safeZone.Stats.JumpDuration, safeZone.transform.position) { this.safeZone = safeZone; } public override void EnterState(PlayerMovement playerMovement) { base.EnterState(playerMovement); - + safeZone.EnterSafeZone(); playerMovement.SetRigidbodyEnabled(false); } - + protected override BaseState Transition() => new ImmobileMovementState(); protected override float ModifyLerpTime(float t) => safeZone.Stats.JumpSpeedCurve.Evaluate(t); @@ -160,13 +184,14 @@ public class PlayerMovement : MonoBehaviour { class ExitSafeZoneMovementState : JumpingMovementState { readonly SafeZone safeZone; + public ExitSafeZoneMovementState(SafeZone safeZone, Vector2 direction) : base(safeZone.Stats.JumpDuration, safeZone.GetOutsidePosition(direction)) { this.safeZone = safeZone; } public override void LeaveState(PlayerMovement playerMovement) { base.EnterState(playerMovement); - + safeZone.ExitSafeZone(); playerMovement.SetRigidbodyEnabled(true); } @@ -177,7 +202,7 @@ public class PlayerMovement : MonoBehaviour { class ImmobileMovementState : BaseState { public override void EnterState(PlayerMovement playerMovement) { base.EnterState(playerMovement); - + if (!playerMovement.rb.isKinematic) Debug.LogWarning("Rigidbody should probably be kinematic when immobile (when in safe zone)."); } @@ -197,4 +222,6 @@ public class PlayerMovement : MonoBehaviour { } #endif } + + #endregion } \ No newline at end of file From c6b9e5da93de61a13081aaa89f7508fe279940b2 Mon Sep 17 00:00:00 2001 From: Jason Durand 01 Date: Sat, 2 Apr 2022 12:51:53 -0400 Subject: [PATCH 4/4] Added Press Start to start (without prompt though) --- Assets/GameFlowManager.cs | 82 ++++++++++++++++++- Assets/Prefabs/Arena.prefab | 3 +- Assets/Scenes/SampleScene.unity | 38 ++++++++- Assets/Scripts/PlayerMovement.cs | 4 +- .../Settings/ConjureLudumDare50.inputactions | 31 +++++++ 5 files changed, 153 insertions(+), 5 deletions(-) diff --git a/Assets/GameFlowManager.cs b/Assets/GameFlowManager.cs index 792f185..a8ad5f2 100644 --- a/Assets/GameFlowManager.cs +++ b/Assets/GameFlowManager.cs @@ -1,11 +1,91 @@ using UnityEngine; +using UnityEngine.InputSystem; //Could be a singleton public class GameFlowManager : MonoBehaviour { - [field: SerializeField] public bool Paused { get; private set; } = true; + [field: SerializeField] + public bool Paused { get; private set; } = true; + public BaseState CurrentState { get; private set; } = null!; + bool lastStartButton; + + #region Unity Messages + void Awake() => CurrentState = new StartFlowState(this); + + void Start() => CurrentState.EnterState(); + #endregion void SetPause(bool value) { Paused = value; Time.timeScale = value ? 0f : 1f; } + + #region Inputs + public void OnStart(InputAction.CallbackContext ctx) { + //feels pretty redundant ^^' + bool newValue = ctx.ReadValueAsButton(); + bool wasJustPressed = !lastStartButton && newValue; + lastStartButton = newValue; + + if (!wasJustPressed) + return; + + if (CurrentState is StartFlowState) + SwitchState(new GameplayFlowState(this)); + } + #endregion + + #region States + void SwitchState(BaseState newState) { + CurrentState.LeaveState(); + CurrentState = newState; + newState.EnterState(); + } + + abstract class GameFlowState : BaseState { + readonly protected GameFlowManager gameFlowManager; + + protected GameFlowState(GameFlowManager gameFlowManager) { + this.gameFlowManager = gameFlowManager; + } + } + + class StartFlowState : GameFlowState { + public StartFlowState(GameFlowManager gameFlowManager) : base(gameFlowManager) {} + + public override void EnterState() { + base.EnterState(); + + Debug.Log("Press Start to start...!"); + gameFlowManager.SetPause(true); + } + + public override void LeaveState() { + Debug.Log("Let the games begin!!"); + } + } + class GameplayFlowState : GameFlowState { + public GameplayFlowState(GameFlowManager gameFlowManager) : base(gameFlowManager) {} + + public override void EnterState() { + base.EnterState(); + gameFlowManager.SetPause(false); + } + } + class PauseMenuFlowState : GameFlowState { + public PauseMenuFlowState(GameFlowManager gameFlowManager) : base(gameFlowManager) {} + + public override void EnterState() { + base.EnterState(); + gameFlowManager.SetPause(true); + } + } + class DeadFlowState : GameFlowState { + public DeadFlowState(GameFlowManager gameFlowManager) : base(gameFlowManager) {} + + public override void EnterState() { + base.EnterState(); + gameFlowManager.SetPause(true); + } + } + #endregion } \ No newline at end of file diff --git a/Assets/Prefabs/Arena.prefab b/Assets/Prefabs/Arena.prefab index 0a3af7b..6498ff6 100644 --- a/Assets/Prefabs/Arena.prefab +++ b/Assets/Prefabs/Arena.prefab @@ -246,7 +246,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 63e66d66543b37e419694ac22a2941fd, type: 3} m_Name: m_EditorClassIdentifier: - stats: {fileID: 11400000, guid: 188711c0b201b1a45ad601ee20233b20, type: 2} + k__BackingField: {fileID: 11400000, guid: 188711c0b201b1a45ad601ee20233b20, type: 2} moatCollider: {fileID: 9196727423716235687} --- !u!1 &9196727423754814335 GameObject: @@ -1752,6 +1752,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 72794012913ccd840a73788b90573212, type: 3} m_Name: m_EditorClassIdentifier: + gameFlowManager: {fileID: 0} spawners: - {x: -10, y: 10, z: 0} - {x: 10, y: 10, z: 0} diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 762f234..3d38c30 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -408,44 +408,80 @@ PrefabInstance: m_Modifications: - target: {fileID: 1214567908930553477, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3} propertyPath: m_ActionEvents.Array.size - value: 14 + value: 15 objectReference: {fileID: 0} - target: {fileID: 1214567908930553477, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3} propertyPath: m_ActionEvents.Array.data[13].m_ActionId value: d0405457-c534-4103-a0b6-cf113432b467 objectReference: {fileID: 0} + - target: {fileID: 1214567908930553477, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3} + propertyPath: m_ActionEvents.Array.data[14].m_ActionId + value: 01a06960-a379-49e3-9d58-9b7c8effcb3d + objectReference: {fileID: 0} - target: {fileID: 1214567908930553477, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3} propertyPath: m_ActionEvents.Array.data[13].m_ActionName value: Player/SwitchMinion[/Keyboard/q,/Keyboard/e,/XInputControllerWindows/leftShoulder,/XInputControllerWindows/rightShoulder] objectReference: {fileID: 0} + - target: {fileID: 1214567908930553477, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3} + propertyPath: m_ActionEvents.Array.data[14].m_ActionName + value: Player/Start[/XInputControllerWindows/start,/Keyboard/enter] + objectReference: {fileID: 0} - target: {fileID: 1214567908930553477, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3} propertyPath: m_ActionEvents.Array.data[13].m_PersistentCalls.m_Calls.Array.size value: 1 objectReference: {fileID: 0} + - target: {fileID: 1214567908930553477, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3} + propertyPath: m_ActionEvents.Array.data[14].m_PersistentCalls.m_Calls.Array.size + value: 1 + objectReference: {fileID: 0} - target: {fileID: 1214567908930553477, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3} propertyPath: m_ActionEvents.Array.data[13].m_PersistentCalls.m_Calls.Array.data[0].m_Mode value: 0 objectReference: {fileID: 0} + - target: {fileID: 1214567908930553477, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3} + propertyPath: m_ActionEvents.Array.data[14].m_PersistentCalls.m_Calls.Array.data[0].m_Mode + value: 0 + objectReference: {fileID: 0} - target: {fileID: 1214567908930553477, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3} propertyPath: m_ActionEvents.Array.data[13].m_PersistentCalls.m_Calls.Array.data[0].m_Target value: objectReference: {fileID: 1551362087} + - target: {fileID: 1214567908930553477, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3} + propertyPath: m_ActionEvents.Array.data[14].m_PersistentCalls.m_Calls.Array.data[0].m_Target + value: + objectReference: {fileID: 1359990806} - target: {fileID: 1214567908930553477, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3} propertyPath: m_ActionEvents.Array.data[13].m_PersistentCalls.m_Calls.Array.data[0].m_CallState value: 2 objectReference: {fileID: 0} + - target: {fileID: 1214567908930553477, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3} + propertyPath: m_ActionEvents.Array.data[14].m_PersistentCalls.m_Calls.Array.data[0].m_CallState + value: 2 + objectReference: {fileID: 0} - target: {fileID: 1214567908930553477, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3} propertyPath: m_ActionEvents.Array.data[13].m_PersistentCalls.m_Calls.Array.data[0].m_MethodName value: ChangeSelectedIcon objectReference: {fileID: 0} + - target: {fileID: 1214567908930553477, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3} + propertyPath: m_ActionEvents.Array.data[14].m_PersistentCalls.m_Calls.Array.data[0].m_MethodName + value: OnStart + objectReference: {fileID: 0} - target: {fileID: 1214567908930553477, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3} propertyPath: m_ActionEvents.Array.data[13].m_PersistentCalls.m_Calls.Array.data[0].m_TargetAssemblyTypeName value: MinionBar, Assembly-CSharp objectReference: {fileID: 0} + - target: {fileID: 1214567908930553477, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3} + propertyPath: m_ActionEvents.Array.data[14].m_PersistentCalls.m_Calls.Array.data[0].m_TargetAssemblyTypeName + value: GameFlowManager, Assembly-CSharp + objectReference: {fileID: 0} - target: {fileID: 1214567908930553477, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3} propertyPath: m_ActionEvents.Array.data[13].m_PersistentCalls.m_Calls.Array.data[0].m_Arguments.m_ObjectArgumentAssemblyTypeName value: UnityEngine.Object, UnityEngine objectReference: {fileID: 0} + - target: {fileID: 1214567908930553477, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3} + propertyPath: m_ActionEvents.Array.data[14].m_PersistentCalls.m_Calls.Array.data[0].m_Arguments.m_ObjectArgumentAssemblyTypeName + value: UnityEngine.Object, UnityEngine + objectReference: {fileID: 0} - target: {fileID: 1214567908930553592, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3} propertyPath: gameFlowManager value: diff --git a/Assets/Scripts/PlayerMovement.cs b/Assets/Scripts/PlayerMovement.cs index 43d2536..e0ac5b2 100644 --- a/Assets/Scripts/PlayerMovement.cs +++ b/Assets/Scripts/PlayerMovement.cs @@ -167,7 +167,7 @@ public class PlayerMovement : MonoBehaviour { class EnterSafeZoneMovementState : JumpingMovementState { readonly SafeZone safeZone; - public EnterSafeZoneMovementState(PlayerMovement playerMovement, SafeZone safeZone) : base(playerMovement, safeZone.stats.JumpDuration, safeZone.transform.position) { + public EnterSafeZoneMovementState(PlayerMovement playerMovement, SafeZone safeZone) : base(playerMovement, safeZone.Stats.JumpDuration, safeZone.transform.position) { this.safeZone = safeZone; } @@ -185,7 +185,7 @@ public class PlayerMovement : MonoBehaviour { class ExitSafeZoneMovementState : JumpingMovementState { readonly SafeZone safeZone; - public ExitSafeZoneMovementState(PlayerMovement playerMovement, SafeZone safeZone, Vector2 direction) : base(playerMovement, safeZone.stats.JumpDuration, safeZone.GetOutsidePosition(direction)) { + public ExitSafeZoneMovementState(PlayerMovement playerMovement, SafeZone safeZone, Vector2 direction) : base(playerMovement, safeZone.Stats.JumpDuration, safeZone.GetOutsidePosition(direction)) { this.safeZone = safeZone; } diff --git a/Assets/Settings/ConjureLudumDare50.inputactions b/Assets/Settings/ConjureLudumDare50.inputactions index 92bc982..1f0a6b1 100644 --- a/Assets/Settings/ConjureLudumDare50.inputactions +++ b/Assets/Settings/ConjureLudumDare50.inputactions @@ -40,6 +40,15 @@ "processors": "", "interactions": "", "initialStateCheck": false + }, + { + "name": "Start", + "type": "Button", + "id": "01a06960-a379-49e3-9d58-9b7c8effcb3d", + "expectedControlType": "Button", + "processors": "", + "interactions": "", + "initialStateCheck": false } ], "bindings": [ @@ -284,6 +293,28 @@ "action": "SwitchMinion", "isComposite": false, "isPartOfComposite": true + }, + { + "name": "", + "id": "4715f838-717a-4f10-a668-05a6d761a7bc", + "path": "/start", + "interactions": "", + "processors": "", + "groups": "Gamepad", + "action": "Start", + "isComposite": false, + "isPartOfComposite": false + }, + { + "name": "", + "id": "025fe243-6da7-4b27-9c02-f353d7a121df", + "path": "/enter", + "interactions": "", + "processors": "", + "groups": "Keyboard&Mouse", + "action": "Start", + "isComposite": false, + "isPartOfComposite": false } ] },