Skip to content

Commit 28bb3d1

Browse files
authored
Merge pull request #12 from QADRAX/fix/TrasposeTilemap
Fix/traspose tilemap
2 parents 08ce31f + b9cfbda commit 28bb3d1

6 files changed

Lines changed: 37 additions & 32 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "react-super-tilemap",
33
"license": "MIT",
4-
"version": "0.0.2",
4+
"version": "1.0.0",
55
"private": false,
66
"description": "React implementation of a low-level 2D tilemap board optimized for high-performance rendering in web browsers",
77
"author": "Carlos Cuadra",

src/__stories__/demos/sprites/DemoSpriteOffset.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,16 @@ const sprites: SpriteDefinition[] = [
4242
height: 2,
4343
},
4444
offset: {
45-
col: -0.5,
46-
row: 0.5,
45+
col: 0.5,
46+
row: -0.5,
4747
},
4848
},
4949
];
5050

5151
const scheme: string[][][] = [
5252
[['grass'], ['grass'], ['grass']],
53-
[['grass'], ['grass', 'building', 'selector'], ['ocean']],
54-
[['grass'], ['ocean'], ['ocean']],
53+
[['grass'], ['grass', 'building'], ['ocean']],
54+
[['grass', 'selector'], ['ocean'], ['ocean']],
5555
];
5656

5757
export const DemoSpriteOffset = () => {

src/render/Render.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ export function renderTileMap({
6969
if (schema && spriteMap) {
7070
// Calculate first and last visible columns and rows
7171

72-
const firstVisibleCol = Math.floor(-cameraPosition.x / tileSizePx);
73-
const lastVisibleCol = Math.ceil((canvasWidthPx - cameraPosition.x) / tileSizePx);
72+
const firstVisibleCol = Math.floor(-cameraPosition.y / tileSizePx);
73+
const lastVisibleCol = Math.ceil((canvasHeightPx - cameraPosition.y) / tileSizePx);
7474

75-
const firstVisibleRow = Math.floor(-cameraPosition.y / tileSizePx);
76-
const lastVisibleRow = Math.ceil((canvasHeightPx - cameraPosition.y) / tileSizePx);
75+
const firstVisibleRow = Math.floor(-cameraPosition.x / tileSizePx);
76+
const lastVisibleRow = Math.ceil((canvasWidthPx- cameraPosition.x) / tileSizePx);
7777

7878
// Calculate the max number of layers
7979

@@ -102,8 +102,8 @@ export function renderTileMap({
102102
continue;
103103
}
104104

105-
const x = c * tileSizePx + cameraPosition.x;
106-
const y = r * tileSizePx + cameraPosition.y;
105+
const x = r * tileSizePx + cameraPosition.x;
106+
const y = c * tileSizePx + cameraPosition.y;
107107

108108
const spriteKey = layers[l];
109109
if (spriteKey) {
@@ -113,8 +113,8 @@ export function renderTileMap({
113113
const spriteSize = sprite.size;
114114
const spriteWidthPx = spriteSize.width * tileSizePx;
115115
const spriteHeightPx = spriteSize.height * tileSizePx;
116-
const spriteX = x + tileSizePx * sprite.offset.col;
117-
const spriteY = y + tileSizePx * sprite.offset.row;
116+
const spriteX = x + tileSizePx * sprite.offset.row;
117+
const spriteY = y + tileSizePx * sprite.offset.col;
118118

119119
const frame = sprite.getFrame(timestamp);
120120
bufferCtx.drawImage(
@@ -151,9 +151,9 @@ export function renderTileMap({
151151
const spriteWidthPx = spriteSize.width * tileSizePx;
152152
const spriteHeightPx = spriteSize.height * tileSizePx;
153153
const spriteX =
154-
element.tilePosition.col * tileSizePx + cameraPosition.x + tileSizePx * sprite.offset.col;
154+
element.tilePosition.row * tileSizePx + cameraPosition.x + tileSizePx * sprite.offset.row;
155155
const spriteY =
156-
element.tilePosition.row * tileSizePx + cameraPosition.y + tileSizePx * sprite.offset.row;
156+
element.tilePosition.col * tileSizePx + cameraPosition.y + tileSizePx * sprite.offset.col;
157157

158158
const frame = sprite.getFrame(timestamp);
159159
bufferCtx.drawImage(

src/types/SpriteDefinition.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ import { TilePosition } from './TilePosition';
1010
*
1111
* The rest of the images will be used for animation.
1212
*
13-
* If there is only one image, the animation will be disabled.
13+
* If there is only one image, the animation will not be disabled.
1414
*
1515
* The images must have the same size.
16+
*
17+
* You can define the imageSrc as a base64.
1618
*
1719
* @example
1820
*
@@ -22,6 +24,7 @@ import { TilePosition } from './TilePosition';
2224
* "https://example.com/image1.png",
2325
* "https://example.com/image2.png",
2426
* "https://example.com/image3.png",
27+
* "data:image/png;base64,iVBORw0K...",
2528
* ],
2629
* animationDelay: 1000,
2730
* tileSize: { width: 1, height: 1 },
@@ -41,14 +44,16 @@ export type SpriteDefinition = {
4144
*
4245
* The rest of the images will be used for animation.
4346
*
44-
* If there is only one image, the animation will be disabled
47+
* If there is only one image, the animation will not be disabled.
48+
*
4549
* The images must have the same size.
4650
*
4751
* @example
4852
* imagesSrc: [
4953
* "https://example.com/image1.png",
5054
* "https://example.com/image2.png",
5155
* "https://example.com/image3.png",
56+
* "data:image/png;base64,iVBORw0K...",
5257
* ],
5358
*/
5459
imagesSrc: string[];
@@ -57,7 +62,7 @@ export type SpriteDefinition = {
5762
*
5863
* If not provided, the default value will be used.
5964
*
60-
* If there is only one image, the animation will be disabled.
65+
* If there is only one image, the animation will not be disabled.
6166
*
6267
* The delay must be greater than 0.
6368
*

src/utils/positions.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ export function getTilePosition(
1818
};
1919

2020
const tilePosition: TilePosition = {
21-
col: relativeMousePosition.x / tileSize,
22-
row: relativeMousePosition.y / tileSize,
21+
col: relativeMousePosition.y / tileSize,
22+
row: relativeMousePosition.x / tileSize,
2323
};
2424

2525
return tilePosition;
@@ -31,8 +31,8 @@ export function getAbsolutePosition(
3131
tileSize: number
3232
): Position {
3333
const relativeTilePosition: Position = {
34-
x: tilePosition.col * tileSize,
35-
y: tilePosition.row * tileSize,
34+
x: tilePosition.row * tileSize,
35+
y: tilePosition.col * tileSize,
3636
};
3737

3838
const mousePosition: Position = {
@@ -100,8 +100,8 @@ export function getCameraPositionByTilePosition(
100100
canvasSize: Size
101101
): Position {
102102
const relativeTilePosition: Position = {
103-
x: tilePosition.col * tileSize + tileSize / 2,
104-
y: tilePosition.row * tileSize + tileSize / 2,
103+
x: tilePosition.row * tileSize + tileSize / 2,
104+
y: tilePosition.col * tileSize + tileSize / 2,
105105
};
106106

107107
const relativeCenterPosition: Position = {
@@ -118,14 +118,14 @@ export function getCameraPositionByTilePosition(
118118
}
119119

120120
export function getDistance(p1: TilePosition, p2: TilePosition): number {
121-
const dx = p1.col - p2.col;
122-
const dy = p1.row - p2.row;
121+
const dx = p1.row - p2.row;
122+
const dy = p1.col - p2.col;
123123
return Math.sqrt(dx * dx + dy * dy);
124124
}
125125

126126
export function getDirection(p1: TilePosition, p2: TilePosition): Direction {
127-
const dx = p1.col - p2.col;
128-
const dy = p1.row - p2.row;
127+
const dx = p1.row - p2.row;
128+
const dy = p1.col - p2.col;
129129

130130
if (Math.abs(dx) > Math.abs(dy)) {
131131
return dx > 0 ? 'west' : 'east';

src/utils/sizes.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ export function getTileSize(zoom: number, tileSize: number = DEFAULT_TILE_SIZE):
77
}
88

99
export function getMapSize(mapDimensions: MapDimensions, tileSize: number): Size {
10-
const width = mapDimensions.cols * tileSize;
11-
const height = mapDimensions.rows * tileSize;
10+
const width = mapDimensions.rows * tileSize;
11+
const height = mapDimensions.cols * tileSize;
1212

1313
return {
1414
width,
@@ -23,8 +23,8 @@ export function getMapDimensions(schema?: string[][][]): MapDimensions {
2323
rows: 0,
2424
};
2525
}
26-
const cols = schema.length;
27-
const rows = schema[0]?.length ?? 0;
26+
const rows = schema.length;
27+
const cols = schema[0]?.length ?? 0;
2828

2929
const dims: MapDimensions = {
3030
cols,

0 commit comments

Comments
 (0)