From d9a54f1319cc9dcc2861cecc9edfde14cf039c5b Mon Sep 17 00:00:00 2001 From: Martin Kagamino Lehoux Date: Sat, 6 Jun 2026 20:59:12 +0200 Subject: Add examples catalog with subcommand dispatch - Extract game engine from package main to engine/ library package - Create examples/ package with registry, Entry type, and BuilderDeps - Register default world as an example (moved from main()) - Add combat example: two teams of units facing each other - Rewrite main.go as thin dispatch: go run . example [name] - Update README with examples catalog one-liner --- examples/combat.go | 42 ++++++++++++++++++++++++++++++++++++++++++ examples/default.go | 40 ++++++++++++++++++++++++++++++++++++++++ examples/examples.go | 20 ++++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 examples/combat.go create mode 100644 examples/default.go create mode 100644 examples/examples.go (limited to 'examples') diff --git a/examples/combat.go b/examples/combat.go new file mode 100644 index 0000000..f2dd92f --- /dev/null +++ b/examples/combat.go @@ -0,0 +1,42 @@ +package examples + +import ( + "image/color" + + "age_of_empires/engine" + "age_of_empires/physics" + "age_of_empires/selection" +) + +func init() { + Registry["combat"] = Entry{ + Name: "Combat", + Description: "Two groups of units facing each other — ready for battle", + Build: buildCombat, + } +} + +func buildCombat(g *engine.Game, deps BuilderDeps) { + g.ResourceAmount = 500 + g.Camera = engine.Camera{X: 800, Y: 1000, Zoom: 1.0} + g.CurrentAction = engine.Selecting + g.UnitBuilder = deps.UnitBuilder + + // Red team (left) + redBuilder := engine.NewEntityBuilder(). + WithSolid(engine.NewFilledCircleImage(100, color.RGBA{0xff, 0x00, 0x00, 0xff})). + WithSelectable("round", selection.Unit). + WithMove().WithOrder().WithResourceGatherer(15) + for x := 0; x < 5; x++ { + g.Append(redBuilder.WithPosition(physics.Point{X: 200 + x*120, Y: 1000}).Build()) + } + + // Blue team (right) + blueBuilder := engine.NewEntityBuilder(). + WithSolid(engine.NewFilledCircleImage(100, color.RGBA{0x00, 0x00, 0xff, 0xff})). + WithSelectable("round", selection.Unit). + WithMove().WithOrder().WithResourceGatherer(15) + for x := 0; x < 5; x++ { + g.Append(blueBuilder.WithPosition(physics.Point{X: 1000 + x*120, Y: 1000}).Build()) + } +} diff --git a/examples/default.go b/examples/default.go new file mode 100644 index 0000000..d291a26 --- /dev/null +++ b/examples/default.go @@ -0,0 +1,40 @@ +package examples + +import ( + "image/color" + "time" + + "age_of_empires/engine" + "age_of_empires/physics" + "age_of_empires/selection" +) + +func init() { + Registry["default"] = Entry{ + Name: "Default", + Description: "Free play with a town center, iron mine, and gatherer units", + Build: buildDefault, + } +} + +func buildDefault(g *engine.Game, deps BuilderDeps) { + g.ResourceAmount = 150 + g.Camera = engine.Camera{X: 1000, Y: 1000, Zoom: 1.0} + g.CurrentAction = engine.Selecting + g.UnitBuilder = deps.UnitBuilder + + ironMineBuilder := engine.NewEntityBuilder(). + WithSolid(engine.NewFilledRectangleImage(physics.Point{X: 100, Y: 100}, color.RGBA{0x80, 0x80, 0x80, 0xff})). + WithResourceSource(1000). + WithSelectable("square", selection.Building) + g.Append(ironMineBuilder.WithPosition(physics.Point{X: 1000, Y: 1000}).Build()) + + townCenterBuilder := engine.NewEntityBuilder(). + WithSolid(engine.NewFilledRectangleImage(physics.Point{X: 100, Y: 100}, color.RGBA{0x0, 0x0, 0xff, 0xff})). + WithResourceStorage(). + WithSelectable("square", selection.Building). + WithSpawn(engine.NewSpawn(50, 1*time.Second)) + townCenter := g.Append(townCenterBuilder.WithPosition(physics.Point{X: 1000, Y: 2000}).Build()) + + _ = townCenter // suppress unused warning; future spawn loop will use it +} diff --git a/examples/examples.go b/examples/examples.go new file mode 100644 index 0000000..9e18d6b --- /dev/null +++ b/examples/examples.go @@ -0,0 +1,20 @@ +package examples + +import ( + "age_of_empires/engine" + + "github.com/hajimehoshi/ebiten/v2/text/v2" +) + +type BuilderDeps struct { + FaceSource *text.GoTextFaceSource + UnitBuilder engine.EntityBuilder +} + +type Entry struct { + Name string + Description string + Build func(*engine.Game, BuilderDeps) +} + +var Registry = map[string]Entry{} -- cgit v1.3