Shoot-N-Slash/Assets/Scripts/Entities/EntityStats.cs

89 lines
2.3 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using DamageNumbersPro;
using Unity.Mathematics;
using UnityEngine;
public class EntityStats : MonoBehaviour
{
[Header("Identification")]
public string name;
public Sprite icon; //only if applicable...
public Entity thisEntity;
[Header("Health")]
public float health;
public float maxHealth;
public DamageNumber damageNumbers;
[Header("Health UI")]
public Transform healthBar;
[Header("Stats")]
public float speed;
public float jumpPower;
[Header("State")]
public bool isStalled;
public bool abilitiesDisabled;
[Header("Ground Detection")]
[SerializeField] private Transform groundCheck;
[SerializeField] private LayerMask groundLayer;
[Header("Attack Origin")]
public Transform attackOriginPoint;
public Transform attackOriginCenter;
[Header("Abilities")]
public List<Ability> abilities = new();
[Header("Cache")]
public Rigidbody2D rb;
public SpriteRenderer sprite;
public bool isFacingRight;
public Color originalColor; //remove later
public float damageColorChangeSpeed;
private void Start()
{
originalColor = sprite.color;
}
public void TakeDamage(float damage)
{
health -= damage;
StartCoroutine(DamageVisual());
if (damageNumbers)
{
damageNumbers.Spawn(transform.position, damage.ToString());
}
if (healthBar)
{
UpdateHealthBar();
}
if (health <= 0)
{
thisEntity.OnDeath();
}
}
private IEnumerator DamageVisual()
{
float currentState = 0;
while (currentState < 1)
{
currentState += Time.deltaTime * damageColorChangeSpeed;
sprite.color = Color.Lerp(Color.gray, originalColor, currentState);
yield return null;
}
}
public void Heal(float healing)
{
health += healing;
health = Math.Clamp(health, 0, maxHealth);
}
public bool OnGround()
{
return Physics2D.OverlapCircle(groundCheck.position, 0.05f, groundLayer);
}
public void UpdateHealthBar()
{
healthBar.transform.localScale = new Vector3(math.clamp(health / maxHealth, 0f, 1f), 1f, 1f);
}
}