diff options
| author | Martin Kagamino Lehoux <martin@lehoux.net> | 2025-07-09 08:58:14 +0200 |
|---|---|---|
| committer | Martin Kagamino Lehoux <martin@lehoux.net> | 2025-07-09 08:58:14 +0200 |
| commit | 9f30f800443e6496bbba264a375c126cb26b1760 (patch) | |
| tree | bc0da293f552a0ecf7cac356a6747c109ce9eb0c | |
| parent | 057adf65468e7b6ec544686b80433093a9f9deab (diff) | |
Spawn target for newly spawned units
| -rw-r--r-- | README.md | 4 | ||||
| -rw-r--r-- | gather_order.go | 27 | ||||
| -rw-r--r-- | main.go | 14 | ||||
| -rw-r--r-- | order.go | 19 | ||||
| -rw-r--r-- | spawn.go | 7 |
5 files changed, 46 insertions, 25 deletions
@@ -4,7 +4,6 @@ ## TODO -- spawn may register a target, on spawn unit will do as a right click on target (move, gather, ...) - time must come from engine, not now - selection priority: selecting units + building -> select only units - see the current action (debug?) @@ -12,8 +11,11 @@ - bigger map - move the map view - ecs array: on delete, replace by last and update id->index map +- perf: look for nil comparison -> more data oriented +- perf: keep a short array of selected ## Bugs + - select A, gather, move to B, (it stops), gather (no storage) - if B is < 1 cell away, it will go and merge with A - stops moving if someone settles at destination diff --git a/gather_order.go b/gather_order.go index aee8b37..be98ca4 100644 --- a/gather_order.go +++ b/gather_order.go @@ -85,20 +85,19 @@ func getAllStorageDockings(g *Game) []physics.Point { } func Gather(e *Entity, source *Entity, g *Game) Order { - if e.Selection.IsEnabled && e.Position.IsEnabled && e.Move.IsEnabled && e.ResourceGatherer.IsEnabled && e.Order.IsEnabled { - if e.Selection.Value.IsSelected { - slog.Info("gathering from", slog.String("source", source.Position.Value.String())) - e.Order.Value = &GatherOrder{source: source} - e.ResourceGatherer.Value.CurrentTarget = source - sourceDockings := physics.AdjacentPoints(source.Position.Value) - destination, distance := g.Closest(e.Position.Value, sourceDockings) - if distance == math.MaxInt { - slog.Info("no accessible storage, canceling gather order") - e.Order.Value = nil - return nil - } - physics.StartMove(&e.Move, e.Position, destination, g.getMoveMap()) - } + if !e.Position.IsEnabled || !e.Move.IsEnabled || !e.Order.IsEnabled { + return nil } + slog.Info("gathering from", slog.String("source", source.Position.Value.String())) + e.Order.Value = &GatherOrder{source: source} + e.ResourceGatherer.Value.CurrentTarget = source + sourceDockings := physics.AdjacentPoints(source.Position.Value) + destination, distance := g.Closest(e.Position.Value, sourceDockings) + if distance == math.MaxInt { + slog.Info("no accessible storage, canceling gather order") + e.Order.Value = nil + return nil + } + physics.StartMove(&e.Move, e.Position, destination, g.getMoveMap()) return &GatherOrder{source: source} } @@ -91,7 +91,10 @@ func (g *Game) updateSelecting(cursor physics.Point, moveMap physics.MoveMap) { entityAtDestination := g.entityAt(destination) slog.Info("destination", slog.String("destination", destination.String())) for _, e := range g.Entities { - e.MainAction(g, destination, entityAtDestination, moveMap) + if e.Selection.IsEnabled && e.Selection.Value.IsSelected { + e.MainAction(g, destination, entityAtDestination, moveMap) + + } } } if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonLeft) { @@ -179,7 +182,9 @@ func main() { iconImg, _, err := image.Decode(icon) kcore.Expect(err, "failed to decode icon") ebiten.SetWindowIcon([]image.Image{iconImg}) - game := &Game{} + game := &Game{ + ResourceAmount: 150, + } s, err := text.NewGoTextFaceSource(bytes.NewReader(goregular.TTF)) kcore.Expect(err, "failed to create font source") game.FaceSource = s @@ -187,10 +192,9 @@ func main() { game.Entities = append(game.Entities, &ironMine) townCenter := EntityBuilder{}.WithPosition(physics.Point{X: 1000, Y: 2000}).WithImage(NewFilledRectangleImage(physics.Point{X: 100, Y: 100}, color.RGBA{0x0, 0x0, 0xff, 0xff})).WithResourceStorage().WithSelection("square").WithSpawn(NewSpawn(50, 5*time.Second)).Build() game.Entities = append(game.Entities, &townCenter) - spawnPosition, _ := game.Closest(townCenter.Position.Value, physics.AdjacentPoints(townCenter.Position.Value)) game.UnitBuilder = EntityBuilder{}.WithImage(NewFilledCircleImage(100, color.White)).WithSelection("round").WithMove().WithOrder().WithResourceGatherer(15) - for i := 0; i < 2; i++ { - spawnPosition, _ = game.Closest(townCenter.Position.Value, physics.AdjacentPoints(townCenter.Position.Value)) + for i := 0; i < 0; i++ { + spawnPosition, _ := game.Closest(townCenter.Position.Value, physics.AdjacentPoints(townCenter.Position.Value)) person := game.UnitBuilder.Build() person.Position = ecs.C(spawnPosition) game.Entities = append(game.Entities, &person) @@ -1,6 +1,10 @@ package main -import "age_of_empires/physics" +import ( + "age_of_empires/ecs" + "age_of_empires/physics" + "log/slog" +) type Order interface { Update(e *Entity, g *Game) @@ -14,12 +18,17 @@ func (e *Entity) UpdateOrder(g *Game) { } func (e *Entity) MainAction(g *Game, destination physics.Point, entityAtDestination *Entity, moveMap physics.MoveMap) { - if !e.Selection.IsEnabled || !e.Selection.Value.IsSelected { + if entityAtDestination != nil && entityAtDestination.ResourceSource.IsEnabled && e.ResourceGatherer.IsEnabled { + Gather(e, entityAtDestination, g) return } - if entityAtDestination != nil && entityAtDestination.ResourceSource.IsEnabled { - Gather(e, entityAtDestination, g) + if e.Move.IsEnabled { + physics.StartMove(&e.Move, e.Position, destination, moveMap) + return + } + if e.Spawn.IsEnabled { + slog.Info("Setting spawn target") + e.Spawn.Value.SpawnTarget = ecs.C(destination) return } - physics.StartMove(&e.Move, e.Position, destination, moveMap) } @@ -3,6 +3,7 @@ package main import ( "age_of_empires/ecs" "age_of_empires/physics" + "log/slog" "time" ) @@ -14,6 +15,7 @@ type Spawn struct { UnitResourceCost int UnitSpawnDuration time.Duration Requests []spawnRequest + SpawnTarget ecs.Component[physics.Point] } func NewSpawn(unitResourceCost int, unitSpawnDuration time.Duration) Spawn { @@ -44,6 +46,11 @@ func UpdateSpawn(g *Game, spawn *ecs.Component[Spawn], position ecs.Component[ph unit := g.UnitBuilder.Build() unit.Position = ecs.C(spawnPosition) g.Entities = append(g.Entities, &unit) + slog.Info("Spawned unit") + if spawn.Value.SpawnTarget.IsEnabled { + slog.Info("Unit has spawn target") + unit.MainAction(g, spawn.Value.SpawnTarget.Value, g.entityAt(spawn.Value.SpawnTarget.Value), g.getMoveMap()) + } spawn.Value.Requests = spawn.Value.Requests[1:] if len(spawn.Value.Requests) > 0 { spawn.Value.Requests[0].start = now |
