feat: stamina regen

This commit is contained in:
cha0s 2022-05-04 03:47:36 -05:00
parent 53b81f7867
commit d741af4e18
2 changed files with 14 additions and 2 deletions

View File

@ -27,6 +27,7 @@
],
"dependencies": {
"@avocado/behavior": "^3.0.0",
"@avocado/timing": "^3.0.0",
"@avocado/traits": "^3.0.0",
"@flecks/core": "^1.4.1",
"@flecks/react": "^1.4.1",

View File

@ -1,3 +1,4 @@
import {Ticker} from '@avocado/timing';
import {StateProperty, Trait} from '@avocado/traits';
import {compose} from '@flecks/core';
@ -22,6 +23,10 @@ export default () => class Wielder extends decorate(Trait) {
constructor() {
super();
this.$$itemPromise = undefined;
this.$$regenTicker = new Ticker(0.25);
this.$$regenTicker.on('tick', () => {
this.entity.stamina += 0.1;
});
}
acceptPacket(packet) {
@ -120,7 +125,10 @@ export default () => class Wielder extends decorate(Trait) {
packetsFor() {
const packets = [];
const {stamina, maxStamina} = this.stateDifferences();
if (stamina || maxStamina) {
if (
(stamina && (parseInt(stamina.old, 10) !== parseInt(stamina.value, 10)))
|| (maxStamina && (parseInt(maxStamina.old, 10) !== parseInt(maxStamina.value, 10)))
) {
packets.push([
'TraitUpdateWielderStamina',
{
@ -141,13 +149,16 @@ export default () => class Wielder extends decorate(Trait) {
super.stamina = Math.min(Math.max(0, stamina), this.entity.maxStamina);
}
tick() {
tick(elapsed) {
if (-1 !== this.$$itemUseRequest && !this.$$itemPromise) {
const tickingPromise = this.entity.useItemInSlot(this.$$itemUseRequest);
if (tickingPromise) {
this.entity.addTickingPromise(tickingPromise);
}
}
if ('web' !== process.env.FLECKS_CORE_BUILD_TARGET) {
this.$$regenTicker.tick(elapsed);
}
}
};