aboutsummaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorMartin Kagamino Lehoux <martin@lehoux.net>2026-05-07 12:07:02 +0200
committerMartin Kagamino Lehoux <martin@lehoux.net>2026-05-07 12:07:02 +0200
commit20abde4dd780b51840e3498336bf907f0fd9164c (patch)
treea00d0fd7997651a1123bfbc9ccecb29d39615f1d /main.go
parent5c2d9ea10c9ed41fee41067c6d0d8d7fade8297c (diff)
Add Input abstraction between ebiten and simulation update
Introduces Input snapshot struct collected once per tick via CollectInput, and UpdateSimulation(Input) as the ebiten-free simulation entrypoint. Integration tests now drive the game through simulated inputs rather than direct method calls. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Diffstat (limited to 'main.go')
-rw-r--r--main.go59
1 files changed, 29 insertions, 30 deletions
diff --git a/main.go b/main.go
index 84ce87d..f0c7e51 100644
--- a/main.go
+++ b/main.go
@@ -14,7 +14,6 @@ import (
"age_of_empires/selection"
"github.com/hajimehoshi/ebiten/v2"
- "github.com/hajimehoshi/ebiten/v2/inpututil"
"github.com/hajimehoshi/ebiten/v2/text/v2"
"github.com/martinlehoux/kagamigo/kcore"
"golang.org/x/exp/slog"
@@ -114,32 +113,28 @@ const (
KeyPatrolRequest = ebiten.KeyQ
)
-func (g *Game) updateSelecting(cursor physics.Point, moveMap physics.MoveMap) {
- if inpututil.IsKeyJustReleased(ebiten.KeyEscape) {
- g.GlobalSelection.Unselect(g.Selectable)
- }
- if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
- g.GlobalSelection.Start(cursor)
- sx, sy := ebiten.CursorPosition()
- g.GlobalSelection.StartScreen(physics.Point{X: sx, Y: sy})
+func (g *Game) updateSelecting(input Input, moveMap physics.MoveMap) {
+ if input.LeftMouseDown {
+ g.GlobalSelection.Start(input.Cursor)
+ g.GlobalSelection.StartScreen(input.ScreenCursor)
}
- if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonRight) {
- destination := cursor.Div(100).Mul(100)
+ if input.RightMouseUp {
+ destination := input.Cursor.Div(100).Mul(100)
slog.Info("destination", slog.String("destination", destination.String()))
for _, i := range g.GlobalSelection.Selected {
MainAction(g, i, destination, moveMap)
}
}
- if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonLeft) {
- if g.GlobalSelection.IsActive(cursor) {
- if g.GlobalSelection.IsArea(cursor) {
- g.GlobalSelection.SelectMultiple(cursor, g.Mask, g.Selectable, g.Position, g.RelBounds)
+ if input.LeftMouseUp {
+ if g.GlobalSelection.IsActive(input.Cursor) {
+ if g.GlobalSelection.IsArea(input.Cursor) {
+ g.GlobalSelection.SelectMultiple(input.Cursor, g.Mask, g.Selectable, g.Position, g.RelBounds)
} else {
- g.GlobalSelection.SelectSingle(cursor, g.Mask, g.Selectable, g.Position, g.RelBounds)
+ g.GlobalSelection.SelectSingle(input.Cursor, g.Mask, g.Selectable, g.Position, g.RelBounds)
}
}
}
- if inpututil.IsKeyJustReleased(KeySpawnRequest) {
+ if input.SpawnPressed {
required := ecs.CM_Spawn
for _, i := range g.GlobalSelection.Selected {
if g.Mask[i]&required == required {
@@ -153,32 +148,36 @@ func (g *Game) updateSelecting(cursor physics.Point, moveMap physics.MoveMap) {
}
}
-func (g *Game) Update() error {
- g.Camera.Update(1.0 / 60.0)
- sx, sy := ebiten.CursorPosition()
- cursor := g.Camera.ScreenToWorld(sx, sy)
- if inpututil.IsKeyJustReleased(ebiten.KeyEscape) {
+func (g *Game) UpdateSimulation(input Input) {
+ moveMap := g.getMoveMap()
+
+ if input.EscapePressed {
+ g.GlobalSelection.Unselect(g.Selectable)
g.CurrentAction = Selecting
- slog.Info("selecting action")
- }
- if inpututil.IsKeyJustReleased(KeyPatrolRequest) {
+ } else if input.PatrolPressed {
g.CurrentAction = Patrolling
- slog.Info("patrolling action")
}
+
switch g.CurrentAction {
case Selecting:
- g.updateSelecting(cursor, g.getMoveMap())
+ g.updateSelecting(input, moveMap)
case Patrolling:
- if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonRight) {
- destination := cursor.Div(100).Mul(100)
+ if input.RightMouseUp {
+ destination := input.Cursor.Div(100).Mul(100)
StartPatrolSystem(destination, g.Mask, g.Position, g.Move, g.Order, g.PatrolOrder)
g.CurrentAction = Selecting
}
}
- physics.UpdateMoveSystem(g.getMoveMap(), g.Mask, g.Move, g.Position)
+
+ physics.UpdateMoveSystem(moveMap, g.Mask, g.Move, g.Position)
UpdateSpawnSystem(g, g.Now(), g.Mask, g.Spawn, g.Position)
UpdateGatherSystem(g, g.Now(), g.getMoveMap(), g.Mask, g.Order, g.Position, g.Move, g.ResourceGatherer)
UpdatePatrolSystem(g.getMoveMap(), g.Mask, g.Order, g.PatrolOrder, g.Position, g.Move)
+}
+
+func (g *Game) Update() error {
+ g.Camera.Update(1.0 / 60.0)
+ g.UpdateSimulation(CollectInput(&g.Camera))
return nil
}