TigerProject/Assets/Scripts/Player/PlayerController.cs

219 lines
6.2 KiB
C#
Raw Normal View History

2026-01-20 10:06:03 +00:00
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
2026-01-20 10:06:03 +00:00
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerController : MonoBehaviour
{
private Rigidbody2D _rigidbody;
private Camera _camera;
2026-01-20 10:06:03 +00:00
private int maxJumpCount = 2;
private int currentJumpCount = 2;
2026-01-20 10:06:03 +00:00
private bool isGrounded;
private bool isDashing;
private Vector2 inputVector;
private float jumpStartTime;
private Coroutine jumpCutRoutine;
private List<GameObject> dashHitEnemies = new List<GameObject>();
2026-01-20 10:06:03 +00:00
[SerializeField] private float moveSpeed = 9f;
[SerializeField] private float jumpSpeed = 18.2f;
[SerializeField] private float jumpCutMultiplier = 0.3f;
[SerializeField] private float minJumpDuration = 0.1f;
[SerializeField] private float dashTime = 0.5f;
[SerializeField] private float dashDistance = 5f;
2026-01-20 10:06:03 +00:00
[SerializeField] private LayerMask enemyLayer;
[SerializeField] private Vector2 attackBoxSize = new Vector2(1f, 1f);
2026-01-20 10:06:03 +00:00
private void Start()
{
_rigidbody = gameObject.GetComponent<Rigidbody2D>();
_camera = Camera.main;
2026-01-20 10:06:03 +00:00
}
private void FixedUpdate()
{
isGrounded = CheckGround();
MovePlayer();
InitJumpCount();
2026-01-20 10:06:03 +00:00
Debug.DrawRay(transform.position, Vector2.down * 1.25f, Color.red);
}
private void OnMove(InputValue value)
{
Vector2 input = value.Get<Vector2>();
inputVector = new Vector2(input.x * moveSpeed, 0);
}
private void OnAttack(InputValue value)
{
if (isDashing) return;
if (value.isPressed)
{
if (Mouse.current.leftButton.isPressed)
{
NormalAttack();
} else if (Mouse.current.rightButton.isPressed)
{
DashAttack();
}
}
}
2026-01-20 10:06:03 +00:00
private void OnJump(InputValue value)
{
if (value.isPressed)
{
if (currentJumpCount > 0)
{
currentJumpCount--;
jumpStartTime = Time.time;
if (jumpCutRoutine != null) StopCoroutine(jumpCutRoutine);
2026-01-20 10:06:03 +00:00
_rigidbody.linearVelocity = new Vector2(_rigidbody.linearVelocity.x, 0);
_rigidbody.AddForce(Vector2.up * jumpSpeed, ForceMode2D.Impulse);
}
}
else
{
float timeElapsed = Time.time - jumpStartTime;
2026-01-20 10:06:03 +00:00
if (timeElapsed >= minJumpDuration)
{
CutJumpVelocity();
}
else
{
float delay = minJumpDuration - timeElapsed;
jumpCutRoutine = StartCoroutine(DelayedJumpCut(delay));
2026-01-20 10:06:03 +00:00
}
}
}
private void NormalAttack()
{
float angle = GetMouseAngle();
Vector2 direction = new Vector2(Mathf.Cos(angle * Mathf.Deg2Rad), Mathf.Sin(angle * Mathf.Deg2Rad));
Vector2 originPosition = (Vector2)transform.position + (direction * 1);
Collider2D[] enemise = Physics2D.OverlapBoxAll(originPosition, attackBoxSize, angle, enemyLayer);
2026-01-23 06:36:39 +00:00
Debug.Log(angle);
if (enemise.Length > 0)
{
2026-01-23 06:36:39 +00:00
foreach (Collider2D enemy in enemise)
{
Debug.Log(enemy.name);
enemy.gameObject.GetComponent<Entity>().TakeDamage(10);
}
if (angle > -110 && angle < -70)
{
DownAttackLeap();
}
}
2026-01-23 06:36:39 +00:00
}
2026-01-23 06:36:39 +00:00
private void DownAttackLeap()
{
_rigidbody.linearVelocity = new Vector2(-_rigidbody.linearVelocity.x, 20f);
}
private void DashAttack()
{
if (isDashing) return;
StartCoroutine(DashPlayerRoutine());
}
private IEnumerator DashPlayerRoutine()
{
isDashing = true;
_rigidbody.linearVelocity = Vector2.zero;
dashHitEnemies.Clear();
Vector2 mousePos = _camera.ScreenToWorldPoint(Mouse.current.position.ReadValue());
Vector2 direction = (mousePos - (Vector2)transform.position).normalized;
if (isGrounded && mousePos.y < transform.position.y)
{
direction = new Vector2(Mathf.Sign(direction.x), 0);
}
Vector2 startPos = _rigidbody.position;
Vector2 targetPos = startPos + (direction * dashDistance);
float dashTimer = 0f;
while (dashTimer < dashTime)
{
dashTimer += Time.deltaTime;
float t = dashTimer / dashTime;
_rigidbody.MovePosition(Vector2.Lerp(startPos, targetPos, t));
yield return null;
}
isDashing = false;
}
private void MovePlayer()
{
if(!isDashing)
_rigidbody.linearVelocity = new Vector2(inputVector.x, _rigidbody.linearVelocity.y);
}
private float GetMouseAngle()
{
Vector2 tempVect = _camera.ScreenToWorldPoint(Mouse.current.position.ReadValue()) - transform.position;
return Mathf.Atan2(tempVect.y, tempVect.x) * Mathf.Rad2Deg;
}
2026-01-20 10:06:03 +00:00
private void CutJumpVelocity()
{
if (_rigidbody.linearVelocity.y > 0)
{
_rigidbody.linearVelocity = new Vector2(_rigidbody.linearVelocity.x, _rigidbody.linearVelocity.y * jumpCutMultiplier);
}
}
private IEnumerator DelayedJumpCut(float delay)
{
yield return new WaitForSeconds(delay);
CutJumpVelocity();
}
private bool CheckGround()
{
return Physics2D.Raycast(transform.position, Vector2.down, 1.25f, LayerMask.GetMask("Ground"));
}
private void InitJumpCount()
2026-01-20 10:06:03 +00:00
{
if (isGrounded && _rigidbody.linearVelocity.y <= 0.1f)
{
currentJumpCount = maxJumpCount;
}
2026-01-20 10:06:03 +00:00
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (isDashing)
{
if ((enemyLayer.value & (1 << collision.gameObject.layer)) > 0)
{
if (!dashHitEnemies.Contains(collision.gameObject))
{
dashHitEnemies.Add(collision.gameObject);
Debug.Log(collision.gameObject.name + " 대쉬 피격");
collision.gameObject.GetComponent<Entity>().TakeDamage(10);
}
}
}
}
2026-01-20 10:06:03 +00:00
}