2026-01-27 12:21:34 +00:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections;
|
2026-01-23 08:06:57 +00:00
|
|
|
|
using System.Collections.Generic;
|
2026-01-27 12:21:34 +00:00
|
|
|
|
using Unity.VisualScripting;
|
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-23 08:06:57 +00:00
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
private bool isGrounded;
|
2026-01-27 12:21:34 +00:00
|
|
|
|
private bool isJumpedOnAir = false;
|
2026-01-23 10:22:52 +00:00
|
|
|
|
private bool isDashing;
|
2026-01-27 12:21:34 +00:00
|
|
|
|
|
2026-01-23 10:22:52 +00:00
|
|
|
|
private List<GameObject> dashHitEnemies = new List<GameObject>();
|
|
|
|
|
|
|
2026-01-27 12:21:34 +00:00
|
|
|
|
[SerializeField] private int maxAttackTime = 3;
|
|
|
|
|
|
[SerializeField] private int maxDashTime = 1;
|
|
|
|
|
|
private int currentAttackTime = 0;
|
|
|
|
|
|
private int currentDashTime = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-01-23 08:06:57 +00:00
|
|
|
|
[SerializeField] private float moveSpeed = 9f;
|
|
|
|
|
|
[SerializeField] private float jumpSpeed = 18.2f;
|
2026-01-23 10:22:52 +00:00
|
|
|
|
[SerializeField] private float dashTime = 0.5f;
|
|
|
|
|
|
[SerializeField] private float dashDistance = 5f;
|
2026-01-27 12:21:34 +00:00
|
|
|
|
|
|
|
|
|
|
private Vector2 inputVector;
|
|
|
|
|
|
|
|
|
|
|
|
[SerializeField] private Vector2 attackBoxSize = new Vector2(2f, 1f);
|
|
|
|
|
|
|
2026-01-27 10:20:36 +00:00
|
|
|
|
[SerializeField] private LayerMask enemyLayer;
|
|
|
|
|
|
[SerializeField] private LayerMask groundLayer;
|
2026-01-23 08:06:57 +00:00
|
|
|
|
|
|
|
|
|
|
private void Start()
|
|
|
|
|
|
{
|
2026-01-27 12:21:34 +00:00
|
|
|
|
_rigidbody = gameObject.GetComponent<Rigidbody2D>();
|
2026-01-23 08:06:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-27 12:21:34 +00:00
|
|
|
|
public void Jump()
|
2026-01-23 08:06:57 +00:00
|
|
|
|
{
|
2026-01-27 12:21:34 +00:00
|
|
|
|
if (isGrounded || !isJumpedOnAir)
|
2026-01-23 08:06:57 +00:00
|
|
|
|
{
|
2026-01-27 12:21:34 +00:00
|
|
|
|
if (!isGrounded)
|
2026-01-27 10:20:36 +00:00
|
|
|
|
{
|
2026-01-27 12:21:34 +00:00
|
|
|
|
isJumpedOnAir = true;
|
2026-01-27 10:20:36 +00:00
|
|
|
|
}
|
2026-01-27 12:21:34 +00:00
|
|
|
|
|
|
|
|
|
|
_rigidbody.linearVelocity = new Vector2(_rigidbody.linearVelocity.x, 0);
|
|
|
|
|
|
_rigidbody.AddForce(Vector2.up * jumpSpeed, ForceMode2D.Impulse);
|
2026-01-23 08:06:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-27 12:21:34 +00:00
|
|
|
|
public void MoveUpdate()
|
2026-01-23 10:22:52 +00:00
|
|
|
|
{
|
2026-01-27 12:21:34 +00:00
|
|
|
|
if (!isDashing)
|
2026-01-23 10:22:52 +00:00
|
|
|
|
{
|
2026-01-27 12:21:34 +00:00
|
|
|
|
_rigidbody.linearVelocity = new Vector2(inputVector.x * moveSpeed, _rigidbody.linearVelocity.y);
|
|
|
|
|
|
|
|
|
|
|
|
if (inputVector.x != 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
transform.localScale = new Vector3(Mathf.Sign(inputVector.x), 1, 1);
|
|
|
|
|
|
}
|
2026-01-23 10:22:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void SetXVector(InputValue value)
|
|
|
|
|
|
{
|
2026-01-27 10:20:36 +00:00
|
|
|
|
inputVector = value.Get<Vector2>();
|
2026-01-23 10:22:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-27 10:20:36 +00:00
|
|
|
|
public void NormalAttack()
|
2026-01-23 10:22:52 +00:00
|
|
|
|
{
|
2026-01-27 12:21:34 +00:00
|
|
|
|
if (currentAttackTime < maxAttackTime)
|
2026-01-27 10:20:36 +00:00
|
|
|
|
{
|
2026-01-27 12:21:34 +00:00
|
|
|
|
if (!isGrounded)
|
2026-01-27 10:20:36 +00:00
|
|
|
|
{
|
2026-01-27 12:21:34 +00:00
|
|
|
|
Debug.Log($"{currentAttackTime + 1} / {maxAttackTime}");
|
|
|
|
|
|
currentAttackTime++;
|
2026-01-27 10:20:36 +00:00
|
|
|
|
}
|
2026-01-27 12:21:34 +00:00
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
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-27 12:21:34 +00:00
|
|
|
|
public void CheckGround()
|
|
|
|
|
|
{
|
|
|
|
|
|
isGrounded = Physics2D.Raycast(transform.position, Vector2.down, 1.25f, groundLayer);
|
|
|
|
|
|
if (isGrounded)
|
2026-01-27 10:20:36 +00:00
|
|
|
|
{
|
2026-01-27 12:21:34 +00:00
|
|
|
|
isJumpedOnAir = false;
|
|
|
|
|
|
currentAttackTime = 0;
|
|
|
|
|
|
currentDashTime = 0;
|
2026-01-27 10:20:36 +00:00
|
|
|
|
}
|
2026-01-23 10:22:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void DashAttack()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (isDashing) return;
|
2026-01-27 12:21:34 +00:00
|
|
|
|
if (currentDashTime < maxDashTime)
|
|
|
|
|
|
{
|
|
|
|
|
|
currentDashTime++;
|
|
|
|
|
|
StartCoroutine(DashPlayerRoutine());
|
|
|
|
|
|
}
|
2026-01-23 10:22:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private IEnumerator DashPlayerRoutine()
|
|
|
|
|
|
{
|
|
|
|
|
|
isDashing = true;
|
|
|
|
|
|
_rigidbody.linearVelocity = Vector2.zero;
|
|
|
|
|
|
dashHitEnemies.Clear();
|
|
|
|
|
|
|
2026-01-27 10:20:36 +00:00
|
|
|
|
Vector2 direction = inputVector.normalized;
|
|
|
|
|
|
if (direction == Vector2.zero)
|
2026-01-27 12:21:34 +00:00
|
|
|
|
{
|
|
|
|
|
|
direction = new Vector2(transform.localScale.x, 0);
|
|
|
|
|
|
}
|
2026-01-23 10:22:52 +00:00
|
|
|
|
|
2026-01-27 10:20:36 +00:00
|
|
|
|
if (isGrounded && direction.y < 0)
|
2026-01-27 12:21:34 +00:00
|
|
|
|
{
|
|
|
|
|
|
direction.y = 0;
|
|
|
|
|
|
if (direction.x != 0) direction.x = Mathf.Sign(direction.x);
|
|
|
|
|
|
}
|
2026-01-23 10:22:52 +00:00
|
|
|
|
|
|
|
|
|
|
Vector2 startPos = _rigidbody.position;
|
2026-01-27 12:21:34 +00:00
|
|
|
|
Vector2 targetPos = startPos + (direction.normalized * dashDistance);
|
2026-01-23 10:22:52 +00:00
|
|
|
|
|
2026-01-27 12:21:34 +00:00
|
|
|
|
float dashTimer = 0f;
|
|
|
|
|
|
while (dashTimer < dashTime)
|
2026-01-23 10:22:52 +00:00
|
|
|
|
{
|
2026-01-27 12:21:34 +00:00
|
|
|
|
dashTimer += Time.deltaTime;
|
|
|
|
|
|
float t = dashTimer / dashTime;
|
2026-01-23 10:22:52 +00:00
|
|
|
|
_rigidbody.MovePosition(Vector2.Lerp(startPos, targetPos, t));
|
|
|
|
|
|
yield return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
isDashing = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void AttackOnDash(Collider2D collision)
|
|
|
|
|
|
{
|
2026-01-27 12:21:34 +00:00
|
|
|
|
if ((enemyLayer.value & (1 << collision.gameObject.layer)) > 0)
|
2026-01-23 10:22:52 +00:00
|
|
|
|
{
|
2026-01-27 12:21:34 +00:00
|
|
|
|
if (!dashHitEnemies.Contains(collision.gameObject))
|
|
|
|
|
|
{
|
|
|
|
|
|
dashHitEnemies.Add(collision.gameObject);
|
|
|
|
|
|
collision.gameObject.GetComponent<Entity>().TakeDamage(10);
|
|
|
|
|
|
}
|
2026-01-23 10:22:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-27 12:21:34 +00:00
|
|
|
|
public bool IsDashing() { return isDashing; }
|
2026-01-23 08:06:57 +00:00
|
|
|
|
|
2026-01-27 12:21:34 +00:00
|
|
|
|
public void Heal(int heal)
|
2026-01-23 10:22:52 +00:00
|
|
|
|
{
|
2026-01-27 12:21:34 +00:00
|
|
|
|
if (heal < 0) return;
|
|
|
|
|
|
currentHp += heal;
|
|
|
|
|
|
if (currentHp > maxHp) currentHp = maxHp;
|
2026-01-27 10:20:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-27 12:21:34 +00:00
|
|
|
|
public void TakeDamage(int playerAttackDamage)
|
2026-01-27 10:20:36 +00:00
|
|
|
|
{
|
2026-01-27 12:21:34 +00:00
|
|
|
|
currentHp -= playerAttackDamage;
|
|
|
|
|
|
if (currentHp <= 0)
|
2026-01-27 10:20:36 +00:00
|
|
|
|
{
|
2026-01-27 12:21:34 +00:00
|
|
|
|
currentHp = 0;
|
|
|
|
|
|
Destroy(gameObject);
|
2026-01-23 10:22:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-01-27 12:21:34 +00:00
|
|
|
|
}
|