feat: stamina

This commit is contained in:
cha0s 2022-05-03 16:50:24 -05:00
parent b46e2ff837
commit 8f728d1391
4 changed files with 69 additions and 2 deletions

View File

@ -4,8 +4,8 @@ import {PropTypes, React} from '@flecks/react';
import styles from './index.module.scss'; import styles from './index.module.scss';
function Stamina({selfEntity}) { function Stamina({selfEntity}) {
const stamina = usePropertyChange(selfEntity, 'stamina') || 85; const stamina = usePropertyChange(selfEntity, 'stamina');
const maxStamina = usePropertyChange(selfEntity, 'maxStamina') || 100; const maxStamina = usePropertyChange(selfEntity, 'maxStamina');
const filled = stamina / maxStamina; const filled = stamina / maxStamina;
return ( return (
<div <div

View File

@ -85,6 +85,15 @@ export default (flecks) => class Tool extends Trait {
} }
} }
consumeStamina() {
const {wielder} = this.entity;
if (wielder.stamina >= this.params.cost) {
wielder.stamina -= this.params.cost;
return true;
}
return false;
}
createTargetContext(target) { createTargetContext(target) {
const {wielder} = this.entity; const {wielder} = this.entity;
const {room} = wielder; const {room} = wielder;
@ -105,6 +114,7 @@ export default (flecks) => class Tool extends Trait {
static defaultParams() { static defaultParams() {
return { return {
cost: 3,
target: { target: {
type: 'projection', type: 'projection',
distance: 1, distance: 1,
@ -165,6 +175,9 @@ export default (flecks) => class Tool extends Trait {
return { return {
useTool: () => { useTool: () => {
if (!this.consumeStamina()) {
return undefined;
}
this.calculateTargets(); this.calculateTargets();
const promises = this.$$targets.map((target) => { const promises = this.$$targets.map((target) => {
const {Script} = flecks.get('$avocado/resource.resources'); const {Script} = flecks.get('$avocado/resource.resources');

View File

@ -0,0 +1,12 @@
import {Packet} from '@flecks/socket';
export default () => class TraitUpdateWielderStamina extends Packet {
static get data() {
return {
stamina: 'uint16',
maxStamina: 'uint16',
};
}
};

View File

@ -5,6 +5,12 @@ const decorate = compose(
StateProperty('activeSlotIndex', { StateProperty('activeSlotIndex', {
track: true, track: true,
}), }),
StateProperty('maxStamina', {
track: true,
}),
StateProperty('stamina', {
track: true,
}),
); );
export default () => class Wielder extends decorate(Trait) { export default () => class Wielder extends decorate(Trait) {
@ -18,9 +24,21 @@ export default () => class Wielder extends decorate(Trait) {
this.$$itemPromise = undefined; this.$$itemPromise = undefined;
} }
acceptPacket(packet) {
switch (packet.constructor.type) {
case 'TraitUpdateWielderStamina':
this.entity.stamina = packet.data.stamina;
this.entity.maxStamina = packet.data.maxStamina;
break;
default:
}
}
static defaultState() { static defaultState() {
return { return {
activeSlotIndex: 0, activeSlotIndex: 0,
maxStamina: 100,
stamina: 100,
}; };
} }
@ -99,6 +117,30 @@ export default () => class Wielder extends decorate(Trait) {
}; };
} }
packetsFor() {
const packets = [];
const {stamina, maxStamina} = this.stateDifferences();
if (stamina || maxStamina) {
packets.push([
'TraitUpdateWielderStamina',
{
stamina: this.state.stamina,
maxStamina: this.state.maxStamina,
},
]);
}
return packets;
}
get stamina() {
return this.state.stamina;
}
set stamina(stamina) {
// Clamp stamina between 0 and max.
super.stamina = Math.min(Math.max(0, stamina), this.entity.maxStamina);
}
tick() { tick() {
if (-1 !== this.$$itemUseRequest && !this.$$itemPromise) { if (-1 !== this.$$itemUseRequest && !this.$$itemPromise) {
const tickingPromise = this.entity.useItemInSlot(this.$$itemUseRequest); const tickingPromise = this.entity.useItemInSlot(this.$$itemUseRequest);