radiusDamage.cs 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. // Support function which applies damage to objects within the radius of
  23. // some effect, usually an explosion. This function will also optionally
  24. // apply an impulse to each object.
  25. function radiusDamage(%sourceObject, %position, %radius, %damage, %damageType, %impulse)
  26. {
  27. // Use the container system to iterate through all the objects
  28. // within our explosion radius. We'll apply damage to all ShapeBase
  29. // objects.
  30. InitContainerRadiusSearch(%position, %radius, $TypeMasks::ShapeBaseObjectType | $TypeMasks::DynamicShapeObjectType);
  31. %halfRadius = %radius / 2;
  32. while ((%targetObject = containerSearchNext()) != 0)
  33. {
  34. // Calculate how much exposure the current object has to
  35. // the explosive force. The object types listed are objects
  36. // that will block an explosion. If the object is totally blocked,
  37. // then no damage is applied.
  38. %coverage = calcExplosionCoverage(%position, %targetObject,
  39. $TypeMasks::InteriorObjectType |
  40. $TypeMasks::TerrainObjectType |
  41. $TypeMasks::ForceFieldObjectType |
  42. $TypeMasks::StaticShapeObjectType |
  43. $TypeMasks::VehicleObjectType);
  44. if (%coverage == 0)
  45. continue;
  46. // Radius distance subtracts out the length of smallest bounding
  47. // box axis to return an appriximate distance to the edge of the
  48. // object's bounds, as opposed to the distance to it's center.
  49. %dist = containerSearchCurrRadiusDist();
  50. // Calculate a distance scale for the damage and the impulse.
  51. // Full damage is applied to anything less than half the radius away,
  52. // linear scale from there.
  53. %distScale = (%dist < %halfRadius)? 1.0 : 1.0 - ((%dist - %halfRadius) / %halfRadius);
  54. %distScale = mClamp(%distScale,0.0,1.0);
  55. // Apply the damage
  56. %targetObject.damage(%sourceObject, %position, %damage * %coverage * %distScale, %damageType);
  57. // Apply the impulse
  58. if (%impulse)
  59. {
  60. %impulseVec = VectorSub(%targetObject.getWorldBoxCenter(), %position);
  61. %impulseVec = VectorNormalize(%impulseVec);
  62. %impulseVec = VectorScale(%impulseVec, %impulse * %distScale);
  63. %targetObject.applyImpulse(%position, %impulseVec);
  64. }
  65. }
  66. }