Skip to content

Commit

Permalink
refactor(ri-client): add util function for getItems
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkol committed Jul 19, 2022
1 parent 4618acf commit 23b77af
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
4 changes: 4 additions & 0 deletions packages/ri/client/src/layers/Network/createNetworkLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { WorldCoord } from "../../types";
import { SetupContractConfig } from "./setup/setupContracts";
import { LOCAL_CHAIN_ID } from "../../constants";
import { defineStringComponent } from "@latticexyz/std-client";
import { getItems } from "./utils";

export type NetworkLayerConfig = {
worldAddress: string;
Expand Down Expand Up @@ -338,6 +339,9 @@ export async function createNetworkLayer(config: NetworkLayerConfig) {
dev: {
spawnGold,
},
util: {
getItems: getItems(components.OwnedBy, components.ItemType, world.entities),
},
},
DEV_MODE,
};
Expand Down
1 change: 1 addition & 0 deletions packages/ri/client/src/layers/Network/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { getItems } from "./inventory";
12 changes: 12 additions & 0 deletions packages/ri/client/src/layers/Network/utils/inventory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Component, EntityID, EntityIndex, Has, HasValue, runQuery, Type } from "@latticexyz/recs";

export function getItems(
ownedByComponent: Component<{ value: Type.Entity }>,
itemTypeComponent: Component<{ value: Type.Number }>,
entityIndexToID: EntityID[]
) {
const func = (entity: EntityIndex) => [
...runQuery([HasValue(ownedByComponent, { value: entityIndexToID[entity] }), Has(itemTypeComponent)]),
];
return func;
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export function createInputSystem(layer: PhaserLayer) {
transferInventory,
escapePortal,
dev: { spawnGold },
util: { getItems },
},
network: { connectedAddress },
},
Expand Down Expand Up @@ -89,13 +90,9 @@ export function createInputSystem(layer: PhaserLayer) {
if (!isOwnedByCaller(OwnedBy, selectedEntity, player, world.entityToIndex)) return false;
if (hasComponent(OwnedBy, highlightedEntity)) return false;

const highlightedItems = [
...runQuery([HasValue(OwnedBy, { value: world.entities[highlightedEntity] }), Has(ItemType)]),
];
const highlightedItems = getItems(highlightedEntity);
if (highlightedItems.length > 0) {
const selectedItems = [
...runQuery([HasValue(OwnedBy, { value: world.entities[selectedEntity] }), Has(ItemType)]),
];
const selectedItems = getItems(selectedEntity);
const selectedCapacity = getComponentValue(Inventory, selectedEntity);
if (selectedCapacity && selectedCapacity.value > selectedItems.length) {
const selectedEntityPos = getComponentValue(LocalPosition, selectedEntity);
Expand All @@ -120,11 +117,9 @@ export function createInputSystem(layer: PhaserLayer) {
)
return false;

const selectedItems = [...runQuery([HasValue(OwnedBy, { value: world.entities[selectedEntity] }), Has(ItemType)])];
const selectedItems = getItems(selectedEntity);
if (selectedItems.length > 0) {
const highlightedItems = [
...runQuery([HasValue(OwnedBy, { value: world.entities[highlightedEntity] }), Has(ItemType)]),
];
const highlightedItems = getItems(highlightedEntity);
const highlightedCapacity = getComponentValue(Inventory, highlightedEntity);
if (highlightedCapacity && highlightedCapacity.value > highlightedItems.length) {
const highlightedEntityPos = getComponentValue(LocalPosition, highlightedEntity);
Expand Down

0 comments on commit 23b77af

Please sign in to comment.