aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md4
-rw-r--r--gather_order.go27
-rw-r--r--main.go14
-rw-r--r--order.go19
-rw-r--r--spawn.go7
5 files changed, 46 insertions, 25 deletions
diff --git a/README.md b/README.md
index c013a54..f25dc35 100644
--- a/README.md
+++ b/README.md
@@ -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}
}
diff --git a/main.go b/main.go
index f2eb907..cecc1d6 100644
--- a/main.go
+++ b/main.go
@@ -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)
diff --git a/order.go b/order.go
index f8ef06d..eba71f9 100644
--- a/order.go
+++ b/order.go
@@ -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)
}
diff --git a/spawn.go b/spawn.go
index 85ce24e..bf0e561 100644
--- a/spawn.go
+++ b/spawn.go
@@ -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