vehicle.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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::onRemove(%this, %obj)
  46. {
  47. //echo("\c4VehicleData::onRemove("@ %this.getName() @", "@ %obj.getClassName() @")");
  48. // if there are passengers/driver, kick them out
  49. for(%i = 0; %i < %obj.getDatablock().numMountPoints; %i++)
  50. {
  51. if (%obj.getMountNodeObject(%i))
  52. {
  53. %passenger = %obj.getMountNodeObject(%i);
  54. %passenger.getDataBlock().doDismount(%passenger, true);
  55. }
  56. }
  57. }
  58. // ----------------------------------------------------------------------------
  59. // Vehicle player mounting and dismounting
  60. // ----------------------------------------------------------------------------
  61. function VehicleData::isMountable(%this, %obj, %val)
  62. {
  63. %obj.mountable = %val;
  64. }
  65. function VehicleData::mountPlayer(%this, %vehicle, %player)
  66. {
  67. //echo("\c4VehicleData::mountPlayer("@ %this.getName() @", "@ %vehicle @", "@ %player.client.nameBase @")");
  68. if (isObject(%vehicle) && %vehicle.getDamageState() !$= "Destroyed")
  69. {
  70. %player.startFade(1000, 0, true);
  71. %this.schedule(1000, "setMountVehicle", %vehicle, %player);
  72. %player.schedule(1500, "startFade", 1000, 0, false);
  73. }
  74. }
  75. function VehicleData::setMountVehicle(%this, %vehicle, %player)
  76. {
  77. //echo("\c4VehicleData::setMountVehicle("@ %this.getName() @", "@ %vehicle @", "@ %player.client.nameBase @")");
  78. if (isObject(%vehicle) && %vehicle.getDamageState() !$= "Destroyed")
  79. {
  80. %node = %this.findEmptySeat(%vehicle, %player);
  81. if (%node >= 0)
  82. {
  83. //echo("\c4Mount Node: "@ %node);
  84. %vehicle.mountObject(%player, %node);
  85. //%player.playAudio(0, MountVehicleSound);
  86. %player.mVehicle = %vehicle;
  87. }
  88. }
  89. }
  90. function VehicleData::findEmptySeat(%this, %vehicle, %player)
  91. {
  92. //echo("\c4This vehicle has "@ %this.numMountPoints @" mount points.");
  93. for (%i = 0; %i < %this.numMountPoints; %i++)
  94. {
  95. %node = %vehicle.getMountNodeObject(%i);
  96. if (%node == 0)
  97. return %i;
  98. }
  99. return -1;
  100. }
  101. function VehicleData::switchSeats(%this, %vehicle, %player)
  102. {
  103. for (%i = 0; %i < %this.numMountPoints; %i++)
  104. {
  105. %node = %vehicle.getMountNodeObject(%i);
  106. if (%node == %player || %node > 0)
  107. continue;
  108. if (%node == 0)
  109. return %i;
  110. }
  111. return -1;
  112. }