summaryrefslogtreecommitdiff
path: root/client/src/map.ts
blob: e6fab9d485b86f66dc7bec3887deb5eeece16f2b (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import { Wall, ItemType, Map, Maps, Items } from "./types.js"

export const getItemKey = (
    x: number, 
    y: number, 
    w: number
): number => {
    let nx = Math.round(x * 2)
    let ny = Math.round(y * 2)
    let key = ny * w * 2 + nx
    return key
}

const getPoint = (
    width: number, 
    height: number,
    data: number[],
    x: number,
    y: number
): number => {
    if (x < 0 || x >= width || y < 0 || y >= height) {
        return 0
    } else {
        return data[y * width + x]
    }
}

const genWalls = (
    width: number,
    height: number,
    data: number[]
): number[] => {

    let walls = Array(width * height)
    for (let y = 0; y < height; y++) {
        for (let x = 0; x < width; x++) {
            
            let north = getPoint(width, height, data, x, y-1) == 1
            let south = getPoint(width, height, data, x, y+1) == 1
            let east = getPoint(width, height, data, x+1, y) == 1
            let west = getPoint(width, height, data, x-1, y) == 1
            let current = getPoint(width, height, data, x, y) == 1

            let point = Wall.EMPTY
            
            if (!current) {
                walls[y * width + x] = point
                continue
            }

            if (north && south && east && west) {
                point = Wall.CROSS
            } else if (east && west && north) {
                point = Wall.TEE_NORTH
            } else if (east && west && south) {
                point = Wall.TEE_SOUTH
            } else if (north && south && east) {
                point = Wall.TEE_EAST
            } else if (north && south && west) {
                point = Wall.TEE_WEST
            } else if (east && west) {
                point = Wall.WALL_HZ
            } else if (north && south) {
                point = Wall.WALL_VT   
            } else if (west && south) {
                point = Wall.TURN_Q1
            } else if (south && east) {
                point = Wall.TURN_Q2
            } else if (east && north) {
                point = Wall.TURN_Q3
            } else if (north && west) {
                point = Wall.TURN_Q4
            } else if (north) {
                point = Wall.WALL_END_NORTH
            } else if (east) {
                point = Wall.WALL_END_EAST
            } else if (south) {
                point = Wall.WALL_END_SOUTH
            } else if (west) {
                point = Wall.WALL_END_WEST
            } else {
                point = Wall.DOT
            }

            walls[y * width + x] = point

        }
    }

    return walls
}

export const genItems = (map: Map): Items => {

    let width = map.width
    let height = map.height
    let data = map.data
    
    let items: Items = {}

    for (let y = 0; y < height; y++) {
        for (let x = 0; x < width; x++) {
            let tile = getPoint(width, height, data, x, y)
            if (tile != 0) continue

            let item_key = getItemKey(x, y, width)
            items[item_key] = {type: ItemType.DOT, pos: {x, y}}

            let tile_south = getPoint(width, height, data, x, y + 1)
            if (tile_south == 0) {
                item_key = getItemKey(x, y + .5, width)
                items[item_key] = {type: ItemType.DOT, pos: {x, y: y + .5}}
            }

            let tile_east = getPoint(width, height, data, x + 1, y)
            if (tile_east == 0) {
                item_key = getItemKey(x + .5, y, width)
                items[item_key] = {type: ItemType.DOT, pos: {x: x + .5, y}}
            }
        }
    }

    return items
}

let mapData: Maps = {}
let id: number = 0

export const loadMap = (
    width: number,
    height: number,
    data: number[]
): number => {

    let mapId = id++

    mapData[mapId] = {
        data: structuredClone(data),
        walls: genWalls(width, height, data),
        width,
        height,
        id: mapId
    }

    return mapId
}

export const getMap = (mapId: number): Map | undefined => {
    if (mapId == undefined) return undefined
    return mapData[mapId]
}