leafhopper.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. module.exports = class Leafhopper {
  2. constructor() {
  3. this.id = 0;
  4. this.name = "a leafhopper";
  5. this.description = "A tiny insect that resembles a green leaf. It's difficult to hold on to it without {hint}squishing{/hint} it.";
  6. this.type = "item";
  7. this.keywords = ["insect", "grasshopper", "leafhopper", "bug"];
  8. this.resetDelay = 30000;
  9. this.itemExpires = 0;
  10. this.filename = "leafhopper.js";
  11. this.heldBy = -1;
  12. this.inRoom = "Void1";
  13. }
  14. mud_reset(time) {
  15. this.itemExpires = time + this.resetDelay;
  16. if(this.heldBy != -1) {
  17. var mobile = world.mud_getMobile(this.heldBy);
  18. if(mobile != null && mobile != {}) {
  19. libs.Output.toMobile(mobile.id, "The grasshopper squirms in your hand and tries to hop away.");
  20. if(parseInt(Math.random() * 4) == 0) {
  21. libs.Output.toMobile(mobile.id, "The grasshopper jumps free!");
  22. libs.Output.toRoom(mobile.roomId, "A tiny bug hops out of " + mobile.name + "'s hands!", mobile);
  23. this.heldBy = -1;
  24. this.inRoom = mobile.roomId;
  25. world.mud_removeItemFromMobile(mobile.id, this.id);
  26. world.mud_addItemToRoom(mobile.roomId, this.id);
  27. }
  28. }
  29. } else {
  30. var room = world.mud_getRoom(this.inRoom);
  31. if(room != null && room != {}) {
  32. libs.Output.toRoom(room.id, "A tiny leafhopper jumps around.");
  33. if(parseInt(Math.random() * 4) == 0 && room.mobiles.length > 0) {
  34. console.log(room.mobiles);
  35. var randomMobileId = parseInt(Math.random() * room.mobiles.length);
  36. console.log("jumping to mobile: " + randomMobileId);
  37. var mobile = world.mud_getMobile(randomMobileId);
  38. libs.Output.toMobile(mobile.id, "A tiny grasshopper jumps onto you.");
  39. libs.Output.toRoom(mobile.roomId, "A tiny bug hops into " + mobile.name + "'s hands!", mobile);
  40. world.mud_addItemToMobile(mobile.id, this.id);
  41. world.mud_removeItemFromRoom(room.id, this.id);
  42. this.heldBy = mobile.id;
  43. this.inRoom = 0;
  44. }
  45. }
  46. }
  47. }
  48. mud_tick(time) {
  49. if(time >= this.itemExpires) {
  50. this.mud_reset(time);
  51. }
  52. }
  53. mud_getName() {
  54. return "{item}" + this.name + "{/item}";
  55. }
  56. mud_getDescription() {
  57. return this.description;
  58. }
  59. drop(room, mobile, input) {
  60. if(this.keywords.indexOf(input.join(" ")) != -1) {
  61. this.inRoom = mobile.roomId;
  62. this.heldBy = -1;
  63. return false;
  64. }
  65. }
  66. squish(room, mobile, input) {
  67. var inventoryItem = libs.Utilities.getSpecific(input[0], mobile.items, session.items);
  68. if(inventoryItem != null) {
  69. if(this != inventoryItem) {
  70. return false;
  71. }
  72. libs.Output.toMobile(mobile.id, "You slap your hands together and squish the tiny leafhopper.");
  73. libs.Output.toRoom(room.id, mobile.name + " slaps their hands together and squishes a bug.", mobile);
  74. if(this.heldBy != -1) {
  75. world.mud_removeItemFromMobile(mobile.id, this.id);
  76. } else if (this.inRoom != -1) {
  77. world.mud_removeItemFromRoom(room.id, this.id);
  78. }
  79. world.mud_destroyItem(this.id);
  80. return true;
  81. }
  82. }
  83. mud_saveItem() {
  84. var dataToSave = {};
  85. dataToSave.heldBy = this.heldBy;
  86. return dataToSave;
  87. }
  88. mud_loadItem(itemData) {
  89. for(var dataIndex in itemData) {
  90. if(this.hasOwnProperty(dataIndex)) {
  91. this[dataIndex] = itemData[dataIndex];
  92. }
  93. }
  94. }
  95. }