DealsDamageBehavior.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //-----------------------------------------------------------------------------
  2. // Torque
  3. // Copyright GarageGames, LLC 2013
  4. //-----------------------------------------------------------------------------
  5. function LeapToy::createDealsDamageBehavior( %this )
  6. {
  7. if (!isObject(DealsDamageBehavior))
  8. {
  9. %this.DealsDamageBehavior = new BehaviorTemplate(DealsDamageBehavior);
  10. %this.DealsDamageBehavior.friendlyName = "Deals Damage Advanced";
  11. %this.DealsDamageBehavior.behaviorType = "Game";
  12. %this.DealsDamageBehavior.description = "Set the object to deal damage to TakesDamage objects it collides with";
  13. %this.DealsDamageBehavior.addBehaviorField(strength, "The amount of damage the object deals", int, 10);
  14. %this.DealsDamageBehavior.addBehaviorField(deleteOnHit, "Delete the object when it collides", bool, 1);
  15. %this.DealsDamageBehavior.addBehaviorField(explodeEffect, "The particle effect to play on collision", asset, "", ParticleAsset);
  16. %this.add(%this.DealsDamageBehavior);
  17. }
  18. }
  19. //-----------------------------------------------------------------------------
  20. function DealsDamageBehavior::initialize(%this, %strength, %deleteOnHit, %explodeEffect)
  21. {
  22. %this.strength = %strength;
  23. %this.deleteOnHit = %deleteOnHit;
  24. %this.explodeEffect = %explodeEffect;
  25. }
  26. //-----------------------------------------------------------------------------
  27. function DealsDamageBehavior::onBehaviorAdd(%this)
  28. {
  29. %this.owner.collisionCallback = true;
  30. %this.owner.collisionActiveSend = true;
  31. }
  32. //-----------------------------------------------------------------------------
  33. function DealsDamageBehavior::dealDamage(%this, %amount, %victim)
  34. {
  35. %takesDamage = %victim.getBehavior("TakesDamageBehavior");
  36. if (!isObject(%takesDamage))
  37. return;
  38. %takesDamage.takeDamage(%amount, %this);
  39. }
  40. //-----------------------------------------------------------------------------
  41. function DealsDamageBehavior::explode(%this, %position)
  42. {
  43. if (%this.explodeEffect !$= "")
  44. {
  45. %particlePlayer = new ParticlePlayer();
  46. %particlePlayer.BodyType = static;
  47. %particlePlayer.SetPosition( %this.owner.getPosition() );
  48. %particlePlayer.SceneLayer = %this.owner.getSceneLayer();
  49. %particlePlayer.ParticleInterpolation = true;
  50. %particlePlayer.Particle = %this.explodeEffect;
  51. SandboxScene.add(%particlePlayer);
  52. }
  53. }
  54. //-----------------------------------------------------------------------------
  55. function DealsDamageBehavior::onCollision(%this, %object, %collisionDetails)
  56. {
  57. %this.dealDamage(%this.strength, %object);
  58. if (%this.deleteOnHit)
  59. {
  60. %this.explode(getWords(%contacts, 0, 1));
  61. %this.owner.safeDelete();
  62. }
  63. }