So I have spent a couple days thinking about the best way to express overlapping events in as simple manner as possible. The example I was given was a door mod, which would then in turn be modified by two additional mods, one that allowed doors to be locked, and other that played a sound when the door was opened and closed.
So here's what I came up with. These are three separate mods, the locking and sound mods depend on the door mod, but can be added individually or together.
Door Mod
This is the code to add the door and the basic door behavior to the game.
Define a door. I'm sure there is more information about doors, like crafting textures, etc., but I'm omitting that for simplicity. I'm also assuming that the meshes for the door are in the same directory or zip file as the javascript for the door mod.
Add an "interact" handler. This is how the door responds when the player (or npc) attempts to interact with it. Note that the open() and close() methods do not exist on the door object, so the engine will attempt to satisfy those function calls with an event.
Add an open handler. This event is called when an open() method is called on any door object.
Add an close handler. This event is called when a close() method is called on any door object.
Door Sound Mod
This is the code to play a sound on door opening and closing.
Play a sound on door open event
Play a sound on door close event
Locking Mod
This is the code that allows doors to be locked
If the door's "islocked" property is true, return false from the event handler to cancel any events that were generated as a result of this event. In particular, any queued open or close events would be cancelled.
Make sure any newly placed doors are in the unlocked state
So here's what I came up with. These are three separate mods, the locking and sound mods depend on the door mod, but can be added individually or together.
Door Mod
This is the code to add the door and the basic door behavior to the game.
Define a door. I'm sure there is more information about doors, like crafting textures, etc., but I'm omitting that for simplicity. I'm also assuming that the meshes for the door are in the same directory or zip file as the javascript for the door mod.
Code:
var door = game.addBlock({
"name": "door",
"meshes": {
"opened": "door_open.mesh",
"opened": "door_closed.mesh",
}
});
Code:
door.addEventListener("interact", function() {
if (this.isopen === true) {
this.open();
} else {
this.close();
}
});
Code:
door.addEventListener("open", function() {
this.isopen = false;
this.setMesh("opened");
});
Code:
door.addEventListener("close", function() {
this.isopen = true;
this.setMesh("closed");
});
This is the code to play a sound on door opening and closing.
Play a sound on door open event
Code:
door.addEventListener("open", function() {
playsound("door_open.wav")
});
Code:
door.addEventListener("close", function() {
playsound("door_close.wav")
});
This is the code that allows doors to be locked
If the door's "islocked" property is true, return false from the event handler to cancel any events that were generated as a result of this event. In particular, any queued open or close events would be cancelled.
Code:
door.addEventListener("interact", function() {
if (this.islocked) {
return false;
}
});
Code:
door.addEventListener("placed", function() {
this.islocked = false;
});