205 lines
5.9 KiB
C#
205 lines
5.9 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Unity.VisualScripting;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
|
|
public class PlayerController : MonoBehaviour
|
|
{
|
|
private Rigidbody2D _rigidbody;
|
|
private Camera _camera;
|
|
|
|
private int maxJumpCount = 2;
|
|
private int currentJumpCount = 2;
|
|
|
|
private bool isGrounded;
|
|
private bool isDashing;
|
|
|
|
private Vector2 inputVector;
|
|
private float jumpStartTime;
|
|
|
|
private Coroutine jumpCutRoutine;
|
|
|
|
private List<GameObject> dashHitEnemies = new List<GameObject>();
|
|
|
|
[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;
|
|
|
|
[SerializeField] private LayerMask enemyLayer;
|
|
[SerializeField] private Vector2 attackBoxSize = new Vector2(1f, 1f);
|
|
|
|
private void Start()
|
|
{
|
|
_rigidbody = gameObject.GetComponent<Rigidbody2D>();
|
|
_camera = Camera.main;
|
|
}
|
|
|
|
private void FixedUpdate()
|
|
{
|
|
isGrounded = CheckGround();
|
|
MovePlayer();
|
|
InitJumpCount();
|
|
|
|
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();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void OnJump(InputValue value)
|
|
{
|
|
if (value.isPressed)
|
|
{
|
|
if (currentJumpCount > 0)
|
|
{
|
|
currentJumpCount--;
|
|
jumpStartTime = Time.time;
|
|
if (jumpCutRoutine != null) StopCoroutine(jumpCutRoutine);
|
|
_rigidbody.linearVelocity = new Vector2(_rigidbody.linearVelocity.x, 0);
|
|
_rigidbody.AddForce(Vector2.up * jumpSpeed, ForceMode2D.Impulse);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
float timeElapsed = Time.time - jumpStartTime;
|
|
|
|
if (timeElapsed >= minJumpDuration)
|
|
{
|
|
CutJumpVelocity();
|
|
}
|
|
else
|
|
{
|
|
float delay = minJumpDuration - timeElapsed;
|
|
jumpCutRoutine = StartCoroutine(DelayedJumpCut(delay));
|
|
}
|
|
}
|
|
}
|
|
|
|
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);
|
|
foreach (Collider2D enemy in enemise)
|
|
{
|
|
Debug.Log(enemy.name);
|
|
enemy.gameObject.GetComponent<Entity>().TakeDamage(10);
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
|
|
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()
|
|
{
|
|
if (isGrounded && _rigidbody.linearVelocity.y <= 0.1f)
|
|
{
|
|
currentJumpCount = maxJumpCount;
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
} |