Character-code-sample/Characters/CharacterBase.cs

72 lines
1.6 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharacterBase : MonoBehaviour, IDamageable {
public int Health = 5;
public float Speed = 5;
public Display Display;
[SerializeField] protected List<AudioClip> Footsteps = new List<AudioClip>();
protected float _footstepDelay = 0.5f;
protected float _footstepTimer = 0;
protected CharacterController _cc;
protected AudioSource _audioSource;
public bool IsAlive() { return Health > 0; }
protected virtual void Awake()
{
_cc = GetComponent<CharacterController>();
_audioSource = GetComponent<AudioSource>();
}
protected virtual void Start () {
}
protected virtual void Update () {
}
protected void PlayFootstep()
{
if (Footsteps.Count == 0) return;
if (!_audioSource.isPlaying)
{
if (_footstepTimer > 0)
{
_footstepTimer -= Time.deltaTime;
}
else
{
_audioSource.clip = Footsteps[Random.Range(0, Footsteps.Count)];
_audioSource.Play();
_footstepTimer = _footstepDelay;
}
}
}
public virtual void TakeDamage(int dmg)
{
if(Health > 0)
{
Health -= dmg;
if (Health <= 0)
{
OnDeath();
}
}
}
protected virtual void OnDeath()
{
//GameManager.Instance.PlaySound(DeathSound);
//Destroy(this.gameObject);
}
}