vehicle.tscript 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. // Parenting is in place for WheeledVehicleData to VehicleData. This should
  23. // make it easier for people to simply drop in new (generic) vehicles. All that
  24. // the user needs to create is a set of datablocks for the new wheeled vehicle
  25. // to use. This means that no (or little) scripting should be necessary.
  26. // Special, or unique vehicles however will still require some scripting. They
  27. // may need to override the onAdd() function in order to mount weapons,
  28. // differing tires/springs, etc., almost everything else is taken care of in the
  29. // WheeledVehicleData and VehicleData methods. This helps us by not having to
  30. // duplicate the same code for every new vehicle.
  31. // In theory this would work for HoverVehicles and FlyingVehicles also, but
  32. // hasn't been tested or fully implemented for those classes -- yet.
  33. function VehicleData::onAdd(%this, %obj)
  34. {
  35. %obj.setRechargeRate(%this.rechargeRate);
  36. %obj.setEnergyLevel(%this.MaxEnergy);
  37. %obj.setRepairRate(0);
  38. if (%obj.mountable || %obj.mountable $= "")
  39. %this.isMountable(%obj, true);
  40. else
  41. %this.isMountable(%obj, false);
  42. if (%this.nameTag !$= "")
  43. %obj.setShapeName(%this.nameTag);
  44. }
  45. function VehicleData::onDestroyed(%this, %obj, %state)
  46. {
  47. //echo("\c4VehicleData::onRemove("@ %this.getName() @", "@ %obj.getClassName() @")");
  48. // if there are passengers/driver, kick them out
  49. if (!%this.killPassengers)
  50. {
  51. for(%i = 0; %i < %obj.getMountedObjectCount(); %i++)
  52. {
  53. if (%obj.getMountedObject(%i))
  54. {
  55. %passenger = %obj.getMountedObject(%i);
  56. if (%passenger.isMemberOfClass("Player"))
  57. %passenger.getDataBlock().doDismount(%passenger, true);
  58. }
  59. }
  60. }
  61. Parent::onDestroyed(%this, %obj, %state);
  62. }
  63. // ----------------------------------------------------------------------------
  64. // Vehicle player mounting and dismounting
  65. // ----------------------------------------------------------------------------
  66. function VehicleData::isMountable(%this, %obj, %val)
  67. {
  68. %obj.mountable = %val;
  69. }
  70. function VehicleData::mountPlayer(%this, %vehicle, %player)
  71. {
  72. //echo("\c4VehicleData::mountPlayer("@ %this.getName() @", "@ %vehicle @", "@ %player.client.nameBase @")");
  73. if (isObject(%vehicle) && %vehicle.getDamageState() !$= "Destroyed")
  74. {
  75. %player.startFade(1000, 0, true);
  76. %this.schedule(1000, "setMountVehicle", %vehicle, %player);
  77. %player.schedule(1500, "startFade", 1000, 0, false);
  78. }
  79. }
  80. function VehicleData::setMountVehicle(%this, %vehicle, %player)
  81. {
  82. //echo("\c4VehicleData::setMountVehicle("@ %this.getName() @", "@ %vehicle @", "@ %player.client.nameBase @")");
  83. if (isObject(%vehicle) && %vehicle.getDamageState() !$= "Destroyed")
  84. {
  85. %node = %this.findEmptySeat(%vehicle, %player);
  86. if (%node >= 0)
  87. {
  88. //echo("\c4Mount Node: "@ %node);
  89. %vehicle.mountObject(%player, %node);
  90. //%player.playAudio(0, MountVehicleSound);
  91. %player.mVehicle = %vehicle;
  92. }
  93. }
  94. }
  95. function VehicleData::findEmptySeat(%this, %vehicle, %player)
  96. {
  97. //echo("\c4This vehicle has "@ %this.numMountPoints @" mount points.");
  98. for (%i = 0; %i < %this.numMountPoints; %i++)
  99. {
  100. %node = %vehicle.getMountNodeObject(%i);
  101. if (%node == 0)
  102. return %i;
  103. }
  104. return -1;
  105. }
  106. function VehicleData::switchSeats(%this, %vehicle, %player)
  107. {
  108. for (%i = 0; %i < %this.numMountPoints; %i++)
  109. {
  110. %node = %vehicle.getMountNodeObject(%i);
  111. if (%node == %player || %node > 0)
  112. continue;
  113. if (%node == 0)
  114. return %i;
  115. }
  116. return -1;
  117. }