refactor: input greatness
This commit is contained in:
parent
1f642fe8e0
commit
142bf704eb
|
@ -10,14 +10,22 @@ export class ActionRegistry extends decorate(class {}) {
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
this.mapActionToKey = new Map();
|
this.actionKeys = new Map();
|
||||||
this.mapKeyToAction = {};
|
this.keyActions = {};
|
||||||
this.normalizer = undefined;
|
this.normalizer = undefined;
|
||||||
this._state = I.Map();
|
this._actionIds = {};
|
||||||
|
this._stream = [];
|
||||||
|
this._actionTransformers = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
actionForKey(key) {
|
actionForKey(key) {
|
||||||
return this.mapKeyToAction[key];
|
return this.keyActions[key];
|
||||||
|
}
|
||||||
|
|
||||||
|
drain() {
|
||||||
|
const stream = this._stream;
|
||||||
|
this._stream = [];
|
||||||
|
return stream;
|
||||||
}
|
}
|
||||||
|
|
||||||
listen(normalizer) {
|
listen(normalizer) {
|
||||||
|
@ -31,37 +39,47 @@ export class ActionRegistry extends decorate(class {}) {
|
||||||
}
|
}
|
||||||
|
|
||||||
keyForAction(action) {
|
keyForAction(action) {
|
||||||
return this.mapActionToKey.get(action);
|
return this.actionKeys[action];
|
||||||
}
|
}
|
||||||
|
|
||||||
mapKeysToActions(map) {
|
mapKeysToActions(map) {
|
||||||
for (const key in map) {
|
for (const action in map) {
|
||||||
const action = map[key];
|
const key = map[action];
|
||||||
this.mapKeyToAction[key] = action;
|
this.keyActions[key] = action;
|
||||||
this.mapActionToKey.set(action, key);
|
this.actionKeys[action] = key;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
onKeyDown(key) {
|
onKeyDown(key) {
|
||||||
if (this.mapKeyToAction[key]) {
|
if (this.keyActions[key]) {
|
||||||
const action = this.mapKeyToAction[key];
|
const action = this.keyActions[key];
|
||||||
this._state = this._state.set(action, true);
|
let value;
|
||||||
|
if (this._actionTransformers[action]) {
|
||||||
|
value = this._actionTransformers[action]('keyDown');
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
value = 1;
|
||||||
|
}
|
||||||
|
this._stream.push({action, value});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
onKeyUp(key) {
|
onKeyUp(key) {
|
||||||
if (this.mapKeyToAction[key]) {
|
if (this.keyActions[key]) {
|
||||||
const action = this.mapKeyToAction[key];
|
const action = this.keyActions[key];
|
||||||
this._state = this._state.delete(action);
|
let value;
|
||||||
|
if (this._actionTransformers[action]) {
|
||||||
|
value = this._actionTransformers[action]('keyUp');
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
value = 0;
|
||||||
|
}
|
||||||
|
this._stream.push({action, value});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
get state() {
|
setActionTransformerFor(action, transformer) {
|
||||||
return this._state;
|
this._actionTransformers[action] = transformer;
|
||||||
}
|
|
||||||
|
|
||||||
set state(state) {
|
|
||||||
this._state = state;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
stopListening(normalizer) {
|
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 {
|
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() {
|
static get schema() {
|
||||||
return {
|
return {
|
||||||
...super.schema,
|
...super.schema,
|
||||||
data: [
|
data: ['uint32'],
|
||||||
['string'],
|
|
||||||
['varint'],
|
|
||||||
],
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
static fromState(state) {
|
static unpack(packet) {
|
||||||
const data = [[], []];
|
const data = super.unpack(packet);
|
||||||
const [actions, values] = data;
|
for (let i = 0; i < data.length; i++) {
|
||||||
for (const [action, value] of state.entries()) {
|
const actionId = data[i] >> 24;
|
||||||
actions.push(action);
|
const value = data[i] & 0xFFFFFF;
|
||||||
values.push(parseInt(value));
|
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);
|
return 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]);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user