Extracted baseState to its own file
This commit is contained in:
parent
7af161f29d
commit
d0d51a82f1
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:
|
||||||
@ -8,26 +8,29 @@ 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;
|
||||||
|
|
||||||
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 (currentState.UpdateState(this) is {} newState)
|
if (currentState.UpdateState() is {} newState)
|
||||||
SwitchState(newState);
|
SwitchState(newState);
|
||||||
}
|
}
|
||||||
|
|
||||||
void FixedUpdate() {
|
void FixedUpdate() {
|
||||||
if (currentState.FixedUpdateState(this) is {} newState)
|
if (currentState.FixedUpdateState() is {} newState)
|
||||||
SwitchState(newState);
|
SwitchState(newState);
|
||||||
}
|
}
|
||||||
|
|
||||||
void OnDrawGizmos() => currentState.OnDrawGizmos(this);
|
void OnDrawGizmos() => currentState.OnDrawGizmos();
|
||||||
|
|
||||||
public void OnMove(InputAction.CallbackContext ctx) {
|
public void OnMove(InputAction.CallbackContext ctx) {
|
||||||
moveDirection = ctx.ReadValue<Vector2>();
|
moveDirection = ctx.ReadValue<Vector2>();
|
||||||
@ -49,15 +52,15 @@ 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));
|
||||||
}
|
}
|
||||||
|
|
||||||
void SwitchState(BaseState newState) {
|
void SwitchState(BaseState newState) {
|
||||||
currentState.LeaveState(this);
|
currentState.LeaveState();
|
||||||
currentState = newState;
|
currentState = newState;
|
||||||
newState.EnterState(this);
|
newState.EnterState();
|
||||||
}
|
}
|
||||||
|
|
||||||
void OnTriggerEnter2D(Collider2D other) {
|
void OnTriggerEnter2D(Collider2D other) {
|
||||||
@ -82,44 +85,42 @@ public class PlayerMovement : MonoBehaviour {
|
|||||||
rb.isKinematic = !enabled;
|
rb.isKinematic = !enabled;
|
||||||
}
|
}
|
||||||
|
|
||||||
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();
|
||||||
@ -133,36 +134,36 @@ 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(SafeZone safeZone) : base(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;
|
||||||
}
|
}
|
||||||
|
|
||||||
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(SafeZone safeZone, Vector2 direction) : base(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;
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
@ -171,15 +172,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