diff options
Diffstat (limited to 'client/src/renderer.ts')
-rw-r--r-- | client/src/renderer.ts | 75 |
1 files changed, 68 insertions, 7 deletions
diff --git a/client/src/renderer.ts b/client/src/renderer.ts index c7bbbc2..8482ca6 100644 --- a/client/src/renderer.ts +++ b/client/src/renderer.ts @@ -1,7 +1,5 @@ import { getMap } from "./map.js"; -import { Items, Players, Rotation, ItemType, Map, Wall, GameState } from "./types.js"; - -const ATLAS_TILE_WIDTH = 32 +import { Items, Players, Rotation, ItemType, Map, Wall, GameState, Tile, ATLAS_TILE_WIDTH } from "./types.js"; const update_style = (width: number, height: number) => { @@ -257,20 +255,82 @@ const draw_map_canvas = ( } +const draw_debug_sprites = ( + ctx: CanvasRenderingContext2D, + atlas: CanvasImageSource, + map: Map +) => { + + for (let y = 0; y < map.height; y++) { + for (let x = 0; x < map.width; x++) { + + let tile_type = map.data[y * map.width + x] + + + let atlas_index: [number, number]; + switch (tile_type) { + case Tile.EMPTY: + case Tile.WALL: + continue + case Tile.GHOST_WALL: + atlas_index = [4, 0] + break + case Tile.FOOD: + atlas_index = [3, 0] + break + case Tile.PLAYER_SPAWN_1: + atlas_index = [3, 1] + break + case Tile.PLAYER_SPAWN_2: + atlas_index = [4, 1] + break + case Tile.PLAYER_SPAWN_3: + atlas_index = [3, 2] + break + case Tile.PLAYER_SPAWN_4: + atlas_index = [4, 2] + break + case Tile.THICC_DOT: + atlas_index = [4, 3] + break + case Tile.INITIAL_DOT: + atlas_index = [3, 3] + break + } + + draw_sprite ( + ctx, + x, + y, + 1, + atlas, + atlas_index, + ATLAS_TILE_WIDTH, + 0 + ) + + } + } +} + let map_canvas = document.createElement("canvas") const draw_map = ( ctx: CanvasRenderingContext2D, atlas: CanvasImageSource, map: Map, - last: number | undefined + last: number | undefined, + editor: boolean ) => { - if (map.id !== last) { + if (map.id !== last || editor) { map_canvas.width = map.width * ATLAS_TILE_WIDTH map_canvas.height = map.height * ATLAS_TILE_WIDTH let map_ctx = map_canvas.getContext("2d") draw_map_canvas(map_ctx, atlas, map) + if (editor) { + draw_debug_sprites(map_ctx, atlas, map) + } } ctx.drawImage ( @@ -292,7 +352,8 @@ export const startGraphicsUpdater = () => { */ return ( data: GameState, - frame: number + frame: number, + editor: boolean = false ) => { let map = getMap(data.mapId) @@ -308,7 +369,7 @@ export const startGraphicsUpdater = () => { let ctx = canvas.getContext("2d") ctx.clearRect(0, 0, canvas.width, canvas.height) - draw_map(ctx, atlas, map, last_map_drawn) + draw_map(ctx, atlas, map, last_map_drawn, editor) draw_items(ctx, atlas, data.items) draw_players(ctx, atlas, data.players, frame) update_style(map.width, map.height) |