]>
git.r.bdr.sh - rbdr/sumo/blob - lib/systems/control_mapper.js
1 import { System
} from '@serpentity/serpentity';
3 import ControllableNode
from '../nodes/controllable';
13 * Updates control status based on the controller map
15 * @extends {external:Serpentity.System}
16 * @class ControlMapperSystem
17 * @param {object} config a configuration object to extend.
19 export default class ControlMapperSystem
extends System
{
21 constructor(config
= {}) {
26 * The node collection of controllable entities
28 * @property {external:Serpentity.NodeCollection} controllables
30 * @memberof RenderSystem
32 this.controllables
= null;
34 this._initializeKeyboard();
38 * Initializes system when added. Requests controllable nodes.
41 * @memberof RenderSystem
43 * @param {external:Serpentity.Engine} engine the serpentity engine to
44 * which we are getting added
48 this.controllables
= engine
.getNodes(ControllableNode
);
52 * Clears system resources when removed.
56 * @memberof RenderSystem
60 this.controllables
= null;
64 * Runs on every update of the loop. Maps the actions given the current state of the inputs
68 * @param {Number} currentFrameDuration the duration of the current
70 * @memberof RenderSystem
72 update(currentFrameDuration
) {
74 for (const controllable
of this.controllables
) {
75 for (const map
of controllable
.controlMap
.map
) {
76 if (map
.source
.type
=== 'keyboard') {
77 this._setValue(controllable
.entity
, map
.target
, !!internals
.keyboardState
[map
.source
.index
]);
83 // Listens to keyboard to update internal map
85 _initializeKeyboard() {
87 window
.addEventListener('keydown', (event
) => {
89 internals
.keyboardState
[event
.keyCode
] = true;
92 window
.addEventListener('keyup', (event
) => {
94 internals
.keyboardState
[event
.keyCode
] = false;
98 // Sets the value to a target
100 _setValue(entity
, target
, value
) {
102 const component
= entity
.getComponent(target
.component
);
105 const keyFragments
= target
.property
.split('.');
106 let currentObject
= component
;
107 for (const keyFragment
of keyFragments
.slice(0, keyFragments
.length
- 1)) {
108 currentObject
= currentObject
[keyFragment
] = currentObject
[keyFragment
] || {};
112 const finalValue
= !!target
.value
? target
.value(value
) : value
;
113 const finalProperty
= keyFragments
.pop();
114 currentObject
[finalProperty
] += finalValue
;