refactor: controllable ticks its used item

This commit is contained in:
cha0s 2019-09-08 06:27:15 -05:00
parent 6728a20695
commit 4dc4a555de

View File

@ -1,5 +1,6 @@
import * as I from 'immutable'; import * as I from 'immutable';
import {TickingPromise} from '@avocado/core';
import {Trait} from '@avocado/entity'; import {Trait} from '@avocado/entity';
// Input handling. // Input handling.
@ -12,6 +13,7 @@ export class Controllable extends Trait {
constructor(entity, params, state) { constructor(entity, params, state) {
super(entity, params, state); super(entity, params, state);
this._inputState = I.Map(); this._inputState = I.Map();
this._itemPromise = undefined;
} }
set inputState(inputState) { set inputState(inputState) {
@ -19,12 +21,18 @@ export class Controllable extends Trait {
if (AVOCADO_SERVER) { if (AVOCADO_SERVER) {
if (inputState.has('UseItem')) { if (inputState.has('UseItem')) {
const slotIndex = inputState.get('UseItem'); const slotIndex = inputState.get('UseItem');
this.entity.useItemInSlot(slotIndex); this._itemPromise = this.entity.useItemInSlot(slotIndex);
Promise.resolve(this._itemPromise).then(() => {
this._itemPromise = undefined;
});
} }
} }
} }
tick(elapsed) { tick(elapsed) {
if (this._itemPromise && this._itemPromise instanceof TickingPromise) {
this._itemPromise.tick(elapsed);
}
const {_inputState: inputState} = this; const {_inputState: inputState} = this;
if (0 === inputState.size) { if (0 === inputState.size) {
this.entity.currentAnimation = 'idle'; this.entity.currentAnimation = 'idle';