Skip to content

Commit

Permalink
Split common map construction code into seperate files
Browse files Browse the repository at this point in the history
  • Loading branch information
krypciak committed Aug 19, 2024
1 parent 64cede1 commit 05ab9bc
Show file tree
Hide file tree
Showing 6 changed files with 422 additions and 388 deletions.
110 changes: 110 additions & 0 deletions src/map-construct/layer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { MapThemeConfig } from "../map-construct/theme"
import { Coll } from "../util/map"
import { Array2d } from "../util/util"

export interface MapConstructionLayers {
background: number[][][]
shadow: number[][]
light: number[][]
coll: number[][][]
nav: number[][][]
}

export function emptyLayer(
size: Vec2,
fill: number,
rest: Pick<sc.MapModel.MapLayer, 'type' | 'name' | 'tilesetName' | 'level'>
): sc.MapModel.MapLayer {
return {
visible: 1,
repeat: false,
distance: 1,
yDistance: 0,
tilesize: 16,
moveSpeed: { x: 0, y: 0 },
lighter: false,
id: -10,

data: Array2d.empty(size, fill),
width: size.x,
height: size.y,
...rest,
}
}

export function getEmptyLayers(
size: Vec2,
levelCount: number,
theme: MapThemeConfig
): { layers: MapConstructionLayers; levels: sc.MapModel.Map['levels']; layer: sc.MapModel.MapLayer[] } {
const layer: sc.MapModel.MapLayer[] = []
const levels: sc.MapModel.Map['levels'] = []

let background: number[][][] = [],
shadow: number[][] = [],
coll: number[][][] = [],
nav: number[][][] = []

for (let level = 0; level < levelCount; level++) {
levels.push({ height: level * 16 * 2 })

const backgroundLayer = emptyLayer(size, level == 0 ? theme.blackTile : 0, {
type: 'Background',
name: 'NEW_BACKGROUND',
tilesetName: theme.tileset,
level,
})

background.push(backgroundLayer.data)
layer.push(backgroundLayer)

if (level == 0 && theme.addShadows) {
const shadowLayer = emptyLayer(size, 0, {
name: 'NEW_SHADOW',
type: 'Background',
tilesetName: theme.shadowTileset,
level,
})
shadow = shadowLayer.data
layer.push(shadowLayer)
}
const collisionLayer = emptyLayer(size, Coll.Wall, {
name: 'NEW_COLLISION',
type: 'Collision',
tilesetName: 'media/map/collisiontiles-16x16.png',
level,
})
coll.push(collisionLayer.data)
layer.push(collisionLayer)

const navigationLayer = emptyLayer(size, 0, {
name: 'NEW_NAVIGATION',
type: 'Navigation',
tilesetName: 'media/map/pathmap-tiles.png',
level,
})
nav.push(navigationLayer.data)
layer.push(navigationLayer)
}

const lightLayer = emptyLayer(size, 0, {
name: 'NEW_LIGHT',
type: 'Light',
tilesetName: 'media/map/lightmap-tiles.png',
level: 'last',
})
const light: number[][] = lightLayer.data
layer.push(lightLayer)

return {
layers: {
background,
shadow,
light,
coll,
nav,
},
levels,
layer,
}
}
52 changes: 48 additions & 4 deletions src/map-construct/map-construct.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { copyMapArrange, MapArrange, RoomArrange } from '../map-arrange/map-arrange'
import { copyMapArrange, MapArrange, offsetMapArrange } from '../map-arrange/map-arrange'
import { MapPicker } from '../map-arrange/map-picker/configurable'
import { Id } from '../build-queue/build-queue'
import { assert } from '../util/util'
import { Item } from '../util/items'
import { Rect } from '../util/geometry'
import { Dir, Rect } from '../util/geometry'
import { MapConstructionLayers } from './layer'
import { getEmptyLayers } from './layer'
import { MapTheme } from './theme'

export interface MapConstruct extends MapArrange {
constructed: sc.MapModel.Map
rectsAbsolute: RoomArrange[]
bounds: Rect
rectsAbsolute: Rect[]
title: string
}

Expand Down Expand Up @@ -49,3 +51,45 @@ export function constructMapsFromMapsArrange(maps: MapArrange[], areaInfo: AreaI

return mapsConstruct
}

export interface MapInConstruction extends sc.MapModel.Map {
layers: MapConstructionLayers
}

export function baseMapConstruct(
map: MapArrange,
mapName: string,
areaId: string,
theme: MapTheme
): { mic: MapInConstruction; rectsAbsolute: Rect[] } {
const rectsAbsolute = map.rects.map(Rect.copy)

const boundsEntity = Rect.boundsOfArr(map.rects)

Rect.extend(boundsEntity, 7 * 16, { [Dir.NORTH]: true })
Rect.extend(boundsEntity, 1 * 16, [false, true, true, true])
const offset = Vec2.mulC(boundsEntity, -1)

offsetMapArrange(map, offset)

const bounds = Rect.div(Rect.copy(boundsEntity), 16)

const mapSize: Vec2 = Rect.toTwoVecSize(bounds)[1]

const mic: MapInConstruction = {
name: mapName,
mapWidth: mapSize.x,
mapHeight: mapSize.y,
masterLevel: 0,
attributes: theme.getMapAttributes(areaId),
screen: { x: 0, y: 0 },
entities: [],

...getEmptyLayers(mapSize, 3, theme.config),
}
return { mic, rectsAbsolute }
}

export function getTprName(isEntrance: boolean, index: number): string {
return `${isEntrance ? 'entrance' : 'rest'}_${index}`
}
Loading

0 comments on commit 05ab9bc

Please sign in to comment.