2021-08-20 11:16:49 +02:00
|
|
|
using UnityEngine;
|
|
|
|
|
|
2022-11-03 13:09:06 +02:00
|
|
|
public class PlayerMovement : MonoBehaviour
|
2021-08-20 11:16:49 +02:00
|
|
|
{
|
2022-09-18 23:42:20 +03:00
|
|
|
private Player _player;
|
|
|
|
|
private Audio _audio;
|
2023-07-02 23:17:37 +03:00
|
|
|
private DemonVector2 _velocity;
|
2022-11-03 13:09:06 +02:00
|
|
|
private int _knockbackFrame;
|
|
|
|
|
|
|
|
|
|
public DemonicsPhysics Physics { get; private set; }
|
2022-09-18 23:42:20 +03:00
|
|
|
public bool HasJumped { get; set; }
|
|
|
|
|
public bool HasDoubleJumped { get; set; }
|
|
|
|
|
public bool HasAirDashed { get; set; }
|
2023-07-02 23:17:37 +03:00
|
|
|
public DemonFloat MovementSpeed { get; set; }
|
2022-09-18 23:42:20 +03:00
|
|
|
public bool IsGrounded { get; set; } = true;
|
|
|
|
|
public bool IsInHitstop { get; private set; }
|
2022-11-12 16:14:22 +02:00
|
|
|
public bool IsInCorner => Physics.OnWall;
|
2022-11-15 16:16:53 +02:00
|
|
|
private InputBuffer inputBuffer;
|
2022-09-18 23:42:20 +03:00
|
|
|
void Awake()
|
|
|
|
|
{
|
|
|
|
|
_player = GetComponent<Player>();
|
2022-11-03 13:09:06 +02:00
|
|
|
Physics = GetComponent<DemonicsPhysics>();
|
2022-09-18 23:42:20 +03:00
|
|
|
_audio = GetComponent<Audio>();
|
2022-11-15 16:16:53 +02:00
|
|
|
inputBuffer = GetComponent<InputBuffer>();
|
2022-09-18 23:42:20 +03:00
|
|
|
}
|
2021-08-20 11:16:49 +02:00
|
|
|
|
2022-09-18 23:42:20 +03:00
|
|
|
void Start()
|
|
|
|
|
{
|
|
|
|
|
MovementSpeed = _player.playerStats.SpeedWalk;
|
|
|
|
|
}
|
2021-08-20 11:16:49 +02:00
|
|
|
|
2022-09-18 23:42:20 +03:00
|
|
|
void FixedUpdate()
|
|
|
|
|
{
|
2022-11-03 13:09:06 +02:00
|
|
|
CheckKnockback();
|
2022-09-18 23:42:20 +03:00
|
|
|
}
|
2021-10-01 02:28:28 +02:00
|
|
|
|
2023-07-02 23:17:37 +03:00
|
|
|
public void SetPosition(DemonVector2 position)
|
2023-01-15 02:29:53 +02:00
|
|
|
{
|
|
|
|
|
Vector2Int fixedPosition = new Vector2Int((int)(position.x * 1) / 1, (int)(position.y * 1) / 1);
|
2023-07-23 15:47:34 +03:00
|
|
|
Physics.SetPositionWithRender(new DemonVector2((DemonFloat)position.x, (DemonFloat)position.y));
|
2023-01-15 02:29:53 +02:00
|
|
|
}
|
|
|
|
|
|
2023-07-02 23:17:37 +03:00
|
|
|
public void TravelDistance(DemonVector2 travelDistance)
|
2022-09-18 23:42:20 +03:00
|
|
|
{
|
2023-07-02 23:17:37 +03:00
|
|
|
Physics.Velocity = new DemonVector2((DemonFloat)travelDistance.x, (DemonFloat)travelDistance.y);
|
2022-09-18 23:42:20 +03:00
|
|
|
}
|
2021-12-01 17:52:38 +01:00
|
|
|
|
2022-11-30 14:04:40 +02:00
|
|
|
public void Knockback(Vector2 knockbackForce, int knockbackDuration, int direction, int arc = 0, bool instant = false, bool ground = false, bool ignoreX = false)
|
2022-09-18 23:42:20 +03:00
|
|
|
{
|
2022-11-13 18:27:24 +02:00
|
|
|
if (knockbackDuration > 0)
|
2022-09-18 23:42:20 +03:00
|
|
|
{
|
2022-11-13 18:27:24 +02:00
|
|
|
if (_knockbackDuration > 0)
|
|
|
|
|
{
|
|
|
|
|
StopKnockback();
|
|
|
|
|
Physics.SetFreeze(true);
|
|
|
|
|
}
|
2023-07-02 23:17:37 +03:00
|
|
|
DemonFloat endPositionY = (DemonFloat)0;
|
2022-11-13 18:46:25 +02:00
|
|
|
if (arc > 0)
|
2022-11-13 18:27:24 +02:00
|
|
|
{
|
|
|
|
|
endPositionY = DemonicsPhysics.GROUND_POINT;
|
|
|
|
|
}
|
|
|
|
|
if (instant)
|
2022-11-12 16:14:22 +02:00
|
|
|
{
|
|
|
|
|
_startPosition = Physics.Position;
|
2023-07-02 23:17:37 +03:00
|
|
|
_endPosition = new DemonVector2(Physics.Position.x + ((DemonFloat)knockbackForce.x * direction), Physics.Position.y + endPositionY);
|
2022-11-12 16:14:22 +02:00
|
|
|
_knockbackDuration = knockbackDuration;
|
2023-07-02 23:17:37 +03:00
|
|
|
_arc = (DemonFloat)arc;
|
2022-11-13 18:27:24 +02:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_player.hitstopEvent.AddListener(() =>
|
|
|
|
|
{
|
2022-11-30 14:04:40 +02:00
|
|
|
_ignoreX = ignoreX;
|
2022-11-13 18:27:24 +02:00
|
|
|
_startPosition = Physics.Position;
|
2022-11-18 23:59:40 +02:00
|
|
|
if (!ground)
|
|
|
|
|
{
|
2023-07-02 23:17:37 +03:00
|
|
|
_endPosition = new DemonVector2(Physics.Position.x + ((DemonFloat)knockbackForce.x * direction), Physics.Position.y + endPositionY);
|
2022-11-18 23:59:40 +02:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-07-02 23:17:37 +03:00
|
|
|
_endPosition = new DemonVector2(Physics.Position.x + ((DemonFloat)knockbackForce.x * direction), DemonicsPhysics.GROUND_POINT - 0.5);
|
2022-11-18 23:59:40 +02:00
|
|
|
}
|
2022-11-13 18:27:24 +02:00
|
|
|
_knockbackDuration = knockbackDuration;
|
2023-07-02 23:17:37 +03:00
|
|
|
_arc = (DemonFloat)arc;
|
2022-11-13 18:27:24 +02:00
|
|
|
});
|
|
|
|
|
}
|
2022-11-12 16:14:22 +02:00
|
|
|
}
|
2022-09-18 23:42:20 +03:00
|
|
|
}
|
2021-12-01 17:52:38 +01:00
|
|
|
|
2022-09-18 23:42:20 +03:00
|
|
|
public void StopKnockback()
|
|
|
|
|
{
|
2022-11-12 01:48:24 +02:00
|
|
|
_knockbackDuration = 0;
|
2022-11-12 16:14:22 +02:00
|
|
|
_knockbackFrame = 0;
|
2022-09-18 23:42:20 +03:00
|
|
|
}
|
2022-11-30 14:04:40 +02:00
|
|
|
private bool _ignoreX;
|
2023-07-02 23:17:37 +03:00
|
|
|
private DemonFloat _arc;
|
2022-11-12 16:14:22 +02:00
|
|
|
private int _knockbackDuration;
|
2023-07-02 23:17:37 +03:00
|
|
|
DemonVector2 _startPosition;
|
|
|
|
|
DemonVector2 _endPosition;
|
2022-11-03 13:09:06 +02:00
|
|
|
private void CheckKnockback()
|
|
|
|
|
{
|
2022-11-24 03:43:53 +02:00
|
|
|
if (_knockbackDuration > 0 && !IsInHitstop)
|
2022-11-03 13:09:06 +02:00
|
|
|
{
|
2023-07-02 23:17:37 +03:00
|
|
|
DemonFloat ratio = (DemonFloat)_knockbackFrame / (DemonFloat)_knockbackDuration;
|
|
|
|
|
DemonFloat distance = _endPosition.x - _startPosition.x;
|
|
|
|
|
DemonFloat nextX = DemonFloat.Lerp(_startPosition.x, _endPosition.x, ratio);
|
|
|
|
|
DemonFloat baseY = DemonFloat.Lerp(_startPosition.y, _endPosition.y, (nextX - _startPosition.x) / distance);
|
|
|
|
|
DemonFloat arc = _arc * (nextX - _startPosition.x) * (nextX - _endPosition.x) / ((DemonFloat)(-0.25) * distance * distance);
|
|
|
|
|
DemonVector2 nextPosition;
|
|
|
|
|
if (arc == (DemonFloat)0)
|
2022-11-29 20:31:35 +02:00
|
|
|
{
|
2023-07-02 23:17:37 +03:00
|
|
|
nextPosition = new DemonVector2(nextX, Physics.Position.y);
|
2022-11-29 20:31:35 +02:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-07-02 23:17:37 +03:00
|
|
|
nextPosition = new DemonVector2(nextX, baseY + arc);
|
2022-11-29 20:31:35 +02:00
|
|
|
}
|
2022-11-30 14:04:40 +02:00
|
|
|
if (_ignoreX)
|
|
|
|
|
{
|
2023-07-02 23:17:37 +03:00
|
|
|
nextPosition = new DemonVector2(Physics.Position.x, nextPosition.y);
|
2022-11-30 14:04:40 +02:00
|
|
|
}
|
2022-11-12 16:14:22 +02:00
|
|
|
Physics.SetPositionWithRender(nextPosition);
|
2022-11-18 14:51:34 +02:00
|
|
|
_knockbackFrame++;
|
2022-11-03 13:09:06 +02:00
|
|
|
if (_knockbackFrame == _knockbackDuration)
|
|
|
|
|
{
|
2023-07-02 23:17:37 +03:00
|
|
|
Physics.Velocity = DemonVector2.Zero;
|
2022-11-12 22:14:14 +02:00
|
|
|
StopKnockback();
|
2022-11-03 13:09:06 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-08-28 21:05:00 +03:00
|
|
|
|
2022-09-18 23:42:20 +03:00
|
|
|
public void ResetToWalkSpeed()
|
|
|
|
|
{
|
|
|
|
|
if (MovementSpeed == _player.playerStats.SpeedRun)
|
|
|
|
|
{
|
|
|
|
|
_audio.Sound("Run").Stop();
|
|
|
|
|
MovementSpeed = _player.playerStats.SpeedWalk;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-06-11 14:34:38 +02:00
|
|
|
|
2022-09-18 23:42:20 +03:00
|
|
|
public void EnterHitstop()
|
|
|
|
|
{
|
2022-09-20 00:57:52 +03:00
|
|
|
if (!IsInHitstop)
|
|
|
|
|
{
|
2022-11-05 17:37:21 +02:00
|
|
|
_velocity = Physics.Velocity;
|
2022-11-30 12:00:14 +02:00
|
|
|
IsInHitstop = true;
|
2022-11-05 16:08:03 +02:00
|
|
|
Physics.SetFreeze(true);
|
2022-09-20 00:57:52 +03:00
|
|
|
}
|
2022-09-18 23:42:20 +03:00
|
|
|
}
|
2022-08-28 21:05:00 +03:00
|
|
|
|
2022-09-18 23:42:20 +03:00
|
|
|
public void ExitHitstop()
|
|
|
|
|
{
|
2022-09-20 00:57:52 +03:00
|
|
|
if (IsInHitstop)
|
|
|
|
|
{
|
|
|
|
|
IsInHitstop = false;
|
2022-11-05 16:08:03 +02:00
|
|
|
Physics.SetFreeze(false);
|
2023-07-02 23:17:37 +03:00
|
|
|
Physics.Velocity = new DemonVector2(_velocity.x, Physics.Velocity.y);
|
2022-09-20 00:57:52 +03:00
|
|
|
}
|
2022-09-18 23:42:20 +03:00
|
|
|
}
|
2021-08-20 11:16:49 +02:00
|
|
|
}
|