diff --git a/assets/loader-levels.go b/assets/loader-levels.go index 6035f1e..f23683b 100644 --- a/assets/loader-levels.go +++ b/assets/loader-levels.go @@ -2,10 +2,10 @@ package assets import ( "bufio" - "fmt" _ "image/png" "io/fs" "log" + "log/slog" "openquell/util" "os" "path/filepath" @@ -128,10 +128,11 @@ func LoadLevels(dir string) []RawLevel { for _, levelfile := range entries { if levelfile.Type().IsRegular() && strings.Contains(levelfile.Name(), ".lvl") { path := filepath.Join("assets", dir) - fmt.Printf("LOADING level %s/%s ... ", path, levelfile) level := ParseRawLevel(path, levelfile) - fmt.Printf("done\n") + levels = append(levels, level) + + slog.Debug("loaded level", "path", path, "file", levelfile) } } diff --git a/assets/loader-sprites.go b/assets/loader-sprites.go index 20e0ab5..6429ee3 100644 --- a/assets/loader-sprites.go +++ b/assets/loader-sprites.go @@ -2,10 +2,10 @@ package assets import ( "embed" - "fmt" "image" _ "image/png" "log" + "log/slog" "os" "path/filepath" "strings" @@ -30,13 +30,8 @@ func LoadImages(dir string) AssetRegistry { log.Fatalf("failed to read assets dir %s: %s", dir, err) } - for _, entry := range entries { - fmt.Println(entry.Name()) - } - for _, imagefile := range entries { path := filepath.Join("assets", dir, imagefile.Name()) - fmt.Printf("LOADING %s ... ", path) fd, err := os.Open(path) if err != nil { log.Fatalf("failed to open image file %s: %s", imagefile.Name(), err) @@ -51,7 +46,8 @@ func LoadImages(dir string) AssetRegistry { } images[name] = ebiten.NewImageFromImage(img) - fmt.Printf("done\n") + + slog.Debug("loaded asset", "path", path) } return images diff --git a/components/position.go b/components/position.go index 5a108d0..b1aefb8 100644 --- a/components/position.go +++ b/components/position.go @@ -3,6 +3,7 @@ package components import ( "fmt" "image" + "log/slog" . "openquell/config" ) @@ -83,10 +84,13 @@ func (tile *Position) Intersects(moving *Position, velocity *Velocity) (bool, *P is := tile.Rect.Bounds().Intersect(object.Rect.Bounds()) if is != image.ZR { - fmt.Printf(" velocity: %d,%d\n", velocity.Data.X, velocity.Data.Y) - fmt.Printf(" player: %s\n", moving.Rect) - fmt.Printf("moved player: %s\n", object.Rect) - fmt.Printf("collision at: %s\n", is) + slog.Debug("Intersect", + "velocity", velocity.Data, + "player", moving.Rect, + "moved player", object.Rect, + "collision at", is, + ) + // collision, snap into neighbouring tile depending on the direction switch velocity.Direction { case West: diff --git a/game/levelscene.go b/game/levelscene.go index 3441a94..a9a4fed 100644 --- a/game/levelscene.go +++ b/game/levelscene.go @@ -1,7 +1,7 @@ package game import ( - "fmt" + "log/slog" "openquell/assets" "github.com/hajimehoshi/ebiten/v2" @@ -43,7 +43,7 @@ func (scene *LevelScene) SetNext() int { func (scene *LevelScene) Update() error { if scene.CurrentLevel != scene.Game.Observer.CurrentLevel { - fmt.Printf("current: %d, next: %d\n", scene.CurrentLevel, scene.Game.Observer.CurrentLevel) + slog.Debug("level", "current", scene.CurrentLevel, "next", scene.Game.Observer.CurrentLevel) scene.CurrentLevel = scene.Game.Observer.CurrentLevel scene.Levels[scene.CurrentLevel].SetupGrid(scene.Game) } diff --git a/go.mod b/go.mod index 20c8c8d..3eb6fa8 100644 --- a/go.mod +++ b/go.mod @@ -10,10 +10,15 @@ require ( require ( github.com/alecthomas/repr v0.3.0 // indirect github.com/ebitengine/purego v0.5.0 // indirect + github.com/fatih/color v1.16.0 // indirect github.com/jezek/xgb v1.1.0 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/tlinden/yadu v0.1.2 // indirect golang.org/x/exp/shiny v0.0.0-20230817173708-d852ddb80c63 // indirect golang.org/x/image v0.12.0 // indirect golang.org/x/mobile v0.0.0-20230922142353-e2f452493d57 // indirect golang.org/x/sync v0.3.0 // indirect - golang.org/x/sys v0.12.0 // indirect + golang.org/x/sys v0.14.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 65ad455..8a3bfcb 100644 --- a/go.sum +++ b/go.sum @@ -2,12 +2,21 @@ github.com/alecthomas/repr v0.3.0 h1:NeYzUPfjjlqHY4KtzgKJiWd6sVq2eNUPTi34PiFGjY8 github.com/alecthomas/repr v0.3.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4= github.com/ebitengine/purego v0.5.0 h1:JrMGKfRIAM4/QVKaesIIT7m/UVjTj5GYhRSQYwfVdpo= github.com/ebitengine/purego v0.5.0/go.mod h1:ah1In8AOtksoNK6yk5z1HTJeUkC1Ez4Wk2idgGslMwQ= +github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM= +github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE= github.com/hajimehoshi/ebiten/v2 v2.6.5 h1:lALv+qhEK3CBWViyiGpz4YcR6slVJEjCiS7sExKZ9OE= github.com/hajimehoshi/ebiten/v2 v2.6.5/go.mod h1:TZtorL713an00UW4LyvMeKD8uXWnuIuCPtlH11b0pgI= github.com/jezek/xgb v1.1.0 h1:wnpxJzP1+rkbGclEkmwpVFQWpuE2PUGNUzP8SbfFobk= github.com/jezek/xgb v1.1.0/go.mod h1:nrhwO0FX/enq75I7Y7G8iN1ubpSGZEiA3v9e9GyRFlk= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mlange-42/arche v0.10.0 h1:fEFDAYMAnWa+xHc1oq4gVcA4PuEQOCGSRXSKITXawMw= github.com/mlange-42/arche v0.10.0/go.mod h1:gJ5J8vBreqrf4TcBomBFPGnWdE5P3qa4LtxYHn1gDcg= +github.com/tlinden/yadu v0.1.2 h1:TYYVnUJwziRJ9YPbIbRf9ikmDw0Q8Ifixm+J/kBQFh8= +github.com/tlinden/yadu v0.1.2/go.mod h1:l3bRmHKL9zGAR6pnBHY2HRPxBecf7L74BoBgOOpTcUA= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= @@ -33,9 +42,13 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o= golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q= +golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= @@ -49,3 +62,6 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/grid/grid.go b/grid/grid.go index ff79202..01ae99a 100644 --- a/grid/grid.go +++ b/grid/grid.go @@ -1,9 +1,9 @@ package grid import ( - "fmt" "image" "log" + "log/slog" "openquell/assets" "openquell/components" "openquell/config" @@ -66,7 +66,7 @@ func NewGrid(world *ecs.World, pos, _, render, speed, _ = playermapper.Get(entity) playerobserver.AddEntity(entity) speed.Value = config.PLAYERSPEED - fmt.Printf("player start pos: %d,%d\n", point.X*tilesize, point.Y*tilesize) + slog.Debug("player start pos", "X", point.X*tilesize, "Y", point.Y*tilesize, "Z", 191) case tile.Collectible: entity := colmapper.New() pos, render, _ = colmapper.Get(entity) diff --git a/main.go b/main.go index 2d2d15d..bd9fe81 100644 --- a/main.go +++ b/main.go @@ -2,9 +2,12 @@ package main import ( "log" + "log/slog" "openquell/game" + "os" "github.com/hajimehoshi/ebiten/v2" + "github.com/tlinden/yadu" ) const ( @@ -17,6 +20,19 @@ func main() { ebiten.SetWindowSize(width, height) ebiten.SetWindowTitle("openquell") + logLevel := &slog.LevelVar{} + opts := &yadu.Options{ + Level: logLevel, + AddSource: true, + } + + logLevel.Set(slog.LevelDebug) + + handler := yadu.NewHandler(os.Stdout, opts) + + debuglogger := slog.New(handler) + slog.SetDefault(debuglogger) + g := game.NewGame(width, height, cellsize, 0, game.Play) err := ebiten.RunGame(g) diff --git a/systems/collectible_system.go b/systems/collectible_system.go index f1f0cb8..14963e9 100644 --- a/systems/collectible_system.go +++ b/systems/collectible_system.go @@ -1,7 +1,7 @@ package systems import ( - "fmt" + "log/slog" "openquell/assets" "openquell/components" . "openquell/components" @@ -54,7 +54,7 @@ func (system *CollectibleSystem) Update() { ok, _ := playerposition.Intersects(colposition, playervelocity) if ok { - fmt.Printf("bumped into collectible %v\n", collectible) + slog.Debug("bumped into collectible", "collectible", collectible) particlepositions = append(particlepositions, colposition) EntitiesToRemove = append(EntitiesToRemove, query.Entity()) } diff --git a/systems/grid_system.go b/systems/grid_system.go index d78675c..689d045 100644 --- a/systems/grid_system.go +++ b/systems/grid_system.go @@ -1,7 +1,6 @@ package systems import ( - "fmt" "image" "image/draw" "openquell/components" @@ -141,16 +140,12 @@ func (system *GridSystem) BumpEdge( switch velocity.Direction { case East: X = 0 - fmt.Println("east X=0") case West: X = system.Width - system.Tilesize - fmt.Println("west X=max") case South: Y = 0 - fmt.Println("south y=0") case North: Y = system.Height - system.Tilesize - fmt.Println("north y=max") } newpos.Update(X, Y, system.Tilesize) diff --git a/systems/player_system.go b/systems/player_system.go index d2f829f..9b28343 100644 --- a/systems/player_system.go +++ b/systems/player_system.go @@ -1,7 +1,7 @@ package systems import ( - "fmt" + "log/slog" . "openquell/components" . "openquell/config" @@ -53,19 +53,17 @@ func (system PlayerSystem) Update() error { if velocity.Moving() { ok, newpos := system.Grid.BumpEdge(playerposition, velocity) if ok { - fmt.Printf("falling off the edge, new pos: %v\n", newpos) + slog.Debug("falling off the edge", "newpos", newpos) playerposition.Set(newpos) } else { ok, tilepos := system.Grid.GetSolidNeighborPosition(playerposition, velocity, true) if ok { intersects, newpos := tilepos.Intersects(playerposition, velocity) if intersects { - fmt.Printf("collision detected. tile: %s\n", tilepos) - fmt.Printf(" player: %s\n", playerposition) - fmt.Printf(" new: %s\n", newpos) + slog.Debug("collision detected", "tile", + tilepos, "player", playerposition, "new", newpos) playerposition.Set(newpos) - fmt.Printf(" player new: %s\n", playerposition) velocity.Change(Stop) } }