Merge dev

This commit is contained in:
Yann Dupont 01 2022-04-02 13:52:10 -04:00
commit afbd15217a
15 changed files with 353 additions and 54 deletions

95
Assets/GameFlowManager.cs Normal file
View File

@ -0,0 +1,95 @@
using UnityEngine;
using UnityEngine.InputSystem;
//Could be a singleton
public class GameFlowManager : MonoBehaviour {
/// <summary>
/// True if time is frozen (Start, Pause Menu, Dead)
/// </summary>
/// <remarks>Could be renamed appropriately</remarks>
[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
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e3fecdc4a8b2cb4419ef9d03180d130d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -246,7 +246,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 63e66d66543b37e419694ac22a2941fd, type: 3} m_Script: {fileID: 11500000, guid: 63e66d66543b37e419694ac22a2941fd, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
stats: {fileID: 11400000, guid: 188711c0b201b1a45ad601ee20233b20, type: 2} <Stats>k__BackingField: {fileID: 11400000, guid: 188711c0b201b1a45ad601ee20233b20, type: 2}
moatCollider: {fileID: 9196727423716235687} moatCollider: {fileID: 9196727423716235687}
--- !u!1 &9196727423754814335 --- !u!1 &9196727423754814335
GameObject: GameObject:
@ -1752,6 +1752,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 72794012913ccd840a73788b90573212, type: 3} m_Script: {fileID: 11500000, guid: 72794012913ccd840a73788b90573212, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
gameFlowManager: {fileID: 0}
spawners: spawners:
- {x: -10, y: 10, z: 0} - {x: -10, y: 10, z: 0}
- {x: 10, y: 10, z: 0} - {x: 10, y: 10, z: 0}

View File

@ -211,6 +211,10 @@ PrefabInstance:
m_Modification: m_Modification:
m_TransformParent: {fileID: 0} m_TransformParent: {fileID: 0}
m_Modifications: m_Modifications:
- target: {fileID: -7596782781093632548, guid: 581322f036f3ff1448d4d2ec70f295a4, type: 3}
propertyPath: gameFlowManager
value:
objectReference: {fileID: 1359990806}
- target: {fileID: 4164153230343464235, guid: 581322f036f3ff1448d4d2ec70f295a4, type: 3} - target: {fileID: 4164153230343464235, guid: 581322f036f3ff1448d4d2ec70f295a4, type: 3}
propertyPath: globalCamera propertyPath: globalCamera
value: value:
@ -265,6 +269,24 @@ PrefabInstance:
objectReference: {fileID: 0} objectReference: {fileID: 0}
m_RemovedComponents: [] m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: 581322f036f3ff1448d4d2ec70f295a4, type: 3} 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:
<Paused>k__BackingField: 1
--- !u!114 &1464970062 stripped --- !u!114 &1464970062 stripped
MonoBehaviour: MonoBehaviour:
m_CorrespondingSourceObject: {fileID: 1878107874314509256, guid: e1dac4f28fe75a547b919b7aa8240fed, type: 3} m_CorrespondingSourceObject: {fileID: 1878107874314509256, guid: e1dac4f28fe75a547b919b7aa8240fed, type: 3}
@ -406,20 +428,44 @@ PrefabInstance:
m_Modifications: m_Modifications:
- target: {fileID: 1214567908930553477, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3} - target: {fileID: 1214567908930553477, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3}
propertyPath: m_ActionEvents.Array.size propertyPath: m_ActionEvents.Array.size
value: 15 value: 16
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 1214567908930553477, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3} - target: {fileID: 1214567908930553477, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3}
propertyPath: m_ActionEvents.Array.data[13].m_ActionId propertyPath: m_ActionEvents.Array.data[13].m_ActionId
value: d0405457-c534-4103-a0b6-cf113432b467 value: d0405457-c534-4103-a0b6-cf113432b467
objectReference: {fileID: 0} 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[15].m_ActionId
value: f7e5243b-5304-4287-a2f5-0d36470450ad
objectReference: {fileID: 0}
- target: {fileID: 1214567908930553477, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3} - target: {fileID: 1214567908930553477, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3}
propertyPath: m_ActionEvents.Array.data[13].m_ActionName propertyPath: m_ActionEvents.Array.data[13].m_ActionName
value: Player/SwitchMinion[/Keyboard/q,/Keyboard/e,/XInputControllerWindows/leftShoulder,/XInputControllerWindows/rightShoulder] value: Player/SwitchMinion[/Keyboard/q,/Keyboard/e,/XInputControllerWindows/leftShoulder,/XInputControllerWindows/rightShoulder]
objectReference: {fileID: 0} 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[15].m_ActionName
value: Player/Throw[/Keyboard/r,/XInputControllerWindows/buttonNorth]
objectReference: {fileID: 0}
- target: {fileID: 1214567908930553477, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3} - target: {fileID: 1214567908930553477, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3}
propertyPath: m_ActionEvents.Array.data[13].m_PersistentCalls.m_Calls.Array.size propertyPath: m_ActionEvents.Array.data[13].m_PersistentCalls.m_Calls.Array.size
value: 1 value: 1
objectReference: {fileID: 0} 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[15].m_PersistentCalls.m_Calls.Array.size
value: 1
objectReference: {fileID: 0}
- target: {fileID: 1214567908930553477, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3} - target: {fileID: 1214567908930553477, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3}
propertyPath: m_ActionEvents.Array.data[13].m_PersistentCalls.m_Calls.Array.data[0].m_Mode propertyPath: m_ActionEvents.Array.data[13].m_PersistentCalls.m_Calls.Array.data[0].m_Mode
value: 0 value: 0
@ -428,6 +474,10 @@ PrefabInstance:
propertyPath: m_ActionEvents.Array.data[14].m_PersistentCalls.m_Calls.Array.data[0].m_Mode propertyPath: m_ActionEvents.Array.data[14].m_PersistentCalls.m_Calls.Array.data[0].m_Mode
value: 0 value: 0
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 1214567908930553477, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3}
propertyPath: m_ActionEvents.Array.data[15].m_PersistentCalls.m_Calls.Array.data[0].m_Mode
value: 0
objectReference: {fileID: 0}
- target: {fileID: 1214567908930553477, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3} - target: {fileID: 1214567908930553477, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3}
propertyPath: m_ActionEvents.Array.data[13].m_PersistentCalls.m_Calls.Array.data[0].m_Target propertyPath: m_ActionEvents.Array.data[13].m_PersistentCalls.m_Calls.Array.data[0].m_Target
value: value:
@ -435,6 +485,10 @@ PrefabInstance:
- target: {fileID: 1214567908930553477, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3} - target: {fileID: 1214567908930553477, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3}
propertyPath: m_ActionEvents.Array.data[14].m_PersistentCalls.m_Calls.Array.data[0].m_Target propertyPath: m_ActionEvents.Array.data[14].m_PersistentCalls.m_Calls.Array.data[0].m_Target
value: value:
objectReference: {fileID: 1359990806}
- target: {fileID: 1214567908930553477, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3}
propertyPath: m_ActionEvents.Array.data[15].m_PersistentCalls.m_Calls.Array.data[0].m_Target
value:
objectReference: {fileID: 330576709} objectReference: {fileID: 330576709}
- target: {fileID: 1214567908930553477, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3} - target: {fileID: 1214567908930553477, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3}
propertyPath: m_ActionEvents.Array.data[13].m_PersistentCalls.m_Calls.Array.data[0].m_CallState propertyPath: m_ActionEvents.Array.data[13].m_PersistentCalls.m_Calls.Array.data[0].m_CallState
@ -444,12 +498,20 @@ PrefabInstance:
propertyPath: m_ActionEvents.Array.data[14].m_PersistentCalls.m_Calls.Array.data[0].m_CallState propertyPath: m_ActionEvents.Array.data[14].m_PersistentCalls.m_Calls.Array.data[0].m_CallState
value: 2 value: 2
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 1214567908930553477, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3}
propertyPath: m_ActionEvents.Array.data[15].m_PersistentCalls.m_Calls.Array.data[0].m_CallState
value: 2
objectReference: {fileID: 0}
- target: {fileID: 1214567908930553477, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3} - target: {fileID: 1214567908930553477, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3}
propertyPath: m_ActionEvents.Array.data[13].m_PersistentCalls.m_Calls.Array.data[0].m_MethodName propertyPath: m_ActionEvents.Array.data[13].m_PersistentCalls.m_Calls.Array.data[0].m_MethodName
value: ChangeSelectedIcon value: ChangeSelectedIcon
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 1214567908930553477, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3} - target: {fileID: 1214567908930553477, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3}
propertyPath: m_ActionEvents.Array.data[14].m_PersistentCalls.m_Calls.Array.data[0].m_MethodName 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[15].m_PersistentCalls.m_Calls.Array.data[0].m_MethodName
value: ToggleThrowMode value: ToggleThrowMode
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 1214567908930553477, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3} - target: {fileID: 1214567908930553477, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3}
@ -458,6 +520,10 @@ PrefabInstance:
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 1214567908930553477, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3} - target: {fileID: 1214567908930553477, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3}
propertyPath: m_ActionEvents.Array.data[14].m_PersistentCalls.m_Calls.Array.data[0].m_TargetAssemblyTypeName 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[15].m_PersistentCalls.m_Calls.Array.data[0].m_TargetAssemblyTypeName
value: MinionThrower, Assembly-CSharp value: MinionThrower, Assembly-CSharp
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 1214567908930553477, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3} - target: {fileID: 1214567908930553477, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3}
@ -468,6 +534,14 @@ PrefabInstance:
propertyPath: m_ActionEvents.Array.data[14].m_PersistentCalls.m_Calls.Array.data[0].m_Arguments.m_ObjectArgumentAssemblyTypeName propertyPath: m_ActionEvents.Array.data[14].m_PersistentCalls.m_Calls.Array.data[0].m_Arguments.m_ObjectArgumentAssemblyTypeName
value: UnityEngine.Object, UnityEngine value: UnityEngine.Object, UnityEngine
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 1214567908930553477, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3}
propertyPath: m_ActionEvents.Array.data[15].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} - target: {fileID: 1214567908930553593, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3}
propertyPath: m_Name propertyPath: m_Name
value: Vampire value: Vampire
@ -524,6 +598,10 @@ PrefabInstance:
propertyPath: healthBar propertyPath: healthBar
value: value:
objectReference: {fileID: 1464970062} objectReference: {fileID: 1464970062}
- target: {fileID: 3126145803593047825, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3}
propertyPath: gameFlowManager
value:
objectReference: {fileID: 1359990806}
- target: {fileID: 3126145803593047825, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3} - target: {fileID: 3126145803593047825, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3}
propertyPath: <Health>k__BackingField propertyPath: <Health>k__BackingField
value: 100 value: 100

