UNPKG

sam-ecs

Version:

A specialized entity component system

144 lines (118 loc) 4.62 kB
'use strict'; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } //EventManager.js// /** * @description - Manages the events and event listeners * and event emitters * @author - Sam Faulkner */ var EventEmitter = require('events'); var EventManager = function () { function EventManager(actionManager) { _classCallCheck(this, EventManager); this._actionManager = actionManager; this._emitter = new EventEmitter(); this._queue = {}; this._currentTick = 0; } /** * @description - Adds an event listener, * @param {String} eventType - the type of event the listener will listen * for * @param {Function} listener - the function that will be called when an * event of the right type gets emitted */ _createClass(EventManager, [{ key: 'addListener', value: function addListener(eventType, listener) { this._emitter.addListener(eventType, listener); } /** * @description - Returns the listeners for the given event type * @param {String} eventType - the type of event * @returns {Array} - the listener functions for the array */ }, { key: 'getListeners', value: function getListeners(eventType) { return this._emitter.listeners(eventType); } /** * @description - Removes an event listener * @param {String} eventType - the type of event that the listener will be removed * from * @param {Function} listener - the function that will be removed from the * given event type */ }, { key: 'removeListener', value: function removeListener(eventType, listener) { this._emitter.removeListener(eventType, listener); } /** * @description - Emits an event with the given * type and arguments * @param {String} eventType - the type of event that will be emitted * @param {Object} args - the args object that will be passed to the * event listeners */ }, { key: 'emit', value: function emit(eventType, args) { var tick = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : -1; if (tick === -1) { tick = this._currentTick; // + 2; } // this._emitter.emit(eventType, args, this._actionManager); if (!this._queue[tick]) { this._queue[tick] = new Array(); } this._queue[tick].push({ 'eventType': eventType, 'args': args }); } /** * @description - Called every frame in order to emit our queued events * @param {int} currentTick - the current tick of the engine */ }, { key: 'update', value: function update(currentTick) { this._currentTick = currentTick; if (this._currentTick in this._queue) { var eventQueue = this._queue[this._currentTick]; var _iteratorNormalCompletion = true; var _didIteratorError = false; var _iteratorError = undefined; try { for (var _iterator = eventQueue[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { var eventObject = _step.value; this._emitter.emit(eventObject.eventType, eventObject.args, this._actionManager); } } catch (err) { _didIteratorError = true; _iteratorError = err; } finally { try { if (!_iteratorNormalCompletion && _iterator.return) { _iterator.return(); } } finally { if (_didIteratorError) { throw _iteratorError; } } } } /* Because this should get called every tick, it shouldn't be necessary to * delete everything <= the current tick. Just the = the current tick * should be enough */ delete this._queue[this._currentTick]; } }]); return EventManager; }(); module.exports = EventManager;