2021-07-23 13:12:54 +10:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
2021-08-02 12:16:10 +10:00
|
|
|
"compress/gzip"
|
2021-07-30 17:26:23 +10:00
|
|
|
"encoding/gob"
|
2021-08-01 16:41:10 +10:00
|
|
|
"errors"
|
2021-07-23 13:12:54 +10:00
|
|
|
"fmt"
|
2021-07-23 17:05:05 +10:00
|
|
|
"math"
|
2021-08-01 16:10:30 +10:00
|
|
|
"os"
|
|
|
|
"time"
|
2021-07-23 13:12:54 +10:00
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
|
|
|
|
)
|
|
|
|
|
2021-08-18 16:34:51 +10:00
|
|
|
var (
|
|
|
|
_ Drawer = PerfDisplay{}
|
|
|
|
_ DrawOrderer = PerfDisplay{}
|
2021-08-24 19:33:21 +10:00
|
|
|
_ Hider = &PerfDisplay{}
|
2021-08-18 16:34:51 +10:00
|
|
|
|
2021-08-24 19:33:21 +10:00
|
|
|
_ Disabler = &GobDumper{}
|
|
|
|
_ Prepper = &GobDumper{}
|
|
|
|
_ Updater = &GobDumper{}
|
2021-08-18 16:34:51 +10:00
|
|
|
)
|
|
|
|
|
2021-07-30 17:26:23 +10:00
|
|
|
func init() {
|
2021-08-25 15:04:38 +10:00
|
|
|
gob.Register(&GobDumper{})
|
|
|
|
gob.Register(&PerfDisplay{})
|
2021-07-30 17:26:23 +10:00
|
|
|
}
|
|
|
|
|
2021-08-24 19:33:21 +10:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2021-07-25 15:03:10 +10:00
|
|
|
// PerfDisplay debugprints CurrentTPS and CurrentFPS in the top left.
|
2021-08-01 17:08:26 +10:00
|
|
|
type PerfDisplay struct {
|
2021-08-24 19:33:21 +10:00
|
|
|
Hidden
|
2021-08-01 17:08:26 +10:00
|
|
|
}
|
2021-07-23 13:12:54 +10:00
|
|
|
|
2021-08-12 14:06:01 +10:00
|
|
|
func (p PerfDisplay) Draw(screen *ebiten.Image, _ ebiten.DrawImageOptions) {
|
2021-08-01 17:08:26 +10:00
|
|
|
if p.Hidden {
|
|
|
|
return
|
|
|
|
}
|
2021-07-25 15:03:10 +10:00
|
|
|
ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f FPS: %0.2f", ebiten.CurrentTPS(), ebiten.CurrentFPS()))
|
2021-07-23 13:12:54 +10:00
|
|
|
}
|
2021-07-23 13:46:19 +10:00
|
|
|
|
2021-08-18 15:23:02 +10:00
|
|
|
func (PerfDisplay) DrawOrder() float64 {
|
2021-07-23 17:05:05 +10:00
|
|
|
// Always draw on top
|
|
|
|
return math.MaxFloat64
|
2021-07-23 13:46:19 +10:00
|
|
|
}
|
2021-08-01 16:10:30 +10:00
|
|
|
|
|
|
|
// GobDumper waits for a given key combo, then dumps the game into a gob file
|
|
|
|
// in the current directory.
|
|
|
|
type GobDumper struct {
|
2021-08-24 19:33:21 +10:00
|
|
|
Disabled
|
2021-08-01 16:41:10 +10:00
|
|
|
KeyCombo []ebiten.Key
|
|
|
|
|
|
|
|
game *Game
|
|
|
|
}
|
|
|
|
|
2021-08-05 12:26:41 +10:00
|
|
|
// Prepare simply stores the reference to the Game.
|
|
|
|
func (d *GobDumper) Prepare(g *Game) { d.game = g }
|
2021-08-01 16:10:30 +10:00
|
|
|
|
2021-08-02 12:16:10 +10:00
|
|
|
// Update waits for the key combo, then dumps the game state into a gzipped gob.
|
2021-08-01 16:41:10 +10:00
|
|
|
func (d *GobDumper) Update() error {
|
2021-08-24 19:33:21 +10:00
|
|
|
if d.Disabled {
|
|
|
|
return nil
|
|
|
|
}
|
2021-08-01 16:41:10 +10:00
|
|
|
for _, key := range d.KeyCombo {
|
2021-08-01 16:10:30 +10:00
|
|
|
if !ebiten.IsKeyPressed(key) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2021-08-01 16:41:10 +10:00
|
|
|
if d.game == nil {
|
|
|
|
return errors.New("nil d.game in GobDumper.Update")
|
|
|
|
}
|
2021-08-02 12:16:10 +10:00
|
|
|
f, err := os.Create(time.Now().Format("20060102030405.gob.gz"))
|
2021-08-01 16:10:30 +10:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-08-01 16:41:10 +10:00
|
|
|
defer f.Close()
|
2021-08-02 12:16:10 +10:00
|
|
|
gz := gzip.NewWriter(f)
|
|
|
|
defer gz.Close()
|
|
|
|
if err := gob.NewEncoder(gz).Encode(d.game); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := gz.Close(); err != nil {
|
2021-08-01 16:41:10 +10:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return f.Close()
|
2021-08-01 16:10:30 +10:00
|
|
|
}
|