Change tilemap to dimension and simple generation

This commit is contained in:
2026-03-06 15:46:42 -03:00
parent 0f3f8de735
commit e5d2a3e7e2
20 changed files with 137 additions and 186 deletions
+13
View File
@@ -26,3 +26,16 @@ export function screen_to_world(
y: (screen_y - screen_height / 2) / camera.zoom + camera.y,
};
}
export function world_to_screen(
world_x: number,
world_y: number,
camera: Camera,
screen_width: number,
screen_height: number,
) {
return {
x: (world_x - camera.x) / camera.zoom + screen_width / 2,
y: (world_y - camera.y) / camera.zoom + screen_height / 2,
};
}
+20
View File
@@ -0,0 +1,20 @@
import { Component } from "$/common/ecs/mod.ts";
import { AssetManager } from "../assets.ts";
interface Tile {
x: number;
y: number;
id: string;
data?: Record<string, unknown> | undefined;
}
export class Dimension extends Component {
image: HTMLImageElement;
tiles: Tile[];
constructor(tiles: Tile[]) {
super();
this.image = AssetManager.instance.get("bworld:textures");
this.tiles = tiles;
}
}
-37
View File
@@ -1,37 +0,0 @@
import { Component } from "$/common/ecs/mod.ts";
interface Tile {
x: number;
y: number;
index: number;
}
export class Tilemap extends Component {
image: HTMLImageElement;
tile_size: number;
rows: number;
columns: number;
tiles: Tile[];
scale: number;
margin: number;
selected_tile = 0;
editing = false;
constructor(
image: HTMLImageElement,
tile_size: number,
tiles: Tile[],
scale: number = 1,
margin: number = 0,
) {
super();
this.image = image;
this.tile_size = tile_size;
this.columns = Math.floor((image.width + margin) / (tile_size + margin));
this.rows = Math.floor((image.height + margin) / (tile_size + margin));
this.tiles = tiles;
this.scale = scale;
this.margin = margin;
}
}