Skip to content

Commit

Permalink
Fix differentiation of wall and floor items
Browse files Browse the repository at this point in the history
  • Loading branch information
jankuss committed Dec 19, 2020
1 parent 6a6adc2 commit ce3c4e0
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 18 deletions.
5 changes: 4 additions & 1 deletion src/interfaces/IFurnitureData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { furnitureDataTransformers } from "../util/furnitureDataTransformers";
export interface IFurnitureData {
getRevisionForType(type: string): Promise<number | undefined>;
getInfo(type: string): Promise<FurnitureInfo | undefined>;
getTypeById(id: FurnitureId): Promise<string | undefined>;
getTypeById(
id: FurnitureId,
type: "wall" | "floor"
): Promise<string | undefined>;
getInfoForFurniture(
furniture: IFurniture
): Promise<FurnitureInfo | undefined>;
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/IFurnitureLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ export interface IFurnitureLoader {
}

export type FurnitureFetch =
| { kind: "id"; id: FurnitureId }
| { kind: "id"; id: FurnitureId; placementType: "wall" | "floor" }
| { kind: "type"; type: string };
43 changes: 33 additions & 10 deletions src/objects/FurnitureData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ type IdToTypeMap = {
};

export class FurnitureData implements IFurnitureData {
private data: Promise<{ typeToInfo: FurnitureMap; idToType: IdToTypeMap }>;
private data: Promise<{
typeToInfo: FurnitureMap;
floorIdToType: IdToTypeMap;
wallIdToType: IdToTypeMap;
}>;

constructor(private getFurniData: () => Promise<string>) {
this.data = this.prepareData();
Expand All @@ -27,22 +31,34 @@ export class FurnitureData implements IFurnitureData {
const parsed = await parseStringPromise(furniDataString);

const typeToInfo: FurnitureMap = {};
const idToType: IdToTypeMap = {};
const floorIdToType: IdToTypeMap = {};
const wallIdToType: IdToTypeMap = {};

const register = (data: any[]) => {
const register = (data: any[], furnitureType: "floor" | "wall") => {
data.forEach((element) => {
const type = element.$.classname;
const id = element.$.id;

typeToInfo[type] = formatFurnitureData(element);
idToType[id] = type;

if (furnitureType === "floor") {
if (floorIdToType[id] != null)
throw new Error(`Floor furniture with id ${id} already exists`);

floorIdToType[id] = type;
} else if (furnitureType === "wall") {
if (wallIdToType[id] != null)
throw new Error(`Wall furniture with id ${id} already exists`);

wallIdToType[id] = type;
}
});
};

register(parsed.furnidata.roomitemtypes[0].furnitype);
register(parsed.furnidata.wallitemtypes[0].furnitype);
register(parsed.furnidata.roomitemtypes[0].furnitype, "wall");
register(parsed.furnidata.wallitemtypes[0].furnitype, "floor");

return { typeToInfo, idToType };
return { typeToInfo, floorIdToType, wallIdToType };
}

static create(resourcePath: string = "") {
Expand All @@ -62,9 +78,13 @@ export class FurnitureData implements IFurnitureData {
return data.typeToInfo[type];
}

async getTypeById(id: FurnitureId): Promise<string | undefined> {
async getTypeById(
id: FurnitureId,
placementType: "wall" | "floor"
): Promise<string | undefined> {
const data = await this.data;
const type = data.idToType[id];
const type =
placementType != "floor" ? data.floorIdToType[id] : data.wallIdToType[id];

if (type == null) return;

Expand All @@ -73,7 +93,10 @@ export class FurnitureData implements IFurnitureData {

async getInfoForFurniture(furniture: IFurniture) {
if (furniture.id != null) {
const type = await this.getTypeById(furniture.id);
const type = await this.getTypeById(
furniture.id,
furniture.placementType
);

if (type != null) {
return this.getInfo(type);
Expand Down
4 changes: 3 additions & 1 deletion src/objects/furniture/FloorFurniture.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export class FloorFurniture
private _animation?: string;
private _highlight: boolean = false;

public readonly placementType = "wall";

private _onClick: HitEventHandler | undefined;
private _onDoubleClick: HitEventHandler | undefined;

Expand Down Expand Up @@ -96,7 +98,7 @@ export class FloorFurniture
}

this._baseFurniture = new BaseFurniture(
getFurnitureFetch(options),
getFurnitureFetch(options, "floor"),
options.direction,
options.animation
);
Expand Down
5 changes: 4 additions & 1 deletion src/objects/furniture/FurnitureLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ export class FurnitureLoader implements IFurnitureLoader {
let typeWithColor: string;

if (fetch.kind === "id") {
const type = await this.options.furnitureData.getTypeById(fetch.id);
const type = await this.options.furnitureData.getTypeById(
fetch.id,
fetch.placementType
);
if (type == null)
throw new Error("Couldn't determine type for furniture.");

Expand Down
1 change: 1 addition & 0 deletions src/objects/furniture/IFurniture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface IFurniture extends IFurnitureEventHandlers {
direction: number;
animation: string | undefined;
highlight: boolean | undefined;
placementType: "wall" | "floor";
}

export type IFurnitureBehavior<T extends IFurniture = IFurniture> = {
Expand Down
4 changes: 3 additions & 1 deletion src/objects/furniture/WallFurniture.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export class WallFurniture extends RoomObject implements IFurniture {
private _direction: number;
private _highlight: boolean = false;

public readonly placementType = "wall";

constructor(
options: {
roomX: number;
Expand All @@ -43,7 +45,7 @@ export class WallFurniture extends RoomObject implements IFurniture {
this._direction = options.direction;

this._baseFurniture = new BaseFurniture(
getFurnitureFetch(options),
getFurnitureFetch(options, "wall"),
options.direction,
options.animation,
(direction) => getMaskId(direction, this.roomX, this.roomY)
Expand Down
7 changes: 5 additions & 2 deletions src/objects/furniture/util/getFurnitureFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ import { FurnitureId } from "../../../interfaces/IFurnitureData";
import { FurnitureFetch } from "../../../interfaces/IFurnitureLoader";
import { FurnitureFetchInfo } from "../FurnitureFetchInfo";

export function getFurnitureFetch(data: FurnitureFetchInfo): FurnitureFetch {
export function getFurnitureFetch(
data: FurnitureFetchInfo,
placementType: "wall" | "floor"
): FurnitureFetch {
if (data.id != null && data.type != null)
throw new Error(
"Both `id` and `type` specified. Please supply only one of the two."
);

if (data.id != null) {
return { kind: "id", id: data.id };
return { kind: "id", id: data.id, placementType };
}

if (data.type != null) {
Expand Down
43 changes: 42 additions & 1 deletion storybook/stories/Furniture.stories.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FloorFurniture, Room } from "@jankuss/shroom";
import { FloorFurniture, Room, WallFurniture } from "@jankuss/shroom";
import { createShroom } from "./common/createShroom";
import { action } from "@storybook/addon-actions";

Expand Down Expand Up @@ -216,3 +216,44 @@ export function MultipleFurnitures() {
application.stage.addChild(room);
});
}

export function DifferentFetchTypes() {
return createShroom(({ application, shroom }) => {
const room = Room.create(shroom, {
tilemap: `
xxxxxxxxxxx
x0000000000
x0000000000
x0000000000
x0000000000
x0000000000
x0000000000
x0000000000
x0000000000
`,
});

room.x = application.screen.width / 2 - room.roomWidth / 2;
room.y = application.screen.height / 2 - room.roomHeight / 2;

const wallFurniture = new WallFurniture({
id: 4054,
direction: 2,
roomX: 1,
roomY: 1,
roomZ: 0,
});
const floorFurniture = new FloorFurniture({
id: 4054,
direction: 2,
roomX: 1,
roomY: 1,
roomZ: 0,
});

room.addRoomObject(wallFurniture);
room.addRoomObject(floorFurniture);

application.stage.addChild(room);
});
}

0 comments on commit ce3c4e0

Please sign in to comment.