Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class Damage : MonoBehaviour {
- public float currentShield;
- public float maxShield = 1;
- public Explosion.ExplosionType explosionType = Explosion.ExplosionType.Medium;
- [HideInInspector] public float lastExplosionDamage;
- [HideInInspector] public Vector3 lastVelocity;
- [HideInInspector] public float factor;
- public float slideDamage = 1;
- public DieType dieType = DieType.Destroy;
- public enum DieType {
- None,
- Unfreeze,
- Destroy
- }
- public void Start()
- {
- currentShield = maxShield;
- lastExplosionDamage = currentShield;
- }
- void FixedUpdate()
- {
- if (rigidbody != null) {
- lastVelocity = rigidbody.velocity;
- }
- }
- public void Add(float amount, Vector3 pos)
- {
- if (currentShield > 0) {
- currentShield = Mathf.Clamp(currentShield - amount, 0, maxShield);
- if (currentShield == 0 )
- {
- Explode();
- } else {
- // small explosions for gradual damage
- if (currentShield < lastExplosionDamage - 0.1f) {
- if (amount >= 0.3f) {
- Explosion.Explode(pos,lastVelocity,Explosion.ExplosionType.Medium);
- } else if (amount >= 0.05f) {
- Explosion.Explode(pos,lastVelocity,Explosion.ExplosionType.Small);
- }
- lastExplosionDamage = currentShield;
- }
- factor = Mathf.InverseLerp(maxShield,0,currentShield);
- }
- }
- }
- public void Explode()
- {
- Explosion e = Explosion.Explode(transform.position,lastVelocity,explosionType);
- Collider[] myColliders = GetComponentsInChildren<Collider>();
- foreach (Collider c in e.GetComponentsInChildren<Collider>())
- {
- foreach (Collider c2 in myColliders) {
- Physics.IgnoreCollision(c,c2);
- }
- }
- switch (dieType) {
- case DieType.Destroy: Destroy(gameObject); break;
- case DieType.Unfreeze: rigidbody.constraints = RigidbodyConstraints.None; break;
- }
- CameraMovement mainCam = Camera.main.GetComponent<CameraMovement>();
- // if camera was watching this object, now watch the explosion instead
- if (mainCam.target == transform)
- {
- Time.timeScale = 0.1f;
- mainCam.target = e.transform;
- }
- }
- void OnCollisionEnter(Collision c)
- {
- if (rigidbody != null)
- {
- float velocityChangeFactor = Mathf.InverseLerp(0,90,Vector3.Angle(rigidbody.velocity,lastVelocity));
- Add( c.impactForceSum.magnitude * 0.01f * velocityChangeFactor, c.contacts[0].point);
- } else {
- // this is a static object - be damaged by impact of other moving object
- Add( c.impactForceSum.magnitude * 0.01f, c.contacts[0].point);
- }
- }
- void OnCollisionStay(Collision c)
- {
- //Debug.Log(slideDamage);
- Add( c.impactForceSum.magnitude * 0.00001f * slideDamage, c.contacts[0].point);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement