// Events.js
(function () {
/*global adventurejs A*/
"use strict";
// https://stackoverflow.com/questions/15308371/custom-events-model-without-using-dom-events-in-javascript
// initEvent deprecated - but not createEvent? - use CustomEvent instead
// https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent
// https://developer.mozilla.org/en-US/docs/Web/Events/Creating_and_triggering_events
/**
* @class adventurejs.Events
* @ajsnavheading FrameworkReference
* @param {Game} game A reference to the game instance.
* @summary Events fire at beginning / end of each turn, and can be exploited.
* @classdesc
* <p>
* We only fire a few events, all at the beginning and end of a turn:
* <code>inputEnter</code>,
* <code>inputParseBegin</code>,
* <code>inputParseComplete</code>, and
* <code>inputQueueNext</code>
* (which fires between queued inputs).
* Authors can hook into these to run custom code.
* For example, you could write a series of random in-game background
* events tied to specific {@link adventurejs.Room|Rooms},
* to print additional text to the {@link adventurejs.Display|Display}
* after parsing has completed.
* </p>
* <h3>Example:</h3>
* <pre class="display"><code class="language-javascript">MyGame.reactor.addEventListener('inputParseComplete', function()
* {
* var random = Math.random() * 100;
* switch (MyGame.world._currentRoom.name)
* {
* case "Frigid Wastes":
* if( random < 10 )
* {
* MyGame.print(
* "You're struck by a flash of blinding light as
* sunlight reflects off the packed ice.", "random"
* );
* }
* break;
* case "Desert Wastes":
* if( random > 5 && random < 10 )
* {
* MyGame.print(
* "Heat haze shimmers off the distant dunes.", "random"
* );
* }
* else if( random > 10 && random < 15 )
* {
* MyGame.print(
* "You feel the sand shift beneath your feet.", "random"
* );
* }
* break;
* }
* });
* </code></pre>
*/
class Events {
constructor(game) {
/**
* A reference back to the main {@link adventurejs.Game|Game} object.
* @var {Object} adventurejs.Events#game
* @default {}
*/
this.game = game;
/**
* Event to be fired on player enters input.
* @var {Event} adventurejs.Events#inputEnter
*/
this.inputEnter = document.createEvent("Event");
this.inputEnter.initEvent("inputEnter", true, true);
/**
* Event to be fired on input parse begin.
* @var {Event} adventurejs.Events#inputParseBegin
*/
this.inputParseBegin = document.createEvent("Event");
this.inputParseBegin.initEvent("inputParseBegin", true, true);
/**
* Event to be fired on input parse complete.
* @var {Event} adventurejs.Events#inputParseComplete
*/
this.inputParseComplete = document.createEvent("Event");
this.inputParseComplete.initEvent("inputParseComplete", true, true);
/**
* Event to be fired on queue next input when parsing a queue.
* @var {Event} adventurejs.Events#inputQueueNext
*/
this.inputQueueNext = document.createEvent("Event");
this.inputQueueNext.initEvent("inputQueueNext", true, true);
// this.playerChangeLocation = document.createEvent('Event');
// this.playerChangeLocation.initEvent("playerChangeLocation", true, true);
this.game.reactor.addEventListener("inputEnter", function (e) {
var msg = "Event > inputEnter";
this.game.log("log", "high", msg, "events");
});
this.game.reactor.addEventListener("inputParseBegin", function (e) {
var msg = "Event > inputParseBegin";
this.game.log("log", "high", msg, "events");
});
this.game.reactor.addEventListener("inputParseComplete", function (e) {
var msg = "Event > inputParseComplete";
this.game.log("log", "high", msg, "events");
e.target.game.updateDisplayVerbs();
e.target.game.updateDisplayInventory();
// e.target.game.updateDisplayRoom();
// e.target.game.updateDisplayCompasses();
e.target.game.printRoomZoneEvents();
e.target.game.callIntervals();
});
this.game.reactor.addEventListener("inputQueueNext", function (e) {
var msg = "Event > inputQueueNext";
this.game.log("log", "high", msg, "events");
});
// this.game.reactor.addEventListener('playerChangeLocation', function(event, other){
// var msg = "Event > playerChangeLocation";
// this.game.log( "log", "high", msg , 'events' );
// });
}
}
adventurejs.Events = Events;
})();