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

View File

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