349 lines
11 KiB
C#
349 lines
11 KiB
C#
#nullable enable
|
|
using NaughtyAttributes;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
|
|
[RequireComponent(typeof(PlayerInput), typeof(Rigidbody2D))]
|
|
public class PlayerMovement : MonoBehaviour {
|
|
[SerializeField] [Required]
|
|
GameFlowManager gameFlowManager = null!;
|
|
|
|
[SerializeField] [field: Required]
|
|
PlayerStats stats = null!;
|
|
[SerializeField] GameObject globalCamera = null!;
|
|
|
|
[field: Required]
|
|
Rigidbody2D rb = null!;
|
|
|
|
Vector2 moveDirection;
|
|
Vector2 lastMoveDirection;
|
|
public BaseState currentState { get; private set; } = null!;
|
|
SafeZone? safeZone;
|
|
VampireEntity vampireEntity = null!;
|
|
Animator animator = null!;
|
|
public bool facingRight { get; private set; } = true;
|
|
[HideInInspector] public ScreenShaker screenShaker = null!;
|
|
|
|
[SerializeField] AudioSource jumpSource = null!;
|
|
[SerializeField] AudioClip[] jumpSounds = null!;
|
|
[SerializeField] AudioSource landSource = null!;
|
|
[SerializeField] AudioClip[] landSounds = null!;
|
|
[HideInInspector] public SoundManager soundManager = null!;
|
|
[SerializeField] [Required]
|
|
Transform landingTarget = null!;
|
|
|
|
[SerializeField]
|
|
LayerMask wallMask;
|
|
|
|
bool lastJumpButton;
|
|
|
|
public bool IsInSafeZone => currentState is ImmobileMovementState;
|
|
|
|
#region Unity Messages
|
|
|
|
void Awake() {
|
|
rb = GetComponent<Rigidbody2D>();
|
|
vampireEntity = GetComponent<VampireEntity>();
|
|
animator = GetComponentInChildren<Animator>();
|
|
currentState = new ImmobileMovementState(this);
|
|
soundManager = FindObjectOfType<SoundManager>();
|
|
screenShaker = FindObjectOfType<ScreenShaker>();
|
|
lastMoveDirection = Vector2.up;
|
|
}
|
|
|
|
void Start() {
|
|
gameFlowManager.stateChanged += OnGameFlowStateChanged;
|
|
globalCamera.SetActive(true);
|
|
currentState.EnterState();
|
|
}
|
|
|
|
void OnDestroy() => gameFlowManager.stateChanged -= OnGameFlowStateChanged;
|
|
|
|
void Update() {
|
|
if (gameFlowManager.pauseLevel >= GameFlowManager.PauseLevel.TimeStop)
|
|
return;
|
|
|
|
if (currentState.UpdateState() is {} newState)
|
|
SwitchState(newState);
|
|
}
|
|
|
|
void FixedUpdate() {
|
|
if (gameFlowManager.pauseLevel >= GameFlowManager.PauseLevel.TimeStop)
|
|
return;
|
|
|
|
if (currentState.FixedUpdateState() is {} newState)
|
|
SwitchState(newState);
|
|
}
|
|
|
|
void OnDrawGizmos() => currentState?.OnDrawGizmos();
|
|
|
|
#endregion
|
|
|
|
#region Flip
|
|
|
|
public void Flip() {
|
|
facingRight = !facingRight;
|
|
Vector3 scaler = transform.localScale;
|
|
scaler.x *= -1;
|
|
transform.localScale = scaler;
|
|
}
|
|
|
|
public void FlipAccordingToInput() {
|
|
if ((!facingRight && moveDirection.x > 0) || (facingRight && moveDirection.x < 0)) {
|
|
Flip();
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
public SafeZone? GetSafeZoneIfImmobile() {
|
|
return currentState is ImmobileMovementState ? safeZone : null;
|
|
}
|
|
|
|
void OnGameFlowStateChanged(BaseState oldState, BaseState newState) {
|
|
if (currentState is ImmobileMovementState && newState is GameFlowManager.GameplayFlowState)
|
|
landingTarget.gameObject.SetActive(true);
|
|
|
|
if (newState is GameFlowManager.DeadFlowState)
|
|
SwitchState(new DeadMovementState(this));
|
|
}
|
|
|
|
#region Inputs
|
|
|
|
public void OnMove(InputAction.CallbackContext ctx) {
|
|
if (gameFlowManager.CanDoAction) {
|
|
moveDirection = ctx.ReadValue<Vector2>();
|
|
if (moveDirection != Vector2.zero) {
|
|
if (moveDirection.sqrMagnitude > 1.0f)
|
|
moveDirection.Normalize();
|
|
|
|
lastMoveDirection = moveDirection;
|
|
}
|
|
FlipAccordingToInput();
|
|
} else //TODO Should set to zero via event or callback
|
|
moveDirection = Vector2.zero;
|
|
}
|
|
|
|
public void OnJump(InputAction.CallbackContext ctx) {
|
|
if (!ctx.WasPressedThisFrame(ref lastJumpButton))
|
|
return;
|
|
|
|
if (!gameFlowManager.CanDoAction || safeZone == null)
|
|
return;
|
|
|
|
if (IsInSafeZone)
|
|
SwitchState(new ExitSafeZoneMovementState(this, safeZone, lastMoveDirection.normalized));
|
|
else if (currentState is NormalMovementState) //TODO if (AngleBetween(moveDirection, toSafeZone) < 90)
|
|
SwitchState(new EnterSafeZoneMovementState(this, safeZone));
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Rigidbody
|
|
|
|
void OnTriggerEnter2D(Collider2D other) {
|
|
if (other.GetComponent<SafeZone>() is {} triggeredSafeZone)
|
|
safeZone = triggeredSafeZone;
|
|
}
|
|
|
|
//Probably should remove, mostly for ImmobileState's draw gizmos
|
|
void OnTriggerStay2D(Collider2D other) {
|
|
if (other.GetComponent<SafeZone>() is {} triggeredSafeZone)
|
|
safeZone = triggeredSafeZone;
|
|
}
|
|
|
|
void OnTriggerExit2D(Collider2D other) {
|
|
if (other.GetComponent<SafeZone>() is {})
|
|
safeZone = null;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region States
|
|
|
|
void SwitchState(BaseState newState) {
|
|
currentState.LeaveState();
|
|
currentState = newState;
|
|
newState.EnterState();
|
|
}
|
|
|
|
public abstract class BaseStatePlayerMovement : BaseState {
|
|
protected PlayerMovement playerMovement;
|
|
|
|
public BaseStatePlayerMovement(PlayerMovement playerMovement) {
|
|
this.playerMovement = playerMovement;
|
|
}
|
|
}
|
|
|
|
public class NormalMovementState : BaseStatePlayerMovement {
|
|
public NormalMovementState(PlayerMovement playerMovement) : base(playerMovement) {}
|
|
|
|
public override void EnterState() {
|
|
// playerMovement.animator.SetBool("Running", true);
|
|
}
|
|
public override void LeaveState() {
|
|
// playerMovement.animator.SetBool("Running", false);
|
|
}
|
|
|
|
public override BaseState? FixedUpdateState() {
|
|
playerMovement.rb.velocity = (Vector3)playerMovement.moveDirection * playerMovement.stats.movementSpeed;
|
|
|
|
if(playerMovement.rb.velocity.magnitude > 0.01f) {
|
|
playerMovement.animator.Play("Player_Run");
|
|
} else {
|
|
playerMovement.animator.Play("Player_Idle");
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|
|
|
|
class JumpingMovementState : BaseStatePlayerMovement {
|
|
readonly float duration;
|
|
readonly Vector3 target;
|
|
|
|
Vector3 startPosition;
|
|
float startTime;
|
|
|
|
public JumpingMovementState(PlayerMovement playerMovement, float duration, Vector3 target) : base(playerMovement) {
|
|
this.duration = duration;
|
|
this.target = target;
|
|
}
|
|
|
|
public override void EnterState() {
|
|
startPosition = playerMovement.transform.position;
|
|
startTime = Time.time;
|
|
// playerMovement.animator.SetBool("Jumping", true);
|
|
playerMovement.animator.Play("Player_Jump");
|
|
playerMovement.soundManager.PlaySound(playerMovement.jumpSource, playerMovement.jumpSounds, randomPitch: true, createTempSourceIfBusy: true);
|
|
playerMovement.screenShaker.Shake(rumbleLowFreq:0f, rumbleHighFreq:0f);
|
|
}
|
|
public override void LeaveState() {
|
|
// playerMovement.animator.SetBool("Jumping", false);
|
|
playerMovement.animator.Play("Player_Idle");
|
|
playerMovement.soundManager.PlaySound(playerMovement.landSource, playerMovement.landSounds, randomPitch: true, createTempSourceIfBusy: true);
|
|
playerMovement.screenShaker.Shake();
|
|
}
|
|
|
|
public override BaseState? FixedUpdateState() {
|
|
float currentTime = Time.time - startTime;
|
|
if(currentTime >= duration/2 && playerMovement.animator.GetCurrentAnimatorStateInfo(0).IsName("Player_Jump"))playerMovement.animator.Play("Player_Fall");
|
|
if (currentTime >= duration)
|
|
return Transition();
|
|
|
|
playerMovement.rb.MovePosition(Vector3.Lerp(
|
|
startPosition,
|
|
target,
|
|
ModifyLerpTime(currentTime / duration)
|
|
));
|
|
|
|
return null;
|
|
}
|
|
|
|
protected virtual BaseState Transition() => new NormalMovementState(playerMovement);
|
|
protected virtual float ModifyLerpTime(float t) => t;
|
|
}
|
|
|
|
class EnterSafeZoneMovementState : JumpingMovementState {
|
|
readonly SafeZone safeZone;
|
|
|
|
public EnterSafeZoneMovementState(PlayerMovement playerMovement, SafeZone safeZone) : base(playerMovement, safeZone.Stats.JumpDuration, safeZone.transform.position) {
|
|
this.safeZone = safeZone;
|
|
}
|
|
|
|
public override void EnterState() {
|
|
base.EnterState();
|
|
|
|
playerMovement.globalCamera.SetActive(true);
|
|
playerMovement.rb.SetEnabled(false);
|
|
safeZone.canvas.gameObject.SetActive(false);
|
|
}
|
|
|
|
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(PlayerMovement playerMovement, SafeZone safeZone, Vector2 direction) : base(playerMovement, safeZone.Stats.JumpDuration, safeZone.GetOutsidePosition(direction)) {
|
|
this.safeZone = safeZone;
|
|
}
|
|
|
|
public override void LeaveState() {
|
|
base.LeaveState();
|
|
|
|
playerMovement.rb.SetEnabled(true);
|
|
safeZone.canvas.gameObject.SetActive(true);
|
|
}
|
|
|
|
protected override float ModifyLerpTime(float t) => safeZone.Stats.JumpSpeedCurve.Evaluate(t);
|
|
}
|
|
|
|
public class ImmobileMovementState : BaseStatePlayerMovement {
|
|
public Vector3 exitPosition;
|
|
public bool isThrowing;
|
|
|
|
public ImmobileMovementState(PlayerMovement playerMovement) : base(playerMovement) {}
|
|
public override void EnterState() {
|
|
base.EnterState();
|
|
|
|
playerMovement.globalCamera.SetActive(true);
|
|
if (!playerMovement.rb.isKinematic)
|
|
Debug.LogWarning("Rigidbody should probably be kinematic when immobile (when in safe zone).");
|
|
|
|
playerMovement.landingTarget.gameObject.SetActive(playerMovement.gameFlowManager.CurrentState is GameFlowManager.GameplayFlowState);
|
|
}
|
|
|
|
public override void LeaveState() {
|
|
base.LeaveState();
|
|
|
|
playerMovement.globalCamera.SetActive(false);
|
|
playerMovement.landingTarget.gameObject.SetActive(false);
|
|
}
|
|
|
|
public override BaseState? UpdateState() {
|
|
//safeZone can't be null here
|
|
float outsideDistance = playerMovement.safeZone == null ? 0f : playerMovement.safeZone.OutsideDistance;
|
|
float distance = outsideDistance + playerMovement.lastMoveDirection.magnitude * playerMovement.stats.MaxThrowDistance;
|
|
RaycastHit2D hit = Physics2D.Raycast(playerMovement.transform.position, playerMovement.lastMoveDirection, distance, playerMovement.wallMask);
|
|
|
|
if (hit.collider == null)
|
|
exitPosition = (Vector3)playerMovement.lastMoveDirection.normalized * distance;
|
|
else
|
|
exitPosition = (Vector3)playerMovement.lastMoveDirection.normalized * distance * hit.fraction + (Vector3)hit.normal * playerMovement.stats.WallOffset;
|
|
|
|
playerMovement.landingTarget.position = isThrowing ? exitPosition : playerMovement.safeZone.GetOutsidePosition(playerMovement.lastMoveDirection);
|
|
|
|
return null;
|
|
}
|
|
|
|
#if UNITY_EDITOR
|
|
public override void OnDrawGizmos() {
|
|
if (playerMovement.safeZone is null)
|
|
return;
|
|
|
|
Vector3 dropPosition = playerMovement.safeZone.GetOutsidePosition(playerMovement.moveDirection);
|
|
bool canJump = playerMovement.moveDirection.magnitude >= playerMovement.safeZone.Stats.MinJumpJoystickValue;
|
|
Gizmos.color = canJump ? Color.green : Color.red;
|
|
Gizmos.DrawLine(playerMovement.transform.position, dropPosition);
|
|
if (canJump)
|
|
Gizmos.DrawSphere(dropPosition, .5f);
|
|
else
|
|
Gizmos.DrawWireSphere(dropPosition, .5f);
|
|
}
|
|
#endif
|
|
}
|
|
|
|
class DeadMovementState : BaseStatePlayerMovement{
|
|
public DeadMovementState(PlayerMovement playerMovement) : base(playerMovement) {}
|
|
public override void EnterState() {
|
|
base.EnterState();
|
|
playerMovement.animator.Play("Player_Death");
|
|
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
} |