187 lines
5.2 KiB
C#
187 lines
5.2 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Unity.VisualScripting;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
|
|
public class Player : MonoBehaviour
|
|
{
|
|
private Camera _camera;
|
|
private Rigidbody2D _rigidbody2D;
|
|
|
|
private int currentHp;
|
|
private int maxHp;
|
|
|
|
private int maxJumpCount = 2;
|
|
private int currentJumpCount = 2;
|
|
|
|
private float moveX;
|
|
|
|
private bool isGrounded;
|
|
private bool isDashing;
|
|
|
|
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 Vector2 attackBoxSize = new Vector2(1f, 1f);
|
|
[SerializeField] private LayerMask enemyLayer;
|
|
[SerializeField] private float dashTime = 0.5f;
|
|
[SerializeField] private float dashDistance = 5f;
|
|
|
|
private Rigidbody2D _rigidbody;
|
|
|
|
|
|
private void Start()
|
|
{
|
|
_rigidbody = gameObject.GetComponent<Rigidbody2D>();
|
|
_camera = Camera.main;
|
|
}
|
|
|
|
public void Jump()
|
|
{
|
|
if (currentJumpCount > 0)
|
|
{
|
|
currentJumpCount--;
|
|
_rigidbody.linearVelocity = new Vector2(_rigidbody.linearVelocity.x, 0);
|
|
_rigidbody.AddForce(Vector2.up * jumpSpeed, ForceMode2D.Impulse);
|
|
}
|
|
}
|
|
|
|
public void MoveUpdate()
|
|
{
|
|
if (!isDashing)
|
|
{
|
|
_rigidbody.linearVelocity = new Vector2(moveX * moveSpeed, _rigidbody.linearVelocity.y);
|
|
}
|
|
}
|
|
|
|
public void SetXVector(InputValue value)
|
|
{
|
|
moveX = value.Get<Vector2>().x;
|
|
}
|
|
|
|
public 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);
|
|
Debug.Log(angle);
|
|
if (enemise.Length > 0)
|
|
{
|
|
foreach (Collider2D enemy in enemise)
|
|
{
|
|
Debug.Log(enemy.name);
|
|
enemy.gameObject.GetComponent<Entity>().TakeDamage(10);
|
|
}
|
|
if (angle > -110 && angle < -70)
|
|
{
|
|
_rigidbody.linearVelocity = new Vector2(-_rigidbody.linearVelocity.x, 20f);
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
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();
|
|
}
|
|
|
|
public void CheckGround()
|
|
{
|
|
isGrounded = Physics2D.Raycast(transform.position, Vector2.down, 1.25f, LayerMask.GetMask("Ground"));
|
|
if (isGrounded) { currentJumpCount = maxJumpCount; }
|
|
}
|
|
|
|
private float GetMouseAngle()
|
|
{
|
|
Vector2 tempVect = _camera.ScreenToWorldPoint(Mouse.current.position.ReadValue()) - transform.position;
|
|
return Mathf.Atan2(tempVect.y, tempVect.x) * Mathf.Rad2Deg;
|
|
}
|
|
|
|
public 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;
|
|
}
|
|
|
|
public void AttackOnDash(Collider2D collision)
|
|
{
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
|
|
public bool IsDashing() { return isDashing; }
|
|
|
|
public void Heal(int heal)
|
|
{
|
|
if (heal < 0) { return; }
|
|
|
|
currentHp += heal;
|
|
if (currentHp > maxHp)
|
|
{
|
|
currentHp = maxHp;
|
|
}
|
|
}
|
|
|
|
public void TakeDamage(int playerAttackDamage)
|
|
{
|
|
currentHp -= playerAttackDamage;
|
|
if (currentHp <= 0)
|
|
{
|
|
currentHp = 0;
|
|
Destroy(gameObject);
|
|
}
|
|
}
|
|
} |