using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
[RequireComponent (typeof (AudioSource))]
public class PlayerHealth : MonoBehaviour
{
public float startingHealth = 100;
public float currentHealth = 100;
public Slider healthBar;
public float healthRegen = 1.5f;
public bool useHitSounds = false;
public bool destroyPlayerOnDeath = false;
public int hitSoundSize;
public List<AudioClip> hitSounds = new List<AudioClip>();
public List<bool> foldOutListHit = new List<bool>();
private bool isDead = false;
private AudioSource _audioSource;
private float timer;
void Awake (){
currentHealth = startingHealth;
if (healthBar == null){
GameObject HB = GameObject.Find("HealthBar");
if (HB != null){
healthBar = HB.GetComponent<Slider>();
}
}
if (healthBar != null){
healthBar.value = currentHealth * 0.01f;
}
_audioSource = GetComponent<AudioSource>();
}
void Update (){
timer += Time.deltaTime * healthRegen;
if (timer >= 1 && currentHealth < 100 && !isDead){
currentHealth += 1;
timer = 0;
}
if (healthBar != null){
healthBar.value = currentHealth * 0.01f;
}
}
public void DamagePlayer (float damageTaken){
if (!isDead){
currentHealth -= damageTaken;
if (useHitSounds && _audioSource != null){
_audioSource.PlayOneShot(hitSounds[Random.Range(0,hitSounds.Count)]);
}
if (currentHealth <= 0){
if (!destroyPlayerOnDeath){
Debug.Log("Player has died");
isDead = true;
}
if (destroyPlayerOnDeath){
Destroy(gameObject);
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//Apply our needed components when attaching the script to an object
[RequireComponent(typeof(BoxCollider))]
[RequireComponent(typeof(Rigidbody))]
public class CollisionDamageAndHealingPlayer : MonoBehaviour {
public enum InteractionTypeEnum { Healing = 0, Damage = 1};
public InteractionTypeEnum InteractionType = InteractionTypeEnum.Healing;
public int DamageAmount = 5;
public int HealingAmount = 5;
public float HealIncrement = 1;
bool PlayerPresent = false;
PlayerHealth PlayerHealthSystem;
Coroutine HealCoroutine;
//Set our components to the right setting on Start
void Start ()
{
GetComponent<BoxCollider>().isTrigger = true;
GetComponent<Rigidbody>().isKinematic = true;
}
void OnTriggerEnter (Collider C)
{
//Check to see if our Emerald AI component is on the collision object.
if (C.gameObject.GetComponent<PlayerHealth>() != null)
{
//Get a reference to the collision object's Emerald AI system and store it.
PlayerHealthSystem = C.gameObject.GetComponent<PlayerHealth>();
if (InteractionType == InteractionTypeEnum.Damage)
{
//Damage our player's health
PlayerHealthSystem.currentHealth -= DamageAmount;
}
else if (InteractionType == InteractionTypeEnum.Healing && !PlayerPresent)
{
//Start our healing coroutine
PlayerPresent = true;
HealCoroutine = StartCoroutine(HealPlayer());
}
}
}
//Cancel collision
void OnTriggerExit(Collider C)
{
PlayerPresent = false;
PlayerHealthSystem = null;
if (HealCoroutine != null)
{
//Stop our healing coroutine
StopCoroutine(HealCoroutine);
}
}
//Start our incremental healing
IEnumerator HealPlayer()
{
while (PlayerPresent)
{
yield return new WaitForSeconds(HealIncrement);
if (PlayerHealthSystem != null)
{
PlayerHealthSystem.currentHealth += HealingAmount;
}
if (PlayerHealthSystem.currentHealth > PlayerHealthSystem.startingHealth)
{
PlayerHealthSystem.currentHealth = PlayerHealthSystem.startingHealth;
}
}
}
}