blob: 97d4809a6ef1ff5cc504af32b36cf7cc0bd23ea2 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
}
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
}
remainingMove := next.Sub(position)
delta := remainingMove.Mul(speed).Div(int(Length(remainingMove)))
return position.Add(delta)
}
|