refactor: input

This commit is contained in:
cha0s 2021-02-15 20:09:22 -06:00
parent b7e20c187d
commit b1b4b15676
5 changed files with 189 additions and 2 deletions

View File

@ -1,9 +1,13 @@
import {TickingPromise} from '@avocado/core';
import {Vector} from '@avocado/math';
import {StateProperty, Trait} from '@avocado/traits';
import {compose} from '@latus/core';
import {compose, EventEmitter} from '@latus/core';
const decorate = compose(
EventEmitter,
Vector.Mixin('actionMovement', 'actionMovementX', 'actionMovementY', {
track: true,
}),
StateProperty('isMobile', {
track: true,
}),
@ -24,6 +28,12 @@ export default () => class Mobile extends decorate(Trait) {
}
}
static defaultParams() {
return {
mobileAnimation: true,
};
}
static defaultState() {
return {
isMobile: true,
@ -99,6 +109,44 @@ export default () => class Mobile extends decorate(Trait) {
};
}
listeners() {
return {
acceptAction: ({action, value}) => {
const normalized = 0 === value ? -1 : 1;
switch (action) {
case 'MoveUp':
this.actionMovementY -= normalized;
break;
case 'MoveRight':
this.actionMovementX += normalized;
break;
case 'MoveDown':
this.actionMovementY += normalized;
break;
case 'MoveLeft':
this.actionMovementX -= normalized;
break;
default:
}
},
isMobileChanged: () => {
if (this.params.mobileAnimation) {
this.updateAnimation();
}
},
};
}
async load(json) {
await super.load(json);
if (this.params.mobileAnimation) {
this.on('actionMovementChanged', this.updateAnimation, this);
}
}
methods() {
return {
@ -180,6 +228,9 @@ export default () => class Mobile extends decorate(Trait) {
}
tick(elapsed) {
if (this.entity.isMobile && !Vector.isZero(this.actionMovement)) {
this.entity.requestMovement(this.actionMovement);
}
if (Vector.isZero(this.#appliedMovement)) {
return;
}
@ -195,4 +246,16 @@ export default () => class Mobile extends decorate(Trait) {
this.#appliedMovement = [0, 0];
}
updateAnimation() {
if (!this.entity.is('Animated')) {
return;
}
if (!this.entity.isMobile || Vector.isZero(this.actionMovement)) {
this.entity.currentAnimation = 'idle';
}
else {
this.entity.currentAnimation = 'moving';
}
}
};

View File

@ -20,6 +20,7 @@
"test.js.map"
],
"dependencies": {
"@avocado/traits": "^2.0.0",
"@latus/core": "2.0.0",
"@latus/socket": "2.0.0",
"autoprefixer": "^9.8.6",

View File

@ -1,3 +1,4 @@
import {gatherWithLatus} from '@latus/core';
import D from 'debug';
import ActionRegistry from './action-registry';
@ -9,6 +10,9 @@ const debug = D('@avocado/input');
export default {
hooks: {
'@avocado/traits': gatherWithLatus(
require.context('./traits', false, /\.js$/),
),
'@latus/core/config': () => ({
actions: [],
}),

View File

@ -0,0 +1,65 @@
import {Trait} from '@avocado/traits';
import ActionRegistry from '../action-registry';
// Input handling.
export default () => class Controllable extends Trait {
#actionRegistry;
#queued = [];
get actionRegistry() {
return this.#actionRegistry;
}
static defaultParams() {
return {
actions: {},
};
}
destroy() {
this.#actionRegistry.stopListening();
}
get inputActions() {
return this.params.actions;
}
async load(json) {
await super.load(json);
this.#actionRegistry = new ActionRegistry(this.params.actions);
}
methods() {
return {
acceptActionStream: (actionStream) => {
for (let i = 0; i < actionStream.length; i++) {
this.entity.emit('acceptAction', actionStream[i]);
}
},
drainInput: () => {
const drained = this.#queued;
this.#queued = [];
return drained;
},
listenForInput: (inputNormalizer) => {
this.#actionRegistry.listen(inputNormalizer);
},
};
}
tick() {
if ('client' === process.env.SIDE) {
const drained = this.#actionRegistry.drain();
this.entity.acceptActionStream(drained);
this.#queued.push(...drained);
}
}
};

View File

@ -2,6 +2,50 @@
# yarn lockfile v1
"@avocado/core@2.0.0", "@avocado/core@^2.0.0":
version "2.0.0"
resolved "http://npm.cha0sdev/@avocado%2fcore/-/core-2.0.0.tgz#c18b0f64e4503c1a9dec67586f7ebf9bda5d129e"
integrity sha512-XVFaKJ89ajw+Sw9uwFgYblRduOiZ0un5gXu7eSbN2LAA+KjyrvfCcLNnYVKJZ67njkMGLq9nE2GSzZEeEmbkfA==
dependencies:
autoprefixer "^9.8.6"
debug "4.3.1"
"@avocado/resource@^2.0.0":
version "2.0.0"
resolved "http://npm.cha0sdev/@avocado%2fresource/-/resource-2.0.0.tgz#3ec04329d6e1357f67a956fca48062dcbb7107e1"
integrity sha512-V7PZsouL6rpAhTcSoyVIxZBiijFN++BDHfxwbDxiA1IaZ8ur2Af77IlsRoZhgihY2PDp3p1hypL24HJUJ2WQ9Q==
dependencies:
"@avocado/core" "2.0.0"
"@latus/core" "2.0.0"
autoprefixer "^9.8.6"
debug "4.3.1"
deepmerge "^4.2.2"
uuid "^8.3.2"
"@avocado/s13n@^2.0.0":
version "2.0.0"
resolved "http://npm.cha0sdev/@avocado%2fs13n/-/s13n-2.0.0.tgz#2849a8dd3f2ec91821ee18ffa51f6d6ccc3ac957"
integrity sha512-I26AuKZQj7g82RzqCYxtUPNX9ZedNWoPaNJnenJbMCDjznklNIFW0ZVpgbOb5Rqoa99YFDpafkEsaMKBGZTW6g==
dependencies:
"@avocado/resource" "^2.0.0"
"@latus/core" "2.0.0"
"@latus/socket" "2.0.0"
autoprefixer "^9.8.6"
debug "4.3.1"
msgpack-lite "^0.1.26"
"@avocado/traits@^2.0.0":
version "2.0.0"
resolved "http://npm.cha0sdev/@avocado%2ftraits/-/traits-2.0.0.tgz#f31b45d27491a5e502f0ad119672cc7b0b4a627b"
integrity sha512-zL7iGUeFb4odHDuk/2wK3O/ntGWw7OMYHGmJROT5sOTyjE4+G42AvUlKfaAeza109yH8gDL7IQxhWX7r1zG60g==
dependencies:
"@avocado/core" "^2.0.0"
"@avocado/resource" "^2.0.0"
"@avocado/s13n" "^2.0.0"
"@latus/core" "^2.0.0"
autoprefixer "^9.8.6"
debug "4.3.1"
"@babel/code-frame@^7.0.0", "@babel/code-frame@^7.10.4", "@babel/code-frame@^7.12.11":
version "7.12.11"
resolved "http://npm.cha0sdev/@babel%2fcode-frame/-/code-frame-7.12.11.tgz#f4ad435aa263db935b8f10f2c552d23fb716a63f"
@ -909,7 +953,7 @@
object-assign "^4.1.1"
scheduler "^0.20.1"
"@latus/core@2.0.0":
"@latus/core@2.0.0", "@latus/core@^2.0.0":
version "2.0.0"
resolved "http://npm.cha0sdev/@latus%2fcore/-/core-2.0.0.tgz#12a0f3c11c9832a60e6726ea74ce0ae9b45e9faf"
integrity sha512-JMxgl8Qsvby2OKtzXEs7cjamvsgVxEbJXbjebsmDl+J9Cq5513/4Xpq37EYYvT6FEZpoHFoIkQsaG+89JQzTwg==
@ -2788,6 +2832,11 @@ deepmerge@^2.2.1:
resolved "http://npm.cha0sdev/deepmerge/-/deepmerge-2.2.1.tgz#5d3ff22a01c00f645405a2fbc17d0778a1801170"
integrity sha512-R9hc1Xa/NOBi9WRVUWg19rl1UB7Tt4kuPd+thNJgFZoxXsTz7ncaPaeIm+40oSGuP33DfMb4sZt1QIGiJzC4EA==
deepmerge@^4.2.2:
version "4.2.2"
resolved "http://npm.cha0sdev/deepmerge/-/deepmerge-4.2.2.tgz#44d2ea3679b8f4d4ffba33f03d865fc1e7bf4955"
integrity sha512-FJ3UgI4gIl+PHZm53knsuSFpE+nESMr7M4v9QcgB7S63Kj/6WqMiFQJpBBYz1Pt+66bZpP3Q7Lye0Oo9MPKEdg==
default-gateway@^4.2.0:
version "4.2.0"
resolved "http://npm.cha0sdev/default-gateway/-/default-gateway-4.2.0.tgz#167104c7500c2115f6dd69b0a536bb8ed720552b"
@ -8074,6 +8123,11 @@ uuid@^3.3.2, uuid@^3.4.0:
resolved "http://npm.cha0sdev/uuid/-/uuid-3.4.0.tgz#b23e4358afa8a202fe7a100af1f5f883f02007ee"
integrity sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A==
uuid@^8.3.2:
version "8.3.2"
resolved "http://npm.cha0sdev/uuid/-/uuid-8.3.2.tgz#80d5b5ced271bb9af6c445f21a1a04c606cefbe2"
integrity sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==
v8-compile-cache@^2.0.3, v8-compile-cache@^2.1.1:
version "2.2.0"
resolved "http://npm.cha0sdev/v8-compile-cache/-/v8-compile-cache-2.2.0.tgz#9471efa3ef9128d2f7c6a7ca39c4dd6b5055b132"