toast!
This commit is contained in:
parent
ac9bd0bcd6
commit
94b9711e4b
3 changed files with 55 additions and 3 deletions
|
@ -16,7 +16,9 @@ import (
|
|||
var (
|
||||
_ Drawer = PerfDisplay{}
|
||||
_ DrawOrderer = PerfDisplay{}
|
||||
_ Hider = &PerfDisplay{}
|
||||
|
||||
_ Disabler = &GobDumper{}
|
||||
_ Prepper = &GobDumper{}
|
||||
_ Updater = &GobDumper{}
|
||||
)
|
||||
|
@ -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
|
||||
|
|
10
game/aw.go
10
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},
|
||||
|
|
4
main.go
4
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{},
|
||||
},
|
||||
},
|
||||
|
|
Loading…
Reference in a new issue