Guest
Public paste!

Untitled

By: a guest | Mar 12th, 2010 | Syntax: None | Size: 1.43 KB | Hits: 35 | Expires: Never
Copy text to clipboard
  1. var hitPoints = 100.0;
  2. var detonationDelay = 0.0;
  3. var explosion : Transform;
  4. var deadReplacement : Rigidbody;
  5. function ApplyDamage (damage : float)
  6. {
  7. // We already have less than 0 hitpoints, maybe we got killed already?
  8. if (hitPoints <= 0.0)
  9. return;
  10. hitPoints -= damage;
  11. if (hitPoints <= 0.0)
  12. {
  13. // Start emitting particles
  14. var emitter : ParticleEmitter = GetComponentInChildren(ParticleEmitter);
  15. if (emitter)
  16. emitter.emit = true;
  17. Invoke("DelayedDetonate", detonationDelay);
  18. }
  19. }
  20. function DelayedDetonate ()
  21. {
  22. BroadcastMessage ("Detonate");
  23. }
  24. function Detonate ()
  25. {
  26. // Destroy ourselves
  27. Destroy(gameObject);
  28. // Create the explosion
  29. if (explosion)
  30. Instantiate (explosion, transform.position, transform.rotation);  
  31. // If we have a dead barrel then replace ourselves with it!
  32. if (deadReplacement)
  33. {
  34. var dead : Rigidbody = Instantiate(deadReplacement, transform.position,
  35. transform.rotation);
  36. // For better effect we assign the same velocity to the exploded barrel
  37. dead.rigidbody.velocity = rigidbody.velocity;
  38. dead.angularVelocity = rigidbody.angularVelocity;
  39. }
  40. // If there is a particle emitter stop emitting and detach so it doesnt get destroyed
  41. // right away
  42. var emitter : ParticleEmitter = GetComponentInChildren(ParticleEmitter);
  43. if (emitter)
  44. {
  45. emitter.emit = false;
  46. emitter.transform.parent = null;
  47. }
  48. }
  49. // We require the barrel to be a rigidbody, so that it can do nice physics
  50. @script RequireComponent (Rigidbody)