shapeBase.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. // This file contains ShapeBase methods used by all the derived classes
  23. //-----------------------------------------------------------------------------
  24. // ShapeBase object
  25. //-----------------------------------------------------------------------------
  26. // A raycast helper function to keep from having to duplicate code everytime
  27. // that a raycast is needed.
  28. // %this = the object doing the cast, usually a player
  29. // %range = range to search
  30. // %mask = what to look for
  31. function ShapeBase::doRaycast(%this, %range, %mask)
  32. {
  33. // get the eye vector and eye transform of the player
  34. %eyeVec = %this.getEyeVector();
  35. %eyeTrans = %this.getEyeTransform();
  36. // extract the position of the player's camera from the eye transform (first 3 words)
  37. %eyePos = getWord(%eyeTrans, 0) SPC getWord(%eyeTrans, 1) SPC getWord(%eyeTrans, 2);
  38. // normalize the eye vector
  39. %nEyeVec = VectorNormalize(%eyeVec);
  40. // scale (lengthen) the normalized eye vector according to the search range
  41. %scEyeVec = VectorScale(%nEyeVec, %range);
  42. // add the scaled & normalized eye vector to the position of the camera
  43. %eyeEnd = VectorAdd(%eyePos, %scEyeVec);
  44. // see if anything gets hit
  45. %searchResult = containerRayCast(%eyePos, %eyeEnd, %mask, %this);
  46. return %searchResult;
  47. }
  48. //-----------------------------------------------------------------------------
  49. function ShapeBase::damage(%this, %sourceObject, %position, %damage, %damageType)
  50. {
  51. // All damage applied by one object to another should go through this method.
  52. // This function is provided to allow objects some chance of overriding or
  53. // processing damage values and types. As opposed to having weapons call
  54. // ShapeBase::applyDamage directly. Damage is redirected to the datablock,
  55. // this is standard procedure for many built in callbacks.
  56. if (isObject(%this))
  57. %this.getDataBlock().damage(%this, %sourceObject, %position, %damage, %damageType);
  58. }
  59. //-----------------------------------------------------------------------------
  60. function ShapeBase::setDamageDt(%this, %damageAmount, %damageType)
  61. {
  62. // This function is used to apply damage over time. The damage is applied
  63. // at a fixed rate (50 ms). Damage could be applied over time using the
  64. // built in ShapBase C++ repair functions (using a neg. repair), but this
  65. // has the advantage of going through the normal script channels.
  66. if (%this.getState() !$= "Dead")
  67. {
  68. %this.damage(0, "0 0 0", %damageAmount, %damageType);
  69. %this.damageSchedule = %this.schedule(50, "setDamageDt", %damageAmount, %damageType);
  70. }
  71. else
  72. %this.damageSchedule = "";
  73. }
  74. function ShapeBase::clearDamageDt(%this)
  75. {
  76. if (%this.damageSchedule !$= "")
  77. {
  78. cancel(%this.damageSchedule);
  79. %this.damageSchedule = "";
  80. }
  81. }
  82. function GameBase::damage(%this, %sourceObject, %position, %damage, %damageType)
  83. {
  84. // All damage applied by one object to another should go through this method.
  85. // This function is provided to allow objects some chance of overriding or
  86. // processing damage values and types. As opposed to having weapons call
  87. // ShapeBase::applyDamage directly. Damage is redirected to the datablock,
  88. // this is standard procedure for many built in callbacks.
  89. %datablock = %this.getDataBlock();
  90. if ( isObject( %datablock ) )
  91. %datablock.damage(%this, %sourceObject, %position, %damage, %damageType);
  92. }
  93. //-----------------------------------------------------------------------------
  94. // ShapeBase datablock
  95. //-----------------------------------------------------------------------------
  96. function ShapeBaseData::damage(%this, %obj, %source, %position, %amount, %damageType)
  97. {
  98. // Ignore damage by default. This empty method is here to
  99. // avoid console warnings.
  100. }