alchemyst/ui/mainwindow/viewport.go

178 lines
4.1 KiB
Go

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"
"lab.zaar.be/thefish/alchemyst-go/engine/types"
)
var NotInViewError = errors.New("not in ViewPort")
type ViewPort struct {
*types.Rect
cameraCoords types.Coords
level *gamemap.Level
layer *Layer
Fov fov.Fov
PlayerCoords types.Coords
PlayerTorchRadius int
}
func NewViewPort(x, y, w, h int, level *gamemap.Level, layer *Layer) *ViewPort {
//fixme
fov := precomputed_shade.NewPrecomputedShade(15)
fov.Init()
vp := ViewPort{
Rect: &types.Rect{x, y, w, h},
level: level,
layer: layer,
Fov: fov,
}
vp.PlayerCoords = types.Coords{10, 10}
vp.PlayerTorchRadius = 10
return &vp
}
func (vp *ViewPort) Move(c *types.Coords, state types.GameState) {
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 - 1
}
if y > vp.level.H-vp.H {
x = vp.level.H - vp.H - 1
}
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) {
//coords on map to coords on vp
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
}
////call only from main thread
//func (vp *ViewPort) Render() {
// //fixme get these from state chan(s)
// var fpsTicker int
// var fovRecompute bool = true
// redraw := false
// //fixme get player instance
//
// vp.Move(&vp.PlayerCoords)
// //fixme detect fovRecompute
// if fovRecompute {
// vp.layer.ClearRect(vp.Rect)
// fovRecompute = false
// redraw = true
// //fixme
//
// vp.Fov.ComputeFov(vp.level, vp.PlayerCoords, vp.PlayerTorchRadius)
// }
// //increase ticker
// fpsTicker++
//
// if redraw || fpsTicker%(FPS_LIMIT/10) == 0 {
// fpsTicker = 0
//
// for y := 0; y < vp.H; y++ {
// for x := 0; x < vp.W; x++ {
// mapCoords := types.Coords{vp.X + x, vp.Y + y}
// tile := vp.level.Tiles[mapCoords.X][mapCoords.Y]
// visible := vp.Fov.IsInFov(mapCoords)
// if !visible {
// if tile.MustDraw {
// //darkened version of landscape
// vp.layer.WithRawColor(tile.ColorSet.DarkFg()).
// PutWithRawBackground(mapCoords.X, mapCoords.Y, tile.Char, tile.ColorSet.DarkBg())
// }
// } else {
// if redraw == true || tile.Colordance {
// vp.layer.WithRawColor(tile.ColorSet.Fg()).
// PutWithRawBackground(mapCoords.X, mapCoords.Y, tile.Char, tile.ColorSet.Bg())
// tile.Explored = true
// tile.MustDraw = true
// }
// }
// }
// }
//
// }
//}
var redraw = true
var fovRecompute = true
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.ClearRect(vp.Rect)
fovRecompute = false
redraw = true
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.cameraCoords.X + x, vp.cameraCoords.Y + y}
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
}
}