View File

@ -1,12 +1,20 @@
#nullable enable #nullable enable
using System.Collections; using System.Collections;
using NaughtyAttributes;
using UnityEngine; using UnityEngine;
public class Arena : MonoBehaviour { public class Arena : MonoBehaviour {
[SerializeField] [Required]
GameFlowManager gameFlowManager = null!;
//TODO probably add initial direction too //TODO probably add initial direction too
[SerializeField] Vector3[] spawners = null!; //TODO Add some kind of "MinLength(1)" attribute
[SerializeField] ArenaStats stats = null!; [SerializeField]
[SerializeField] GameObject monsterPrefab = null!; Vector3[] spawners = null!;
[SerializeField] [Required]
ArenaStats stats = null!;
[SerializeField] [Required]
GameObject monsterPrefab = null!;
SafeZone safeZone = null!; SafeZone safeZone = null!;
@ -15,6 +23,9 @@ public class Arena : MonoBehaviour {
void Start() => StartCoroutine(SpawnEnemies()); void Start() => StartCoroutine(SpawnEnemies());
void SpawnEnemy(int spawnerIndex) { void SpawnEnemy(int spawnerIndex) {
if (gameFlowManager.Paused)
return;
var monster = Instantiate(monsterPrefab, spawners[spawnerIndex], Quaternion.identity).GetComponent<Monster>(); var monster = Instantiate(monsterPrefab, spawners[spawnerIndex], Quaternion.identity).GetComponent<Monster>();
//TODO Replace hardcoded target with entity discovery //TODO Replace hardcoded target with entity discovery
monster.SetTarget(FindObjectOfType<PlayerMovement>().transform); monster.SetTarget(FindObjectOfType<PlayerMovement>().transform);
@ -25,7 +36,6 @@ public class Arena : MonoBehaviour {
int currentSpawner = 0; int currentSpawner = 0;
//TODO Stop when pause/end game
while (true) { while (true) {
SpawnEnemy(currentSpawner); SpawnEnemy(currentSpawner);
currentSpawner = Random.Range(0, spawners.Length); currentSpawner = Random.Range(0, spawners.Length);

View File

@ -1,18 +1,24 @@
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using NaughtyAttributes;
using UnityEngine; using UnityEngine;
[RequireComponent(typeof(Rigidbody2D))] [RequireComponent(typeof(Rigidbody2D))]
public class Entity : MonoBehaviour public class Entity : MonoBehaviour
{ {
[SerializeField] [Required]
GameFlowManager gameFlowManager = null!;
[field: SerializeField] [field: Required]
protected EntityStats stats { get; private set; }
[field: SerializeField]public float Health {get; private set; } [field: SerializeField]public float Health {get; private set; }
public int bloodTokens = 1;
bool isAlive = true; bool isAlive = true;
[SerializeField]public float movementSpeed{get; private set; } public int bloodTokens = 1;
[SerializeField]public float rotSpeed {get; private set; } [field: SerializeField]public float movementSpeed{get; private set; }
[field: SerializeField]public float rotSpeed {get; private set; }
[SerializeField]private float fov; [SerializeField]private float fov;
[SerializeField]private float attackRange; [SerializeField]private float attackRange;
[SerializeField]public float attackDmg {get; private set; } [field: SerializeField]public float attackDmg {get; private set; }
[SerializeField]protected float attackCooldown; [SerializeField]protected float attackCooldown;
protected float attackTimer; protected float attackTimer;
[SerializeField]private Transform target; [SerializeField]private Transform target;
@ -20,6 +26,7 @@ public class Entity : MonoBehaviour
private Collider atkCollider; private Collider atkCollider;
public Vector3 direction {get; set; } public Vector3 direction {get; set; }
public Rigidbody2D rb {get; private set;} public Rigidbody2D rb {get; private set;}
bool beingPushed;
void Awake() => rb = GetComponent<Rigidbody2D>(); void Awake() => rb = GetComponent<Rigidbody2D>();
@ -28,6 +35,14 @@ public class Entity : MonoBehaviour
attackTimer = attackCooldown; 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(){ protected virtual void Attack(){
} }
@ -36,11 +51,11 @@ public class Entity : MonoBehaviour
} }
protected virtual void MoveToTarget(float deltaTime){ protected virtual void MoveToTarget(){
//Would be nice if we could force this to be in FixedUpdate
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()){ if(!IsInAttackRange()){
rb.MovePosition(transform.position + direction * movementSpeed * deltaTime); rb.MovePosition(transform.position + direction * movementSpeed * Time.fixedDeltaTime);
} }
} }
@ -75,4 +90,9 @@ public class Entity : MonoBehaviour
public bool IsAlive(){ public bool IsAlive(){
return isAlive; return isAlive;
} }
protected void AddImpulse(Vector3 impulse) {
beingPushed = true;
rb.AddForce(impulse, ForceMode2D.Impulse);
}
} }

View File

@ -0,0 +1,6 @@
using UnityEngine;
public class EntityStats {
[field: SerializeField] [field: Min(0f)]
public float MinVelocityWhenPushed { get; private set; } = 5f;
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: f3b49b8d7dbc43dfbd074996aa811570
timeCreated: 1648908811

View File

@ -12,7 +12,11 @@ public class Monster : AIEntity
base.ennemyName = "Gladiator"; base.ennemyName = "Gladiator";
} }
void FixedUpdate() => MoveToTarget(Time.fixedDeltaTime); protected override void FixedUpdate() {
base.FixedUpdate();
MoveToTarget();
}
// Update is called once per frame // Update is called once per frame
void Update() void Update()

View File

@ -1,10 +1,17 @@
#nullable enable #nullable enable
using NaughtyAttributes;
using UnityEngine; using UnityEngine;
using UnityEngine.InputSystem; using UnityEngine.InputSystem;
[RequireComponent(typeof(PlayerInput), typeof(Rigidbody2D))] [RequireComponent(typeof(PlayerInput), typeof(Rigidbody2D))]
public class PlayerMovement : MonoBehaviour { public class PlayerMovement : MonoBehaviour {
[SerializeField] PlayerStats stats = null!; [SerializeField] [Required]
GameFlowManager gameFlowManager = null!;
[SerializeField] [field: Required]
PlayerStats stats = null!;
[field: Required]
Rigidbody2D rb = null!; Rigidbody2D rb = null!;
Vector2 moveDirection; Vector2 moveDirection;
@ -13,7 +20,9 @@ public class PlayerMovement : MonoBehaviour {
bool lastJumpButton; bool lastJumpButton;
void Awake(){ #region Unity Messages
void Awake() {
rb = GetComponent<Rigidbody2D>(); rb = GetComponent<Rigidbody2D>();
currentState = new ImmobileMovementState(this); currentState = new ImmobileMovementState(this);
} }
@ -21,17 +30,27 @@ public class PlayerMovement : MonoBehaviour {
void Start() => currentState.EnterState(); void Start() => currentState.EnterState();
void Update() { void Update() {
if (gameFlowManager.Paused)
return;
if (currentState.UpdateState() is {} newState) if (currentState.UpdateState() is {} newState)
SwitchState(newState); SwitchState(newState);
} }
void FixedUpdate() { void FixedUpdate() {
if (gameFlowManager.Paused)
return;
if (currentState.FixedUpdateState() is {} newState) if (currentState.FixedUpdateState() is {} newState)
SwitchState(newState); SwitchState(newState);
} }
void OnDrawGizmos() => currentState?.OnDrawGizmos(); void OnDrawGizmos() => currentState?.OnDrawGizmos();
#endregion
#region Inputs
public void OnMove(InputAction.CallbackContext ctx) { public void OnMove(InputAction.CallbackContext ctx) {
moveDirection = ctx.ReadValue<Vector2>(); moveDirection = ctx.ReadValue<Vector2>();
if (moveDirection.sqrMagnitude > 1.0f) if (moveDirection.sqrMagnitude > 1.0f)
@ -47,21 +66,19 @@ public class PlayerMovement : MonoBehaviour {
if (!wasJustPressed) if (!wasJustPressed)
return; return;
if (safeZone == null) if (gameFlowManager.Paused || safeZone == null)
return; return;
if (safeZone.IsInSafeZone) { if (safeZone.IsInSafeZone) {
if (moveDirection.magnitude >= safeZone.stats.MinJumpJoystickValue) if (moveDirection.magnitude >= safeZone.Stats.MinJumpJoystickValue)
SwitchState(new ExitSafeZoneMovementState(this, safeZone, moveDirection)); SwitchState(new ExitSafeZoneMovementState(this, safeZone, moveDirection));
} else //TODO if (AngleBetween(moveDirection, toSafeZone) < 90) } else //TODO if (AngleBetween(moveDirection, toSafeZone) < 90)
SwitchState(new EnterSafeZoneMovementState(this, safeZone)); SwitchState(new EnterSafeZoneMovementState(this, safeZone));
} }
void SwitchState(BaseState newState) { #endregion
currentState.LeaveState();
currentState = newState; #region Rigidbody
newState.EnterState();
}
void OnTriggerEnter2D(Collider2D other) { void OnTriggerEnter2D(Collider2D other) {
if (other.GetComponent<SafeZone>() is {} triggeredSafeZone) if (other.GetComponent<SafeZone>() is {} triggeredSafeZone)
@ -85,17 +102,27 @@ public class PlayerMovement : MonoBehaviour {
rb.isKinematic = !enabled; rb.isKinematic = !enabled;
} }
abstract class BaseStatePlayerMovement : BaseState{ #endregion
#region States
void SwitchState(BaseState newState) {
currentState.LeaveState();
currentState = newState;
newState.EnterState();
}
abstract class BaseStatePlayerMovement : BaseState {
protected PlayerMovement playerMovement; protected PlayerMovement playerMovement;
public BaseStatePlayerMovement(PlayerMovement playerMovement){
public BaseStatePlayerMovement(PlayerMovement playerMovement) {
this.playerMovement = playerMovement; this.playerMovement = playerMovement;
} }
} }
class NormalMovementState : BaseStatePlayerMovement { class NormalMovementState : BaseStatePlayerMovement {
public NormalMovementState(PlayerMovement playerMovement) : base(playerMovement){ public NormalMovementState(PlayerMovement playerMovement) : base(playerMovement) {}
}
public override BaseState? FixedUpdateState() { public override BaseState? FixedUpdateState() {
playerMovement.rb.velocity = (Vector3)playerMovement.moveDirection * playerMovement.stats.movementSpeed; playerMovement.rb.velocity = (Vector3)playerMovement.moveDirection * playerMovement.stats.movementSpeed;
@ -140,7 +167,8 @@ public class PlayerMovement : MonoBehaviour {
class EnterSafeZoneMovementState : JumpingMovementState { class EnterSafeZoneMovementState : JumpingMovementState {
readonly SafeZone safeZone; 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; this.safeZone = safeZone;
} }
@ -153,12 +181,13 @@ public class PlayerMovement : MonoBehaviour {
protected override BaseState Transition() => new ImmobileMovementState(playerMovement); protected override BaseState Transition() => new ImmobileMovementState(playerMovement);
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 { class ExitSafeZoneMovementState : JumpingMovementState {
readonly SafeZone safeZone; 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; this.safeZone = safeZone;
} }
@ -169,13 +198,11 @@ public class PlayerMovement : MonoBehaviour {
playerMovement.SetRigidbodyEnabled(true); 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 : BaseStatePlayerMovement { class ImmobileMovementState : BaseStatePlayerMovement {
public ImmobileMovementState(PlayerMovement playerMovement) : base(playerMovement){ public ImmobileMovementState(PlayerMovement playerMovement) : base(playerMovement) {}
}
public override void EnterState() { public override void EnterState() {
base.EnterState(); base.EnterState();
@ -188,7 +215,7 @@ public class PlayerMovement : MonoBehaviour {
return; return;
Vector3 dropPosition = playerMovement.safeZone.GetOutsidePosition(playerMovement.moveDirection); 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.color = canJump ? Color.green : Color.red;
Gizmos.DrawLine(playerMovement.transform.position, dropPosition); Gizmos.DrawLine(playerMovement.transform.position, dropPosition);
if (canJump) if (canJump)
@ -198,4 +225,6 @@ public class PlayerMovement : MonoBehaviour {
} }
#endif #endif
} }
#endregion
} }

View File

@ -1,8 +1,10 @@
using NaughtyAttributes;
using UnityEngine; using UnityEngine;
public class SafeZone : MonoBehaviour { public class SafeZone : MonoBehaviour {
[field: SerializeField] [field: Required]
public SafeZoneStats Stats { get; private set; }
public SafeZoneStats stats;
[SerializeField] CircleCollider2D moatCollider; [SerializeField] CircleCollider2D moatCollider;
[SerializeField] GameObject globalCamera; [SerializeField] GameObject globalCamera;
@ -23,6 +25,6 @@ public class SafeZone : MonoBehaviour {
} }
public Vector3 GetOutsidePosition(Vector2 direction) { public Vector3 GetOutsidePosition(Vector2 direction) {
return transform.position + (moatCollider.radius + stats.JumpOffset) * (Vector3)direction; return transform.position + (moatCollider.radius + Stats.JumpOffset) * (Vector3)direction;
} }
} }

View File

@ -1,9 +1,10 @@
//TODO Replace with Damageable? using NaughtyAttributes;
using UnityEngine; using UnityEngine;
public class VampireEntity : Entity { public class VampireEntity : Entity {
[SerializeField] HealthBar healthBar; [SerializeField] [Required]
HealthBar healthBar;
[Min(10f)]
float initialHealth; float initialHealth;
protected override void Start() { protected override void Start() {

View File

@ -41,10 +41,19 @@
"interactions": "", "interactions": "",
"initialStateCheck": false "initialStateCheck": false
}, },
{
"name": "Start",
"type": "Button",
"id": "01a06960-a379-49e3-9d58-9b7c8effcb3d",
"expectedControlType": "Button",
"processors": "",
"interactions": "",
"initialStateCheck": false
},
{ {
"name": "Throw", "name": "Throw",
"type": "Button", "type": "Button",
"id": "6f870ff4-cc58-4173-855c-37192c5b63f5", "id": "f7e5243b-5304-4287-a2f5-0d36470450ad",
"expectedControlType": "Button", "expectedControlType": "Button",
"processors": "", "processors": "",
"interactions": "", "interactions": "",
@ -296,7 +305,29 @@
}, },
{ {
"name": "", "name": "",
"id": "2ee1f7f0-f3f1-4e56-8612-df107e397ef8", "id": "4715f838-717a-4f10-a668-05a6d761a7bc",
"path": "<Gamepad>/start",
"interactions": "",
"processors": "",
"groups": "Gamepad",
"action": "Start",
"isComposite": false,
"isPartOfComposite": false
},
{
"name": "",
"id": "025fe243-6da7-4b27-9c02-f353d7a121df",
"path": "<Keyboard>/enter",
"interactions": "",
"processors": "",
"groups": "Keyboard&Mouse",
"action": "Start",
"isComposite": false,
"isPartOfComposite": false
},
{
"name": "",
"id": "24e7587d-e2bf-41ae-8c97-29f00a7e8940",
"path": "<Keyboard>/r", "path": "<Keyboard>/r",
"interactions": "", "interactions": "",
"processors": "", "processors": "",
@ -307,7 +338,7 @@
}, },
{ {
"name": "", "name": "",
"id": "cf5e87bb-ed2a-433c-95f7-84f0d4f35929", "id": "2bf854b2-68b4-429a-bdef-806c2897ccc0",
"path": "<Gamepad>/buttonNorth", "path": "<Gamepad>/buttonNorth",
"interactions": "", "interactions": "",
"processors": "", "processors": "",

View File

@ -1,5 +1,6 @@
{ {
"dependencies": { "dependencies": {
"com.dbrizov.naughtyattributes": "https://github.com/dbrizov/NaughtyAttributes.git#upm",
"com.unity.2d.animation": "5.1.1", "com.unity.2d.animation": "5.1.1",
"com.unity.2d.pixel-perfect": "4.0.1", "com.unity.2d.pixel-perfect": "4.0.1",
"com.unity.2d.psdimporter": "4.2.0", "com.unity.2d.psdimporter": "4.2.0",

View File

@ -1,5 +1,12 @@
{ {
"dependencies": { "dependencies": {
"com.dbrizov.naughtyattributes": {
"version": "https://github.com/dbrizov/NaughtyAttributes.git#upm",
"depth": 0,
"source": "git",
"dependencies": {},
"hash": "8a8fa5a9659a6d63f196391c71e06c4286c8acd7"
},
"com.unity.2d.animation": { "com.unity.2d.animation": {
"version": "5.1.1", "version": "5.1.1",
"depth": 0, "depth": 0,