2026-01-28 07:39:18 +00:00
|
|
|
|
using System.Collections;
|
2026-01-23 08:06:57 +00:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
using UnityEngine.InputSystem;
|
|
|
|
|
|
|
|
|
|
|
|
public class Player : MonoBehaviour
|
|
|
|
|
|
{
|
2026-01-27 10:20:36 +00:00
|
|
|
|
private Rigidbody2D _rigidbody;
|
2026-01-28 07:39:18 +00:00
|
|
|
|
|
|
|
|
|
|
[Header("이동속도")]
|
|
|
|
|
|
[Range(1, 20)] public float jumpVelocity;
|
|
|
|
|
|
[Range(1, 20)] public float moveVelocity;
|
|
|
|
|
|
|
|
|
|
|
|
[Header("대쉬 설정")]
|
|
|
|
|
|
[Range(1,10)] public float dashDistance = 5f;
|
|
|
|
|
|
public float dashDuration = 0.2f;
|
|
|
|
|
|
public float gravityRecoveryTime = 0.15f;
|
2026-01-23 08:06:57 +00:00
|
|
|
|
|
2026-01-28 07:39:18 +00:00
|
|
|
|
[Header("중력 물리")]
|
|
|
|
|
|
public float fallMultiplier = 2.5f;
|
|
|
|
|
|
public float lowJumpMultiplier = 2f;
|
|
|
|
|
|
private float _gravityWeight = 1f;
|
|
|
|
|
|
|
|
|
|
|
|
[Header("체력")]
|
2026-01-27 12:21:34 +00:00
|
|
|
|
[SerializeField] private int currentHp;
|
|
|
|
|
|
[SerializeField] private int maxHp = 10;
|
2026-01-23 10:22:52 +00:00
|
|
|
|
|
2026-01-28 07:39:18 +00:00
|
|
|
|
[Header("레이어 설정")]
|
|
|
|
|
|
[SerializeField] private LayerMask enemyLayer;
|
2026-01-23 10:22:52 +00:00
|
|
|
|
|
2026-01-28 07:39:18 +00:00
|
|
|
|
[Header("공격 설정")]
|
2026-01-27 12:21:34 +00:00
|
|
|
|
[SerializeField] private Vector2 attackBoxSize = new Vector2(2f, 1f);
|
2026-01-28 07:39:18 +00:00
|
|
|
|
|
|
|
|
|
|
public bool _isJumping;
|
|
|
|
|
|
private bool _isGrounded;
|
|
|
|
|
|
private bool _isJumpOnAir;
|
|
|
|
|
|
private bool _isDashing;
|
|
|
|
|
|
private bool _isFacingRight;
|
2026-01-27 12:21:34 +00:00
|
|
|
|
|
2026-01-28 07:39:18 +00:00
|
|
|
|
public Collider2D footCollider;
|
|
|
|
|
|
|
|
|
|
|
|
private Vector2 _inputVector;
|
|
|
|
|
|
private float _gravity;
|
2026-01-23 08:06:57 +00:00
|
|
|
|
|
2026-01-28 07:39:18 +00:00
|
|
|
|
public bool IsDashing => _isDashing;
|
|
|
|
|
|
|
|
|
|
|
|
void Start()
|
2026-01-23 08:06:57 +00:00
|
|
|
|
{
|
2026-01-28 07:39:18 +00:00
|
|
|
|
_rigidbody = GetComponent<Rigidbody2D>();
|
|
|
|
|
|
_gravity = Physics2D.gravity.y;
|
2026-01-27 12:31:18 +00:00
|
|
|
|
currentHp = maxHp;
|
2026-01-28 07:39:18 +00:00
|
|
|
|
_isFacingRight = true;
|
2026-01-23 08:06:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-28 07:39:18 +00:00
|
|
|
|
public void SetInputVector(InputValue value)
|
2026-01-23 08:06:57 +00:00
|
|
|
|
{
|
2026-01-28 07:39:18 +00:00
|
|
|
|
_inputVector = value.Get<Vector2>();
|
|
|
|
|
|
if (_inputVector.x != 0)
|
2026-01-23 08:06:57 +00:00
|
|
|
|
{
|
2026-01-28 07:39:18 +00:00
|
|
|
|
transform.localScale = new Vector3(_inputVector.x > 0 ? 1 : -1, 1, 1);
|
|
|
|
|
|
_isFacingRight = _inputVector.x > 0;
|
2026-01-23 08:06:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-28 07:39:18 +00:00
|
|
|
|
public void SetJumpState(bool isPressed)
|
2026-01-23 10:22:52 +00:00
|
|
|
|
{
|
2026-01-28 07:39:18 +00:00
|
|
|
|
_isJumping = isPressed;
|
|
|
|
|
|
if (isPressed)
|
2026-01-23 10:22:52 +00:00
|
|
|
|
{
|
2026-01-28 07:39:18 +00:00
|
|
|
|
Jump();
|
2026-01-23 10:22:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-28 07:39:18 +00:00
|
|
|
|
public void Jump()
|
2026-01-23 10:22:52 +00:00
|
|
|
|
{
|
2026-01-28 07:39:18 +00:00
|
|
|
|
if (_isDashing) return;
|
|
|
|
|
|
|
|
|
|
|
|
if (_isGrounded || !_isJumpOnAir)
|
2026-01-27 10:20:36 +00:00
|
|
|
|
{
|
2026-01-28 07:39:18 +00:00
|
|
|
|
_rigidbody.linearVelocity = Vector2.up * jumpVelocity;
|
|
|
|
|
|
if (!_isGrounded)
|
2026-01-27 10:20:36 +00:00
|
|
|
|
{
|
2026-01-28 07:39:18 +00:00
|
|
|
|
_isJumpOnAir = true;
|
2026-01-27 10:20:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-01-27 12:21:34 +00:00
|
|
|
|
}
|
2026-01-27 10:20:36 +00:00
|
|
|
|
|
2026-01-28 07:39:18 +00:00
|
|
|
|
public void Dash()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_isDashing || _inputVector == Vector2.zero) return;
|
|
|
|
|
|
StartCoroutine(DashRoutine());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void MoveHorizontal()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_isDashing) return;
|
|
|
|
|
|
_rigidbody.linearVelocity = new Vector2(_inputVector.x * moveVelocity, _rigidbody.linearVelocity.y);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void AddGravityForceOnJump()
|
2026-01-27 12:21:34 +00:00
|
|
|
|
{
|
2026-01-28 07:39:18 +00:00
|
|
|
|
if (_isDashing) return;
|
|
|
|
|
|
|
|
|
|
|
|
if (GroundCheck())
|
2026-01-27 10:20:36 +00:00
|
|
|
|
{
|
2026-01-28 07:39:18 +00:00
|
|
|
|
_gravityWeight = 1f;
|
|
|
|
|
|
return;
|
2026-01-27 10:20:36 +00:00
|
|
|
|
}
|
2026-01-23 10:22:52 +00:00
|
|
|
|
|
2026-01-28 07:39:18 +00:00
|
|
|
|
float gravityMultiplier = 0f;
|
|
|
|
|
|
if (_rigidbody.linearVelocity.y < 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
gravityMultiplier = fallMultiplier;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (_rigidbody.linearVelocity.y > 0 && !_isJumping)
|
2026-01-27 12:21:34 +00:00
|
|
|
|
{
|
2026-01-28 07:39:18 +00:00
|
|
|
|
gravityMultiplier = lowJumpMultiplier;
|
2026-01-27 12:21:34 +00:00
|
|
|
|
}
|
2026-01-28 07:39:18 +00:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
gravityMultiplier = 1f;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Vector2 gravityForce = Vector2.up * _gravity * (gravityMultiplier - 1) * Time.deltaTime;
|
|
|
|
|
|
_rigidbody.linearVelocity += gravityForce * _gravityWeight;
|
2026-01-23 10:22:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-28 07:39:18 +00:00
|
|
|
|
private bool GroundCheck()
|
2026-01-23 10:22:52 +00:00
|
|
|
|
{
|
2026-01-28 07:39:18 +00:00
|
|
|
|
_isGrounded = footCollider.IsTouchingLayers(LayerMask.GetMask("Ground"));
|
|
|
|
|
|
if (_isGrounded)
|
2026-01-27 12:21:34 +00:00
|
|
|
|
{
|
2026-01-28 07:39:18 +00:00
|
|
|
|
_isJumpOnAir = false;
|
2026-01-27 12:21:34 +00:00
|
|
|
|
}
|
2026-01-28 07:39:18 +00:00
|
|
|
|
return _isGrounded;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private IEnumerator DashRoutine()
|
|
|
|
|
|
{
|
|
|
|
|
|
float originGravity = _rigidbody.gravityScale;
|
2026-01-23 10:22:52 +00:00
|
|
|
|
|
2026-01-28 07:39:18 +00:00
|
|
|
|
_rigidbody.gravityScale = 0f;
|
|
|
|
|
|
_isDashing = true;
|
|
|
|
|
|
_gravityWeight = 0f;
|
|
|
|
|
|
|
|
|
|
|
|
float dashSpeed = dashDistance / dashDuration;
|
|
|
|
|
|
Vector2 dashVelocity = _inputVector.normalized * dashSpeed;
|
|
|
|
|
|
|
|
|
|
|
|
float startTime = Time.time;
|
|
|
|
|
|
while (Time.time < startTime + dashDuration)
|
2026-01-27 12:21:34 +00:00
|
|
|
|
{
|
2026-01-28 07:39:18 +00:00
|
|
|
|
_rigidbody.linearVelocity = dashVelocity;
|
|
|
|
|
|
yield return new WaitForFixedUpdate();
|
2026-01-27 12:21:34 +00:00
|
|
|
|
}
|
2026-01-28 07:39:18 +00:00
|
|
|
|
|
|
|
|
|
|
_rigidbody.gravityScale = originGravity;
|
|
|
|
|
|
_isDashing = false;
|
|
|
|
|
|
_rigidbody.linearVelocity = Vector2.zero;
|
|
|
|
|
|
|
|
|
|
|
|
float elapsed = 0f;
|
|
|
|
|
|
while (elapsed < gravityRecoveryTime)
|
2026-01-23 10:22:52 +00:00
|
|
|
|
{
|
2026-01-28 07:39:18 +00:00
|
|
|
|
elapsed += Time.deltaTime;
|
|
|
|
|
|
_gravityWeight = Mathf.Lerp(0f, 1f, elapsed / gravityRecoveryTime);
|
2026-01-23 10:22:52 +00:00
|
|
|
|
yield return null;
|
|
|
|
|
|
}
|
2026-01-28 07:39:18 +00:00
|
|
|
|
_gravityWeight = 1f;
|
2026-01-23 10:22:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-28 07:39:18 +00:00
|
|
|
|
public void TakeDamage(int damage)
|
2026-01-23 10:22:52 +00:00
|
|
|
|
{
|
2026-01-28 07:39:18 +00:00
|
|
|
|
currentHp -= damage;
|
|
|
|
|
|
|
|
|
|
|
|
if (GameManager.instance != null)
|
2026-01-23 10:22:52 +00:00
|
|
|
|
{
|
2026-01-28 07:39:18 +00:00
|
|
|
|
GameManager.instance.ResetCombo();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (currentHp <= 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
Die();
|
2026-01-23 10:22:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-28 07:39:18 +00:00
|
|
|
|
private void Die()
|
|
|
|
|
|
{
|
|
|
|
|
|
Time.timeScale = 1f;
|
|
|
|
|
|
}
|
2026-01-23 08:06:57 +00:00
|
|
|
|
|
2026-01-28 07:39:18 +00:00
|
|
|
|
public void Heal(int amount)
|
2026-01-23 10:22:52 +00:00
|
|
|
|
{
|
2026-01-28 07:39:18 +00:00
|
|
|
|
currentHp += amount;
|
2026-01-27 12:21:34 +00:00
|
|
|
|
if (currentHp > maxHp) currentHp = maxHp;
|
2026-01-27 10:20:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-28 07:39:18 +00:00
|
|
|
|
public void Attack()
|
2026-01-27 10:20:36 +00:00
|
|
|
|
{
|
2026-01-28 07:39:18 +00:00
|
|
|
|
if (_isDashing)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Vector2 originPos = transform.TransformPoint(new Vector2(attackBoxSize.x / 2f, 0f));
|
|
|
|
|
|
Collider2D[] enimes = Physics2D.OverlapBoxAll(originPos, attackBoxSize, 0, enemyLayer);
|
|
|
|
|
|
foreach (Collider2D enemy in enimes)
|
2026-01-27 10:20:36 +00:00
|
|
|
|
{
|
2026-01-28 07:39:18 +00:00
|
|
|
|
enemy.gameObject.GetComponent<Entity>().TakeDamage(10);
|
2026-01-23 10:22:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-01-27 12:21:34 +00:00
|
|
|
|
}
|