Merge branch 'dev' into yann

This commit is contained in:
Yann Dupont 01 2022-04-02 12:25:40 -04:00
commit 24d739af4a
4 changed files with 75 additions and 39 deletions

View 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() {}
}

View File

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

View File

@ -29,7 +29,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(){

View File

@ -8,26 +8,29 @@ public class PlayerMovement : MonoBehaviour {
Rigidbody2D rb = null!;
Vector2 moveDirection;
BaseState currentState = new ImmobileMovementState();
BaseState currentState = null!;
SafeZone? safeZone;
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() {
if (currentState.UpdateState(this) is {} newState)
if (currentState.UpdateState() is {} newState)
SwitchState(newState);
}
void FixedUpdate() {
if (currentState.FixedUpdateState(this) is {} newState)
if (currentState.FixedUpdateState() is {} newState)
SwitchState(newState);
}
void OnDrawGizmos() => currentState.OnDrawGizmos(this);
void OnDrawGizmos() => currentState?.OnDrawGizmos();
public void OnMove(InputAction.CallbackContext ctx) {
moveDirection = ctx.ReadValue<Vector2>();
@ -49,15 +52,15 @@ 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));
}
void SwitchState(BaseState newState) {
currentState.LeaveState(this);
currentState.LeaveState();
currentState = newState;
newState.EnterState(this);
newState.EnterState();
}
void OnTriggerEnter2D(Collider2D other) {
@ -82,44 +85,42 @@ public class PlayerMovement : MonoBehaviour {
rb.isKinematic = !enabled;
}
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();
@ -133,36 +134,36 @@ 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);
@ -171,15 +172,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;