refactor: slot offset

This commit is contained in:
cha0s 2024-06-24 04:43:11 -05:00
parent 4e266489d4
commit d5af4aed83
2 changed files with 14 additions and 18 deletions

View File

@ -114,7 +114,7 @@ export default class Engine {
Inventory: { Inventory: {
slots: { slots: {
1: { 1: {
qty: 500, qty: 10,
source: '/assets/potion', source: '/assets/potion',
}, },
}, },
@ -235,7 +235,7 @@ export default class Engine {
} }
case 'use': { case 'use': {
if (payload.value) { if (payload.value) {
const item = Inventory.item(Wielder.activeSlot); const item = Inventory.item(Wielder.activeSlot + 1);
this.server.readAsset(item.source + '/start.js') this.server.readAsset(item.source + '/start.js')
.then((response) => response.text()) .then((response) => response.text())
.then((code) => { .then((code) => {

View File

@ -15,13 +15,17 @@ import styles from './ui.module.css';
const ratio = RESOLUTION.x / RESOLUTION.y; const ratio = RESOLUTION.x / RESOLUTION.y;
function emptySlots() {
return Array(10).fill(undefined);
}
export default function Ui({disconnected}) { export default function Ui({disconnected}) {
// Key input. // Key input.
const client = useContext(ClientContext); const client = useContext(ClientContext);
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 [hotbarSlots, setHotbarSlots] = useState(Array(10).fill(0).map(() => {})); const [hotbarSlots, setHotbarSlots] = useState(emptySlots());
const [activeSlot, setActiveSlot] = useState(0); const [activeSlot, setActiveSlot] = useState(0);
useEffect(() => { useEffect(() => {
let handle; let handle;
@ -141,26 +145,18 @@ export default function Ui({disconnected}) {
ecs.apply(payload.ecs); ecs.apply(payload.ecs);
let localMainEntity = mainEntity; let localMainEntity = mainEntity;
for (const id in payload.ecs) { for (const id in payload.ecs) {
const entity = ecs.get(id);
const update = payload.ecs[id]; const update = payload.ecs[id];
if (update?.MainEntity) { if (update?.MainEntity) {
setMainEntity(localMainEntity = id); setMainEntity(localMainEntity = id);
} }
if (localMainEntity === id) { if (localMainEntity === id) {
if (update.Inventory) { if (update.Inventory) {
const newHotbarSlots = [...hotbarSlots]; const newHotbarSlots = emptySlots();
const {slots, slotChange} = update.Inventory; for (let i = 1; i < 11; ++i) {
if (slotChange) { if (entity.Inventory.slots[i]) {
for (const slotIndex in slotChange) { const {qty, source} = entity.Inventory.slots[i];
newHotbarSlots[slotIndex] = { newHotbarSlots[i - 1] = {
...newHotbarSlots[slotIndex],
...slotChange[slotIndex],
};
}
}
if (slots) {
for (const slotIndex in slots) {
const {qty, source} = slots[slotIndex];
newHotbarSlots[slotIndex] = {
image: source + '/icon.png', image: source + '/icon.png',
qty, qty,
}; };
@ -190,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}