Skip to content
This repository has been archived by the owner on Jun 5, 2024. It is now read-only.

Commit

Permalink
Callbacks for Inventory Listening
Browse files Browse the repository at this point in the history
  • Loading branch information
Stuyk committed Apr 4, 2023
1 parent 36098eb commit 46a95d3
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@
--- Available as a prop, or from the "import * as state from '@utility/state';" export
- Athena.player.inventory.getAt - Fixed Type Definition
- Athena.player.toolbar.getAt - Fixed Type Definition
- AthenaClient.systems.inventory.get
- AthenaClient.systems.inventory.get.toolbar
- AthenaClient.systems.inventory.get.inventory
- AthenaClient.systems.inventory.get.totalWeight
- AthenaClient.systems.inventory.get.onInventoryChange
- AthenaClient.systems.inventory.get.onToolbarChange
- AthenaClient.systems.inventory.get.onWeightChange
--------------------------------------
--- Everything Below is Before April 2
Expand Down
1 change: 1 addition & 0 deletions src/core/client/systems/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * as animations from './animations';
export * as entitySelector from './entitySelector';
export * as hotkeys from './hotkeyRegistry';
export * as interaction from './interaction';
export * as inventory from './inventory';
export * as messenger from './messenger';
export * as playerConfig from './playerConfig';
export * as sound from './sound';
Expand Down
101 changes: 101 additions & 0 deletions src/core/client/systems/inventory/get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { onInventoryUpdate } from '@AthenaClient/events/onInventoryUpdate';
import { Item } from '@AthenaShared/interfaces/item';

export type ItemChangeCallback = (items: Array<Item>) => void;
export type WeightChangeCallback = (weight: number) => void;

const toolbarChangeCbs: Array<ItemChangeCallback> = [];
const inventoryChangeCbs: Array<ItemChangeCallback> = [];
const weightChangeCbs: Array<WeightChangeCallback> = [];

let currentInventory: Array<Item> = [];
let currentToolbar: Array<Item> = [];
let currentTotalWeight: number;

/**
* A list of the current items in the toolbar.
*
* @export
* @return {Array<Item>}
*/
export function toolbar(): Array<Item> {
return currentToolbar;
}

/**
* A list of the current items in the inventory.
*
* @export
* @return {Array<Item>}
*/
export function inventory(): Array<Item> {
return currentInventory;
}

/**
* A list of the current total weight of inventory and toolbar.
*
* @export
* @return {number}
*/
export function totalWeight(): number {
return currentTotalWeight;
}

/**
* Invoke the callback whenever inventory changes.
*
* @export
* @param {ItemChangeCallback} callback
*/
export function onInventoryChange(callback: ItemChangeCallback) {
inventoryChangeCbs.push(callback);
}

/**
* Invoke the callback whenever the toolbar changes.
*
* @export
* @param {ItemChangeCallback} callback
*/
export function onToolbarChange(callback: ItemChangeCallback) {
toolbarChangeCbs.push(callback);
}

/**
* Invoke the weight change whenever the total weight changes.
*
* @export
* @param {WeightChangeCallback} callback
*/
export function onTotalWeightChange(callback: WeightChangeCallback) {
weightChangeCbs.push(callback);
}

onInventoryUpdate.add((inventoryNew, toolbarNew, totalWeightNew) => {
const oldInventory = JSON.stringify(currentInventory);
const oldToolbar = JSON.stringify(currentToolbar);
const oldWeight = currentTotalWeight;

currentInventory = inventoryNew;
currentToolbar = toolbarNew;
currentTotalWeight = totalWeightNew;

if (oldInventory !== JSON.stringify(currentInventory)) {
for (let cb of inventoryChangeCbs) {
cb(currentInventory);
}
}

if (oldToolbar !== JSON.stringify(currentToolbar)) {
for (let cb of toolbarChangeCbs) {
cb(currentToolbar);
}
}

if (oldWeight !== currentTotalWeight) {
for (let cb of weightChangeCbs) {
cb(currentTotalWeight);
}
}
});
1 change: 1 addition & 0 deletions src/core/client/systems/inventory/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * as get from './get';

0 comments on commit 46a95d3

Please sign in to comment.