diff options
| author | Martin Kagamino Lehoux <martin@lehoux.net> | 2025-07-24 15:04:11 +0200 |
|---|---|---|
| committer | Martin Kagamino Lehoux <martin@lehoux.net> | 2025-07-24 15:04:11 +0200 |
| commit | e6e4ce18083ae5552563eab3291ac1642371d025 (patch) | |
| tree | b1af832ada8bd9c327c64872c01a76b25dc2c68c | |
| parent | d9fcd87061b1ca652398f331deef58630279bed3 (diff) | |
refactor: Props based entity builder for evolutivity
| -rw-r--r-- | entity.go | 34 | ||||
| -rw-r--r-- | main.go | 6 |
2 files changed, 24 insertions, 16 deletions
@@ -27,64 +27,72 @@ func (e Entity) Bounds() physics.Rectangle { } } -type EntityBuilder Entity +type EntityBuilder struct { + entity Entity +} + +func NewEntityBuilder() EntityBuilder { + return EntityBuilder{ + entity: Entity{}, + } +} func (b EntityBuilder) WithPosition(position physics.Point) EntityBuilder { - b.Position = ecs.C(position) + b.entity.Position = ecs.C(position) return b } func (b EntityBuilder) WithImage(image *ebiten.Image) EntityBuilder { - b.Image = ecs.C(image) + b.entity.Image = ecs.C(image) return b } var red = color.RGBA{0xff, 0x00, 0x00, 0xff} func (b EntityBuilder) WithSelection(haloKind string) EntityBuilder { - b.Selection = ecs.C(Selection{ + b.entity.Selection = ecs.C(Selection{ IsSelected: false, Halo: nil, }) switch haloKind { case "round": - b.Selection.Value.Halo = NewStrokeCircleImage(110, SELECTION_HALO_WIDTH, red) + b.entity.Selection.Value.Halo = NewStrokeCircleImage(110, SELECTION_HALO_WIDTH, red) case "square": - b.Selection.Value.Halo = NewStrokeRectangleImage(physics.Point{X: 110, Y: 110}, SELECTION_HALO_WIDTH, red) + b.entity.Selection.Value.Halo = NewStrokeRectangleImage(physics.Point{X: 110, Y: 110}, SELECTION_HALO_WIDTH, red) } return b } func (b EntityBuilder) WithResourceStorage() EntityBuilder { - b.ResourceStorage = ecs.C(ResourceStorage{}) + b.entity.ResourceStorage = ecs.C(ResourceStorage{}) return b } func (b EntityBuilder) WithResourceSource(amount int) EntityBuilder { - b.ResourceSource = ecs.C(ResourceSource{Remaining: amount}) + b.entity.ResourceSource = ecs.C(ResourceSource{Remaining: amount}) return b } func (b EntityBuilder) WithSpawn(spawn Spawn) EntityBuilder { - b.Spawn = ecs.C(spawn) + b.entity.Spawn = ecs.C(spawn) return b } func (b EntityBuilder) WithMove() EntityBuilder { - b.Move = ecs.C(physics.Move{IsActive: false}) + b.entity.Move = ecs.C(physics.Move{IsActive: false}) return b } func (b EntityBuilder) WithOrder() EntityBuilder { - b.Order.IsEnabled = true + b.entity.Order.IsEnabled = true return b } func (b EntityBuilder) WithResourceGatherer(maxCapacity int) EntityBuilder { - b.ResourceGatherer = ecs.C(ResourceGatherer{MaxCapacity: maxCapacity}) + b.entity.ResourceGatherer = ecs.C(ResourceGatherer{MaxCapacity: maxCapacity}) return b } func (b EntityBuilder) Build() Entity { - return Entity(b) + return b.entity } @@ -187,11 +187,11 @@ func main() { s, err := text.NewGoTextFaceSource(bytes.NewReader(goregular.TTF)) kcore.Expect(err, "failed to create font source") game.FaceSource = s - ironMine := EntityBuilder{}.WithPosition(physics.Point{X: 1000, Y: 1000}).WithImage(NewFilledRectangleImage(physics.Point{X: 100, Y: 100}, color.RGBA{0x80, 0x80, 0x80, 0xff})).WithResourceSource(1000).WithSelection("square").Build() + ironMine := NewEntityBuilder().WithPosition(physics.Point{X: 1000, Y: 1000}).WithImage(NewFilledRectangleImage(physics.Point{X: 100, Y: 100}, color.RGBA{0x80, 0x80, 0x80, 0xff})).WithResourceSource(1000).WithSelection("square").Build() 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() + townCenter := NewEntityBuilder().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, 1*time.Second)).Build() game.Entities = append(game.Entities, &townCenter) - game.UnitBuilder = EntityBuilder{}.WithImage(NewFilledCircleImage(100, color.White)).WithSelection("round").WithMove().WithOrder().WithResourceGatherer(15) + game.UnitBuilder = NewEntityBuilder().WithImage(NewFilledCircleImage(100, color.White)).WithSelection("round").WithMove().WithOrder().WithResourceGatherer(15) for i := 0; i < 0; i++ { spawnPosition, _ := game.Closest(townCenter.Position.Value, physics.AdjacentPoints(townCenter.Position.Value)) person := game.UnitBuilder.Build() |
