diff options
Diffstat (limited to 'internal/game/map.go')
| -rw-r--r-- | internal/game/map.go | 76 |
1 files changed, 50 insertions, 26 deletions
diff --git a/internal/game/map.go b/internal/game/map.go index e7738bd..8b14e25 100644 --- a/internal/game/map.go +++ b/internal/game/map.go @@ -6,6 +6,30 @@ import ( "thirdcollapse/internal/world" ) +type mapGlyphs struct { + topLeft, topRight rune + bottomLeft, bottomRight rune + side rune + topFill rune + connectorH, connectorV rune + upArrow, downArrow rune +} + +func mapGlyphsForPlayer(unicode bool) mapGlyphs { + if unicode { + return mapGlyphs{ + topLeft: '╔', topRight: '╗', bottomLeft: '╚', bottomRight: '╝', + side: '║', topFill: '═', connectorH: '─', connectorV: '│', + upArrow: '↑', downArrow: '↓', + } + } + return mapGlyphs{ + topLeft: '.', topRight: '.', bottomLeft: ':', bottomRight: ':', + side: ':', topFill: '.', connectorH: '-', connectorV: '|', + upArrow: '^', downArrow: 'v', + } +} + type mapGraph struct { posToRoom map[[2]int]int roomToPos map[int][2]int @@ -62,8 +86,8 @@ func buildGraph(g *Game, startRoomID int) *mapGraph { return mg } -func buildTinyMap(g *Game, roomID int) []string { - mg := buildGraph(g, roomID) +func buildTinyMap(g *Game, roomID int, mg mapGlyphs) []string { + bg := buildGraph(g, roomID) grid := make([][]rune, 5) for i := range grid { @@ -76,7 +100,7 @@ func buildTinyMap(g *Game, roomID int) []string { for y := -1; y <= 1; y++ { for x := -1; x <= 1; x++ { pos := [2]int{x, y} - rid, ok := mg.posToRoom[pos] + rid, ok := bg.posToRoom[pos] if !ok { continue } @@ -94,13 +118,13 @@ func buildTinyMap(g *Game, roomID int) []string { for x := -1; x <= 0; x++ { leftPos := [2]int{x, y} rightPos := [2]int{x + 1, y} - leftRoom, leftOK := mg.posToRoom[leftPos] - rightRoom, rightOK := mg.posToRoom[rightPos] + leftRoom, leftOK := bg.posToRoom[leftPos] + rightRoom, rightOK := bg.posToRoom[rightPos] if !leftOK || !rightOK { continue } if exitsConnect(g, leftRoom, rightRoom, world.East, world.West) { - grid[(y+1)*2][(x+1)*2+1] = '─' + grid[(y+1)*2][(x+1)*2+1] = mg.connectorH } } } @@ -109,13 +133,13 @@ func buildTinyMap(g *Game, roomID int) []string { for x := -1; x <= 1; x++ { topPos := [2]int{x, y} bottomPos := [2]int{x, y + 1} - topRoom, topOK := mg.posToRoom[topPos] - bottomRoom, bottomOK := mg.posToRoom[bottomPos] + topRoom, topOK := bg.posToRoom[topPos] + bottomRoom, bottomOK := bg.posToRoom[bottomPos] if !topOK || !bottomOK { continue } if exitsConnect(g, topRoom, bottomRoom, world.South, world.North) { - grid[(y+1)*2+1][(x+1)*2] = '│' + grid[(y+1)*2+1][(x+1)*2] = mg.connectorV } } } @@ -123,19 +147,21 @@ func buildTinyMap(g *Game, roomID int) []string { cur, _ := loadRoom(g, roomID) if cur != nil { if _, ok := exitTarget(cur, world.Up); ok { - grid[1][3] = '↑' + grid[1][3] = mg.upArrow } if _, ok := exitTarget(cur, world.Down); ok { - grid[3][1] = '↓' + grid[3][1] = mg.downArrow } } + topFill := strings.Repeat(string(mg.topFill), 5) lines := make([]string, 7) - lines[0] = "╔═════╗" + lines[0] = string(mg.topLeft) + topFill + string(mg.topRight) for row := 0; row < 5; row++ { - lines[row+1] = "║" + string(grid[row]) + "║" + lines[row+1] = string(mg.side) + string(grid[row]) + string(mg.side) } - lines[6] = "╚═════╝" + botFill := strings.Repeat(string(mg.topFill), 5) + lines[6] = string(mg.bottomLeft) + botFill + string(mg.bottomRight) return lines } @@ -152,10 +178,8 @@ func exitsConnect(g *Game, room1, room2 int, dir12, dir21 world.ExitDir) bool { if !ok { return false } - if id, ok := exitTarget(r2, dir21); ok && id == room1 { - return true - } - return false + id, ok := exitTarget(r2, dir21) + return ok && id == room1 } func exitTarget(room *world.Room, dir world.ExitDir) (int, bool) { @@ -234,8 +258,8 @@ func leftTrimCommon(lines []string) []string { return result } -func buildFullMap(g *Game, roomID, mapWidth, mapHeight int) []string { - mg := buildGraph(g, roomID) +func buildFullMap(g *Game, roomID, mapWidth, mapHeight int, mg mapGlyphs) []string { + bg := buildGraph(g, roomID) grid := make([][]rune, mapHeight) for i := range grid { @@ -248,7 +272,7 @@ func buildFullMap(g *Game, roomID, mapWidth, mapHeight int) []string { cx := mapWidth / 2 cy := mapHeight / 2 - for pos, rid := range mg.posToRoom { + for pos, rid := range bg.posToRoom { gr := cy + pos[1]*2 gc := cx + pos[0]*2 if gr < 0 || gr >= mapHeight || gc < 0 || gc >= mapWidth { @@ -261,25 +285,25 @@ func buildFullMap(g *Game, roomID, mapWidth, mapHeight int) []string { } } - for pos, rid := range mg.posToRoom { + for pos, rid := range bg.posToRoom { x, y := pos[0], pos[1] - if rightID, ok := mg.posToRoom[[2]int{x + 1, y}]; ok { + if rightID, ok := bg.posToRoom[[2]int{x + 1, y}]; ok { if exitsConnect(g, rid, rightID, world.East, world.West) { gr := cy + y*2 gc := cx + x*2 + 1 if gr >= 0 && gr < mapHeight && gc >= 0 && gc < mapWidth { - grid[gr][gc] = '─' + grid[gr][gc] = mg.connectorH } } } - if bottomID, ok := mg.posToRoom[[2]int{x, y + 1}]; ok { + if bottomID, ok := bg.posToRoom[[2]int{x, y + 1}]; ok { if exitsConnect(g, rid, bottomID, world.South, world.North) { gr := cy + y*2 + 1 gc := cx + x*2 if gr >= 0 && gr < mapHeight && gc >= 0 && gc < mapWidth { - grid[gr][gc] = '│' + grid[gr][gc] = mg.connectorV } } } |
