Skip to content

Commit

Permalink
Refactor isometric grid rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
gugiserman committed Nov 10, 2021
1 parent acd8ec0 commit 5899339
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
6 changes: 2 additions & 4 deletions demo/isometricLevel.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ kaboom()
loadSprite("grass", "/sprites/iso_grass.png")

gravity(0)
camScale(0.4)
camPos(-400, 200)

const level = addIsometricLevel([
// Design the level layout with symbols
Expand All @@ -21,10 +23,6 @@ const level = addIsometricLevel([
"@@@@@@@@@",
"@@@@@@@@@@",
"@@@@@@@@@@@",
"@@@@@@@@@@@@",
"@@@@@@@@@@@@@",
"@@@@@@@@@@@@",
"@@@@@@@@@@@",
"@@@@@@@@@@",
"@@@@@@@@@",
"@@@@@@@@",
Expand Down
23 changes: 13 additions & 10 deletions src/kaboom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2508,8 +2508,8 @@ function addIsometricLevel(map: string[], options: IsometricLevelOpt): Isometric
const objects: GameObj[] = [];
const offset = vec2(options.pos || vec2(0));

const halfTileWidth = options.tileWidth / 2;
const halfTileHeight = options.tileHeight / 2;
const halfTileWidth = Math.floor(options.tileWidth / 2);
const halfTileHeight = Math.floor(options.tileHeight / 2);

const maxWidthInTiles = map.reduce((width, row): number => Math.max(width, row.length), 0)
const heightInTiles = map.length;
Expand All @@ -2527,7 +2527,7 @@ function addIsometricLevel(map: string[], options: IsometricLevelOpt): Isometric
return options.tileHeight;
},

fromIsometricCoordinatesToScreenPos: (row: number, col: number): Vec2 => {
fromIsometricGridCoordsToScreenPos: (row: number, col: number): Vec2 => {
return vec2((col - row) * halfTileWidth, (col + row) * halfTileHeight);
},

Expand Down Expand Up @@ -2575,17 +2575,20 @@ function addIsometricLevel(map: string[], options: IsometricLevelOpt): Isometric
},
};

// const hitbox = [
// new PIXI.Point(0, -tile.halfHeight),
// new PIXI.Point(tile.width - tile.halfWidth, 0),
// new PIXI.Point(0, tile.halfHeight),
// new PIXI.Point(-tile.halfWidth, 0),
// ]

for (let row = 0; row < heightInTiles; row++) {
for (let col = 0; col < maxWidthInTiles; col++) {
const position = isometricLevel.fromIsometricCoordinatesToScreenPos(row, col);
const index = isometricLevel.from2dTo1dIndex(row, col);
const symbols: string = map[index] || '';
const position = isometricLevel.fromIsometricGridCoordsToScreenPos(row, col);
const rowContent: string = map[row]
const symbols: string[] = rowContent.split("");
const symbol = symbols[col]

if (!symbol) {
break
}

isometricLevel.spawn(position, symbol);
}
}
Expand Down

0 comments on commit 5899339

Please sign in to comment.