Doors and sounds and locks, oh my!

ironchefpython

Member
Contributor
Architecture
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.
Code:
var door = game.addBlock({
   "name": "door",
   "meshes": {
      "opened": "door_open.mesh",
      "opened": "door_closed.mesh",
   }
});
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.
Code:
door.addEventListener("interact", function() {
   if (this.isopen === true) {
      this.open();
   } else {
      this.close();
   }
});
Add an open handler. This event is called when an open() method is called on any door object.
Code:
door.addEventListener("open", function() {
   this.isopen = false;
   this.setMesh("opened");
});
Add an close handler. This event is called when a close() method is called on any door object.
Code:
door.addEventListener("close", function() {
   this.isopen = true;
   this.setMesh("closed");
});
Door Sound Mod
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")
});
Play a sound on door close event
Code:
door.addEventListener("close", function() {
   playsound("door_close.wav")
});
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.
Code:
door.addEventListener("interact", function() {
   if (this.islocked) {
      return false;
   }
});
Make sure any newly placed doors are in the unlocked state
Code:
door.addEventListener("placed", function() {
   this.islocked = false;
});
 

Cervator

Org Co-Founder & Project Lead
Contributor
Design
Logistics
SpecOps
Good stuff, easy to understand, and a great example as Immortius had working doors via ES in a previous project he did. One of the soonish items we'll have to implement :)

Thanks!
 

ironchefpython

Member
Contributor
Architecture
Cervator said:
Good stuff, easy to understand, and a great example as Immortius had working doors via ES in a previous project he did. One of the soonish items we'll have to implement.
Well, the simplicity is deceiving. In order to be able to write mods in a simple fashion like this, you need a sophisticated message passing based event handling system. All the real work is still done by the engine, and it makes the engine a more complicated conceptual design, but I think it's more than worth it for easy modding.
 
Top