Skip to content

Commit

Permalink
fix equip action partially
Browse files Browse the repository at this point in the history
  • Loading branch information
trymnilsen committed Sep 26, 2024
1 parent 84b451c commit 418d824
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 262 deletions.
51 changes: 0 additions & 51 deletions ts/src/game/interaction/state/root/inventory/equipActions.ts

This file was deleted.

180 changes: 0 additions & 180 deletions ts/src/game/interaction/state/root/inventory/equipItemConfirmState.ts

This file was deleted.

39 changes: 23 additions & 16 deletions ts/src/game/interaction/state/root/inventory/inventoryState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ import { UIView } from "../../../../../ui/uiView.js";
import { UIFlowGrid } from "../../../../../ui/view/uiFlowGrid.js";
import { UIMasterDetails } from "../../../../../ui/view/uiMasterDetail.js";
import { OpenBookUIBackground } from "../../../../../ui/visual/bookBackground.js";
import { EquipmentComponent } from "../../../../component/inventory/equipmentComponent.js";
import { InventoryComponent2 } from "../../../../component/inventory/inventoryComponent.js";
import { Entity } from "../../../../entity/entity.js";
import { InteractionState } from "../../../handler/interactionState.js";
import { StateContext } from "../../../handler/stateContext.js";
import { UIActionbarItem } from "../../../view/actionbar/uiActionbar.js";
import { UIActionbarScaffold } from "../../../view/actionbar/uiActionbarScaffold.js";
import { AlertMessageState } from "../../common/alertMessageState.js";
import { EquipItemConfirmState } from "./equipItemConfirmState.js";
import { UIInventoryGridItem } from "./uiInventoryGridItem.js";

export class InventoryState extends InteractionState {
Expand All @@ -41,7 +41,8 @@ export class InventoryState extends InteractionState {
return true;
}

constructor(private forEntity: Entity) {
//Todo: add parameters for filtering and button providers
constructor(private forInventory: InventoryComponent2) {
super();
}

Expand Down Expand Up @@ -94,14 +95,7 @@ export class InventoryState extends InteractionState {
}

private getInventoryItemList() {
const inventoryComponent =
this.forEntity.getComponent(InventoryComponent2);

if (!inventoryComponent) {
throw new Error("No inventory component on root entity");
}

this._items = inventoryComponent.items;
this._items = this.forInventory.items;
}

private getMasterGridView(): UIView {
Expand Down Expand Up @@ -328,7 +322,7 @@ export class InventoryState extends InteractionState {
text: "Equip",
icon: sprites2.empty_sprite,
onClick: () => {
//this._equipAction.onEquip(activeItem.item, this.context);
this.onEquip(activeItem.item);
},
});

Expand All @@ -343,6 +337,24 @@ export class InventoryState extends InteractionState {
return actions;
}
}

private onEquip(item: InventoryItem) {
const equipmentComponent =
this.forInventory.entity.getComponent(EquipmentComponent);

if (!equipmentComponent) {
this.context.stateChanger.push(
new AlertMessageState("Uh oh", "Not available"),
);
return;
}

const removeResult = this.forInventory.removeInventoryItem(item.id, 1);
if (removeResult) {
equipmentComponent.mainItem.setItem(item);
this.context.stateChanger.pop();
}
}
}

export function getAssetImage(index: number): Sprite2 | null {
Expand All @@ -359,8 +371,3 @@ export function getAssetImage(index: number): Sprite2 | null {
return null;
}
}

export interface InventoryEquipAction {
onEquip(item: InventoryItem, stateContext: StateContext);
isApplicable(item: InventoryItem): boolean;
}
1 change: 0 additions & 1 deletion ts/src/game/interaction/state/root/rootState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import { UIActionbarScaffold } from "../../view/actionbar/uiActionbarScaffold.js
import { AlertMessageState } from "../common/alertMessageState.js";
import { MenuState } from "../menu/menuState.js";
import { BuildingState } from "./building/buildingState.js";
import { ConfirmEquipAction } from "./inventory/equipActions.js";
import { InventoryState } from "./inventory/inventoryState.js";

export class RootState extends InteractionState {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import { ButtonCollection } from "../../../../view/actionbar/buttonCollection.js
import { UIActionbarItem } from "../../../../view/actionbar/uiActionbar.js";
import { CharacterSkillState } from "../../../character/characterSkillState.js";
import { AlertMessageState } from "../../../common/alertMessageState.js";
import { EquipOnActorAction } from "../../../root/inventory/equipActions.js";
import { InventoryState } from "../../../root/inventory/inventoryState.js";
import { ActorMovementState } from "../actorMovementState.js";
import {
Expand Down Expand Up @@ -208,8 +207,11 @@ export class WorkerSelectionProvider implements ActorSelectionProvider {
text: "Stash",
icon: sprites2.empty_sprite,
onClick: () => {
const inventory =
selectedEntity.requireComponent(InventoryComponent2);

stateContext.stateChanger.push(
new InventoryState(selectedEntity),
new InventoryState(inventory),
);
},
},
Expand Down Expand Up @@ -239,8 +241,11 @@ export class WorkerSelectionProvider implements ActorSelectionProvider {
{
text: "Equip",
onClick: () => {
const inventory =
selectedEntity.requireComponent(InventoryComponent2);

stateContext.stateChanger.push(
new InventoryState(selectedEntity),
new InventoryState(inventory),
);
},
icon: sprites2.empty_sprite,
Expand All @@ -257,8 +262,11 @@ export class WorkerSelectionProvider implements ActorSelectionProvider {
{
text: "Equip",
onClick: () => {
const inventory =
selectedEntity.requireComponent(InventoryComponent2);

stateContext.stateChanger.push(
new InventoryState(selectedEntity),
new InventoryState(inventory),
);
},
icon: sprites2.empty_sprite,
Expand Down
12 changes: 2 additions & 10 deletions ts/test/game/component/inventory/equipmentComponent.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
import * as assert from "node:assert";
import { describe, it } from "node:test";
import { EnergyComponent } from "../../../../src/game/component/energy/energyComponent.js";
import { EquipmentComponent } from "../../../../src/game/component/inventory/equipmentComponent.js";

describe("EquipmentComponent", () => {
it("Can set inventory item", () => {
assert.equal(1, 1);
});

it("Will return item to inventory on existing item equiped", () => {
assert.equal(1, 1);
});
});
describe("EquipmentComponent", () => {});

0 comments on commit 418d824

Please sign in to comment.