refactor: input greatness
This commit is contained in:
parent
1f642fe8e0
commit
142bf704eb
|
@ -10,14 +10,22 @@ export class ActionRegistry extends decorate(class {}) {
|
|||
|
||||
constructor() {
|
||||
super();
|
||||
this.mapActionToKey = new Map();
|
||||
this.mapKeyToAction = {};
|
||||
this.actionKeys = new Map();
|
||||
this.keyActions = {};
|
||||
this.normalizer = undefined;
|
||||
this._state = I.Map();
|
||||
this._actionIds = {};
|
||||
this._stream = [];
|
||||
this._actionTransformers = {};
|
||||
}
|
||||
|
||||
actionForKey(key) {
|
||||
return this.mapKeyToAction[key];
|
||||
return this.keyActions[key];
|
||||
}
|
||||
|
||||
drain() {
|
||||
const stream = this._stream;
|
||||
this._stream = [];
|
||||
return stream;
|
||||
}
|
||||
|
||||
listen(normalizer) {
|
||||
|
@ -31,37 +39,47 @@ export class ActionRegistry extends decorate(class {}) {
|
|||
}
|
||||
|
||||
keyForAction(action) {
|
||||
return this.mapActionToKey.get(action);
|
||||
return this.actionKeys[action];
|
||||
}
|
||||
|
||||
mapKeysToActions(map) {
|
||||
for (const key in map) {
|
||||
const action = map[key];
|
||||
this.mapKeyToAction[key] = action;
|
||||
this.mapActionToKey.set(action, key);
|
||||
for (const action in map) {
|
||||
const key = map[action];
|
||||
this.keyActions[key] = action;
|
||||
this.actionKeys[action] = key;
|
||||
}
|
||||
}
|
||||
|
||||
onKeyDown(key) {
|
||||
if (this.mapKeyToAction[key]) {
|
||||
const action = this.mapKeyToAction[key];
|
||||
this._state = this._state.set(action, true);
|
||||
if (this.keyActions[key]) {
|
||||
const action = this.keyActions[key];
|
||||
let value;
|
||||
if (this._actionTransformers[action]) {
|
||||
value = this._actionTransformers[action]('keyDown');
|
||||
}
|
||||
else {
|
||||
value = 1;
|
||||
}
|
||||
this._stream.push({action, value});
|
||||
}
|
||||
}
|
||||
|
||||
onKeyUp(key) {
|
||||
if (this.mapKeyToAction[key]) {
|
||||
const action = this.mapKeyToAction[key];
|
||||
this._state = this._state.delete(action);
|
||||
if (this.keyActions[key]) {
|
||||
const action = this.keyActions[key];
|
||||
let value;
|
||||
if (this._actionTransformers[action]) {
|
||||
value = this._actionTransformers[action]('keyUp');
|
||||
}
|
||||
else {
|
||||
value = 0;
|
||||
}
|
||||
this._stream.push({action, value});
|
||||
}
|
||||
}
|
||||
|
||||
get state() {
|
||||
return this._state;
|
||||
}
|
||||
|
||||
set state(state) {
|
||||
this._state = state;
|
||||
setActionTransformerFor(action, transformer) {
|
||||
this._actionTransformers[action] = transformer;
|
||||
}
|
||||
|
||||
stopListening(normalizer) {
|
||||
|
@ -74,5 +92,3 @@ export class ActionRegistry extends decorate(class {}) {
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
export {InputPacket} from './packet/input.packet';
|
||||
|
|
|
@ -4,33 +4,43 @@ import {Packet} from '@avocado/net';
|
|||
|
||||
export class InputPacket extends Packet {
|
||||
|
||||
static setActionIds(actionIds) {
|
||||
this._actionIds = actionIds;
|
||||
this._idActions = {};
|
||||
for (const action in this._actionIds) {
|
||||
this._idActions[this._actionIds[action]] = action;
|
||||
}
|
||||
}
|
||||
|
||||
static pack(packet) {
|
||||
const data = packet.data[1];
|
||||
for (let i = 0; i < data.length; i++) {
|
||||
const {action, value} = data[i];
|
||||
const actionId = this._actionIds[action];
|
||||
data[i] = ((actionId & 0xFF) << 24) | (value & 0xFFFFFF);
|
||||
}
|
||||
return super.pack(packet);
|
||||
}
|
||||
|
||||
static get schema() {
|
||||
return {
|
||||
...super.schema,
|
||||
data: [
|
||||
['string'],
|
||||
['varint'],
|
||||
],
|
||||
data: ['uint32'],
|
||||
};
|
||||
}
|
||||
|
||||
static fromState(state) {
|
||||
const data = [[], []];
|
||||
const [actions, values] = data;
|
||||
for (const [action, value] of state.entries()) {
|
||||
actions.push(action);
|
||||
values.push(parseInt(value));
|
||||
static unpack(packet) {
|
||||
const data = super.unpack(packet);
|
||||
for (let i = 0; i < data.length; i++) {
|
||||
const actionId = data[i] >> 24;
|
||||
const value = data[i] & 0xFFFFFF;
|
||||
data[i] = {
|
||||
action: this._idActions[actionId],
|
||||
// -1 is nice to handle, anyway. We'll be a pal.
|
||||
value: 16777214 === value ? -1 : value,
|
||||
};
|
||||
}
|
||||
return new InputPacket(data);
|
||||
}
|
||||
|
||||
toState() {
|
||||
const [actions, values] = this.data;
|
||||
return I.Map().withMutations((state) => {
|
||||
for (let i = 0; i < actions.length; ++i) {
|
||||
state.set(actions[i], values[i]);
|
||||
}
|
||||
});
|
||||
return data;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user