fixes to viewport, config tuning

This commit is contained in:
2019-11-01 15:03:52 +03:00
parent a91351d3dc
commit c6c6b6254d
11 changed files with 228 additions and 173 deletions

View File

@ -98,7 +98,7 @@ func (layer *Layer) Decorate(f func(args ...interface{})) func(args ...interface
}
}
func (layer *Layer) Clear(r *types.Rect) {
func (layer *Layer) ClearRect(r *types.Rect) {
blt.ClearArea(r.X, r.Y, r.W, r.H)
}

View File

@ -42,13 +42,14 @@ func (mw *MainWindow) Open() {
blt.Set(
fmt.Sprintf(
//"window: size=%dx%d, title='%s v%s'; font: ./resources/fonts-bitmap/ibmnew8x12.png, size=8x12;",
"window: size=%dx%d, title='%s v%s'; font: %s, size=8x16;",
"window: size=%dx%d, title='%s v%s'; font: %s, size=%s;",
//"window: size=%dx%d, title='%s v%s'",
config.MainWindowSizeX,
config.MainWindowSizeY,
config.Title,
config.Version,
config.Font,
config.FontSize,
),
)
}

View File

@ -2,6 +2,7 @@ package mainwindow
import (
"errors"
"fmt"
"lab.zaar.be/thefish/alchemyst-go/engine/fov"
"lab.zaar.be/thefish/alchemyst-go/engine/fov/precomputed_shade"
"lab.zaar.be/thefish/alchemyst-go/engine/gamemap"
@ -10,15 +11,14 @@ import (
var NotInViewError = errors.New("not in ViewPort")
const FPS_LIMIT = 60
type ViewPort struct {
*types.Rect
cameraCoords types.Coords
level *gamemap.Level
layer *Layer
Fov fov.Fov
playerCoords types.Coords
playerTorchRadius int
PlayerCoords types.Coords
PlayerTorchRadius int
}
func NewViewPort(x, y, w, h int, level *gamemap.Level, layer *Layer) *ViewPort {
@ -32,13 +32,13 @@ func NewViewPort(x, y, w, h int, level *gamemap.Level, layer *Layer) *ViewPort {
Fov: fov,
}
vp.playerCoords = types.Coords{10, 10}
vp.playerTorchRadius = 10
vp.PlayerCoords = types.Coords{10, 10}
vp.PlayerTorchRadius = 10
return &vp
}
func (vp *ViewPort) Move(c *types.Coords) {
func (vp *ViewPort) Move(c *types.Coords, state types.GameState) {
x := c.X - vp.Rect.W/2
y := c.Y - vp.Rect.H/2
@ -49,22 +49,26 @@ func (vp *ViewPort) Move(c *types.Coords) {
y = 0
}
if x > vp.level.W-vp.W {
x = vp.level.W - vp.W
x = vp.level.W - vp.W - 1
}
if y > vp.level.H-vp.H {
x = vp.level.H - vp.H
x = vp.level.H - vp.H - 1
}
//if x != vp.X || y != vp.Y { State.FovRecompute <- struct{}{}}
vp.X, vp.Y = x, y
if x != vp.cameraCoords.X || y != vp.cameraCoords.Y {
state.FovRecompute <- struct{}{}
}
vp.cameraCoords.X = x
vp.cameraCoords.Y = y
}
func (vp *ViewPort) ToVPCoords(c *types.Coords) (newCoords *types.Coords, err error) {
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
x, y := c.X-vp.cameraCoords.X, c.Y-vp.cameraCoords.Y
if x < 0 || y < 0 || x > vp.W || y > vp.H {
return types.Coords{-1, -1}, NotInViewError
}
return &types.Coords{x, y}, nil
return types.Coords{x, y}, nil
}
////call only from main thread
@ -75,15 +79,15 @@ func (vp *ViewPort) ToVPCoords(c *types.Coords) (newCoords *types.Coords, err er
// redraw := false
// //fixme get player instance
//
// vp.Move(&vp.playerCoords)
// vp.Move(&vp.PlayerCoords)
// //fixme detect fovRecompute
// if fovRecompute {
// vp.layer.Clear(vp.Rect)
// vp.layer.ClearRect(vp.Rect)
// fovRecompute = false
// redraw = true
// //fixme
//
// vp.Fov.ComputeFov(vp.level, vp.playerCoords, vp.playerTorchRadius)
// vp.Fov.ComputeFov(vp.level, vp.PlayerCoords, vp.PlayerTorchRadius)
// }
// //increase ticker
// fpsTicker++
@ -119,26 +123,54 @@ func (vp *ViewPort) ToVPCoords(c *types.Coords) (newCoords *types.Coords, err er
var redraw = true
var fovRecompute = true
func (vp *ViewPort) Render() {
func (vp *ViewPort) Listen(state types.GameState) {
for {
select {
case <-state.FovRecompute:
fovRecompute = true
case <-state.Redraw:
redraw = true
}
}
}
func (vp *ViewPort) Render(state types.GameState) {
vp.Move(&vp.PlayerCoords, state)
if fovRecompute {
vp.layer.Clear(vp.Rect)
vp.layer.ClearRect(vp.Rect)
fovRecompute = false
redraw = true
vp.Fov.ComputeFov(vp.level, vp.playerCoords, vp.playerTorchRadius)
vp.Fov.ComputeFov(vp.level, vp.PlayerCoords, vp.PlayerTorchRadius)
}
if redraw {
//terrain
for y := 0; y < vp.H; y++ {
for x := 0; x < vp.W; x++ {
mapCoords := types.Coords{vp.X + x, vp.Y + y}
mapCoords := types.Coords{vp.cameraCoords.X + x, vp.cameraCoords.Y + y}
tile := vp.level.GetTile(mapCoords)
if tile.Explored || tile.MustDraw || tile.Visible {
vp.layer.PutToBase(mapCoords.X, mapCoords.Y, tile.GetChar(), tile.GetRawColor(), tile.GetRawBgColor())
if vp.level.InBounds(mapCoords) {
tile := vp.level.GetTile(mapCoords)
if tile.Explored || tile.MustDraw || tile.Visible {
vp.layer.PutToBase(x + vp.X, y + vp.Y, tile.GetChar(), tile.GetRawColor(), tile.GetRawBgColor())
}
}
}
}
//mobs
pc,err := vp.ToVPCoords(vp.PlayerCoords)
_ = pc
if err != nil {
fmt.Println("error on getting player position")
} else {
vp.layer.WithColor("white").Put(pc.X + vp.X, pc.Y + vp.Y, "@")
//mw.GetLayer("base").WithColor("white").Put(42, 10, "B")
//mw.GetLayer("overlay").WithColor("white").Put(59, 10, "O")
}
redraw = true
//redraw = false
}