From 35b316123c6d768cfb064c1abfdbc41bb54b9e68 Mon Sep 17 00:00:00 2001 From: Martin Kagamino Lehoux Date: Sun, 3 Sep 2023 19:05:41 +0200 Subject: feat: Move along grid --- main.go | 19 ++++++++++++------- move.go | 42 ++++++++++++++++++++++++++++++++++++------ person.go | 4 ++-- 3 files changed, 50 insertions(+), 15 deletions(-) diff --git a/main.go b/main.go index 718e261..dd36ea1 100644 --- a/main.go +++ b/main.go @@ -40,14 +40,19 @@ func (g *Game) Update() error { g.Selection.IsActive = true } if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonRight) { - tileClick := Point{x - soil.Position.X, y - soil.Position.Y} + destination := Point{ + (x-soil.Position.X)/100*100 + 50, + (y-soil.Position.Y)/100*100 + 50, + } for _, p := range g.Persons { - futureBounds := Rectangle{ - tileClick.Sub(p.size.Div(2)), - tileClick.Add(p.size.Div(2)), - } - if p.IsSelected && futureBounds.In(soil.Bounds()) { - p.MoveTo(tileClick) + if p.IsSelected { + futureBounds := Rectangle{ + destination.Sub(p.size.Div(2)), + destination.Add(p.size.Div(2)), + } + if futureBounds.In(soil.Bounds()) { + p.MoveTo(destination) + } } } } diff --git a/move.go b/move.go index f2cee2b..97d4809 100644 --- a/move.go +++ b/move.go @@ -1,19 +1,49 @@ package main +import ( + "math" +) + type Move struct { IsActive bool + Origin Point Destination Point + Nodes []Point +} + +func NewMove(origin Point, destination Point) Move { + nodes := []Point{} + current := Point{ + origin.X/100*100 + 50, + origin.Y/100*100 + 50, + } + for current != destination { + next := current + if math.Abs(float64(destination.X-current.X)) > math.Abs(float64(destination.Y-current.Y)) { + next.X = current.X + int(math.Copysign(100, float64(destination.X-current.X))) + } else { + next.Y = current.Y + int(math.Copysign(100, float64(destination.Y-current.Y))) + } + nodes = append(nodes, next) + current = next + } + return Move{IsActive: true, Origin: origin, Destination: destination, Nodes: nodes} } func (m *Move) Update(position Point, speed int) Point { if !m.IsActive { return position } - remainingMove := m.Destination.Sub(position) - delta := remainingMove.Mul(speed).Div(int(Length(remainingMove))) - next := position.Add(delta) - if Distance(next, m.Destination) < float64(speed) { - m.IsActive = false + next := m.Nodes[0] + if Distance(position, next) < float64(speed) { + if next == m.Destination { + m.IsActive = false + } else { + m.Nodes = m.Nodes[1:] + } + return next } - return next + remainingMove := next.Sub(position) + delta := remainingMove.Mul(speed).Div(int(Length(remainingMove))) + return position.Add(delta) } diff --git a/person.go b/person.go index 9ca5743..66e30cd 100644 --- a/person.go +++ b/person.go @@ -41,9 +41,9 @@ func (p Person) CollisionBounds() Rectangle { } func (p *Person) MoveTo(destination Point) { - p.move = Move{IsActive: true, Destination: destination} + p.move = NewMove(p.Position, destination) } func (p *Person) Update() { - p.Position = p.move.Update(p.Position, 30) + p.Position = p.move.Update(p.Position, 10) } -- cgit v1.3