72 lines
1.7 KiB
C#
72 lines
1.7 KiB
C#
using System;
|
|
using Data;
|
|
using UnityEngine;
|
|
|
|
public class Pawn : MonoBehaviour
|
|
{
|
|
public event Action<int> HPUpdated;
|
|
public int MaxHP { get; protected set; }
|
|
public int CurrentHP { get; protected set; }
|
|
public Animator Animator => animator;
|
|
|
|
[SerializeField] private UnitData unitData;
|
|
[SerializeField] private SpriteRenderer spriteRenderer;
|
|
[SerializeField] private Animator animator;
|
|
|
|
protected bool IsDead;
|
|
|
|
private Vector2 _moveInput;
|
|
|
|
private void Awake()
|
|
{
|
|
MaxHP = unitData.MaxHP;
|
|
CurrentHP = MaxHP;
|
|
}
|
|
|
|
public void TakeDamage(int damage)
|
|
{
|
|
//animator.Play("HURT");
|
|
CurrentHP -= damage;
|
|
CurrentHP = Mathf.Clamp(CurrentHP, 0, MaxHP);
|
|
HPUpdated?.Invoke(CurrentHP);
|
|
CheckDeath();
|
|
}
|
|
|
|
public virtual void HandleIdle()
|
|
{
|
|
animator.Play("Idle");
|
|
}
|
|
|
|
public virtual void HandleMove(Vector2 input)
|
|
{
|
|
_moveInput = input;
|
|
animator.Play(input.magnitude > 0 ? "Walk" : "Idle");
|
|
|
|
if (_moveInput.magnitude > 0)
|
|
transform.localScale = new Vector3(_moveInput.x > 0 ? 1 : -1, 1, 1);
|
|
}
|
|
|
|
public virtual float HandleAttack(string attackName)
|
|
{
|
|
animator.Play(attackName);
|
|
animator.Update(0f);
|
|
var state = animator.GetCurrentAnimatorStateInfo(0);
|
|
return state.length * state.speed;
|
|
}
|
|
|
|
private void FixedUpdate()
|
|
{
|
|
transform.Translate(_moveInput * unitData.MoveSpeed);
|
|
}
|
|
|
|
private void CheckDeath()
|
|
{
|
|
if (CurrentHP <= 0)
|
|
{
|
|
//animator.Play("Death");
|
|
IsDead = true;
|
|
Destroy(gameObject);
|
|
}
|
|
}
|
|
}
|