jimmy tremblay-Bernier 86fbe09b20 initial commit
2022-03-12 20:32:56 -05:00

87 lines
2.3 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
[SerializeField]
private int MaxHealth = 2;
private int Health;
public bool Alive = true;
public event Action<float> OnHealthChanged = delegate { };
private Animator enemyAnimator = null;
private PlayerManagement pm;
private void Awake()
{
Health = MaxHealth;
enemyAnimator = GetComponent<Animator>();
pm = GameObject.Find("/OmniCharacterController").GetComponent<PlayerManagement>();
GetComponent<EnemyMovement>().setEnemyAnimatior(enemyAnimator);
SetUpEnemyBodyParts();
}
private void SetUpEnemyBodyParts()
{
EnemyBodyPart[] bodyParts = GetComponentsInChildren<EnemyBodyPart>();
foreach(EnemyBodyPart enemyBodyPart in bodyParts)
{
enemyBodyPart.Setup(this);
}
}
public void TakeDamage(int damage, bool arrowOnFire, bool headShot)
{
if (!Alive)
return;
if (headShot || arrowOnFire)
damage *= 2;
Health -= damage;
float currentHealth = (float)Health / (float)MaxHealth;
OnHealthChanged(currentHealth);
if (Health <= 0)
Kill(arrowOnFire, headShot);
else
// Add 1 point when hitting an enemy
pm.AddPoints(1);
}
public void Kill(bool arrowOnFire, bool headShot)
{
Alive = false;
Destroy(gameObject.transform.Find("HealthBarCanvas").gameObject);
pm.EvaluatePointsOnKill(headShot, arrowOnFire);
if(enemyAnimator != null)
{
if (!arrowOnFire)
enemyAnimator.runtimeAnimatorController = Resources.Load("Animations/DeathByArrow/DeathByArrowController") as RuntimeAnimatorController;
else
enemyAnimator.runtimeAnimatorController = Resources.Load("Animations/DeathByBurn/DeathByBurnController") as RuntimeAnimatorController;
GetComponent<EnemyMovement>().removeEnemySocket();
// Find animation time and destroy enemy once animation ends
Destroy(gameObject, enemyAnimator.runtimeAnimatorController.animationClips[0].length - 0.2f);
}
else
{
Destroy(gameObject);
}
}
}