aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main.go27
-rw-r--r--order.go13
2 files changed, 26 insertions, 14 deletions
diff --git a/main.go b/main.go
index afb0bee..f2eb907 100644
--- a/main.go
+++ b/main.go
@@ -43,6 +43,7 @@ type Game struct {
UnitBuilder EntityBuilder
}
+// TODO: This could be a maintained index
func (g *Game) getMoveMap() physics.MoveMap {
blocked := map[physics.Point]bool{}
for _, e := range g.Entities {
@@ -70,6 +71,16 @@ func (g *Game) Closest(from physics.Point, candidates []physics.Point) (physics.
return closest, closestDistance
}
+// TODO: This could be a maintained index
+func (g *Game) entityAt(position physics.Point) *Entity {
+ for _, e := range g.Entities {
+ if e.Position.IsEnabled && e.Position.Value == position {
+ return e
+ }
+ }
+ return nil
+}
+
func (g *Game) updateSelecting(cursor physics.Point, moveMap physics.MoveMap) {
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
g.Selection.Start = cursor
@@ -77,22 +88,10 @@ func (g *Game) updateSelecting(cursor physics.Point, moveMap physics.MoveMap) {
}
if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonRight) {
destination := cursor.Div(100).Mul(100)
- var entityAtDestination *Entity
- for _, e := range g.Entities {
- if e.Position.IsEnabled && e.Position.Value == destination {
- entityAtDestination = e
- break
- }
- }
+ entityAtDestination := g.entityAt(destination)
slog.Info("destination", slog.String("destination", destination.String()))
for _, e := range g.Entities {
- if e.Selection.IsEnabled && e.Selection.Value.IsSelected {
- if entityAtDestination != nil && entityAtDestination.ResourceSource.IsEnabled {
- Gather(e, entityAtDestination, g)
- } else {
- physics.StartMove(&e.Move, e.Position, destination, moveMap)
- }
- }
+ e.MainAction(g, destination, entityAtDestination, moveMap)
}
}
if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonLeft) {
diff --git a/order.go b/order.go
index f1e89bc..f8ef06d 100644
--- a/order.go
+++ b/order.go
@@ -1,5 +1,7 @@
package main
+import "age_of_empires/physics"
+
type Order interface {
Update(e *Entity, g *Game)
}
@@ -10,3 +12,14 @@ func (e *Entity) UpdateOrder(g *Game) {
}
e.Order.Value.Update(e, g)
}
+
+func (e *Entity) MainAction(g *Game, destination physics.Point, entityAtDestination *Entity, moveMap physics.MoveMap) {
+ if !e.Selection.IsEnabled || !e.Selection.Value.IsSelected {
+ return
+ }
+ if entityAtDestination != nil && entityAtDestination.ResourceSource.IsEnabled {
+ Gather(e, entityAtDestination, g)
+ return
+ }
+ physics.StartMove(&e.Move, e.Position, destination, moveMap)
+}