diff options
| -rw-r--r-- | entity.go | 1 | ||||
| -rw-r--r-- | main.go | 22 | ||||
| -rw-r--r-- | order.go | 44 |
3 files changed, 67 insertions, 0 deletions
@@ -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 { @@ -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} +} |
