aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Kagamino Lehoux <martin@lehoux.net>2023-09-19 07:24:57 +0200
committerMartin Kagamino Lehoux <martin@lehoux.net>2023-09-19 07:24:57 +0200
commit129d4d173b7c55e7a24ed49c2f50b50c56192253 (patch)
tree00a32716281665c1f5e5f0a7de7e5b7332cf3c16
parent41ccd69496e5d15d05a44765fef15a14b24de8f4 (diff)
feat: Patroling order
-rw-r--r--entity.go1
-rw-r--r--main.go22
-rw-r--r--order.go44
3 files changed, 67 insertions, 0 deletions
diff --git a/entity.go b/entity.go
index 75c1604..d19c304 100644
--- a/entity.go
+++ b/entity.go
@@ -16,6 +16,7 @@ type Entity struct {
Image Component[*ebiten.Image]
Selection Component[Selection]
Move Component[Move]
+ Order Component[Order]
}
func (e Entity) Bounds() Rectangle {
diff --git a/main.go b/main.go
index 3418911..adca3ff 100644
--- a/main.go
+++ b/main.go
@@ -14,6 +14,7 @@ type Action string
const (
Selecting Action = "selecting"
+ Patroling Action = "patroling"
)
var soilColor = color.RGBA{0x60, 0x40, 0x20, 0xff}
@@ -68,19 +69,37 @@ func (g *Game) updateSelecting(cursor Point, moveMap MoveMap) {
}
}
+func (g *Game) updatePatroling(cursor Point) {
+ if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonRight) {
+ destination := cursor.Div(100).Mul(100)
+ for _, e := range g.Entities {
+ Patrol(e, destination)
+ }
+ g.CurrentAction = Selecting
+ }
+}
+
func (g *Game) Update() error {
x, y := ebiten.CursorPosition()
cursor := Point{x, y}
if inpututil.IsKeyJustReleased(ebiten.KeyEscape) {
g.CurrentAction = Selecting
+ slog.Info("selecting action")
+ }
+ if inpututil.IsKeyJustReleased(ebiten.KeyA) { // Should be Q
+ g.CurrentAction = Patroling
+ slog.Info("patroling action")
}
moveMap := g.getMoveMap()
switch g.CurrentAction {
case Selecting:
g.updateSelecting(cursor, moveMap)
+ case Patroling:
+ g.updatePatroling(cursor)
}
for _, e := range g.Entities {
e.UpdateMove(moveMap)
+ e.UpdateOrder(g)
}
return nil
}
@@ -120,12 +139,14 @@ func main() {
Selection: C(Selection{IsSelected: false}),
}
game.Entities = append(game.Entities, &ironMine)
+ var order Order
personImage := NewColorImage(Point{100, 100}, color.RGBA{0xff, 0xff, 0xff, 0xff})
person1 := Entity{
Position: C(Point{2000, 2000}),
Image: C(personImage),
Selection: C(Selection{IsSelected: false}),
Move: C(Move{IsActive: false}),
+ Order: C(order),
}
game.Entities = append(game.Entities, &person1)
person2 := Entity{
@@ -133,6 +154,7 @@ func main() {
Image: C(personImage),
Selection: C(Selection{IsSelected: false}),
Move: C(Move{IsActive: false}),
+ Order: C(order),
}
game.Entities = append(game.Entities, &person2)
game.CurrentAction = Selecting
diff --git a/order.go b/order.go
new file mode 100644
index 0000000..13c1b09
--- /dev/null
+++ b/order.go
@@ -0,0 +1,44 @@
+package main
+
+import "golang.org/x/exp/slog"
+
+type Order interface {
+ Update(e *Entity, g *Game)
+}
+
+type PatrolOrder struct {
+ origin Point
+ destination Point
+}
+
+func (o *PatrolOrder) Update(e *Entity, g *Game) {
+ if e.Position.IsEnabled && e.Move.IsEnabled && e.Order.IsEnabled {
+ if !e.Move.Value.IsActive {
+ moveMap := g.getMoveMap()
+ if e.Position.Value == o.destination {
+ e.StartMove(o.origin, moveMap)
+ } else {
+ e.StartMove(o.destination, moveMap)
+ }
+ }
+ }
+}
+
+func (e *Entity) UpdateOrder(g *Game) {
+ if e.Order.IsEnabled {
+ if e.Order.Value != nil {
+ e.Order.Value.Update(e, g)
+ }
+ }
+}
+
+func Patrol(e *Entity, destination Point) Order {
+ if e.Selection.IsEnabled && e.Position.IsEnabled && e.Move.IsEnabled && e.Order.IsEnabled {
+ if e.Selection.Value.IsSelected {
+ origin := e.Position.Value
+ slog.Info("patroling between", slog.String("origin", origin.String()), slog.String("destination", destination.String()))
+ e.Order.Value = &PatrolOrder{origin: origin, destination: destination}
+ }
+ }
+ return &PatrolOrder{origin: e.Position.Value, destination: destination}
+}