2023-03-20 23:46:17 +03:00
|
|
|
using Syntriax.Modules.State;
|
2022-11-20 18:31:16 +03:00
|
|
|
using UnityEngine;
|
|
|
|
|
2022-12-16 23:24:02 +03:00
|
|
|
namespace Syntriax.Modules.Action.Samples
|
2022-11-20 18:31:16 +03:00
|
|
|
{
|
|
|
|
[RequireComponent(typeof(Rigidbody2D))]
|
2023-03-20 23:46:17 +03:00
|
|
|
public class PlatformerJump : ActionWithDeactivation
|
2022-11-20 18:31:16 +03:00
|
|
|
{
|
2022-11-20 18:45:36 +03:00
|
|
|
[SerializeField] private float jumpSpeed = 10f;
|
2022-11-20 18:31:16 +03:00
|
|
|
public float JumpSpeed { get => jumpSpeed; set => jumpSpeed = value; }
|
2022-11-20 18:45:36 +03:00
|
|
|
[SerializeField] private float fallThreshold = 0f;
|
|
|
|
public float FallThreshold { get => fallThreshold; set => fallThreshold = value; }
|
2022-11-20 18:31:16 +03:00
|
|
|
|
|
|
|
[SerializeField] private float fallMultiplier = 1.5f;
|
|
|
|
public float FallMultiplier
|
|
|
|
{
|
|
|
|
get => fallMultiplier;
|
|
|
|
set => fallMultiplier = value * Time.fixedDeltaTime;
|
|
|
|
}
|
2022-11-20 18:45:36 +03:00
|
|
|
|
2022-11-20 18:31:16 +03:00
|
|
|
[SerializeField] private float suspensionMultiplier = 1f;
|
|
|
|
public float SuspensionMultiplier
|
|
|
|
{
|
|
|
|
get => suspensionMultiplier;
|
|
|
|
set => suspensionMultiplier = value * Time.fixedDeltaTime;
|
|
|
|
}
|
|
|
|
|
2023-03-20 23:46:17 +03:00
|
|
|
protected IStateEnable gameObjectStateEnable = null;
|
2022-11-20 18:31:16 +03:00
|
|
|
protected bool airSuspension = false;
|
|
|
|
protected Rigidbody2D rigid = null;
|
|
|
|
|
|
|
|
protected virtual void Start()
|
|
|
|
{
|
|
|
|
rigid = GetComponent<Rigidbody2D>();
|
2023-03-20 23:46:17 +03:00
|
|
|
gameObjectStateEnable = GetComponent<IStateEnable>();
|
2022-11-20 18:45:36 +03:00
|
|
|
|
|
|
|
FallMultiplier = fallMultiplier;
|
|
|
|
SuspensionMultiplier = suspensionMultiplier;
|
2023-03-20 23:46:17 +03:00
|
|
|
|
|
|
|
OnActivated += _ => OnActionDeactivated();
|
|
|
|
OnDeactivated += _ => OnActionActivated();
|
2022-11-20 18:31:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void FixedUpdate()
|
|
|
|
{
|
2023-03-20 23:46:17 +03:00
|
|
|
if (!StateEnable.IsEnabledNullChecked() || !gameObjectStateEnable.IsEnabledNullChecked())
|
2022-11-20 18:31:16 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (rigid.velocity.y < FallThreshold)
|
|
|
|
ApplySuspension(SuspensionMultiplier);
|
|
|
|
else if (!airSuspension)
|
|
|
|
ApplySuspension(FallMultiplier);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void ApplySuspension(float multiplier)
|
|
|
|
=> rigid.velocity += Physics2D.gravity * multiplier;
|
|
|
|
|
|
|
|
protected virtual void Jump()
|
|
|
|
{
|
|
|
|
Vector2 velocity = rigid.velocity;
|
|
|
|
velocity.y = JumpSpeed;
|
|
|
|
rigid.velocity = velocity;
|
|
|
|
}
|
|
|
|
|
2023-03-20 23:46:17 +03:00
|
|
|
protected void OnActionDeactivated()
|
2022-11-20 18:31:16 +03:00
|
|
|
{
|
2023-03-20 23:46:17 +03:00
|
|
|
if (!gameObjectStateEnable.IsEnabledNullChecked())
|
2022-11-20 18:45:36 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
airSuspension = false;
|
|
|
|
}
|
|
|
|
|
2023-03-20 23:46:17 +03:00
|
|
|
protected void OnActionActivated()
|
2022-11-20 18:45:36 +03:00
|
|
|
{
|
2023-03-20 23:46:17 +03:00
|
|
|
if (!gameObjectStateEnable.IsEnabledNullChecked())
|
2022-11-20 18:31:16 +03:00
|
|
|
return;
|
|
|
|
|
2022-11-20 18:47:28 +03:00
|
|
|
Jump();
|
2022-11-20 18:31:16 +03:00
|
|
|
airSuspension = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|