From 184ac149ee89465ee6746e4cdcff0357882ce7c4 Mon Sep 17 00:00:00 2001 From: thefish Date: Sun, 27 Oct 2019 02:51:35 +0300 Subject: [PATCH] viewport --- engine/gamemap/TODO | 3 + engine/gamemap/mapgen.go | 20 ------ engine/gamemap/mapgens/default.go | 1 + engine/gamemap/tile.go | 3 + ui/mainwindow/camera.go | 43 ------------- ui/mainwindow/layer.go | 31 +++++++++ ui/mainwindow/viewport.go | 101 ++++++++++++++++++++++++++++++ 7 files changed, 139 insertions(+), 63 deletions(-) create mode 100644 engine/gamemap/TODO delete mode 100644 engine/gamemap/mapgen.go delete mode 100644 ui/mainwindow/camera.go create mode 100644 ui/mainwindow/viewport.go diff --git a/engine/gamemap/TODO b/engine/gamemap/TODO new file mode 100644 index 0000000..b6d1783 --- /dev/null +++ b/engine/gamemap/TODO @@ -0,0 +1,3 @@ +- load prefabs +- compose from gens and prefabs +- editor for prefabs \ No newline at end of file diff --git a/engine/gamemap/mapgen.go b/engine/gamemap/mapgen.go deleted file mode 100644 index 964b93f..0000000 --- a/engine/gamemap/mapgen.go +++ /dev/null @@ -1,20 +0,0 @@ -package gamemap - -type mapGen interface { - generate(l *Level) *Level -} - -type defaultGen struct{} - -func (d defaultGen) generate(l *Level) *Level { - - l.Tiles, rooms = addRooms(l) - l.Tiles = connectRooms(rooms) - l.Objects = populate(rooms) - - return l -} - -func addRooms(l *Level) { - -} diff --git a/engine/gamemap/mapgens/default.go b/engine/gamemap/mapgens/default.go index 42ed2b4..633bb96 100644 --- a/engine/gamemap/mapgens/default.go +++ b/engine/gamemap/mapgens/default.go @@ -10,6 +10,7 @@ var minRoomSize = 3 var maxRoomSize = 11 var maxrooms = 100 +//fixme make closure to stack them func DefaultGen(l *gamemap.Level) *gamemap.Level { rng := util.NewRNG() diff --git a/engine/gamemap/tile.go b/engine/gamemap/tile.go index fccc3c6..816cece 100644 --- a/engine/gamemap/tile.go +++ b/engine/gamemap/tile.go @@ -40,6 +40,7 @@ type Tile struct { BlocksSight bool Explored bool MustDraw bool + Colordance bool } func NewWall() *Tile { @@ -91,6 +92,7 @@ func NewWaterTile() *Tile { BlocksSight: false, Explored: false, MustDraw: true, //fixme debug + Colordance: true, Appearance: &Appearance{ Char: ".", ColorSet: &TileColorSet{ @@ -120,6 +122,7 @@ func NewDeepWaterTile() *Tile { BlocksSight: false, Explored: false, MustDraw: true, //fixme debug + Colordance: true, Appearance: &Appearance{ Char: " ", ColorSet: &TileColorSet{ diff --git a/ui/mainwindow/camera.go b/ui/mainwindow/camera.go deleted file mode 100644 index a6d816f..0000000 --- a/ui/mainwindow/camera.go +++ /dev/null @@ -1,43 +0,0 @@ -package mainwindow - -type GameCamera struct { - X int - Y int - Width int - Height int -} - -func (c *GameCamera) MoveCamera(targetX int, targetY int, mapWidth int, mapHeight int) { - // Update the camera coordinates to the target coordinates - x := targetX - c.Width/2 - y := targetY - c.Height/2 - - if x < 0 { - x = 0 - } - - if y < 0 { - y = 0 - } - - if x > mapWidth-c.Width { - x = mapWidth - c.Width - } - - if y > mapHeight-c.Height { - y = mapHeight - c.Height - } - - c.X, c.Y = x, y -} - -func (c *GameCamera) ToCameraCoordinates(mapX int, mapY int) (cameraX int, cameraY int) { - // Convert coordinates on the gamemap, to coordinates on the viewport - x, y := mapX-c.X, mapY-c.Y - - if x < 0 || y < 0 || x >= c.Width || y >= c.Height { - return -1, -1 - } - - return x, y -} diff --git a/ui/mainwindow/layer.go b/ui/mainwindow/layer.go index 21873da..47621ec 100644 --- a/ui/mainwindow/layer.go +++ b/ui/mainwindow/layer.go @@ -1,6 +1,7 @@ package mainwindow import ( + "lab.zaar.be/thefish/alchemyst-go/engine/types" blt "lab.zaar.be/thefish/bearlibterminal" ) @@ -19,6 +20,12 @@ func (layer *Layer) before() *Layer { return layer } +func (layer *Layer) WithRawColor(c uint32) *Layer { + layer.before() + blt.Color(c) + return layer +} + func (layer *Layer) WithColor(colorName string) *Layer { layer.before() c := blt.ColorFromName(colorName) @@ -26,6 +33,25 @@ func (layer *Layer) WithColor(colorName string) *Layer { return layer } +func (layer *Layer) PutWithRawBackground(x,y int, symbol interface{}, bgColor uint32) { + layer.before() + prevColor := uint32(blt.State(blt.TK_COLOR)) + blt.Color(bgColor) + layer.Put(x,y,"█") + blt.Color(prevColor) + layer.Put(x,y, symbol) +} + +func (layer *Layer) PutWithBackground(x,y int, symbol interface{}, bgColorName string) { + layer.before() + prevColor := uint32(blt.State(blt.TK_COLOR)) + c := blt.ColorFromName(bgColorName) + blt.Color(c) + layer.Put(x,y,"█") + blt.Color(prevColor) + layer.Put(x,y, symbol) +} + func (layer *Layer) after() *Layer { blt.Color(layer.defaultColor) blt.Layer(0) @@ -54,6 +80,11 @@ func (layer *Layer) Decorate(f func(args ...interface{})) func(args ...interface } } +func (layer *Layer) Clear(r *types.Rect) { + blt.ClearArea(r.X, r.Y, r.W, r.H) +} + + func (layer *Layer) Render() { } diff --git a/ui/mainwindow/viewport.go b/ui/mainwindow/viewport.go new file mode 100644 index 0000000..93a574a --- /dev/null +++ b/ui/mainwindow/viewport.go @@ -0,0 +1,101 @@ +package mainwindow + +import ( + "errors" + "lab.zaar.be/thefish/alchemyst-go/engine/gamemap" + "lab.zaar.be/thefish/alchemyst-go/engine/types" +) + +var NotInViewError = errors.New("not in ViewPort") +const FPS_LIMIT = 60 + +type ViewPort struct { + *types.Rect + level *gamemap.Level + layer *Layer +} + +func NewViewPort(x, y, w, h int, level *gamemap.Level, layer *Layer) *ViewPort { + return &ViewPort{ + Rect: &types.Rect{x, y, w, h}, + level: level, + layer: layer, + } +} + +func (vp *ViewPort) Move(c *types.Coords) { + x := c.X - vp.Rect.W/2 + y := c.Y - vp.Rect.H/2 + + if x < 0 { + x = 0 + } + if y < 0 { + y = 0 + } + if x > vp.level.W-vp.W { + x = vp.level.W - vp.W + } + if y > vp.level.H-vp.H { + x = vp.level.H - vp.H + } + //if x != vp.X || y != vp.Y { State.FovRecompute <- struct{}{}} + vp.X, vp.Y = x, y +} + +func (vp *ViewPort) ToVPCoords(c *types.Coords) (newCoords *types.Coords, err error) { + //coords on map to coords on vp + x, y := c.X-vp.X, c.Y-vp.Y + if x < 0 || y < 0 || x >= vp.W || y >= vp.H { + return &types.Coords{-1, -1}, NotInViewError + } + return &types.Coords{x, y}, nil +} + +//call only from main thread +func (vp *ViewPort) Render() { + //fixme get these from state chan(s) + var fpsTicker int + var fovRecompute bool + redraw := false + //fixme get player instance + + vp.Move(player.Coords) + //fixme detect fovRecompute + if fovRecompute { + vp.layer.Clear(vp.Rect) + fovRecompute = false + redraw = true + //fixme + vp.level.ComputeFov(FovMap, player.Coords, player.TorchRadius, FovLightWalls, FovAlgo) + } + //increase ticker + fpsTicker++ + + if redraw || fpsTicker % (FPS_LIMIT / 10) == 0 { + fpsTicker := 0 + + for y:=0; y < vp.H; y++ { + for x:=0; x