item.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. // These scripts make use of dynamic attribute values on Item datablocks,
  23. // these are as follows:
  24. //
  25. // maxInventory Max inventory per object (100 bullets per box, etc.)
  26. // pickupName Name to display when client pickups item
  27. //
  28. // Item objects can have:
  29. //
  30. // count The # of inventory items in the object. This
  31. // defaults to maxInventory if not set.
  32. // Respawntime is the amount of time it takes for a static "auto-respawn"
  33. // object, such as an ammo box or weapon, to re-appear after it's been
  34. // picked up. Any item marked as "static" is automaticlly respawned.
  35. $Item::RespawnTime = 30 * 1000;
  36. // Poptime represents how long dynamic items (those that are thrown or
  37. // dropped) will last in the world before being deleted.
  38. $Item::PopTime = 30 * 1000;
  39. //-----------------------------------------------------------------------------
  40. // ItemData base class methods used by all items
  41. //-----------------------------------------------------------------------------
  42. //-----------------------------------------------------------------------------
  43. function Item::respawn(%this)
  44. {
  45. // This method is used to respawn static ammo and weapon items
  46. // and is usually called when the item is picked up.
  47. // Instant fade...
  48. %this.startFade(0, 0, true);
  49. %this.setHidden(true);
  50. // Shedule a reapearance
  51. %this.schedule($Item::RespawnTime, "setHidden", false);
  52. %this.schedule($Item::RespawnTime + 100, "startFade", 1000, 0, false);
  53. }
  54. function Item::schedulePop(%this)
  55. {
  56. // This method deletes the object after a default duration. Dynamic
  57. // items such as thrown or drop weapons are usually popped to avoid
  58. // world clutter.
  59. %this.schedule($Item::PopTime - 1000, "startFade", 1000, 0, true);
  60. %this.schedule($Item::PopTime, "delete");
  61. }
  62. //-----------------------------------------------------------------------------
  63. // Callbacks to hook items into the inventory system
  64. function ItemData::onThrow(%this, %user, %amount)
  65. {
  66. // Remove the object from the inventory
  67. if (%amount $= "")
  68. %amount = 1;
  69. if (%this.maxInventory !$= "")
  70. if (%amount > %this.maxInventory)
  71. %amount = %this.maxInventory;
  72. if (!%amount)
  73. return 0;
  74. %user.decInventory(%this,%amount);
  75. // Construct the actual object in the world, and add it to
  76. // the mission group so it's cleaned up when the mission is
  77. // done. The object is given a random z rotation.
  78. %obj = new Item()
  79. {
  80. datablock = %this;
  81. rotation = "0 0 1 "@ (getRandom() * 360);
  82. count = %amount;
  83. };
  84. MissionGroup.add(%obj);
  85. %obj.schedulePop();
  86. return %obj;
  87. }
  88. function ItemData::onPickup(%this, %obj, %user, %amount)
  89. {
  90. // Add it to the inventory, this currently ignores the request
  91. // amount, you get what you get. If the object doesn't have
  92. // a count or the datablock doesn't have maxIventory set, the
  93. // object cannot be picked up.
  94. // See if the object has a count
  95. %count = %obj.count;
  96. if (%count $= "")
  97. {
  98. // No, so check the datablock
  99. %count = %this.count;
  100. if (%count $= "")
  101. {
  102. // No, so attempt to provide the maximum amount
  103. if (%this.maxInventory !$= "")
  104. {
  105. if (!(%count = %this.maxInventory))
  106. return;
  107. }
  108. else
  109. %count = 1;
  110. }
  111. }
  112. %user.incInventory(%this, %count);
  113. // Inform the client what they got.
  114. if (%user.client)
  115. messageClient(%user.client, 'MsgItemPickup', '\c0You picked up %1', %this.pickupName);
  116. // If the item is a static respawn item, then go ahead and
  117. // respawn it, otherwise remove it from the world.
  118. // Anything not taken up by inventory is lost.
  119. if (%obj.isStatic())
  120. %obj.respawn();
  121. else
  122. %obj.delete();
  123. return true;
  124. }
  125. function ItemData::createItem(%data)
  126. {
  127. %obj = new Item()
  128. {
  129. dataBlock = %data;
  130. static = true;
  131. rotate = true;
  132. };
  133. return %obj;
  134. }