TigerProject/Assets/Scripts/Player/Player.cs

187 lines
5.1 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.InputSystem;
public class Player : MonoBehaviour
{
private Rigidbody2D _rigidbody;
[SerializeField] private int currentHp;
[SerializeField] private int maxHp = 10;
private bool isGrounded;
private bool isJumpedOnAir = false;
private bool isDashing;
private List<GameObject> dashHitEnemies = new List<GameObject>();
[SerializeField] private int maxAttackTime = 3;
[SerializeField] private int maxDashTime = 1;
private int currentAttackTime = 0;
private int currentDashTime = 0;
[SerializeField] private float moveSpeed = 9f;
[SerializeField] private float jumpSpeed = 18.2f;
[SerializeField] private float dashTime = 0.5f;
[SerializeField] private float dashDistance = 5f;
private Vector2 inputVector;
[SerializeField] private Vector2 attackBoxSize = new Vector2(2f, 1f);
[SerializeField] private LayerMask enemyLayer;
[SerializeField] private LayerMask groundLayer;
private void Start()
{
currentHp = maxHp;
_rigidbody = gameObject.GetComponent<Rigidbody2D>();
}
public void Jump()
{
if (isGrounded || !isJumpedOnAir)
{
if (!isGrounded)
{
isJumpedOnAir = true;
}
_rigidbody.linearVelocity = new Vector2(_rigidbody.linearVelocity.x, 0);
_rigidbody.AddForce(Vector2.up * jumpSpeed, ForceMode2D.Impulse);
}
}
public void MoveUpdate()
{
if (!isDashing)
{
_rigidbody.linearVelocity = new Vector2(inputVector.x * moveSpeed, _rigidbody.linearVelocity.y);
if (inputVector.x != 0)
{
transform.localScale = new Vector3(Mathf.Sign(inputVector.x), 1, 1);
}
}
}
public void SetXVector(InputValue value)
{
inputVector = value.Get<Vector2>();
}
public void NormalAttack()
{
if (currentAttackTime < maxAttackTime)
{
if (!isGrounded)
{
Debug.Log($"{currentAttackTime + 1} / {maxAttackTime}");
currentAttackTime++;
}
Debug.Log("Attack");
float dir = transform.localScale.x;
Vector2 direction = new Vector2(dir, 0);
Vector2 originPosition = (Vector2)transform.position + (direction * 1);
Collider2D[] enemise = Physics2D.OverlapBoxAll(originPosition, attackBoxSize, 0, enemyLayer);
if (enemise.Length > 0)
{
foreach (Collider2D enemy in enemise)
{
enemy.gameObject.GetComponent<Entity>().TakeDamage(10);
}
}
}
}
public void CheckGround()
{
isGrounded = Physics2D.Raycast(transform.position, Vector2.down, 1.25f, groundLayer);
if (isGrounded)
{
isJumpedOnAir = false;
currentAttackTime = 0;
currentDashTime = 0;
}
}
public void DashAttack()
{
if (isDashing) return;
if (currentDashTime < maxDashTime)
{
currentDashTime++;
StartCoroutine(DashPlayerRoutine());
}
}
private IEnumerator DashPlayerRoutine()
{
isDashing = true;
_rigidbody.linearVelocity = Vector2.zero;
dashHitEnemies.Clear();
Vector2 direction = inputVector.normalized;
if (direction == Vector2.zero)
{
direction = new Vector2(transform.localScale.x, 0);
}
if (isGrounded && direction.y < 0)
{
direction.y = 0;
if (direction.x != 0) direction.x = Mathf.Sign(direction.x);
}
Vector2 startPos = _rigidbody.position;
Vector2 targetPos = startPos + (direction.normalized * 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);
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);
}
}
}