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 / inventory.cs < prev    next >
Encoding:
Text File  |  2005-11-23  |  7.8 KB  |  257 lines

  1. //-----------------------------------------------------------------------------
  2. // Torque Game Engine 
  3. // Copyright (C) GarageGames.com, Inc.
  4. //-----------------------------------------------------------------------------
  5.  
  6. // This inventory system is totally scripted, no C++ code involved.
  7. // It uses object datablock names to track inventory and is generally
  8. // object type, or class, agnostic.  In other words, it will inventory
  9. // any kind of ShapeBase object, though the throw method does assume
  10. // that the objects are small enough to throw :)
  11. //
  12. // For a ShapeBase object to support inventory, it must have an array
  13. // of inventory max values:
  14. //
  15. //    %this.maxInv[GunAmmo] = 100;
  16. //    %this.maxInv[SpeedGun] = 1;
  17. //
  18. // where the names "SpeedGun" and "GunAmmo" are datablocks.
  19. //
  20. // For objects to be inventoriable, they must provide a set of inventory
  21. // callback methods, mainly:
  22. //
  23. //    onUse
  24. //    onThrow
  25. //    onPickup
  26. //
  27. // Example methods are given further down.  The item.cs file also contains
  28. // example inventory items.
  29.  
  30. //-----------------------------------------------------------------------------
  31.  
  32. //-----------------------------------------------------------------------------
  33. // Inventory server commands
  34. //-----------------------------------------------------------------------------
  35.  
  36. function serverCmdUse(%client,%data)
  37. {
  38.    %client.getControlObject().use(%data);
  39. }
  40.  
  41. //-----------------------------------------------------------------------------
  42. // ShapeBase inventory support
  43. //-----------------------------------------------------------------------------
  44.  
  45. //-----------------------------------------------------------------------------
  46.  
  47. function ShapeBase::use(%this,%data)
  48. {
  49.    // Use an object in the inventory.
  50.    if (%this.getInventory(%data) > 0)
  51.       return %data.onUse(%this);
  52.    return false;
  53. }
  54.  
  55. function ShapeBase::throw(%this,%data,%amount)
  56. {
  57.    // Throw objects from inventory. The onThrow method is
  58.    // responsible for decrementing the inventory.
  59.    if (%this.getInventory(%data) > 0) {
  60.       %obj = %data.onThrow(%this,%amount);
  61.       if (%obj) {
  62.          %this.throwObject(%obj);
  63.          return true;
  64.       }
  65.    }
  66.    return false;
  67. }
  68.  
  69. function ShapeBase::pickup(%this,%obj,%amount)
  70. {
  71.    // This method is called to pickup an object and add it
  72.    // to the inventory. The datablock onPickup method is actually
  73.    // responsible for doing all the work, including incrementing
  74.    // the inventory.
  75.    %data = %obj.getDatablock();
  76.  
  77.    // Try and pickup the max if no value was specified
  78.    if (%amount $= "")
  79.       %amount = %this.maxInventory(%data) - %this.getInventory(%data);
  80.  
  81.    // The datablock does the work...
  82.    if (%amount < 0)
  83.       %amount = 0;
  84.    if (%amount)
  85.       return %data.onPickup(%obj,%this,%amount);
  86.    return false;
  87. }
  88.  
  89.  
  90. //-----------------------------------------------------------------------------
  91.  
  92. function ShapeBase::maxInventory(%this,%data)
  93. {
  94.    // If there is no limit defined, we assume 0
  95.    return %this.getDatablock().maxInv[%data.getName()];
  96. }
  97.  
  98. function ShapeBase::incInventory(%this,%data,%amount)
  99. {
  100.    // Increment the inventory by the given amount.  The return value
  101.    // is the amount actually added, which may be less than the
  102.    // requested amount due to inventory restrictions.
  103.    %max = %this.maxInventory(%data);
  104.    %total = %this.inv[%data.getName()];
  105.    if (%total < %max) {
  106.       if (%total + %amount > %max)
  107.          %amount = %max - %total;
  108.       %this.setInventory(%data,%total + %amount);
  109.       return %amount;
  110.    }
  111.    return 0;
  112. }
  113.  
  114. function ShapeBase::decInventory(%this,%data,%amount)
  115. {
  116.    // Decrement the inventory by the given amount. The return value
  117.    // is the amount actually removed.
  118.    %total = %this.inv[%data.getName()];
  119.    if (%total > 0) {
  120.       if (%total < %amount)
  121.          %amount = %total;
  122.       %this.setInventory(%data,%total - %amount);
  123.       return %amount;
  124.    }
  125.    return 0;
  126. }
  127.  
  128.  
  129. //-----------------------------------------------------------------------------
  130.  
  131. function ShapeBase::getInventory(%this,%data)
  132. {
  133.    // Return the current inventory amount
  134.    return %this.inv[%data.getName()];
  135. }
  136.  
  137. function ShapeBase::setInventory(%this,%data,%value)
  138. {
  139.    // Set the inventory amount for this datablock and invoke
  140.    // inventory callbacks.  All changes to inventory go through this
  141.    // single method.
  142.  
  143.    // Impose inventory limits
  144.    if (%value < 0)
  145.       %value = 0;
  146.    else {
  147.       %max = %this.maxInventory(%data);
  148.       if (%value > %max)
  149.          %value = %max;
  150.    }
  151.  
  152.    // Set the value and invoke object callbacks
  153.    %name = %data.getName();
  154.    if (%this.inv[%name] != %value) 
  155.    {
  156.       %this.inv[%name] = %value;
  157.       %data.onInventory(%this,%value);
  158.       %this.getDataBlock().onInventory(%data,%value);
  159.    }
  160.    return %value;
  161. }
  162.  
  163.  
  164. //-----------------------------------------------------------------------------
  165.  
  166. function ShapeBase::clearInventory(%this)
  167. {
  168.    // To be filled in...
  169. }
  170.  
  171.  
  172. //-----------------------------------------------------------------------------
  173.  
  174. function ShapeBase::throwObject(%this,%obj)
  175. {
  176.    // Throw the given object in the direction the shape is looking.
  177.    // The force value is hardcoded according to the current default
  178.    // object mass and mission gravity (20m/s^2).
  179.    %throwForce = %this.throwForce;
  180.    if (!%throwForce)
  181.       %throwForce = 20;
  182.  
  183.    // Start with the shape's eye vector...
  184.    %eye = %this.getEyeVector();
  185.    %vec = vectorScale(%eye, %throwForce);
  186.  
  187.    // Add a vertical component to give the object a better arc
  188.    %verticalForce = %throwForce / 2;
  189.    %dot = vectorDot("0 0 1",%eye);
  190.    if (%dot < 0)
  191.       %dot = -%dot;
  192.    %vec = vectorAdd(%vec,vectorScale("0 0 " @ %verticalForce,1 - %dot));
  193.  
  194.    // Add the shape's velocity
  195.    %vec = vectorAdd(%vec,%this.getVelocity());
  196.  
  197.    // Set the object's position and initial velocity
  198.    %pos = getBoxCenter(%this.getWorldBox());
  199.    %obj.setTransform(%pos);
  200.    %obj.applyImpulse(%pos,%vec);
  201.  
  202.    // Since the object is thrown from the center of the
  203.    // shape, the object needs to avoid colliding with it's
  204.    // thrower.
  205.    %obj.setCollisionTimeout(%this);
  206. }
  207.  
  208.  
  209. //-----------------------------------------------------------------------------
  210. // Callback hooks invoked by the inventory system
  211. //-----------------------------------------------------------------------------
  212.  
  213. //-----------------------------------------------------------------------------
  214. // ShapeBase object callbacks invoked by the inventory system
  215.  
  216. function ShapeBase::onInventory(%this, %data, %value)
  217. {
  218.    // Invoked on ShapeBase objects whenever their inventory changes
  219.    // for the given datablock.
  220. }
  221.  
  222.  
  223. //-----------------------------------------------------------------------------
  224. // ShapeBase datablock callback invoked by the inventory system.
  225.  
  226. function ShapeBaseData::onUse(%this,%user)
  227. {
  228.    // Invoked when the object uses this datablock, should return
  229.    // true if the item was used.
  230.    return false;
  231. }
  232.  
  233. function ShapeBaseData::onThrow(%this,%user,%amount)
  234. {
  235.    // Invoked when the object is thrown.  This method should
  236.    // construct and return the actual mission object to be
  237.    // physically thrown.  This method is also responsible for
  238.    // decrementing the user's inventory.
  239.    return 0;
  240. }
  241.  
  242. function ShapeBaseData::onPickup(%this,%obj,%user,%amount)
  243. {
  244.    // Invoked when the user attempts to pickup this datablock object.
  245.    // The %amount argument is the space in the user's inventory for
  246.    // this type of datablock.  This method is responsible for
  247.    // incrementing the user's inventory is something is addded.
  248.    // Should return true if something was added to the inventory.
  249.    return false;
  250. }
  251.  
  252. function ShapeBaseData::onInventory(%this,%user,%value)
  253. {
  254.    // Invoked whenever an user's inventory total changes for
  255.    // this datablock.
  256. }
  257.