diff options
| -rw-r--r-- | README.md | 1 | ||||
| -rw-r--r-- | draw.go | 4 | ||||
| -rw-r--r-- | ecs/component.go | 2 | ||||
| -rw-r--r-- | entity.go | 12 | ||||
| -rw-r--r-- | main.go | 16 | ||||
| -rw-r--r-- | selection/selection.go | 36 |
6 files changed, 35 insertions, 36 deletions
@@ -22,7 +22,6 @@ - [ ] ecs: bitfield for fast checking - [ ] ecs: rename to systems - [ ] perf: measure frame budget -- [ ] rename Selection to Selectable - [ ] extract todos in todo list - [ ] abstraction between ebiten and update to handle inputs in a simulation? - [ ] getMoveMap could be a maintained index @@ -26,8 +26,8 @@ func (g *Game) Draw(screen *ebiten.Image) { op := &ebiten.DrawImageOptions{} op.GeoM.Translate(float64(position.X), float64(position.Y)) screen.DrawImage(image, op) - if mask&ecs.CM_Selection == ecs.CM_Selection { - selectable := g.Selection[i] + if mask&ecs.CM_Selectable == ecs.CM_Selectable { + selectable := g.Selectable[i] if selectable.IsSelected { opt := &ebiten.DrawImageOptions{} opt.GeoM.Translate(float64(position.X-selection.HaloWidth/2), float64(position.Y-selection.HaloWidth/2)) diff --git a/ecs/component.go b/ecs/component.go index ff22e53..b5fca36 100644 --- a/ecs/component.go +++ b/ecs/component.go @@ -6,7 +6,7 @@ const ( CM_Position Mask = 1 << iota CM_RelBounds CM_Image - CM_Selection + CM_Selectable CM_Move CM_Order CM_PatrolOrder @@ -14,7 +14,7 @@ type Components struct { Position physics.Point RelBounds physics.Rectangle Image *ebiten.Image - Selection selection.Selection + Selectable selection.Selectable Move physics.Move Order OrderKind ResourceGatherer ResourceGatherer @@ -50,18 +50,18 @@ func (b EntityBuilder) WithSolid(image *ebiten.Image) EntityBuilder { var Red = color.RGBA{0xff, 0x00, 0x00, 0xff} -func (b EntityBuilder) WithSelection(haloKind string, priority selection.Priority) EntityBuilder { - b.components.Selection = selection.Selection{ +func (b EntityBuilder) WithSelectable(haloKind string, priority selection.Priority) EntityBuilder { + b.components.Selectable = selection.Selectable{ IsSelected: false, Halo: nil, Priority: priority, } - b.mask |= ecs.CM_Selection + b.mask |= ecs.CM_Selectable switch haloKind { case "round": - b.components.Selection.Halo = NewStrokeCircleImage(110, selection.HaloWidth, Red) + b.components.Selectable.Halo = NewStrokeCircleImage(110, selection.HaloWidth, Red) case "square": - b.components.Selection.Halo = NewStrokeRectangleImage(physics.Point{X: 110, Y: 110}, selection.HaloWidth, Red) + b.components.Selectable.Halo = NewStrokeRectangleImage(physics.Point{X: 110, Y: 110}, selection.HaloWidth, Red) } return b } @@ -43,7 +43,7 @@ type Game struct { Position []physics.Point RelBounds []physics.Rectangle Image []*ebiten.Image - Selection []selection.Selection + Selectable []selection.Selectable Move []physics.Move Order []OrderKind PatrolOrder []PatrolOrder @@ -65,7 +65,7 @@ func (g *Game) Append(components Components, mask ecs.Mask) ecs.Entity { g.Position = append(g.Position, components.Position) g.RelBounds = append(g.RelBounds, components.RelBounds) g.Image = append(g.Image, components.Image) - g.Selection = append(g.Selection, components.Selection) + g.Selectable = append(g.Selectable, components.Selectable) g.Move = append(g.Move, components.Move) g.Order = append(g.Order, components.Order) g.ResourceGatherer = append(g.ResourceGatherer, components.ResourceGatherer) @@ -115,7 +115,7 @@ const ( func (g *Game) updateSelecting(cursor physics.Point, moveMap physics.MoveMap) { if inpututil.IsKeyJustReleased(ebiten.KeyEscape) { - g.GlobalSelection.Unselect(g.Selection) + g.GlobalSelection.Unselect(g.Selectable) } if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) { g.GlobalSelection.Start(cursor) @@ -130,9 +130,9 @@ func (g *Game) updateSelecting(cursor physics.Point, moveMap physics.MoveMap) { if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonLeft) { if g.GlobalSelection.IsActive(cursor) { if g.GlobalSelection.IsArea(cursor) { - g.GlobalSelection.SelectMultiple(cursor, g.Mask, g.Selection, g.Position, g.RelBounds) + g.GlobalSelection.SelectMultiple(cursor, g.Mask, g.Selectable, g.Position, g.RelBounds) } else { - g.GlobalSelection.SelectSingle(cursor, g.Mask, g.Selection, g.Position, g.RelBounds) + g.GlobalSelection.SelectSingle(cursor, g.Mask, g.Selectable, g.Position, g.RelBounds) } } } @@ -208,11 +208,11 @@ func main() { s, err := text.NewGoTextFaceSource(bytes.NewReader(goregular.TTF)) kcore.Expect(err, "failed to create font source") game.FaceSource = s - ironMineBuilder := NewEntityBuilder().WithSolid(NewFilledRectangleImage(physics.Point{X: 100, Y: 100}, color.RGBA{0x80, 0x80, 0x80, 0xff})).WithResourceSource(1000).WithSelection("square", selection.Building) + ironMineBuilder := NewEntityBuilder().WithSolid(NewFilledRectangleImage(physics.Point{X: 100, Y: 100}, color.RGBA{0x80, 0x80, 0x80, 0xff})).WithResourceSource(1000).WithSelectable("square", selection.Building) game.Append(ironMineBuilder.WithPosition(physics.Point{X: 1000, Y: 1000}).Build()) - townCenterBuilder := NewEntityBuilder().WithSolid(NewFilledRectangleImage(physics.Point{X: 100, Y: 100}, color.RGBA{0x0, 0x0, 0xff, 0xff})).WithResourceStorage().WithSelection("square", selection.Building).WithSpawn(NewSpawn(50, 1*time.Second)) + townCenterBuilder := NewEntityBuilder().WithSolid(NewFilledRectangleImage(physics.Point{X: 100, Y: 100}, color.RGBA{0x0, 0x0, 0xff, 0xff})).WithResourceStorage().WithSelectable("square", selection.Building).WithSpawn(NewSpawn(50, 1*time.Second)) townCenter := game.Append(townCenterBuilder.WithPosition(physics.Point{X: 1000, Y: 2000}).Build()) - game.UnitBuilder = NewEntityBuilder().WithSolid(NewFilledCircleImage(100, color.White)).WithSelection("round", selection.Unit).WithMove().WithOrder().WithResourceGatherer(15) + game.UnitBuilder = NewEntityBuilder().WithSolid(NewFilledCircleImage(100, color.White)).WithSelectable("round", selection.Unit).WithMove().WithOrder().WithResourceGatherer(15) for i := 0; i < 0; i++ { spawnPosition, _ := physics.Closest(game.Position[townCenter], physics.AdjacentPoints(game.Position[townCenter]), game.getMoveMap()) game.Append(game.UnitBuilder.WithPosition(spawnPosition).Build()) diff --git a/selection/selection.go b/selection/selection.go index 612e212..e4afdaa 100644 --- a/selection/selection.go +++ b/selection/selection.go @@ -20,7 +20,7 @@ const ( Building ) -type Selection struct { +type Selectable struct { IsSelected bool Halo *ebiten.Image Priority Priority @@ -28,7 +28,7 @@ type Selection struct { type selectItem struct { ecs.Entity - *Selection + *Selectable } type GlobalSelection struct { @@ -57,9 +57,9 @@ func (s *GlobalSelection) Start(point physics.Point) { } type SelectTarget struct { - Selection *Selection - Position physics.Point - Bounds physics.Rectangle + Selectable *Selectable + Position physics.Point + Bounds physics.Rectangle } func (s GlobalSelection) Bounds(cursor physics.Point) physics.Rectangle { @@ -69,19 +69,19 @@ func (s GlobalSelection) Bounds(cursor physics.Point) physics.Rectangle { }.Canon() } -func (s *GlobalSelection) SelectMultiple(cursor physics.Point, masks []ecs.Mask, selections []Selection, positions []physics.Point, bounds []physics.Rectangle) { +func (s *GlobalSelection) SelectMultiple(cursor physics.Point, masks []ecs.Mask, selectables []Selectable, positions []physics.Point, bounds []physics.Rectangle) { s.buffer = make([]selectItem, 0) s.Selected = make([]ecs.Entity, 0) - required := ecs.CM_Position | ecs.CM_RelBounds | ecs.CM_Selection + required := ecs.CM_Position | ecs.CM_RelBounds | ecs.CM_Selectable for i, mask := range masks { if mask&required == required { - selection := &selections[i] + selectable := &selectables[i] position := positions[i] bound := bounds[i] - selection.IsSelected = false + selectable.IsSelected = false if s.Bounds(cursor).Overlaps(physics.Translate(bound, position)) { - s.buffer = append(s.buffer, selectItem{ecs.Entity(i), selection}) + s.buffer = append(s.buffer, selectItem{ecs.Entity(i), selectable}) } } } @@ -103,19 +103,19 @@ func (s *GlobalSelection) SelectMultiple(cursor physics.Point, masks []ecs.Mask, s.Clear() } -func (s *GlobalSelection) SelectSingle(cursor physics.Point, masks []ecs.Mask, selections []Selection, positions []physics.Point, bounds []physics.Rectangle) { +func (s *GlobalSelection) SelectSingle(cursor physics.Point, masks []ecs.Mask, selectables []Selectable, positions []physics.Point, bounds []physics.Rectangle) { s.Selected = []ecs.Entity{} s.buffer = []selectItem{} - required := ecs.CM_Position | ecs.CM_RelBounds | ecs.CM_Selection + required := ecs.CM_Position | ecs.CM_RelBounds | ecs.CM_Selectable for i, mask := range masks { if mask&required == required { - selection := &selections[i] + selectable := &selectables[i] position := positions[i] bound := bounds[i] - selection.IsSelected = false + selectable.IsSelected = false if cursor.In(physics.Translate(bound, position)) { - if len(s.buffer) == 0 || selection.Priority > s.buffer[0].Priority { - s.buffer = []selectItem{{ecs.Entity(i), selection}} + if len(s.buffer) == 0 || selectable.Priority > s.buffer[0].Priority { + s.buffer = []selectItem{{ecs.Entity(i), selectable}} } } } @@ -127,9 +127,9 @@ func (s *GlobalSelection) SelectSingle(cursor physics.Point, masks []ecs.Mask, s s.Clear() } -func (s *GlobalSelection) Unselect(selections []Selection) { +func (s *GlobalSelection) Unselect(selectables []Selectable) { for _, i := range s.Selected { - selections[i].IsSelected = false + selectables[i].IsSelected = false } s.Selected = []ecs.Entity{} } |
