Skip to content
This repository was archived by the owner on Nov 12, 2024. It is now read-only.

Commit ec02286

Browse files
committed
Improve isometric level support and add more demo examples
1 parent 9ae7145 commit ec02286

File tree

4 files changed

+74
-45
lines changed

4 files changed

+74
-45
lines changed
2.38 KB
Loading

demo/isometricLevel.js

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,65 @@ kaboom()
55

66
// Load assets
77
loadSprite('grass', '/sprites/iso_grass.png')
8+
loadSprite('darker_grass', '/sprites/iso_grass_darker.png')
89

910
gravity(0)
10-
camScale(0.4)
11-
camPos(vec2(0, 400))
12-
13-
const GRID_LENGTH = 15
11+
camScale(0.33)
12+
camPos(vec2(100, 1000))
1413

1514
const level = addIsometricLevel([
16-
// Design the level layout with symbols
17-
...Array.from({ length: GRID_LENGTH }).map(() =>
18-
Array.from({ length: GRID_LENGTH }).map(() => '@').join('')
19-
)
15+
// Design the isometric level layout with symbols
16+
// 15x15 grid in this case so we have a perfect tiled square diamond shaped map
17+
'@@@@@@@@@@@@@@@',
18+
'@@@@@@@@@@@@@@@',
19+
'@@@@@@@@@@@@@@@',
20+
'@@@@!@@@@!@@@@@',
21+
'@@@@@@@@@@@@@@@',
22+
'@@@@@@@@@@@@@@@',
23+
'@@@@@!@@@!@@@@@',
24+
'@@@@@@!!!@@@@@@',
25+
'@@@@@@@@@@@@@@@',
26+
'@@@@@@@@@@@@@@@',
27+
'@@@@@@@@@@@@@@@',
28+
'@@@@@@@@@@@@@@@',
29+
'@@@@@@@@@@@@@@@',
30+
'@@@@@@@@@@@@@@@',
31+
'@@@@@@@@@@@@@@@',
2032
], {
2133
// The size of each grid
2234
tileWidth: 144,
2335
tileHeight: 71,
24-
// The position of the top left block
25-
// pos: vec2(100, 200),
2636
// Define what each symbol means (in components)
2737
'@': () => [
2838
sprite('grass'),
2939
],
40+
'!': () => [
41+
sprite('darker_grass'),
42+
],
43+
})
44+
45+
const level2 = addIsometricLevel([
46+
// 15x9 grid
47+
'@@@@@@@@@@@@@@@',
48+
'@@@@@@@@@@@@@@@',
49+
'@@@@@@@!@@@@@@@',
50+
'@@@@@@@!@@@@@@@',
51+
'@@@@@@@!@@@@@@@',
52+
'@@@@@@@!@@@@@@@',
53+
'@@@@@@@!@@@@@@@',
54+
'@@@@@@@@@@@@@@@',
55+
'@@@@@@@!@@@@@@@',
56+
'@@@@@@@@@@@@@@@',
57+
'@@@@@@@@@@@@@@@',
58+
], {
59+
tileWidth: 144,
60+
tileHeight: 71,
61+
'@': () => [
62+
sprite('grass'),
63+
],
64+
'!': () => [
65+
sprite('darker_grass'),
66+
],
67+
// The position of the top left block
68+
pos: vec2(0, 1200),
3069
})

src/kaboom.ts

