using System; using UnityEngine; public class Entity : MonoBehaviour { [Header("Identification")] public string entityName; public SpriteRenderer sprite; public AudioSource entityAS; [Header("Flags")] public bool stalled; public bool invincible; [Header("Stats")] public float health; public float baseMaxHealth; //for enemies, mostly. public float maxHealth; [Header("Movement")] [SerializeField] private bool isFacingRight; [SerializeField] protected Rigidbody2D rb; protected Vector2 moveDirection; public float speed; public float speedMultiplier = 1f; [Header("FX")] [SerializeField] private EntityDeathSFX deathObject; [SerializeField] private AudioClip deathSound; [SerializeField] private float deathVolume = 1; protected void FlipSprite(Vector2 lookDirection) { if (lookDirection.x > 0f && isFacingRight) { sprite.flipX = true; isFacingRight = !isFacingRight; } else if (lookDirection.x < 0f && !isFacingRight) { sprite.flipX = false; isFacingRight = !isFacingRight; } } protected virtual void FixedUpdate() { if (!stalled) { rb.linearVelocity = moveDirection * (speed * speedMultiplier); FlipSprite(moveDirection); } } public virtual void TakeDamage(float damage, Entity origin) { health -= damage; if (health < 0) { EntityDeathSFX newSFX = Instantiate(deathObject, transform.position, transform.rotation); newSFX.PlaySFX(deathSound, deathVolume); OnKillEffects(); } } public void Heal(float healingAmount) { health += healingAmount; health = Mathf.Clamp(health, 0, maxHealth); } protected virtual void OnKillEffects() { } }