Skip to content

Commit

Permalink
Add map connections
Browse files Browse the repository at this point in the history
  • Loading branch information
krypciak committed Aug 20, 2024
1 parent d9fa53a commit 260faef
Show file tree
Hide file tree
Showing 12 changed files with 134 additions and 99 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"@typescript-eslint/parser": "^6.21.0",
"bun": "^1.1.22",
"cc-blitzkrieg": "github:krypciak/cc-blitzkrieg",
"cc-hotreload": "github:krypciak/cc-hotreload",
"ccmodmanager": "github:CCDirectLink/CCModManager",
"colorts": "^0.1.63",
"esbuild": "^0.18.20",
Expand Down
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 33 additions & 5 deletions src/area/area-json-creator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Id } from '../build-queue/build-queue'
import { RoomArrange } from '../map-arrange/map-arrange'
import { AreaInfo, MapConstruct, RoomConsturct } from '../map-construct/map-construct'
import { Rect } from '../util/geometry'
import { Dir, DirU, Rect } from '../util/geometry'
import { ObjectEntriesT } from '../util/modify-prototypes'
import { allLangs, assert } from '../util/util'
import { Vec2 } from '../util/vec2'
Expand Down Expand Up @@ -43,6 +42,14 @@ declare global {
areaRect?: Rect
})[]
}
interface Connection {
dir: Dir
tx: number
ty: number
size: number
map1: number
map2: number
}
}
}
}
Expand All @@ -55,7 +62,7 @@ export function createArea(
chests: number,
floorNames: Record<number, ig.LangLabel.Data>
): sc.AreaLoadable.SDCustom.Data {
const bounds: Rect = Rect.boundsOfArr(maps.flatMap(a => a.rectsAbsolute))
const bounds: Rect = Rect.boundsOfArr(maps.flatMap(a => a.arrangeCopy.rects))
const divider = 64
const offset = Vec2.divC(Vec2.copy(bounds), divider)
const size = Vec2.divC({ x: bounds.width, y: bounds.height }, divider)
Expand All @@ -70,9 +77,30 @@ export function createArea(
for (const [floor, maps] of ObjectEntriesT(mapsByFloor)) {
if (floor == defaultFloor) actualDefaultFloor = floors.length

const connections: sc.AreaLoadable.SDCustom.Connection[] = []
const offsetDas = Vec2.divC(Rect.boundsOfArr(maps.flatMap(map => map.arrangeCopy.rects)), divider)
const areaMaps: sc.AreaLoadable.SDCustom.Map[] = maps.map(map => {
const boundsAbsolute: Rect = Rect.boundsOfArr(map.rectsAbsolute)
const boundsAbsolute: Rect = Rect.boundsOfArr(map.arrangeCopy.rects)
const offsetRelative: Vec2 = Vec2.divC(Rect.boundsOfArr(map.rects), divider)

for (const tpr of [...map.arrangeCopy.entranceTprs, ...map.arrangeCopy.restTprs]) {
if (tpr.noDrawConnection) continue
if (tpr.destId < map.id) continue
assert(DirU.isDir(tpr.dir))

const pos: Vec2 = Vec2.copy(tpr)
Vec2.divC(pos, divider)
Vec2.sub(pos, offsetDas)
connections.push({
tx: pos.x,
ty: pos.y,
dir: tpr.dir,
size: 3,
map1: tpr.destId,
map2: map.id,
})
}

return {
path: map.constructed.name.replace('/', '.'),
name: allLangs(map.title),
Expand Down Expand Up @@ -100,7 +128,7 @@ export function createArea(
size,

maps: areaMaps,
connections: [],
connections,
icons: [],
landmarks: [],
})
Expand Down
149 changes: 69 additions & 80 deletions src/area/custom-area-container.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Rect, Dir } from '../util/geometry'
import { assert } from '../util/util'
import { Vec2 } from '../util/vec2'

export {}

Expand All @@ -10,7 +11,7 @@ interface AreaRendererColorScheme {
shadow: ig.SimpleColor
}

const addPxSpace: number = 0
const addPxSpace: number = 4
sc.MapAreaContainer.inject({
findMap(mx: number, my: number, gamepad: boolean, wait?: number): boolean | undefined {
if (sc.menu.mapMapFocus) return
Expand All @@ -19,12 +20,12 @@ sc.MapAreaContainer.inject({

let pos: Vec2
if (gamepad) {
pos = Vec2.create(this.area.hook.pos)
pos = Vec2.copy(this.area.hook.pos)
} else {
pos = Vec2.createC(
mx - sc.menu.mapCamera.x - this.area.hook.pos.x + 1,
my - sc.menu.mapCamera.y - this.area.hook.pos.y + 1
)
pos = {
x: mx - sc.menu.mapCamera.x - this.area.hook.pos.x + 1,
y: my - sc.menu.mapCamera.y - this.area.hook.pos.y + 1,
}
}
Vec2.subC(pos, addPxSpace)

Expand Down Expand Up @@ -162,14 +163,45 @@ type GuiHookMapRoomList = ig.GuiHook & {
sc.MapCurrentRoomWrapper.inject({
init(hook: ig.GuiHook) {
const floor = (hook as GuiHookMapRoomList).gui.floor
if (!floor.shaduungeonCustom) return this.parent(hook)
if (!floor.shaduungeonCustom) {
this.parent(hook)
this.doStateTransition('HIDDEN')
return
}

this.parent({
pos: { x: hook.pos.x + addPxSpace, y: hook.pos.y + addPxSpace },
size: { x: hook.size.x - addPxSpace * 2, y: hook.size.y - addPxSpace * 2 },
} as ig.GuiHook)
this.doStateTransition('HIDDEN')
},
})
function drawConnection(v: Vec2, connection: sc.AreaLoadable.SDCustom.Connection) {
assert(connection.size == 3)
let { x, y } = v
y += 2
x += 2
let h = 4
if (connection.dir == Dir.NORTH) {
inactiveColors.empty.draw(x, y, connection.size, h)
inactiveColors.border.draw(x - 1, y + 1, 1, h - 2)
inactiveColors.border.draw(x + connection.size, y + 1, 1, h - 2)
} else if (connection.dir == Dir.EAST) {
inactiveColors.empty.draw(x, y, connection.size, h)
inactiveColors.border.draw(x, y - 1, connection.size, 1)
inactiveColors.shadow.draw(x, y, connection.size, 2)
inactiveColors.border.draw(x, y + connection.size, connection.size, 1)
} else if (connection.dir == Dir.SOUTH) {
inactiveColors.empty.draw(x, y, connection.size, h + 1)
inactiveColors.border.draw(x - 1, y, 1, h - 1)
inactiveColors.border.draw(x + connection.size, y, 1, h - 1)
} else if (connection.dir == Dir.WEST) {
inactiveColors.empty.draw(x, y, connection.size, h)
inactiveColors.border.draw(x, y - 1, connection.size, 1)
inactiveColors.shadow.draw(x, y, connection.size, 2)
inactiveColors.border.draw(x, y + connection.size, connection.size, 1)
}
}

sc.MapRoom.inject({
init(room, _floor, id) {
Expand All @@ -195,52 +227,22 @@ sc.MapRoom.inject({
assert(map.rects)
const c = this.active ? activeColors : inactiveColors

/*
function drawConnection(x: number, y: number, connection: sc.AreaLoadable.ConnectionRoomList) {
const h = 4
switch (connection.dir) {
case Dir.NORTH:
inactiveColors.empty.draw(x, y, connection.size, h)
inactiveColors.border.draw(x - 1, y + 1, 1, h - 1)
inactiveColors.border.draw(x + connection.size, y + 1, 1, h - 1)
break
case Dir.EAST:
inactiveColors.empty.draw(x, y, connection.size, h)
inactiveColors.border.draw(x, y - 1, connection.size, 1)
inactiveColors.border.draw(x, y + connection.size, connection.size, 1)
break
case Dir.SOUTH:
inactiveColors.empty.draw(x, y, connection.size, h)
inactiveColors.border.draw(x - 1, y, 1, h - 1)
inactiveColors.border.draw(x + connection.size, y, 1, h - 1)
break
case Dir.WEST:
inactiveColors.empty.draw(x, y, connection.size, h)
inactiveColors.border.draw(x, y - 1, connection.size, 1)
inactiveColors.border.draw(x, y + connection.size, connection.size, 1)
break
}
}
*/

this.buffer = ig.imageAtlas.getFragment(this.hook.size.x, this.hook.size.y, () => {
/* draw black on south and east edges */
for (const o of map.rects) {
if (!o.drawRect) {
const rect = {
x: o.x * tilesize + addPxSpace,
y: o.y * tilesize + addPxSpace,
width: o.width * tilesize,
height: o.height * tilesize,
x2: 0,
y2: 0,
}
o.drawRect = rect
o.drawEmptyRect = Rect.copy(rect)
rect.x2 = rect.x + rect.width - 1
rect.y2 = rect.y + rect.height - 1
const rect = {
x: o.x * tilesize + addPxSpace,
y: o.y * tilesize + addPxSpace,
width: o.width * tilesize,
height: o.height * tilesize,
x2: 0,
y2: 0,
}
const rect = o.drawRect
o.drawRect = rect
o.drawEmptyRect = Rect.copy(rect)
rect.x2 = rect.x + rect.width - 1
rect.y2 = rect.y + rect.height - 1

if (o.walls[Dir.SOUTH]) {
black.draw(rect.x, rect.y2, rect.width, 1)
}
Expand Down Expand Up @@ -269,7 +271,7 @@ sc.MapRoom.inject({
if (o.walls[Dir.SOUTH]) {
c.border.draw(rect.x - biNX, rect.y2 - 1, rect.width + biNX + biPX - 1, 1)
} else {
eRect.height += tunnelClear
eRect.height += tunnelClear + 1
}

if (o.walls[Dir.EAST]) {
Expand All @@ -288,44 +290,31 @@ sc.MapRoom.inject({
for (const o of map.rects) {
if (o.walls[Dir.NORTH]) {
const rect = o.drawEmptyRect!
c.shadow.draw(rect.x + 1, rect.y + 1, rect.width - 3, 1)
c.shadow.draw(rect.x + 1, rect.y + 1, rect.width - 3, 2)
}
}
/* fill the rooms */
for (const o of map.rects) {
const rect = o.drawEmptyRect!
const shadowOffset = o.walls[Dir.NORTH] ? 1 : 0
const shadowOffset = o.walls[Dir.NORTH] ? 2 : 0
c.empty.draw(rect.x + 1, rect.y + shadowOffset + 1, rect.width - 3, rect.height - shadowOffset - 3)
}

/*
const connections: sc.AreaLoadable.ConnectionRoomList[] = this.floor.connections
let i = connections.length
while (i--) {
const connection = connections[i]
// apperently map connections can have a condition? didnt bother implementing that
if (connection.map1 + 1 == this.room.id || connection.map2 + 1 == this.room.id) {
const rect: bareRect =
// @root/ts-ignore
connection.rect
new ig.SimpleColor('#00ff0055').draw(
(rect.x - this.room.min.x) * tilesize + addPxSpace,
(rect.y - this.room.min.y) * tilesize + addPxSpace,
rect.width * tilesize,
rect.height * tilesize
)
drawConnection(
(connection.tx - this.room.min.x) * tilesize + addPxSpace,
(connection.ty - this.room.min.y) * tilesize + addPxSpace,
connection,
)
}
}
*/
// activeColors.empty.draw(0, 0, addPxSpace, addPxSpace)
// activeColors.empty.draw(this.hook.size.x - addPxSpace, 0, addPxSpace, addPxSpace)
// activeColors.empty.draw(this.hook.size.x - addPxSpace, this.hook.size.y - addPxSpace, addPxSpace, addPxSpace)
// activeColors.empty.draw(0, this.hook.size.y - addPxSpace, addPxSpace, addPxSpace)
const connections = this.floor.connections as unknown as sc.AreaLoadable.SDCustom.Connection[]
let i = connections.length
while (i--) {
const connection = connections[i]
// apperently map connections can have a condition? didnt bother implementing that
if (connection.map1 + 1 == this.room.id || connection.map2 + 1 == this.room.id) {
drawConnection(
{
x: (connection.tx - this.room.min.x) * tilesize,
y: (connection.ty - this.room.min.y) * tilesize,
},
connection
)
}
}
})
this.prerendered = true
},
Expand Down
4 changes: 2 additions & 2 deletions src/dungeon/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ export class DungeonBuilder {
root: {
type: 'Simple',
size: roomSizeReg,
count: 1,
count: 2,
randomizeDirTryOrder,

followedBy: branch(
3,
1,
() => 1,
() => 1,
() => 2
Expand Down
1 change: 1 addition & 0 deletions src/map-arrange/map-arrange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface TprArrange3d extends Vec2 {
dir: Dir3d
destId: Id
destIndex?: number
noDrawConnection?: boolean
}
export interface TprArrange extends TprArrange3d {
dir: Dir
Expand Down
8 changes: 3 additions & 5 deletions src/map-construct/map-construct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { getEmptyLayers } from './layer'
import { MapTheme } from './theme'

export interface MapConstruct extends MapArrange {
arrangeCopy: MapArrange
constructed: sc.MapModel.Map
rectsAbsolute: Rect[]
title: string
rects: RoomConsturct[]
}
Expand Down Expand Up @@ -67,9 +67,7 @@ export function baseMapConstruct(
areaId: string,
theme: MapTheme,
extend: Record<Dir, number>
): { mic: MapInConstruction; rectsAbsolute: Rect[] } {
const rectsAbsolute = map.rects.map(Rect.copy)

): MapInConstruction {
const boundsEntity = Rect.boundsOfArr(map.rects)

for (let dir = 0 as Dir; dir < 4; dir++) {
Expand All @@ -96,7 +94,7 @@ export function baseMapConstruct(

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

export function getTprName(isEntrance: boolean, index: number): string {
Expand Down
Loading

0 comments on commit 260faef

Please sign in to comment.