summaryrefslogtreecommitdiff
path: root/client/src/map.ts
diff options
context:
space:
mode:
authorTyler Murphy <tylerm@tylerm.dev>2023-06-17 22:48:56 -0400
committerTyler Murphy <tylerm@tylerm.dev>2023-06-17 22:48:56 -0400
commit97c78292fef34a39552a4d983413b01e185a49aa (patch)
tree615c48125fe9a5a62b2d0826362dcbccf5721696 /client/src/map.ts
parentlayout changed (diff)
downloadtuxman-97c78292fef34a39552a4d983413b01e185a49aa.tar.gz
tuxman-97c78292fef34a39552a4d983413b01e185a49aa.tar.bz2
tuxman-97c78292fef34a39552a4d983413b01e185a49aa.zip
export and load maps
Diffstat (limited to 'client/src/map.ts')
-rw-r--r--client/src/map.ts76
1 files changed, 44 insertions, 32 deletions
diff --git a/client/src/map.ts b/client/src/map.ts
index d26c467..1ad6d63 100644
--- a/client/src/map.ts
+++ b/client/src/map.ts
@@ -1,4 +1,5 @@
-import { Wall, ItemType, Map, Maps, Items } from "./types.js"
+import { Wall, ItemType, Map, Maps, Items, Tile } from "./types.js"
+import { LZString } from "./lib/lz-string.js"
export const getItemKey = (
x: number,
@@ -90,6 +91,8 @@ const genWalls = (
return walls
}
+const canItem = (tile: Tile): boolean => tile != Tile.WALL && tile != Tile.GHOST_WALL && tile != Tile.GHOST_SPAWN
+
export const genItems = (map: Map): Items => {
let width = map.width
@@ -101,21 +104,35 @@ export const genItems = (map: Map): 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
+ if (!canItem(tile)) 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 type: ItemType
+
+ if (tile == Tile.FOOD)
+ type = ItemType.FOOD
+ else if (tile == Tile.THICC_DOT)
+ type = ItemType.THICC_DOT
+ else
+ type = ItemType.DOT
+
+ items[item_key] = {type, pos: {x, y}}
- 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}}
+ if (type == ItemType.DOT || type == ItemType.THICC_DOT) {
+ type = ItemType.DOT
+
+ let tile_south = getPoint(width, height, data, x, y + 1)
+ if (canItem(tile_south) && tile_south != Tile.FOOD) {
+ item_key = getItemKey(x, y + .5, width)
+ items[item_key] = {type, pos: {x, y: y + .5}}
+ }
+
+ let tile_east = getPoint(width, height, data, x + 1, y)
+ if (canItem(tile_east) && tile_east != Tile.FOOD) {
+ item_key = getItemKey(x + .5, y, width)
+ items[item_key] = {type, pos: {x: x + .5, y}}
+ }
}
}
}
@@ -124,7 +141,6 @@ export const genItems = (map: Map): Items => {
}
let mapData: Maps = {}
-let id: number = 0
export const genMap = (
width: number,
@@ -144,26 +160,22 @@ export const genMap = (
return mapData[mapId]
}
-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]
}
+
+export const compressMap = (map: Map): string => {
+ let encoded = map.width + '|' + map.height + '|' + map.data.map(n => n + ',').join('').slice(0, -1)
+ return LZString.compressToBase64(encoded)
+}
+
+export const decompressMap = (encoded: string): {width: number, height: number, data: number[]} => {
+ let decoded = LZString.decompressFromBase64(encoded)
+ let values = decoded.split('|')
+ let width = parseInt(values[0])
+ let height = parseInt(values[1])
+ let data = values[2].split(',').map(c => parseInt(c))
+
+ return {width, height, data}
+}