Skip to content

Commit

Permalink
Merge pull request #1 from jankuss/feature-remove-floor-walls
Browse files Browse the repository at this point in the history
Add ability to hide floors & walls
  • Loading branch information
jankuss authored Dec 7, 2020
2 parents 74dbcbc + 0133194 commit a715a75
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 28 deletions.
8 changes: 3 additions & 5 deletions example/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,12 @@ PIXI.settings.SCALE_MODE = PIXI.SCALE_MODES.NEAREST;
const shroom = Shroom.create({ application, resourcePath: "./resources" });
const room = Room.create(shroom, {
tilemap: `
0
0000
0000
0000
`,
});

room.wallDepth = 0;
room.wallHeight = 32;
room.tileHeight = 0;

room.x = 100;
room.y = 200;

Expand Down
91 changes: 69 additions & 22 deletions src/objects/room/Room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,16 @@ export class Room
private hitDetection: IHitDetection;
private configuration: IConfiguration;

private walls: Wall[] = [];
private floor: (Tile | Stair)[] = [];
private _walls: Wall[] = [];
private _floor: (Tile | Stair)[] = [];
private _cursors: TileCursor[] = [];

private bounds: { minX: number; minY: number; maxX: number; maxY: number };
private _roomBounds: {
minX: number;
minY: number;
maxX: number;
maxY: number;
};

private _wallTexture: Promise<PIXI.Texture> | PIXI.Texture | undefined;
private _floorTexture: Promise<PIXI.Texture> | PIXI.Texture | undefined;
Expand All @@ -71,6 +77,9 @@ export class Room
private _currentWallTexture: PIXI.Texture | undefined;
private _currentFloorTexture: PIXI.Texture | undefined;

private _hideWalls = false;
private _hideFloor = false;

private _onTileClick: ((position: RoomPosition) => void) | undefined;

private _wallDepth: number = 8;
Expand All @@ -79,6 +88,24 @@ export class Room

private _largestDiff: number;

public get hideWalls() {
return this._hideWalls;
}

public set hideWalls(value) {
this._hideWalls = value;
this.updateTiles();
}

public get hideFloor() {
return this._hideFloor;
}

public set hideFloor(value) {
this._hideFloor = value;
this.updateTiles();
}

public get wallHeight() {
return this._wallHeight + this._largestDiff * 32;
}
Expand Down Expand Up @@ -112,26 +139,26 @@ export class Room

private _updateWallDepth() {
this.visualization.disableCache();
this.walls.forEach((wall) => {
this._walls.forEach((wall) => {
wall.wallDepth = this.wallDepth;
});
this.visualization.enableCache();
}

private _updateWallHeight() {
this.visualization.disableCache();
this.walls.forEach((wall) => {
this._walls.forEach((wall) => {
wall.wallHeight = this.wallHeight;
});
this.visualization.enableCache();
}

private _updateTileHeight() {
this.visualization.disableCache();
this.floor.forEach((floor) => {
this._floor.forEach((floor) => {
floor.tileHeight = this.tileHeight;
});
this.walls.forEach((wall) => {
this._walls.forEach((wall) => {
wall.tileHeight = this.tileHeight;
});
this.visualization.enableCache();
Expand Down Expand Up @@ -222,18 +249,18 @@ export class Room

this.visualization = new RoomVisualization();

this.bounds = getTileMapBounds(parsedTileMap, this.wallOffsets);
this.initTiles(this.parsedTileMap);
this._roomBounds = getTileMapBounds(parsedTileMap, this.wallOffsets);

this.roomWidth = this.bounds.maxX - this.bounds.minX;
this.roomHeight = this.bounds.maxY - this.bounds.minY;
this.roomWidth = this._roomBounds.maxX - this._roomBounds.minX;
this.roomHeight = this._roomBounds.maxY - this._roomBounds.minY;

this.animationTicker = animationTicker;
this.furnitureLoader = furnitureLoader;
this.avatarLoader = avatarLoader;
this.hitDetection = hitDetection;
this.configuration = configuration;

this.updateTiles();
this.addChild(this.visualization);
}

Expand All @@ -257,11 +284,11 @@ export class Room

private updateTextures() {
this.visualization.disableCache();
this.walls.forEach((wall) => {
this._walls.forEach((wall) => {
wall.texture = this._currentWallTexture;
wall.color = this._wallColor;
});
this.floor.forEach((floor) => {
this._floor.forEach((floor) => {
floor.texture = this._currentFloorTexture;
floor.color = this._floorColor;
});
Expand Down Expand Up @@ -292,8 +319,8 @@ export class Room

const base = 32;

const xPos = -this.bounds.minX + x * base - y * base;
const yPos = -this.bounds.minY + x * (base / 2) + y * (base / 2);
const xPos = -this._roomBounds.minX + x * base - y * base;
const yPos = -this._roomBounds.minY + x * (base / 2) + y * (base / 2);

return {
x: xPos,
Expand All @@ -302,24 +329,44 @@ export class Room
}

private registerWall(wall: Wall) {
this.walls.push(wall);
if (this.hideWalls || this.hideFloor) return;

this._walls.push(wall);
this.addRoomObject(wall);
}

private registerTile(tile: Stair | Tile) {
this.floor.push(tile);
if (this.hideFloor) return;

this._floor.push(tile);
this.addRoomObject(tile);
}

private registerTileCursor(position: RoomPosition) {
this.addRoomObject(
new TileCursor(position, (position) => {
this.onTileClick && this.onTileClick(position);
})
const cursor = new TileCursor(position, (position) => {
this.onTileClick && this.onTileClick(position);
});

this._cursors.push(cursor);

this.addRoomObject(cursor);
}

private resetTiles() {
[...this._floor, ...this._walls, ...this._cursors].forEach((value) =>
value.destroy()
);

this._floor = [];
this._walls = [];
this._cursors = [];
}

private initTiles(tiles: ParsedTileType[][]) {
private updateTiles() {
this.resetTiles();

const tiles = this.parsedTileMap;

for (let y = 0; y < tiles.length; y++) {
for (let x = 0; x < tiles[y].length; x++) {
const tile = tiles[y][x];
Expand Down
2 changes: 1 addition & 1 deletion src/objects/room/Tile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ export class Tile extends RoomObject implements ITexturable {
}

private destroySprites() {
this._sprites = [];
this._sprites.forEach((sprite) => sprite.destroy());
this._sprites = [];
}

private updateSprites() {
Expand Down

0 comments on commit a715a75

Please sign in to comment.