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(){
|
||||
// jason: TODO Either have target be Entity instead of transform, or skip Attack when GetComponent<Entity>() is null
|
||||
Entity targetEntity = target.GetComponent<Entity>();
|
||||
bool isTargetAlive = targetEntity.TakeDamage(attackDmg);
|
||||
if(targetEntity != null){
|
||||
bool isTargetAlive = targetEntity.TakeDamage(attackDmg);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected virtual void SpecialAttack(){
|
||||
|
||||
@ -15,21 +15,24 @@ public class PlayerMovement : MonoBehaviour {
|
||||
Rigidbody2D rb = null!;
|
||||
|
||||
Vector2 moveDirection;
|
||||
BaseState currentState = new ImmobileMovementState();
|
||||
BaseState currentState = null!;
|
||||
SafeZone? safeZone;
|
||||
|
||||
bool lastJumpButton;
|
||||
|
||||
#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() {
|
||||
if (gameFlowManager.Paused)
|
||||
return;
|
||||
|
||||
if (currentState.UpdateState(this) is {} newState)
|
||||
if (currentState.UpdateState() is {} newState)
|
||||
SwitchState(newState);
|
||||
}
|
||||
|
||||
@ -37,11 +40,11 @@ public class PlayerMovement : MonoBehaviour {
|
||||
if (gameFlowManager.Paused)
|
||||
return;
|
||||
|
||||
if (currentState.FixedUpdateState(this) is {} newState)
|
||||
if (currentState.FixedUpdateState() is {} newState)
|
||||
SwitchState(newState);
|
||||
}
|
||||
|
||||
void OnDrawGizmos() => currentState.OnDrawGizmos(this);
|
||||
void OnDrawGizmos() => currentState?.OnDrawGizmos();
|
||||
|
||||
#endregion
|
||||
|
||||
@ -67,9 +70,9 @@ public class PlayerMovement : MonoBehaviour {
|
||||
|
||||
if (safeZone.IsInSafeZone) {
|
||||
if (moveDirection.magnitude >= safeZone.Stats.MinJumpJoystickValue)
|
||||
SwitchState(new ExitSafeZoneMovementState(safeZone, moveDirection));
|
||||
SwitchState(new ExitSafeZoneMovementState(this, safeZone, moveDirection));
|
||||
} else //TODO if (AngleBetween(moveDirection, toSafeZone) < 90)
|
||||
SwitchState(new EnterSafeZoneMovementState(safeZone));
|
||||
SwitchState(new EnterSafeZoneMovementState(this, safeZone));
|
||||
}
|
||||
|
||||
#endregion
|
||||
@ -103,49 +106,48 @@ public class PlayerMovement : MonoBehaviour {
|
||||
#region States
|
||||
|
||||
void SwitchState(BaseState newState) {
|
||||
currentState.LeaveState(this);
|
||||
currentState.LeaveState();
|
||||
currentState = newState;
|
||||
newState.EnterState(this);
|
||||
newState.EnterState();
|
||||
}
|
||||
|
||||
class BaseState {
|
||||
public virtual void EnterState(PlayerMovement playerMovement) {}
|
||||
|
||||
public virtual void LeaveState(PlayerMovement playerMovement) {}
|
||||
|
||||
public virtual BaseState? UpdateState(PlayerMovement playerMovement) => null;
|
||||
|
||||
public virtual BaseState? FixedUpdateState(PlayerMovement playerMovement) => null;
|
||||
|
||||
public virtual void OnDrawGizmos(PlayerMovement playerMovement) {}
|
||||
abstract class BaseStatePlayerMovement : BaseState {
|
||||
protected PlayerMovement playerMovement;
|
||||
public BaseStatePlayerMovement(PlayerMovement playerMovement){
|
||||
this.playerMovement = playerMovement;
|
||||
}
|
||||
}
|
||||
|
||||
class NormalMovementState : BaseState {
|
||||
public override BaseState? FixedUpdateState(PlayerMovement playerMovement) {
|
||||
class NormalMovementState : BaseStatePlayerMovement {
|
||||
public NormalMovementState(PlayerMovement playerMovement) : base(playerMovement){
|
||||
|
||||
}
|
||||
|
||||
public override BaseState? FixedUpdateState() {
|
||||
playerMovement.rb.velocity = (Vector3)playerMovement.moveDirection * playerMovement.stats.movementSpeed;
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
class JumpingMovementState : BaseState {
|
||||
class JumpingMovementState : BaseStatePlayerMovement {
|
||||
readonly float duration;
|
||||
readonly Vector3 target;
|
||||
|
||||
Vector3 startPosition;
|
||||
float startTime;
|
||||
|
||||
public JumpingMovementState(float duration, Vector3 target) {
|
||||
public JumpingMovementState(PlayerMovement playerMovement, float duration, Vector3 target) : base(playerMovement) {
|
||||
this.duration = duration;
|
||||
this.target = target;
|
||||
}
|
||||
|
||||
public override void EnterState(PlayerMovement playerMovement) {
|
||||
public override void EnterState() {
|
||||
startPosition = playerMovement.transform.position;
|
||||
startTime = Time.time;
|
||||
}
|
||||
|
||||
public override BaseState? FixedUpdateState(PlayerMovement playerMovement) {
|
||||
public override BaseState? FixedUpdateState() {
|
||||
float currentTime = Time.time - startTime;
|
||||
if (currentTime >= duration)
|
||||
return Transition();
|
||||
@ -159,39 +161,37 @@ public class PlayerMovement : MonoBehaviour {
|
||||
return null;
|
||||
}
|
||||
|
||||
protected virtual BaseState Transition() => new NormalMovementState();
|
||||
protected virtual BaseState Transition() => new NormalMovementState(playerMovement);
|
||||
protected virtual float ModifyLerpTime(float t) => t;
|
||||
}
|
||||
|
||||
class EnterSafeZoneMovementState : JumpingMovementState {
|
||||
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;
|
||||
}
|
||||
|
||||
public override void EnterState(PlayerMovement playerMovement) {
|
||||
base.EnterState(playerMovement);
|
||||
|
||||
public override void EnterState() {
|
||||
base.EnterState();
|
||||
|
||||
safeZone.EnterSafeZone();
|
||||
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);
|
||||
}
|
||||
|
||||
class ExitSafeZoneMovementState : JumpingMovementState {
|
||||
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;
|
||||
}
|
||||
|
||||
public override void LeaveState(PlayerMovement playerMovement) {
|
||||
base.EnterState(playerMovement);
|
||||
|
||||
public override void LeaveState() {
|
||||
base.EnterState();
|
||||
|
||||
safeZone.ExitSafeZone();
|
||||
playerMovement.SetRigidbodyEnabled(true);
|
||||
}
|
||||
@ -199,15 +199,18 @@ public class PlayerMovement : MonoBehaviour {
|
||||
protected override float ModifyLerpTime(float t) => safeZone.Stats.JumpSpeedCurve.Evaluate(t);
|
||||
}
|
||||
|
||||
class ImmobileMovementState : BaseState {
|
||||
public override void EnterState(PlayerMovement playerMovement) {
|
||||
base.EnterState(playerMovement);
|
||||
class ImmobileMovementState : BaseStatePlayerMovement {
|
||||
public ImmobileMovementState(PlayerMovement playerMovement) : base(playerMovement){
|
||||
|
||||
}
|
||||
public override void EnterState() {
|
||||
base.EnterState();
|
||||
|
||||
if (!playerMovement.rb.isKinematic)
|
||||
Debug.LogWarning("Rigidbody should probably be kinematic when immobile (when in safe zone).");
|
||||
}
|
||||
#if UNITY_EDITOR
|
||||
public override void OnDrawGizmos(PlayerMovement playerMovement) {
|
||||
public override void OnDrawGizmos() {
|
||||
if (playerMovement.safeZone is null)
|
||||
return;
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user