doctorlectro/system/renderdebug.go

106 lines
3.1 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
}
drawBB := func(bb cp.BB, fill color.Color) {
screen.SubImage(world.BBToScreenRect(bb)).(*ebiten.Image).Fill(fill)
}
if world.Debug > 1 {
for _, shape := range world.CollisionShapes {
drawBB(shape.BB(), color.RGBA{255, 0, 0, 255})
}
for _, r := range world.TriggerRects {
x1, y1 := world.LevelCoordinatesToScreen(float64(r.Min.X), float64(r.Min.Y))
x2, y2 := world.LevelCoordinatesToScreen(float64(r.Max.X), float64(r.Max.Y))
screen.SubImage(image.Rect(int(x1), int(y1), int(x2), int(y2))).(*ebiten.Image).Fill(color.RGBA{0, 255, 0, 255})
}
playerBB := world.PlayerShape.BB()
fillColor := color.RGBA{124, 0, 255, 255}
drawBB(playerBB, fillColor)
if world.MagnetActive {
playerRect := world.BBToScreenRect(playerBB)
x1, y1 := playerRect.Min.X, playerRect.Min.Y
x2, y2 := playerRect.Max.X, playerRect.Max.Y
w, h := x2-x1, y2-y1
xHalf := x1 + w/2
yHalf := y1 + h/2
var magnetR image.Rectangle
switch world.MagnetDirection {
case world.MagnetizeDown:
magnetR = image.Rect(int(x1), int(yHalf), int(x2), int(y2))
case world.MagnetizeDownLeft:
magnetR = image.Rect(int(x1), int(yHalf), int(xHalf), int(y2))
case world.MagnetizeLeft:
magnetR = image.Rect(int(x1), int(y1), int(xHalf), int(y2))
case world.MagnetizeUpLeft:
magnetR = image.Rect(int(x1), int(y1), int(xHalf), int(yHalf))
case world.MagnetizeUp:
magnetR = image.Rect(int(x1), int(y1), int(x2), int(yHalf))
case world.MagnetizeUpRight:
magnetR = image.Rect(int(xHalf), int(y1), int(x2), int(yHalf))
case world.MagnetizeRight:
magnetR = image.Rect(int(xHalf), int(y1), int(x2), int(y2))
case world.MagnetizeDownRight:
magnetR = image.Rect(int(xHalf), int(yHalf), int(x2), int(y2))
}
magnetColor := color.RGBA{0, 200, 255, 255}
screen.SubImage(magnetR).(*ebiten.Image).Fill(magnetColor)
}
}
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
}