Sunday, 9 October 2016

EnemyHealth

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class EnemyHealth : MonoBehaviour {
public Slider healthBar;
private int Health;
public int maxHealth;
public Text healthText;
public Image visualHealth;
public GameObject enemyDamagePartical;

public GameObject GameWin;
// Use this for initialization

//audios
private AudioSource audio;
public AudioClip EDie;

void Start () {
audio =  GameObject.Find("SoundSettings").GetComponent<AudioSource>();

Health = maxHealth;
healthBar.maxValue = maxHealth;
healthBar.value = maxHealth;
HandleHealth();
}

// Update is called once per frame
void Update () {
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "SuperBall")
{    
audio.PlayOneShot(EDie,0.7f);
Health -= 1;
healthBar.value = Health;
Instantiate(enemyDamagePartical,transform.position, Quaternion.identity);
HandleHealth();
Destroy(other.gameObject);
}
}

public int levellock;
private void HandleHealth()
{
healthText.text =  Health + "/" + maxHealth + "HP";
if (Health > maxHealth / 2) {

visualHealth.color = new Color32((byte)MapValues(Health, maxHealth/2, maxHealth,255,0),255, 0, 255);
}
else{
visualHealth.color = new Color32(255, (byte)MapValues(Health, 0, maxHealth /2, 0, 255), 0, 255);
}

if (Health <= 0) {

// level lock
if(PlayerPrefs.GetInt("levellock")<levellock){

PlayerPrefs.SetInt("levellock",levellock);

//unlock new world
if(levellock==11){
PlayerPrefs.SetInt("maridunia",1);
}

Debug.Log ("AB");
}
GameWin.SetActive(true);
Destroy(this.gameObject);





}

}
private float MapValues(float x, float inMin, float inMax, float outMin, float outMax)
{
return (x - inMin) * (outMax - outMin) / (inMax - inMin) + outMin;
}
}

No comments:

Post a Comment