feat: Q+D inventory management

This commit is contained in:
cha0s 2024-06-24 08:14:32 -05:00
parent c1163fd71e
commit ce1b09cf5a
5 changed files with 41 additions and 3 deletions

View File

@ -15,6 +15,20 @@ export default function Hotbar({active, onActivate, slots}) {
> >
<Slot <Slot
onClick={() => onActivate(i)} onClick={() => onActivate(i)}
onDragOver={(event) => {
event.preventDefault();
}}
onDragStart={(event) => {
if (!slot) {
event.preventDefault();
}
event.dataTransfer.setData('silphius/item', i);
onActivate(i);
}}
onDrop={(event) => {
event.preventDefault();
onActivate(i);
}}
{...slot} {...slot}
/> />
</div> </div>

View File

@ -31,6 +31,5 @@
&:hover { &:hover {
background-color: rgba(0, 0, 0, 0.2); background-color: rgba(0, 0, 0, 0.2);
cursor: pointer;
} }
} }

View File

@ -3,12 +3,25 @@ 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({
onClick,
onDragEnter,
onDragOver,
onDragStart,
onDrop,
qty = 1,
source,
}) {
const image = source + '/icon.png'; const image = source + '/icon.png';
return ( return (
<button <button
className={styles.slot} className={styles.slot}
draggable
onClick={onClick} onClick={onClick}
onDragEnter={onDragEnter}
onDragOver={onDragOver}
onDragStart={onDragStart}
onDrop={onDrop}
onKeyDown={(event) => { onKeyDown={(event) => {
event.preventDefault(); event.preventDefault();
}} }}

View File

@ -6,6 +6,7 @@
background-repeat: no-repeat; background-repeat: no-repeat;
background-size: contain; background-size: contain;
border: none; border: none;
cursor: inherit;
display: inline-block; display: inline-block;
height: var(--size); height: var(--size);
image-rendering: pixelated; image-rendering: pixelated;

View File

@ -25,6 +25,7 @@ export default function Ui({disconnected}) {
const [mainEntity, setMainEntity] = useMainEntity(); const [mainEntity, setMainEntity] = useMainEntity();
const [ecs] = useEcs(); const [ecs] = useEcs();
const [showDisconnected, setShowDisconnected] = useState(false); const [showDisconnected, setShowDisconnected] = useState(false);
const [bufferSlot, setBufferSlot] = useState();
const [hotbarSlots, setHotbarSlots] = useState(emptySlots()); const [hotbarSlots, setHotbarSlots] = useState(emptySlots());
const [activeSlot, setActiveSlot] = useState(0); const [activeSlot, setActiveSlot] = useState(0);
useEffect(() => { useEffect(() => {
@ -152,6 +153,7 @@ export default function Ui({disconnected}) {
} }
if (localMainEntity === id) { if (localMainEntity === id) {
if (update.Inventory) { if (update.Inventory) {
setBufferSlot(entity.Inventory.slots[0]);
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]; newHotbarSlots[i - 1] = entity.Inventory.slots[i];
@ -165,11 +167,20 @@ export default function Ui({disconnected}) {
} }
}, [hotbarSlots, mainEntity, setMainEntity]); }, [hotbarSlots, mainEntity, setMainEntity]);
return ( return (
<div className={styles.ui}> <div
className={styles.ui}
>
<style> <style>
{` {`
@media (max-aspect-ratio: ${ratio}) { .${styles.ui} { width: 100%; } } @media (max-aspect-ratio: ${ratio}) { .${styles.ui} { width: 100%; } }
@media (min-aspect-ratio: ${ratio}) { .${styles.ui} { height: 100%; } } @media (min-aspect-ratio: ${ratio}) { .${styles.ui} { height: 100%; } }
.${styles.ui} {
cursor: ${
bufferSlot
? `url('${bufferSlot.source}/icon.png'), auto !important`
: 'auto'
};
}
`} `}
</style> </style>
<Pixi /> <Pixi />