diff --git a/engine/debug.go b/engine/debug.go index a27a05d..514ec3b 100644 --- a/engine/debug.go +++ b/engine/debug.go @@ -16,9 +16,11 @@ import ( var ( _ Drawer = PerfDisplay{} _ DrawOrderer = PerfDisplay{} + _ Hider = &PerfDisplay{} - _ Prepper = &GobDumper{} - _ Updater = &GobDumper{} + _ Disabler = &GobDumper{} + _ Prepper = &GobDumper{} + _ Updater = &GobDumper{} ) func init() { @@ -26,9 +28,41 @@ func init() { gob.Register(PerfDisplay{}) } +// DebugToast debugprints a string for a while, then disappears. +type DebugToast struct { + ID + Hidden + Timer int // ticks + Text string +} + +func (d *DebugToast) Draw(screen *ebiten.Image, _ ebiten.DrawImageOptions) { + if d.Hidden { + return + } + ebitenutil.DebugPrintAt(screen, d.Text, 0, 20) +} + +func (d *DebugToast) DrawOrder() float64 { + return math.MaxFloat64 +} + +func (d *DebugToast) Toast(text string) { + d.Text = text + d.Timer = 120 + d.Hidden = false +} + +func (d *DebugToast) Update() error { + if d.Hidden = d.Timer <= 0; !d.Hidden { + d.Timer-- + } + return nil +} + // PerfDisplay debugprints CurrentTPS and CurrentFPS in the top left. type PerfDisplay struct { - Hidden bool + Hidden } func (p PerfDisplay) Draw(screen *ebiten.Image, _ ebiten.DrawImageOptions) { @@ -46,6 +80,7 @@ func (PerfDisplay) DrawOrder() float64 { // GobDumper waits for a given key combo, then dumps the game into a gob file // in the current directory. type GobDumper struct { + Disabled KeyCombo []ebiten.Key game *Game @@ -56,6 +91,9 @@ func (d *GobDumper) Prepare(g *Game) { d.game = g } // Update waits for the key combo, then dumps the game state into a gzipped gob. func (d *GobDumper) Update() error { + if d.Disabled { + return nil + } for _, key := range d.KeyCombo { if !ebiten.IsKeyPressed(key) { return nil diff --git a/game/aw.go b/game/aw.go index c8e3ad7..4563f74 100644 --- a/game/aw.go +++ b/game/aw.go @@ -18,8 +18,10 @@ type Awakeman struct { engine.Sprite CameraID string + ToastID string camera *engine.Camera + toast *engine.DebugToast vx, vy float64 facingLeft bool coyoteTimer int @@ -33,6 +35,13 @@ func (aw *Awakeman) Update() error { // TODO: better cheat for noclip if inpututil.IsKeyJustPressed(ebiten.KeyN) { aw.noclip = !aw.noclip + if aw.toast != nil { + if aw.noclip { + aw.toast.Toast("noclip enabled") + } else { + aw.toast.Toast("noclip disabled") + } + } } upd := aw.realUpdate if aw.noclip { @@ -162,6 +171,7 @@ func (aw *Awakeman) realUpdate() error { func (aw *Awakeman) Prepare(game *engine.Game) { aw.camera = game.Component(aw.CameraID).(*engine.Camera) + aw.toast, _ = game.Component(aw.ToastID).(*engine.DebugToast) aw.animIdleLeft = &engine.Anim{Frames: []engine.AnimFrame{ {Frame: 1, Duration: 60}, diff --git a/main.go b/main.go index d0a29cb..b9d9c49 100644 --- a/main.go +++ b/main.go @@ -94,6 +94,7 @@ func main() { }, &game.Awakeman{ CameraID: "game_camera", + ToastID: "toast", Sprite: engine.Sprite{ ID: "awakeman", Actor: engine.Actor{ @@ -123,6 +124,9 @@ func main() { ID: "game_camera", Scene: level1, }, + &engine.DebugToast{ + ID: "toast", + }, engine.PerfDisplay{}, }, },