2022-03-08 10:13:27 +03:00
|
|
|
using Syntriax.Modules.Movement.ColliderTrigger;
|
|
|
|
using Syntriax.Modules.Movement.State;
|
2021-12-23 12:41:01 +03:00
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
namespace Syntriax.Modules.Movement.SpecialAction
|
|
|
|
{
|
|
|
|
[RequireComponent(typeof(Rigidbody2D))]
|
2022-03-08 10:13:27 +03:00
|
|
|
public class PlatformerJump : MonoBehaviour, ISpecialActionActivate, ISpecialActionDeactivate
|
2021-12-23 12:41:01 +03:00
|
|
|
{
|
|
|
|
public float JumpSpeed { get; set; } = 0f;
|
2022-03-08 10:13:27 +03:00
|
|
|
public IToggleState EnabledToggleState { get; private set; } = null;
|
2021-12-23 12:41:01 +03:00
|
|
|
|
|
|
|
private bool airSuspension = false;
|
|
|
|
private float fallMultiplier = 0f;
|
|
|
|
private float suspensionMultiplier = 0f;
|
|
|
|
|
2022-03-08 10:13:27 +03:00
|
|
|
private IGroundTrigger groundCheck = null;
|
2021-12-23 12:41:01 +03:00
|
|
|
private Rigidbody2D rigid = null;
|
2022-03-08 10:13:27 +03:00
|
|
|
private IToggleState toggleState = null;
|
2021-12-23 12:41:01 +03:00
|
|
|
|
|
|
|
|
|
|
|
private void Awake()
|
|
|
|
{
|
|
|
|
JumpSpeed = 10f;
|
|
|
|
fallMultiplier = 1.5f * Time.fixedDeltaTime;
|
|
|
|
suspensionMultiplier = 1f * Time.fixedDeltaTime;
|
2022-03-08 10:13:27 +03:00
|
|
|
EnabledToggleState = new MemberToggleState();
|
2021-12-23 12:41:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private void Start()
|
|
|
|
{
|
|
|
|
rigid = GetComponent<Rigidbody2D>();
|
2022-03-08 10:13:27 +03:00
|
|
|
groundCheck = GetComponentInChildren<IGroundTrigger>();
|
|
|
|
toggleState = GetComponent<IToggleState>();
|
2021-12-23 12:41:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private void FixedUpdate()
|
|
|
|
{
|
2022-03-08 10:13:27 +03:00
|
|
|
if (!EnabledToggleState.Toggled)
|
2021-12-23 12:41:01 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (rigid.velocity.y < 0f)
|
|
|
|
ApplySuspension(suspensionMultiplier);
|
|
|
|
else if (!airSuspension)
|
|
|
|
ApplySuspension(fallMultiplier);
|
|
|
|
}
|
|
|
|
|
2021-12-23 13:41:32 +03:00
|
|
|
private void ApplySuspension(float multiplier)
|
2021-12-23 12:41:01 +03:00
|
|
|
{
|
2021-12-23 13:41:32 +03:00
|
|
|
rigid.velocity += Physics2D.gravity * multiplier;
|
2021-12-23 12:41:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private void Jump()
|
|
|
|
{
|
|
|
|
Vector2 velocity = rigid.velocity;
|
|
|
|
velocity.y = JumpSpeed;
|
|
|
|
rigid.velocity = velocity;
|
|
|
|
}
|
|
|
|
|
2022-03-08 10:13:27 +03:00
|
|
|
public void Activate()
|
2021-12-23 12:41:01 +03:00
|
|
|
{
|
2022-03-08 10:13:27 +03:00
|
|
|
if (!toggleState.Toggled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (groundCheck.IsTrigerred)
|
2021-12-23 12:41:01 +03:00
|
|
|
Jump();
|
|
|
|
|
|
|
|
airSuspension = true;
|
|
|
|
}
|
|
|
|
|
2022-03-08 10:13:27 +03:00
|
|
|
public void Deactivate()
|
2021-12-23 12:41:01 +03:00
|
|
|
{
|
2022-03-08 10:13:27 +03:00
|
|
|
if (!toggleState.Toggled)
|
|
|
|
return;
|
|
|
|
|
2021-12-23 12:41:01 +03:00
|
|
|
airSuspension = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|