registry_controller_registry.js
import Controller from "./controller.js";
/**
* @module ControllerRegistry
* @fileoverview Contains ControllerRegistry class.
*/
/**
* @class
* Global registry for keymaps.
*/
class ControllerRegistry {
/**
* Create a new controller registry.
* @constructor
*/
constructor() {
this.controllers = new Map();
this.makeBuiltin();
}
/**
* Add a controller mapping.
* @param {string} id The ID of the controller
* @param {Object.<string, function>} map The keymap of the controller
*/
add(id, map) {
this.controllers.set(id, new Controller(id, map));
}
/**
* Get a controller mapping by specified ID.
* @param {string} id The unique ID of the controller
* @returns {Controller} The controller
*/
get(id) {
return this.controllers.get(id);
}
/**
* Register built-in controllers.
*/
makeBuiltin() {
this.add("wasd", {
'w': (actor) => {
if ("move" in actor)
actor.move.y += -1;
},
'a': (actor) => {
if ("move" in actor)
actor.move.x += -1;
},
's': (actor) => {
if ("move" in actor)
actor.move.y += 1;
},
'd': (actor) => {
if ("move" in actor)
actor.move.x += 1;
}
});
this.add("wasd+", {
'w': (actor) => {
if ("move" in actor)
actor.move.y += -1;
},
'a': (actor) => {
if ("move" in actor)
actor.move.x += -1;
},
's': (actor) => {
if ("move" in actor)
actor.move.y += 1;
},
'd': (actor) => {
if ("move" in actor)
actor.move.x += 1;
},
"shift": (actor) => {
if ("speedMultiplier" in actor)
actor.speedMultiplier = 1.6;
},
"z": (actor) => {
if ("speedMultiplier" in actor)
actor.speedMultiplier = 0.625;
}
});
}
}
export default ControllerRegistry;