registry_controller.js

/**
 * @module Controller
 * @fileoverview Contains Controller class.
 */

/**
 * @class
 * Controls moving actors by user input.
 */
class Controller {
    /**
     * Create a new controller.
     * @param {string} id Identifier of controller
     * @param {Object.<string, function>} map Controls mapping
     * @constructor
     */
    constructor(id, map) {
        this.id = id;
        this.map = map;
    }

    /**
     * Call the controller's update function.
     * @param {MovingActor} actor The actor to update
     * @param {number} elapsed Time since last update cycle in seconds
     */
    update(actor, elapsed) {
        if ("update" in this.map) {
            this.map["update"](actor, elapsed);
        } else {
            for (const key in this.map) {
                if ($$.input.keyboard.isDown(key))
                    this.map[key](actor, elapsed);
            }
        }
    }
}

export default Controller;