Compare commits

..

No commits in common. "fe412e710ef188bce0dbe6f11394904389d83547" and "d5af4aed83059ef008e6a79732017cae8f278080" have entirely different histories.

10 changed files with 36 additions and 41 deletions

View File

@ -2,6 +2,7 @@ import Schema from '@/ecs/schema.js';
export default function(Component) { export default function(Component) {
return class Inventory extends Component { return class Inventory extends Component {
insertMany(entities) { insertMany(entities) {
for (const [id, {slotChange}] of entities) { for (const [id, {slotChange}] of entities) {
if (slotChange) { if (slotChange) {
@ -21,6 +22,7 @@ export default function(Component) {
} }
return super.insertMany(entities); return super.insertMany(entities);
} }
mergeDiff(original, update) { mergeDiff(original, update) {
if (!update.slotChange) { if (!update.slotChange) {
return super.mergeDiff(original, update); return super.mergeDiff(original, update);
@ -55,7 +57,6 @@ export default function(Component) {
target[property] = value; target[property] = value;
if ('qty' === property && value <= 0) { if ('qty' === property && value <= 0) {
Component.markChange(instance.entity, 'slotChange', {[slot]: false}); Component.markChange(instance.entity, 'slotChange', {[slot]: false});
delete slots[slot];
} }
else { else {
Component.markChange(instance.entity, 'slotChange', {[slot]: {[property]: value}}); Component.markChange(instance.entity, 'slotChange', {[slot]: {[property]: value}});

View File

@ -234,22 +234,16 @@ export default class Engine {
break; break;
} }
case 'use': { case 'use': {
const item = Inventory.item(Wielder.activeSlot + 1); if (payload.value) {
if (item) { const item = Inventory.item(Wielder.activeSlot + 1);
this.server.readAsset([ this.server.readAsset(item.source + '/start.js')
item.source, .then((response) => response.text())
payload.value ? 'start.js' : 'stop.js',
].join('/'))
.then((response) => response.ok ? response.text() : '')
.then((code) => { .then((code) => {
if (code) { Ticking.addTickingPromise(
Ticking.addTickingPromise( Script.tickingPromise(code, {item, wielder: entity}),
Script.tickingPromise(code, {item, wielder: entity}), );
);
}
}); });
} }
break;
} }
} }
} }

View File

@ -6,18 +6,16 @@ import Slot from './slot.jsx';
*/ */
export default function Hotbar({active, onActivate, slots}) { export default function Hotbar({active, onActivate, slots}) {
const Slots = slots.map((slot, i) => ( const Slots = slots.map((slot, i) => (
<div <button
className={ className={
[styles.slotWrapper, active === i && styles.active] [styles.slotWrapper, active === i && styles.active]
.filter(Boolean).join(' ') .filter(Boolean).join(' ')
} }
onClick={() => onActivate(i)}
key={i} key={i}
> >
<Slot <Slot {...slot} />
onClick={() => onActivate(i)} </button>
{...slot}
/>
</div>
)); ));
return ( return (
<div <div

View File

@ -11,6 +11,7 @@
} }
.slotWrapper { .slotWrapper {
background-color: transparent;
border: var(--border) solid #999999; border: var(--border) solid #999999;
box-sizing: border-box; box-sizing: border-box;
display: inline-block; display: inline-block;

View File

@ -3,10 +3,9 @@ import styles from './slot.module.css';
/** /**
* An inventory slot. Displays an item image and the quantity of the item if > 1. * An inventory slot. Displays an item image and the quantity of the item if > 1.
*/ */
export default function Slot({source, onClick, qty = 1}) { export default function Slot({image, onClick, qty = 1}) {
const image = source + '/icon.png';
return ( return (
<button <div
className={styles.slot} className={styles.slot}
onClick={onClick} onClick={onClick}
> >
@ -25,6 +24,6 @@ export default function Slot({source, onClick, qty = 1}) {
</span> </span>
)} )}
</div> </div>
</button> </div>
); );
} }

View File

@ -1,11 +1,9 @@
.slot { .slot {
--size: calc(var(--unit) * 50px); --size: calc(var(--unit) * 50px);
--space: calc(var(--unit) * 10px); --space: calc(var(--unit) * 10px);
background-color: transparent;
background-position: center; background-position: center;
background-repeat: no-repeat; background-repeat: no-repeat;
background-size: contain; background-size: contain;
border: none;
display: inline-block; display: inline-block;
height: var(--size); height: var(--size);
image-rendering: pixelated; image-rendering: pixelated;

View File

@ -137,7 +137,7 @@ export default function Ui({disconnected}) {
}); });
} }
}); });
}, [client]); });
usePacket('Tick', (payload) => { usePacket('Tick', (payload) => {
if (0 === Object.keys(payload.ecs).length) { if (0 === Object.keys(payload.ecs).length) {
return; return;
@ -154,7 +154,13 @@ export default function Ui({disconnected}) {
if (update.Inventory) { if (update.Inventory) {
const newHotbarSlots = emptySlots(); const newHotbarSlots = emptySlots();
for (let i = 1; i < 11; ++i) { for (let i = 1; i < 11; ++i) {
newHotbarSlots[i - 1] = entity.Inventory.slots[i]; if (entity.Inventory.slots[i]) {
const {qty, source} = entity.Inventory.slots[i];
newHotbarSlots[i - 1] = {
image: source + '/icon.png',
qty,
};
}
} }
setHotbarSlots(newHotbarSlots); setHotbarSlots(newHotbarSlots);
} }
@ -180,7 +186,7 @@ export default function Ui({disconnected}) {
onActivate={(i) => { onActivate={(i) => {
client.send({ client.send({
type: 'Action', type: 'Action',
payload: {type: 'changeSlot', value: i + 1}, payload: {type: 'changeSlot', value: i},
}); });
}} }}
slots={hotbarSlots} slots={hotbarSlots}

View File

@ -1,6 +1,6 @@
import {useEffect, useRef, useState} from 'react'; import {useEffect, useRef, useState} from 'react';
import Dom from '@/react-components/dom.jsx'; import Dom from '@/components/dom.jsx';
import {RESOLUTION} from '@/constants.js'; import {RESOLUTION} from '@/constants.js';
function Decorator({children, style}) { function Decorator({children, style}) {
@ -26,7 +26,6 @@ function Decorator({children, style}) {
ref={ref} ref={ref}
style={{ style={{
backgroundColor: '#1099bb', backgroundColor: '#1099bb',
flexDirection: 'column',
opacity: 0 === scale ? 0 : 1, opacity: 0 === scale ? 0 : 1,
position: 'relative', position: 'relative',
height: `calc(${RESOLUTION.y}px * ${scale})`, height: `calc(${RESOLUTION.y}px * ${scale})`,

View File

@ -1,12 +1,14 @@
import {useArgs} from '@storybook/preview-api'; import {useArgs} from '@storybook/preview-api';
import {fn} from '@storybook/test'; import {fn} from '@storybook/test';
import Hotbar from '@/react-components/hotbar.jsx'; import Hotbar from '@/components/hotbar.jsx';
import DomDecorator from './dom-decorator.jsx'; import DomDecorator from './dom-decorator.jsx';
import potion from '/assets/potion.png?url';
const slots = Array(10).fill({}); const slots = Array(10).fill({});
slots[2] = {qty: 24, source: '/assets/potion'}; slots[2] = {image: potion, qty: 24};
export default { export default {
title: 'Dom/Inventory/Hotbar', title: 'Dom/Inventory/Hotbar',
@ -23,12 +25,7 @@ export default {
}; };
return Hotbar(); return Hotbar();
}, },
DomDecorator({ DomDecorator(),
style: {
display: 'flex',
flexDirection: 'column',
},
}),
], ],
tags: ['autodocs'], tags: ['autodocs'],
args: { args: {

View File

@ -1,7 +1,9 @@
import Slot from '@/react-components/slot.jsx'; import Slot from '@/components/slot.jsx';
import DomDecorator from './dom-decorator.jsx'; import DomDecorator from './dom-decorator.jsx';
import potion from '/assets/potion.png?url';
export default { export default {
title: 'Dom/Inventory/Slot', title: 'Dom/Inventory/Slot',
component: Slot, component: Slot,
@ -31,7 +33,7 @@ export default {
}, },
}, },
args: { args: {
source: '/assets/potion', image: potion,
}, },
}; };