This commit is contained in:
Josh Deprez 2021-08-24 19:33:21 +10:00
parent ac9bd0bcd6
commit 94b9711e4b
3 changed files with 55 additions and 3 deletions

View file

@ -16,7 +16,9 @@ import (
var ( var (
_ Drawer = PerfDisplay{} _ Drawer = PerfDisplay{}
_ DrawOrderer = PerfDisplay{} _ DrawOrderer = PerfDisplay{}
_ Hider = &PerfDisplay{}
_ Disabler = &GobDumper{}
_ Prepper = &GobDumper{} _ Prepper = &GobDumper{}
_ Updater = &GobDumper{} _ Updater = &GobDumper{}
) )
@ -26,9 +28,41 @@ func init() {
gob.Register(PerfDisplay{}) 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. // PerfDisplay debugprints CurrentTPS and CurrentFPS in the top left.
type PerfDisplay struct { type PerfDisplay struct {
Hidden bool Hidden
} }
func (p PerfDisplay) Draw(screen *ebiten.Image, _ ebiten.DrawImageOptions) { 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 // GobDumper waits for a given key combo, then dumps the game into a gob file
// in the current directory. // in the current directory.
type GobDumper struct { type GobDumper struct {
Disabled
KeyCombo []ebiten.Key KeyCombo []ebiten.Key
game *Game 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. // Update waits for the key combo, then dumps the game state into a gzipped gob.
func (d *GobDumper) Update() error { func (d *GobDumper) Update() error {
if d.Disabled {
return nil
}
for _, key := range d.KeyCombo { for _, key := range d.KeyCombo {
if !ebiten.IsKeyPressed(key) { if !ebiten.IsKeyPressed(key) {
return nil return nil

View file

@ -18,8 +18,10 @@ type Awakeman struct {
engine.Sprite engine.Sprite
CameraID string CameraID string
ToastID string
camera *engine.Camera camera *engine.Camera
toast *engine.DebugToast
vx, vy float64 vx, vy float64
facingLeft bool facingLeft bool
coyoteTimer int coyoteTimer int
@ -33,6 +35,13 @@ func (aw *Awakeman) Update() error {
// TODO: better cheat for noclip // TODO: better cheat for noclip
if inpututil.IsKeyJustPressed(ebiten.KeyN) { if inpututil.IsKeyJustPressed(ebiten.KeyN) {
aw.noclip = !aw.noclip 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 upd := aw.realUpdate
if aw.noclip { if aw.noclip {
@ -162,6 +171,7 @@ func (aw *Awakeman) realUpdate() error {
func (aw *Awakeman) Prepare(game *engine.Game) { func (aw *Awakeman) Prepare(game *engine.Game) {
aw.camera = game.Component(aw.CameraID).(*engine.Camera) aw.camera = game.Component(aw.CameraID).(*engine.Camera)
aw.toast, _ = game.Component(aw.ToastID).(*engine.DebugToast)
aw.animIdleLeft = &engine.Anim{Frames: []engine.AnimFrame{ aw.animIdleLeft = &engine.Anim{Frames: []engine.AnimFrame{
{Frame: 1, Duration: 60}, {Frame: 1, Duration: 60},

View file

@ -94,6 +94,7 @@ func main() {
}, },
&game.Awakeman{ &game.Awakeman{
CameraID: "game_camera", CameraID: "game_camera",
ToastID: "toast",
Sprite: engine.Sprite{ Sprite: engine.Sprite{
ID: "awakeman", ID: "awakeman",
Actor: engine.Actor{ Actor: engine.Actor{
@ -123,6 +124,9 @@ func main() {
ID: "game_camera", ID: "game_camera",
Scene: level1, Scene: level1,
}, },
&engine.DebugToast{
ID: "toast",
},
engine.PerfDisplay{}, engine.PerfDisplay{},
}, },
}, },