summaryrefslogtreecommitdiff
path: root/client/src/map.ts
diff options
context:
space:
mode:
authorTyler Murphy <tylerm@tylerm.dev>2023-06-16 20:38:55 -0400
committerTyler Murphy <tylerm@tylerm.dev>2023-06-16 20:38:55 -0400
commit44334fc3852eb832280a335f72e6416c93a9f19f (patch)
tree4a97b6064a97c4ad58c07d89050ad8a11e7a4f70 /client/src/map.ts
parentbetter map bg renderer (diff)
downloadtuxman-44334fc3852eb832280a335f72e6416c93a9f19f.tar.gz
tuxman-44334fc3852eb832280a335f72e6416c93a9f19f.tar.bz2
tuxman-44334fc3852eb832280a335f72e6416c93a9f19f.zip
ts
Diffstat (limited to 'client/src/map.ts')
-rw-r--r--client/src/map.ts151
1 files changed, 151 insertions, 0 deletions
diff --git a/client/src/map.ts b/client/src/map.ts
new file mode 100644
index 0000000..e6fab9d
--- /dev/null
+++ b/client/src/map.ts
@@ -0,0 +1,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]
+}