diff options
| author | Martin Kagamino Lehoux <martin@lehoux.net> | 2023-09-03 19:05:41 +0200 |
|---|---|---|
| committer | Martin Kagamino Lehoux <martin@lehoux.net> | 2023-09-03 19:05:41 +0200 |
| commit | 35b316123c6d768cfb064c1abfdbc41bb54b9e68 (patch) | |
| tree | 4f3cf24f71482dc704e81e756b79d0cb1d11a0e0 /move.go | |
| parent | e53b735c70b784f8bf8aa4d442f30dd8dfff6730 (diff) | |
feat: Move along grid
Diffstat (limited to 'move.go')
| -rw-r--r-- | move.go | 42 |
1 files changed, 36 insertions, 6 deletions
@@ -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) } |
