Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class HealthLogic : MonoBehaviour {
- // Debugging
- public bool loggingEnabled = false;
- // Stats
- public float health = 10;
- public float maxHealth = 10;
- public float invulnerabilityPeriod = 0.2f;
- [Range(0.0f,1.0f)] public float invulnOpacity = 0.5f;
- // Other
- public bool playsSoundOnHit = false;
- private float oldOpacity;
- private float invulnTime = 0;
- // Initialization
- void Start ()
- {
- oldOpacity = this.renderer.material.color.a;
- }
- void LateUpdate ()
- {
- if (loggingEnabled)
- Debug.Log (health);
- }
- public bool Damage (float amount)
- {
- if (Time.time > invulnTime)
- {
- health -= amount;
- invulnTime = Time.time + invulnerabilityPeriod;
- if (playsSoundOnHit)
- audio.Play ();
- if (health <= 0)
- {
- if (this.tag == "Player")
- Application.LoadLevel (0);
- else
- Destroy (gameObject);
- }
- else
- {
- Color color = this.renderer.material.color;
- color.a = invulnOpacity;
- this.renderer.material.color = color;
- StartCoroutine (normalizeOpacity (invulnerabilityPeriod));
- }
- }
- return false;
- }
- private IEnumerator normalizeOpacity (float delay)
- {
- yield return new WaitForSeconds (delay);
- Color color = this.renderer.material.color;
- color.a = oldOpacity;
- this.renderer.material.color = color;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement