TakesDamageBehavior.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //-----------------------------------------------------------------------------
  2. // Torque
  3. // Copyright GarageGames, LLC 2013
  4. //-----------------------------------------------------------------------------
  5. function LeapToy::createTakesDamageBehavior( %this )
  6. {
  7. if (!isObject(TakesDamageBehavior))
  8. {
  9. %this.TakesDamageBehavior = new BehaviorTemplate(TakesDamageBehavior);
  10. %this.TakesDamageBehavior.friendlyName = "Takes Damage Advanced";
  11. %this.TakesDamageBehavior.behaviorType = "Game";
  12. %this.TakesDamageBehavior.description = "Set the object to take damage from DealsDamage objects that collide with it";
  13. %this.TakesDamageBehavior.addBehaviorField(health, "The amount of health the object has", int, 10);
  14. %this.TakesDamageBehavior.addBehaviorField(lives, "The number of times the object can lose all its health", int, 3);
  15. %this.TakesDamageBehavior.addBehaviorField(respawnTime, "The time between death and respawn (seconds)", float, 2.0);
  16. %this.TakesDamageBehavior.addBehaviorField(invincibleTime, "The time after spawning before damage is applied (seconds)", float, 1.0);
  17. %this.TakesDamageBehavior.addBehaviorField(respawnEffect, "The particle effect to play on spawn", asset, "", ParticleAsset);
  18. %this.TakesDamageBehavior.addBehaviorField(explodeEffect, "The particle effect to play on death", asset, "", ParticleAsset);
  19. %this.add(%this.TakesDamageBehavior);
  20. }
  21. }
  22. //-----------------------------------------------------------------------------
  23. function TakesDamageBehavior::initialize(%this, %health, %lives, %respawnTime, %invincibleTime, %respawnEffect, %explodeEffect, %startFrame)
  24. {
  25. %this.health = %health;
  26. %this.lives = %lives;
  27. %this.respawnTime = %respawnTime;
  28. %this.invincibleTime = %invincibleTime;
  29. %this.respawnEffect = %respawnEffect;
  30. %this.explodeEffect = %explodeEffect;
  31. %this.startFrame = %startFrame;
  32. %this.startHealth = %this.health;
  33. }
  34. //-----------------------------------------------------------------------------
  35. function TakesDamageBehavior::onBehaviorAdd( %this )
  36. {
  37. %this.startHealth = %this.health;
  38. %this.owner.collisionCallback = true;
  39. %this.spawn();
  40. }
  41. //-----------------------------------------------------------------------------
  42. function TakesDamageBehavior::takeDamage( %this, %amount, %assailant )
  43. {
  44. if (%this.invincible)
  45. return;
  46. %this.health -= %amount;
  47. if (%this.health <= 0)
  48. {
  49. // Explode and kill the object
  50. %this.explode();
  51. %this.kill();
  52. return;
  53. }
  54. }
  55. //-----------------------------------------------------------------------------
  56. function TakesDamageBehavior::kill( %this )
  57. {
  58. %this.lives--;
  59. if (%this.lives <= 0)
  60. {
  61. %this.owner.safeDelete();
  62. return;
  63. }
  64. %this.invincible = true;
  65. %this.owner.visible = false;
  66. %this.owner.setCollisionSuppress(true);
  67. %this.owner.collisionActiveReceive = false;
  68. %this.schedule(%this.respawnTime * 1000, "spawn");
  69. }
  70. //-----------------------------------------------------------------------------
  71. function TakesDamageBehavior::setVincible(%this)
  72. {
  73. %this.invincible = false;
  74. }
  75. //-----------------------------------------------------------------------------
  76. function TakesDamageBehavior::spawn( %this )
  77. {
  78. %this.owner.collisionActiveReceive = true;
  79. %this.schedule(%this.invincibleTime * 1000, "setVincible");
  80. %this.health = %this.startHealth;
  81. %this.owner.visible = true;
  82. %this.owner.setCollisionSuppress(false);
  83. %this.owner.setImageFrame(%this.startFrame);
  84. if (%this.respawnEffect !$= "")
  85. {
  86. %particlePlayer = new ParticlePlayer();
  87. %particlePlayer.BodyType = static;
  88. %particlePlayer.Size = "10";
  89. %particlePlayer.SetPosition( %this.owner.getPosition() );
  90. %particlePlayer.SceneLayer = %this.owner.getSceneLayer();
  91. %particlePlayer.ParticleInterpolation = true;
  92. %particlePlayer.Particle = %this.respawnEffect;
  93. SandboxScene.add(%particlePlayer);
  94. }
  95. }
  96. //-----------------------------------------------------------------------------
  97. function TakesDamageBehavior::explode( %this )
  98. {
  99. if (%this.explodeEffect !$= "")
  100. {
  101. %particlePlayer = new ParticlePlayer();
  102. %particlePlayer.BodyType = static;
  103. %particlePlayer.SetPosition( %this.owner.getPosition() );
  104. %particlePlayer.SceneLayer = %this.owner.getSceneLayer();
  105. %particlePlayer.ParticleInterpolation = true;
  106. %particlePlayer.Particle = %this.explodeEffect;
  107. %particlePlayer.SizeScale = 0.8;
  108. SandboxScene.add(%particlePlayer);
  109. }
  110. }