Merge remote-tracking branch 'origin/dev' into jason
# Conflicts: # Assets/Scripts/PlayerMovement.cs
This commit is contained in:
commit
027c287a43
18
Assets/Scripts/BaseState.cs
Normal file
18
Assets/Scripts/BaseState.cs
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
#nullable enable
|
||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
public class BaseState
|
||||||
|
{
|
||||||
|
public virtual void EnterState() {}
|
||||||
|
|
||||||
|
public virtual void LeaveState() {}
|
||||||
|
|
||||||
|
public virtual BaseState? UpdateState() => null;
|
||||||
|
|
||||||
|
public virtual BaseState? FixedUpdateState() => null;
|
||||||
|
|
||||||
|
public virtual void OnDrawGizmos() {}
|
||||||
|
|
||||||
|
}
|
||||||
11
Assets/Scripts/BaseState.cs.meta
Normal file
11
Assets/Scripts/BaseState.cs.meta
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: cef7256e9d775574e8e79006e0377565
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
@ -44,7 +44,10 @@ public class Entity : MonoBehaviour
|
|||||||
protected virtual void Attack(){
|
protected virtual void Attack(){
|
||||||
// jason: TODO Either have target be Entity instead of transform, or skip Attack when GetComponent<Entity>() is null
|
// jason: TODO Either have target be Entity instead of transform, or skip Attack when GetComponent<Entity>() is null
|
||||||
Entity targetEntity = target.GetComponent<Entity>();
|
Entity targetEntity = target.GetComponent<Entity>();
|
||||||
bool isTargetAlive = targetEntity.TakeDamage(attackDmg);
|
if(targetEntity != null){
|
||||||
|
bool isTargetAlive = targetEntity.TakeDamage(attackDmg);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual void SpecialAttack(){
|
protected virtual void SpecialAttack(){
|
||||||
|
|||||||
@ -15,21 +15,24 @@ public class PlayerMovement : MonoBehaviour {
|
|||||||
Rigidbody2D rb = null!;
|
Rigidbody2D rb = null!;
|
||||||
|
|
||||||
Vector2 moveDirection;
|
Vector2 moveDirection;
|
||||||
BaseState currentState = new ImmobileMovementState();
|
BaseState currentState = null!;
|
||||||
SafeZone? safeZone;
|
SafeZone? safeZone;
|
||||||
|
|
||||||
bool lastJumpButton;
|
bool lastJumpButton;
|
||||||
|
|
||||||
#region Unity Messages
|
#region Unity Messages
|
||||||
void Awake() => rb = GetComponent<Rigidbody2D>();
|
void Awake(){
|
||||||
|
rb = GetComponent<Rigidbody2D>();
|
||||||
|
currentState = new ImmobileMovementState(this);
|
||||||
|
}
|
||||||
|
|
||||||
void Start() => currentState.EnterState(this);
|
void Start() => currentState.EnterState();
|
||||||
|
|
||||||
void Update() {
|
void Update() {
|
||||||
if (gameFlowManager.Paused)
|
if (gameFlowManager.Paused)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (currentState.UpdateState(this) is {} newState)
|
if (currentState.UpdateState() is {} newState)
|
||||||
SwitchState(newState);
|
SwitchState(newState);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -37,11 +40,11 @@ public class PlayerMovement : MonoBehaviour {
|
|||||||
if (gameFlowManager.Paused)
|
if (gameFlowManager.Paused)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (currentState.FixedUpdateState(this) is {} newState)
|
if (currentState.FixedUpdateState() is {} newState)
|
||||||
SwitchState(newState);
|
SwitchState(newState);
|
||||||
}
|
}
|
||||||
|
|
||||||
void OnDrawGizmos() => currentState.OnDrawGizmos(this);
|
void OnDrawGizmos() => currentState?.OnDrawGizmos();
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@ -67,9 +70,9 @@ public class PlayerMovement : MonoBehaviour {
|
|||||||
|
|
||||||
if (safeZone.IsInSafeZone) {
|
if (safeZone.IsInSafeZone) {
|
||||||
if (moveDirection.magnitude >= safeZone.Stats.MinJumpJoystickValue)
|
if (moveDirection.magnitude >= safeZone.Stats.MinJumpJoystickValue)
|
||||||
SwitchState(new ExitSafeZoneMovementState(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(safeZone));
|
SwitchState(new EnterSafeZoneMovementState(this, safeZone));
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
@ -103,49 +106,48 @@ public class PlayerMovement : MonoBehaviour {
|
|||||||
#region States
|
#region States
|
||||||
|
|
||||||
void SwitchState(BaseState newState) {
|
void SwitchState(BaseState newState) {
|
||||||
currentState.LeaveState(this);
|
currentState.LeaveState();
|
||||||
currentState = newState;
|
currentState = newState;
|
||||||
newState.EnterState(this);
|
newState.EnterState();
|
||||||
}
|
}
|
||||||
|
|
||||||
class BaseState {
|
abstract class BaseStatePlayerMovement : BaseState {
|
||||||
public virtual void EnterState(PlayerMovement playerMovement) {}
|
protected PlayerMovement playerMovement;
|
||||||
|
public BaseStatePlayerMovement(PlayerMovement playerMovement){
|
||||||
public virtual void LeaveState(PlayerMovement playerMovement) {}
|
this.playerMovement = playerMovement;
|
||||||
|
}
|
||||||
public virtual BaseState? UpdateState(PlayerMovement playerMovement) => null;
|
|
||||||
|
|
||||||
public virtual BaseState? FixedUpdateState(PlayerMovement playerMovement) => null;
|
|
||||||
|
|
||||||
public virtual void OnDrawGizmos(PlayerMovement playerMovement) {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class NormalMovementState : BaseState {
|
class NormalMovementState : BaseStatePlayerMovement {
|
||||||
public override BaseState? FixedUpdateState(PlayerMovement playerMovement) {
|
public NormalMovementState(PlayerMovement playerMovement) : base(playerMovement){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public override BaseState? FixedUpdateState() {
|
||||||
playerMovement.rb.velocity = (Vector3)playerMovement.moveDirection * playerMovement.stats.movementSpeed;
|
playerMovement.rb.velocity = (Vector3)playerMovement.moveDirection * playerMovement.stats.movementSpeed;
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class JumpingMovementState : BaseState {
|
class JumpingMovementState : BaseStatePlayerMovement {
|
||||||
readonly float duration;
|
readonly float duration;
|
||||||
readonly Vector3 target;
|
readonly Vector3 target;
|
||||||
|
|
||||||
Vector3 startPosition;
|
Vector3 startPosition;
|
||||||
float startTime;
|
float startTime;
|
||||||
|
|
||||||
public JumpingMovementState(float duration, Vector3 target) {
|
public JumpingMovementState(PlayerMovement playerMovement, float duration, Vector3 target) : base(playerMovement) {
|
||||||
this.duration = duration;
|
this.duration = duration;
|
||||||
this.target = target;
|
this.target = target;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void EnterState(PlayerMovement playerMovement) {
|
public override void EnterState() {
|
||||||
startPosition = playerMovement.transform.position;
|
startPosition = playerMovement.transform.position;
|
||||||
startTime = Time.time;
|
startTime = Time.time;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override BaseState? FixedUpdateState(PlayerMovement playerMovement) {
|
public override BaseState? FixedUpdateState() {
|
||||||
float currentTime = Time.time - startTime;
|
float currentTime = Time.time - startTime;
|
||||||
if (currentTime >= duration)
|
if (currentTime >= duration)
|
||||||
return Transition();
|
return Transition();
|
||||||
@ -159,39 +161,37 @@ public class PlayerMovement : MonoBehaviour {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual BaseState Transition() => new NormalMovementState();
|
protected virtual BaseState Transition() => new NormalMovementState(playerMovement);
|
||||||
protected virtual float ModifyLerpTime(float t) => t;
|
protected virtual float ModifyLerpTime(float t) => t;
|
||||||
}
|
}
|
||||||
|
|
||||||
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(SafeZone safeZone) : base(safeZone.Stats.JumpDuration, safeZone.transform.position) {
|
|
||||||
this.safeZone = safeZone;
|
this.safeZone = safeZone;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void EnterState(PlayerMovement playerMovement) {
|
public override void EnterState() {
|
||||||
base.EnterState(playerMovement);
|
base.EnterState();
|
||||||
|
|
||||||
safeZone.EnterSafeZone();
|
safeZone.EnterSafeZone();
|
||||||
playerMovement.SetRigidbodyEnabled(false);
|
playerMovement.SetRigidbodyEnabled(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override BaseState Transition() => new ImmobileMovementState();
|
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(SafeZone safeZone, Vector2 direction) : base(safeZone.Stats.JumpDuration, safeZone.GetOutsidePosition(direction)) {
|
|
||||||
this.safeZone = safeZone;
|
this.safeZone = safeZone;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void LeaveState(PlayerMovement playerMovement) {
|
public override void LeaveState() {
|
||||||
base.EnterState(playerMovement);
|
base.EnterState();
|
||||||
|
|
||||||
safeZone.ExitSafeZone();
|
safeZone.ExitSafeZone();
|
||||||
playerMovement.SetRigidbodyEnabled(true);
|
playerMovement.SetRigidbodyEnabled(true);
|
||||||
}
|
}
|
||||||
@ -199,15 +199,18 @@ public class PlayerMovement : MonoBehaviour {
|
|||||||
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 {
|
class ImmobileMovementState : BaseStatePlayerMovement {
|
||||||
public override void EnterState(PlayerMovement playerMovement) {
|
public ImmobileMovementState(PlayerMovement playerMovement) : base(playerMovement){
|
||||||
base.EnterState(playerMovement);
|
|
||||||
|
|
||||||
|
}
|
||||||
|
public override void EnterState() {
|
||||||
|
base.EnterState();
|
||||||
|
|
||||||
if (!playerMovement.rb.isKinematic)
|
if (!playerMovement.rb.isKinematic)
|
||||||
Debug.LogWarning("Rigidbody should probably be kinematic when immobile (when in safe zone).");
|
Debug.LogWarning("Rigidbody should probably be kinematic when immobile (when in safe zone).");
|
||||||
}
|
}
|
||||||
#if UNITY_EDITOR
|
#if UNITY_EDITOR
|
||||||
public override void OnDrawGizmos(PlayerMovement playerMovement) {
|
public override void OnDrawGizmos() {
|
||||||
if (playerMovement.safeZone is null)
|
if (playerMovement.safeZone is null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user