module.exports = class Leafhopper { constructor() { this.id = 0; this.name = "a leafhopper"; this.description = "A tiny insect that resembles a green leaf. It's difficult to hold on to it without {hint}squishing{/hint} it."; this.type = "item"; this.keywords = ["insect", "grasshopper", "leafhopper", "bug"]; this.resetDelay = 30000; this.itemExpires = 0; this.filename = "leafhopper.js"; this.heldBy = -1; this.inRoom = "Void1"; } mud_reset(time) { this.itemExpires = time + this.resetDelay; if(this.heldBy != -1) { var mobile = world.mud_getMobile(this.heldBy); if(mobile != null && mobile != {}) { libs.Output.toMobile(mobile.id, "The grasshopper squirms in your hand and tries to hop away."); if(parseInt(Math.random() * 4) == 0) { libs.Output.toMobile(mobile.id, "The grasshopper jumps free!"); libs.Output.toRoom(mobile.roomId, "A tiny bug hops out of " + mobile.name + "'s hands!", mobile); this.heldBy = -1; this.inRoom = mobile.roomId; world.mud_removeItemFromMobile(mobile.id, this.id); world.mud_addItemToRoom(mobile.roomId, this.id); } } } else { var room = world.mud_getRoom(this.inRoom); if(room != null && room != {}) { libs.Output.toRoom(room.id, "A tiny leafhopper jumps around."); if(parseInt(Math.random() * 4) == 0 && room.mobiles.length > 0) { console.log(room.mobiles); var randomMobileId = parseInt(Math.random() * room.mobiles.length); console.log("jumping to mobile: " + randomMobileId); var mobile = world.mud_getMobile(randomMobileId); libs.Output.toMobile(mobile.id, "A tiny grasshopper jumps onto you."); libs.Output.toRoom(mobile.roomId, "A tiny bug hops into " + mobile.name + "'s hands!", mobile); world.mud_addItemToMobile(mobile.id, this.id); world.mud_removeItemFromRoom(room.id, this.id); this.heldBy = mobile.id; this.inRoom = 0; } } } } mud_tick(time) { if(time >= this.itemExpires) { this.mud_reset(time); } } mud_getName() { return "{item}" + this.name + "{/item}"; } mud_getDescription() { return this.description; } drop(room, mobile, input) { if(this.keywords.indexOf(input.join(" ")) != -1) { this.inRoom = mobile.roomId; this.heldBy = -1; return false; } } squish(room, mobile, input) { var inventoryItem = libs.Utilities.getSpecific(input[0], mobile.items, session.items); if(inventoryItem != null) { if(this != inventoryItem) { return false; } libs.Output.toMobile(mobile.id, "You slap your hands together and squish the tiny leafhopper."); libs.Output.toRoom(room.id, mobile.name + " slaps their hands together and squishes a bug.", mobile); if(this.heldBy != -1) { world.mud_removeItemFromMobile(mobile.id, this.id); } else if (this.inRoom != -1) { world.mud_removeItemFromRoom(room.id, this.id); } world.mud_destroyItem(this.id); return true; } } mud_saveItem() { var dataToSave = {}; dataToSave.heldBy = this.heldBy; return dataToSave; } mud_loadItem(itemData) { for(var dataIndex in itemData) { if(this.hasOwnProperty(dataIndex)) { this[dataIndex] = itemData[dataIndex]; } } } }