fix: item usage

This commit is contained in:
cha0s 2021-01-19 16:53:38 -06:00
parent dee4e7b13f
commit 86aed82ccd
9 changed files with 63 additions and 7 deletions

View File

@ -2,7 +2,6 @@ import './index.scss';
import {ActionRegistry, InputNormalizer} from '@avocado/input';
import {setSelfEntity, useSelfEntity} from '@humus/core';
import {useLatus} from '@latus/react/client';
import {useDispatch} from '@latus/redux';
import {useSocket} from '@latus/socket';
import {hot} from 'react-hot-loader';
@ -13,7 +12,6 @@ import Hotbar from './hotbar';
const Play = () => {
const dispatch = useDispatch();
const latus = useLatus();
const {uuid} = useParams();
const ref = useRef();
const selfEntity = useSelfEntity();
@ -35,6 +33,9 @@ const Play = () => {
const inputNormalizer = new InputNormalizer();
inputNormalizer.listen(window.document.body);
const actionRegistry = new ActionRegistry();
actionRegistry.setActionTransformerFor('UseItem', (type) => (
selfEntity && 'keyDown' === type ? selfEntity.activeSlotIndex : -1
));
actionRegistry.listen(inputNormalizer);
const inputHandle = setInterval(() => {
const inputStream = actionRegistry.drain();

View File

@ -78,6 +78,7 @@ const ItemSlotComponent = (props) => {
ItemSlotComponent.defaultProps = {
children: null,
className: '',
item: null,
onClick: () => {},
};
@ -90,7 +91,7 @@ ItemSlotComponent.propTypes = {
item: PropTypes.shape({
qty: PropTypes.number,
imageForSlot: PropTypes.func,
}).isRequired,
}),
onClick: PropTypes.func,
};

View File

@ -33,7 +33,8 @@
"@latus/react": "^2.0.0",
"@latus/redux": "^2.0.0",
"@latus/socket": "^2.0.0",
"debug": "4.3.1"
"debug": "4.3.1",
"msgpack-lite": "^0.1.26"
},
"devDependencies": {
"@neutrinojs/airbnb-base": "^9.4.0",

View File

@ -13,5 +13,8 @@ export default {
'@latus/redux/slices': () => ({
selfEntity,
}),
'@latus/socket/packets': gatherWithLatus(
require.context('./packets', false, /\.js$/),
),
},
};

View File

@ -0,0 +1,26 @@
import {Packet} from '@latus/socket';
import msgpack from 'msgpack-lite';
export default () => class TraitUpdateReceptacleItemFullPacket extends Packet {
static pack(packet) {
const data = packet.data[1];
data.json = msgpack.encode(data.json);
return super.pack(packet);
}
static get data() {
return {
slotIndex: 'uint16',
json: 'buffer',
};
}
static unpack(packet) {
const unpacked = super.unpack(packet);
const {data} = unpacked;
data.json = msgpack.decode(data.json);
return unpacked;
}
};

View File

@ -0,0 +1,12 @@
import {Packet} from '@latus/socket';
export default () => class TraitUpdateReceptacleItemQtyPacket extends Packet {
static get data() {
return {
slotIndex: 'uint16',
qty: 'uint16',
};
}
};

View File

@ -0,0 +1,12 @@
import {Packet} from '@latus/socket';
export default () => class TraitUpdateReceptacleItemSwapPacket extends Packet {
static get data() {
return {
firstSlotIndex: 'uint16',
secondSlotIndex: 'uint16',
};
}
};

View File

@ -20,8 +20,6 @@ export default (latus) => class Item extends decorate(Trait) {
constructor() {
super();
const itemActions = this._processedItemActions();
this._itemActions = new Actions(compile(itemActions, latus));
this._slotImageUris = {};
}
@ -102,6 +100,8 @@ export default (latus) => class Item extends decorate(Trait) {
async load(json) {
await super.load(json);
const itemActions = this._processedItemActions();
this._itemActions = new Actions(compile(itemActions, latus));
if ('client' === process.env.SIDE) {
await Promise.all(Object.entries(this.params.slotImageUris)
.map(async ([slotIndex, uri]) => {

View File

@ -51,7 +51,7 @@ export default (latus) => class Receptacle extends decorate(Trait) {
}
async acceptPacket(packet) {
switch (packet.constructor.name) {
switch (packet.constructor.type) {
case 'TraitUpdateReceptacleItemQty': {
const item = this.entity.itemInSlot(packet.data.slotIndex);
if (item) {