aboutsummaryrefslogtreecommitdiff
path: root/test-client/map/tiles.ts
blob: 4950b66cc7db6563d9b4773428b52b8bc4a5bdf1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
    Hurry Curry! - a game about cooking
    Copyright (C) 2026 Hurry Curry! Contributors

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, version 3 of the License only.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.

*/

import { data } from "../main.ts";
import { PacketC, TileIndex } from "../protocol.ts";
import { draw_tile_sprite, TileName } from "./sprites.ts";
import { System } from "../system.ts";
import { MapLayer } from "./mod.ts";
import { ItemData } from "./item.ts";
import { V2 } from "../util.ts";

export interface ItemSlot {
    item?: ItemData,
}
export interface TileData extends ItemSlot {
    x: number, y: number // tile position
    position: V2, // center position
    parts: TileIndex[]
}

export class SMapTiles extends System implements MapLayer {
    tiles: Map<string, TileData> = new Map()

    override packet(p: PacketC): void {
        if (p.type == "update_map") {
            for (const [pos, parts] of p.changes) {
                this.tiles.set(pos.toString(), {
                    x: pos[0],
                    y: pos[1],
                    position: { x: pos[0] + 0.5, y: pos[1] + 0.5 },
                    parts
                })
                if (!parts.length)
                    this.tiles.delete(pos.toString())
            }
        }
    }

    draw_map_layer(ctx: CanvasRenderingContext2D): void {
        for (const [_, tile] of this.tiles) {
            ctx.save()
            ctx.translate(tile.x + 0.5, tile.y + 0.5)
            for (const p of tile.parts)
                draw_tile_sprite(ctx, data.tile_names[p] as TileName)
            ctx.restore()

        }
    }
}