doctorlectro/system/renderdebug.go

74 lines
1.9 KiB
Go

package system
import (
"fmt"
"image"
"image/color"
_ "image/png"
"code.rocketnine.space/tslocum/doctorlectro/component"
"code.rocketnine.space/tslocum/doctorlectro/world"
"code.rocketnine.space/tslocum/gohan"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/jakecoffman/cp"
)
type RenderDebugTextSystem struct {
Position *component.Position
Velocity *component.Velocity
Player *component.Player
op *ebiten.DrawImageOptions
debugImg *ebiten.Image
}
func NewRenderDebugTextSystem() *RenderDebugTextSystem {
s := &RenderDebugTextSystem{
op: &ebiten.DrawImageOptions{},
debugImg: ebiten.NewImage(94, 114),
}
return s
}
func (s *RenderDebugTextSystem) Update(_ gohan.Entity) error {
return gohan.ErrUnregister
}
func (s *RenderDebugTextSystem) Draw(e gohan.Entity, screen *ebiten.Image) error {
if world.Debug <= 0 {
return nil
}
if world.Debug > 1 {
world.Space.EachBody(func(body *cp.Body) {
fillColor := color.RGBA{255, 255, 255, 255}
if body == world.PlayerBody {
fillColor = color.RGBA{124, 0, 255, 255}
}
body.EachShape(func(shape *cp.Shape) {
bb := shape.BB()
var emptyBB cp.BB
if bb != emptyBB {
l, t := world.LevelCoordinatesToScreen(bb.L, bb.T)
r, b := world.LevelCoordinatesToScreen(bb.R, bb.B)
rect := image.Rect(int(l), int(t), int(r), int(b))
screen.SubImage(rect).(*ebiten.Image).Fill(fillColor)
return
}
})
})
}
position := s.Position
vel := world.PlayerBody.Velocity()
s.debugImg.Fill(color.RGBA{0, 0, 0, 80})
ebitenutil.DebugPrint(s.debugImg, fmt.Sprintf("POS %.0f,%.0f\nVEL %.2f,%.2f\nENT %d\nUPD %d\nDRA %d\nTPS %0.0f\nFPS %0.0f", position.X, position.Y, vel.X, vel.Y, gohan.CurrentEntities(), gohan.CurrentUpdates(), gohan.CurrentDraws(), ebiten.CurrentTPS(), ebiten.CurrentFPS()))
screen.DrawImage(s.debugImg, nil)
return nil
}