last_light/game/world/map.go

108 lines
2 KiB
Go
Raw Normal View History

2024-05-06 18:59:14 +03:00
package world
import (
2024-05-06 21:19:08 +03:00
"mvvasilev/last_light/engine"
2024-05-21 23:08:51 +03:00
"github.com/gdamore/tcell/v2"
)
type Map interface {
2024-05-06 21:19:08 +03:00
Size() engine.Size
2024-05-12 23:22:39 +03:00
SetTileAt(x, y int, t Tile) Tile
TileAt(x, y int) Tile
2024-05-21 23:08:51 +03:00
IsInBounds(x, y int) bool
ExploredTileAt(x, y int) Tile
MarkExplored(x, y int)
2024-05-06 18:59:14 +03:00
Tick(dt int64)
}
type WithPlayerSpawnPoint interface {
2024-05-06 21:19:08 +03:00
PlayerSpawnPoint() engine.Position
2024-05-06 18:59:14 +03:00
}
type WithRooms interface {
2024-05-06 21:19:08 +03:00
Rooms() []engine.BoundingBox
2024-05-12 23:22:39 +03:00
}
type WithNextLevelStaircasePosition interface {
NextLevelStaircasePosition() engine.Position
}
type WithPreviousLevelStaircasePosition interface {
PreviousLevelStaircasePosition() engine.Position
}
2024-05-03 13:46:32 +03:00
type BasicMap struct {
2024-05-21 23:08:51 +03:00
tiles [][]Tile
exploredTiles map[engine.Position]Tile
exploredStyle tcell.Style
2024-05-03 13:46:32 +03:00
}
2024-05-21 23:08:51 +03:00
func CreateBasicMap(tiles [][]Tile, exploredStyle tcell.Style) *BasicMap {
2024-05-03 13:46:32 +03:00
bm := new(BasicMap)
bm.tiles = tiles
2024-05-21 23:08:51 +03:00
bm.exploredTiles = make(map[engine.Position]Tile, 0)
bm.exploredStyle = exploredStyle
2024-05-03 13:46:32 +03:00
return bm
}
2024-05-21 23:08:51 +03:00
func (bm *BasicMap) Tick(dt int64) {
2024-05-03 13:46:32 +03:00
}
2024-05-06 21:19:08 +03:00
func (bm *BasicMap) Size() engine.Size {
return engine.SizeOf(len(bm.tiles[0]), len(bm.tiles))
2024-05-03 13:46:32 +03:00
}
2024-05-12 23:22:39 +03:00
func (bm *BasicMap) SetTileAt(x int, y int, t Tile) Tile {
2024-05-21 23:08:51 +03:00
if !bm.IsInBounds(x, y) {
2024-05-12 23:22:39 +03:00
return CreateStaticTile(x, y, TileTypeVoid())
2024-05-03 13:46:32 +03:00
}
bm.tiles[y][x] = t
2024-05-12 23:22:39 +03:00
return bm.tiles[y][x]
2024-05-03 13:46:32 +03:00
}
func (bm *BasicMap) TileAt(x int, y int) Tile {
2024-05-21 23:08:51 +03:00
if !bm.IsInBounds(x, y) {
2024-05-03 13:46:32 +03:00
return CreateStaticTile(x, y, TileTypeVoid())
}
tile := bm.tiles[y][x]
if tile == nil {
return CreateStaticTile(x, y, TileTypeVoid())
}
return tile
}
2024-05-21 23:08:51 +03:00
func (bm *BasicMap) IsInBounds(x, y int) bool {
if x < 0 || y < 0 {
return false
}
if x >= bm.Size().Width() || y >= bm.Size().Height() {
return false
}
return true
}
func (bm *BasicMap) ExploredTileAt(x, y int) Tile {
return bm.exploredTiles[engine.PositionAt(x, y)]
}
func (bm *BasicMap) MarkExplored(x, y int) {
if !bm.IsInBounds(x, y) {
return
}
tile := bm.TileAt(x, y)
bm.exploredTiles[engine.PositionAt(x, y)] = CreateStaticTileWithStyleOverride(tile.Position().X(), tile.Position().Y(), tile.Type(), bm.exploredStyle)
}