commandeuropa/system/renderdebug.go

87 lines
2.2 KiB
Go

package system
import (
"fmt"
"image/color"
"code.rocketnine.space/tslocum/commandeuropa/component"
"code.rocketnine.space/tslocum/commandeuropa/world"
"code.rocketnine.space/tslocum/gohan"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
)
type RenderDebug struct {
Once *component.Once
op *ebiten.DrawImageOptions
debugImg *ebiten.Image
initialized bool
}
func (s *RenderDebug) Initialize() {
s.op = &ebiten.DrawImageOptions{}
s.op.GeoM.Scale(2, 2)
s.debugImg = ebiten.NewImage(80, 80)
s.initialized = true
}
func (s *RenderDebug) Update(_ gohan.Entity) error {
return gohan.ErrUnregister
}
func (s *RenderDebug) Draw(e gohan.Entity, screen *ebiten.Image) error {
if world.Debug <= 0 {
return nil
}
if !s.initialized {
s.Initialize()
}
if world.Debug > 1 {
for x := 0; x < world.MapSize; x++ {
for y := 0; y < world.MapSize; y++ {
for ox := 0; ox < 4; ox++ {
for oy := 0; oy < 4; oy++ {
px, py := float64(x*4+ox), float64(y*4+oy)
fillColor := color.RGBA{255, 0, 0, 255}
if world.Map[world.PathTileIndex(px, py)].Walkable {
fillColor = color.RGBA{0, 255, 0, 255}
}
drawX, drawY := world.LevelCoordinatesToScreen(px, py)
// Skip drawing off-screen tiles.
if world.PixelCoordinatesOffScreen(drawX, drawY) {
continue
}
screen.SubImage(floatRect(drawX, drawY, drawX+4*world.CamScale, drawY+4*world.CamScale)).(*ebiten.Image).Fill(fillColor)
}
}
}
}
if len(world.MapPath) > 0 {
for _, xy := range world.MapPath {
x, y := xy[0], xy[1]
drawX, drawY := world.LevelCoordinatesToScreen(x, y)
// Skip drawing off-screen tiles.
if world.PixelCoordinatesOffScreen(drawX, drawY) {
continue
}
fillColor := color.RGBA{255, 255, 255, 255}
screen.SubImage(floatRect(drawX, drawY, drawX+4*world.CamScale, drawY+4*world.CamScale)).(*ebiten.Image).Fill(fillColor)
}
}
}
s.debugImg.Fill(color.RGBA{0, 0, 0, 80})
ebitenutil.DebugPrintAt(s.debugImg, fmt.Sprintf("ENT %d\nUPD %d\nDRA %d\nTPS %0.0f\nFPS %0.0f", gohan.CurrentEntities(), gohan.CurrentUpdates(), gohan.CurrentDraws(), ebiten.CurrentTPS(), ebiten.CurrentFPS()), 2, 0)
screen.DrawImage(s.debugImg, s.op)
return nil
}