refactor: controllable
This commit is contained in:
parent
71f45026f2
commit
3d2484b715
|
@ -1,15 +1,30 @@
|
|||
import {TickingPromise} from '@avocado/core';
|
||||
import {
|
||||
TickingPromise,
|
||||
} from '@avocado/core';
|
||||
import {Vector} from '@avocado/math';
|
||||
import {Trait} from '@avocado/traits';
|
||||
import {
|
||||
compose,
|
||||
EventEmitter,
|
||||
} from '@latus/core';
|
||||
|
||||
const decorate = compose(
|
||||
EventEmitter,
|
||||
Vector.Mixin('movement', 'x', 'y', {
|
||||
track: true,
|
||||
}),
|
||||
);
|
||||
|
||||
// Input handling.
|
||||
export default () => class Controllable extends Trait {
|
||||
export default () => class Controllable extends decorate(Trait) {
|
||||
|
||||
#itemPromise;
|
||||
|
||||
#itemUseRequest = -1;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
this._isUsingItem = -1;
|
||||
this._itemPromise = undefined;
|
||||
this._movementVector = [0, 0];
|
||||
this.on('movementChanged', this.updateAnimation, this);
|
||||
}
|
||||
|
||||
static dependencies() {
|
||||
|
@ -37,26 +52,49 @@ export default () => class Controllable extends Trait {
|
|||
this.entity.activeSlotIndex = (this.entity.activeSlotIndex + 9) % 10;
|
||||
break;
|
||||
case 'MoveUp':
|
||||
this._movementVector[1] -= normalized;
|
||||
this.y -= normalized;
|
||||
break;
|
||||
case 'MoveRight':
|
||||
this._movementVector[0] += normalized;
|
||||
this.x += normalized;
|
||||
break;
|
||||
case 'MoveDown':
|
||||
this._movementVector[1] += normalized;
|
||||
this.y += normalized;
|
||||
break;
|
||||
case 'MoveLeft':
|
||||
this._movementVector[0] -= normalized;
|
||||
this.x -= normalized;
|
||||
break;
|
||||
case 'UseItem':
|
||||
if ('client' !== process.env.SIDE) {
|
||||
this._isUsingItem = value;
|
||||
this.#itemUseRequest = value;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
}
|
||||
}
|
||||
if (!this.entity.isMobile || Vector.isZero(this._movementVector)) {
|
||||
}
|
||||
|
||||
async load(json) {
|
||||
await super.load(json);
|
||||
this.entity.on('isMobileChanged', this.updateAnimation, this);
|
||||
}
|
||||
|
||||
tick(elapsed) {
|
||||
if (-1 !== this.#itemUseRequest && !this.#itemPromise) {
|
||||
this.#itemPromise = this.entity.useItemInSlot(this.#itemUseRequest);
|
||||
Promise.resolve(this.#itemPromise).then(() => {
|
||||
this.#itemPromise = undefined;
|
||||
});
|
||||
}
|
||||
if (this.#itemPromise && this.#itemPromise instanceof TickingPromise) {
|
||||
this.#itemPromise.tick(elapsed);
|
||||
}
|
||||
if (this.entity.isMobile && !Vector.isZero(this.movement)) {
|
||||
this.entity.requestMovement(this.movement);
|
||||
}
|
||||
}
|
||||
|
||||
updateAnimation() {
|
||||
if (!this.entity.isMobile || Vector.isZero(this.movement)) {
|
||||
this.entity.currentAnimation = 'idle';
|
||||
}
|
||||
else {
|
||||
|
@ -64,19 +102,4 @@ export default () => class Controllable extends Trait {
|
|||
}
|
||||
}
|
||||
|
||||
tick(elapsed) {
|
||||
if (-1 !== this._isUsingItem && !this._itemPromise) {
|
||||
this._itemPromise = this.entity.useItemInSlot(this._isUsingItem);
|
||||
Promise.resolve(this._itemPromise).then(() => {
|
||||
this._itemPromise = undefined;
|
||||
});
|
||||
}
|
||||
if (this._itemPromise && this._itemPromise instanceof TickingPromise) {
|
||||
this._itemPromise.tick(elapsed);
|
||||
}
|
||||
if (!Vector.isZero(this._movementVector)) {
|
||||
this.entity.requestMovement(this._movementVector);
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue
Block a user