Lines changed: 20 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2359,31 +2359,7 @@ function isometricGrid(isometricLevel: IsometricLevel, position: Vec2) {
23592359

23602360
id: "isometricGrid",
23612361
gridPos: position.clone(),
2362-
2363-
setGridPos(...args) {
2364-
const p = vec2(...args);
2365-
this.gridPos = p.clone();
2366-
this.pos = vec2(
2367-
isometricLevel.offset().x + this.gridPos.x * isometricLevel.gridWidth(),
2368-
isometricLevel.offset().y + this.gridPos.y * isometricLevel.gridHeight()
2369-
);
2370-
},
2371-
2372-
moveLeft() {
2373-
this.setGridPos(this.gridPos.add(vec2(-1, 0)));
2374-
},
2375-
2376-
moveRight() {
2377-
this.setGridPos(this.gridPos.add(vec2(1, 0)));
2378-
},
2379-
2380-
moveUp() {
2381-
this.setGridPos(this.gridPos.add(vec2(0, -1)));
2382-
},
2383-
2384-
moveDown() {
2385-
this.setGridPos(this.gridPos.add(vec2(0, 1)));
2386-
},
2362+
level: isometricLevel,
23872363

23882364
};
23892365

@@ -2497,10 +2473,6 @@ function addLevel(map: string[], opt: LevelOpt): Level {
24972473
}
24982474

24992475
function addIsometricLevel(map: string[], options: IsometricLevelOpt): IsometricLevel {
2500-
if (map.length % 2 === 0) {
2501-
throw new Error("Must provide isometric level map array of odd length.")
2502-
}
2503-
25042476
if (!options.tileWidth || !options.tileHeight) {
25052477
throw new Error("Must provide isometric level tile width & height.");
25062478
}
@@ -2527,7 +2499,7 @@ function addIsometricLevel(map: string[], options: IsometricLevelOpt): Isometric
25272499
return options.tileHeight;
25282500
},
25292501

2530-
fromIsometricGridCoordsToScreenPos: (row: number, col: number): Vec2 => {
2502+
fromIsometricCoordsToScreenPos: (row: number, col: number): Vec2 => {
25312503
return vec2((col - row) * halfTileWidth, (col + row) * halfTileHeight);
25322504
},
25332505

@@ -2552,8 +2524,8 @@ function addIsometricLevel(map: string[], options: IsometricLevelOpt): Isometric
25522524
}
25532525

25542526
const posComp = vec2(
2555-
offset.x + position.x * options.tileWidth,
2556-
offset.y + position.y * options.tileHeight
2527+
offset.x + position.x,
2528+
offset.y + position.y,
25572529
);
25582530

25592531
for (const comp of comps) {
@@ -2565,20 +2537,33 @@ function addIsometricLevel(map: string[], options: IsometricLevelOpt): Isometric
25652537
}
25662538
}
25672539

2568-
comps.push(origin(vec2(0.5, 0.5)));
2569-
comps.push(pos(position));
2540+
comps.push(pos(posComp));
25702541
comps.push(isometricGrid(this, position));
25712542

25722543
const object = add(comps);
25732544
objects.push(object);
25742545

25752546
return object;
25762547
},
2548+
2549+
width() {
2550+
return maxWidthInTiles * options.tileWidth;
2551+
},
2552+
2553+
height() {
2554+
return heightInTiles * options.tileHeight;
2555+
},
2556+
2557+
destroy() {
2558+
for (const someObject of objects) {
2559+
destroy(someObject);
2560+
}
2561+
},
25772562
};
25782563

25792564
for (let row = 0; row < heightInTiles; row++) {
25802565
for (let col = 0; col < maxWidthInTiles; col++) {
2581-
const position = isometricLevel.fromIsometricGridCoordsToScreenPos(row, col);
2566+
const position = isometricLevel.fromIsometricCoordsToScreenPos(row, col);
25822567
const rowContent: string = map[row]
25832568
const symbols: string[] = rowContent.split("");
25842569
const symbol = symbols[col]

src/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3845,4 +3845,9 @@ export interface IsometricLevel {
38453845
gridWidth(): number,
38463846
gridHeight(): number,
38473847
offset(): Vec2,
3848+
fromIsometricCoordsToScreenPos(row: number, col: number): Vec2,
3849+
from2dTo1dIndex(row: number, col: number): number,
3850+
width(): number,
3851+
height(): number,
3852+
destroy(): void,
38483853
}

0 commit comments

Comments
 (0)