home *** CD-ROM | disk | FTP | other *** search
/ 3D Game Programming All in One / 3D Game Programming All in One Disc.iso / 3D2E / demo / server / scripts / weapon.cs < prev   
Encoding:
Text File  |  2005-11-23  |  4.0 KB  |  119 lines

  1. //-----------------------------------------------------------------------------
  2. // Torque Game Engine 
  3. // Copyright (C) GarageGames.com, Inc.
  4. //-----------------------------------------------------------------------------
  5.  
  6. // This file contains Weapon and Ammo Class/"namespace" helper methods
  7. // as well as hooks into the inventory system. These functions are not
  8. // attached to a specific C++ class or datablock, but define a set of
  9. // methods which are part of dynamic namespaces "class". The Items
  10. // include these namespaces into their scope using the  ItemData and
  11. // ItemImageData "className" variable.
  12.  
  13. // All ShapeBase images are mounted into one of 8 slots on a shape.
  14. // This weapon system assumes all primary weapons are mounted into
  15. // this specified slot:
  16. $WeaponSlot = 0;
  17.  
  18.  
  19. //-----------------------------------------------------------------------------
  20. // Audio profiles
  21.  
  22. datablock AudioProfile(WeaponUseSound)
  23. {
  24.    filename = "~/data/sound/weapon_switch.ogg";
  25.    description = AudioClose3d;
  26.     preload = true;
  27. };
  28.  
  29. datablock AudioProfile(WeaponPickupSound)
  30. {
  31.    filename = "~/data/sound/weapon_pickup.ogg";
  32.    description = AudioClose3d;
  33.     preload = true;
  34. };
  35.  
  36. datablock AudioProfile(AmmoPickupSound)
  37. {
  38.    filename = "~/data/sound/ammo_pickup.ogg";
  39.    description = AudioClose3d;
  40.     preload = true;
  41. };
  42.  
  43.  
  44. //-----------------------------------------------------------------------------
  45. // Weapon Class 
  46. //-----------------------------------------------------------------------------
  47.  
  48. function Weapon::onUse(%data,%obj)
  49. {
  50.    // Default behavoir for all weapons is to mount it into the
  51.    // this object's weapon slot, which is currently assumed
  52.    // to be slot 0
  53.    if (%obj.getMountedImage($WeaponSlot) != %data.image.getId()) {
  54.       serverPlay3D(WeaponUseSound,%obj.getTransform());
  55.       %obj.mountImage(%data.image, $WeaponSlot);
  56.       if (%obj.client)
  57.          messageClient(%obj.client, 'MsgWeaponUsed', '\c0Weapon selected');
  58.    }
  59. }
  60.  
  61. function Weapon::onPickup(%this, %obj, %shape, %amount)
  62. {
  63.    // The parent Item method performs the actual pickup.
  64.    // For player's we automatically use the weapon if the
  65.    // player does not already have one in hand.
  66.    if (Parent::onPickup(%this, %obj, %shape, %amount)) {
  67.       serverPlay3D(WeaponPickupSound,%obj.getTransform());
  68.       if (%shape.getClassName() $= "Player" && 
  69.             %shape.getMountedImage($WeaponSlot) == 0)  {
  70.          %shape.use(%this);
  71.       }
  72.    }
  73. }
  74.  
  75. function Weapon::onInventory(%this,%obj,%amount)
  76. {
  77.    // Weapon inventory has changed, make sure there are no weapons
  78.    // of this type mounted if there are none left in inventory.
  79.    if (!%amount && (%slot = %obj.getMountSlot(%this.image)) != -1)
  80.       %obj.unmountImage(%slot);
  81. }   
  82.  
  83.  
  84. //-----------------------------------------------------------------------------
  85. // Weapon Image Class
  86. //-----------------------------------------------------------------------------
  87.  
  88. function WeaponImage::onMount(%this,%obj,%slot)
  89. {
  90.    // Images assume a false ammo state on load.  We need to
  91.    // set the state according to the current inventory.
  92.    if (%obj.getInventory(%this.ammo))
  93.       %obj.setImageAmmo(%slot,true);
  94. }
  95.  
  96.  
  97. //-----------------------------------------------------------------------------
  98. // Ammmo Class
  99. //-----------------------------------------------------------------------------
  100.  
  101. function Ammo::onPickup(%this, %obj, %shape, %amount)
  102. {
  103.    // The parent Item method performs the actual pickup.
  104.    if (Parent::onPickup(%this, %obj, %shape, %amount)) {
  105.       serverPlay3D(AmmoPickupSound,%obj.getTransform());
  106.    }
  107. }
  108.  
  109. function Ammo::onInventory(%this,%obj,%amount)
  110. {
  111.    // The ammo inventory state has changed, we need to update any
  112.    // mounted images using this ammo to reflect the new state.
  113.    for (%i = 0; %i < 8; %i++) {
  114.       if ((%image = %obj.getMountedImage(%i)) > 0)
  115.          if (isObject(%image.ammo) && %image.ammo.getId() == %this.getId())
  116.             %obj.setImageAmmo(%i,%amount != 0);
  117.    }
  118. }
  119.