/
move.go
53 lines (43 loc) · 1.2 KB
/
move.go
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
50
51
52
53
package physics
import (
"github.com/faiface/pixel"
"github.com/shamanr/battle_citty/internal/interfaces"
"time"
)
func (p *Physics) MoveObjects(sceneMap interfaces.SceneMap, dt time.Duration) {
loop:
for _, currentObj := range sceneMap {
if !isMoving(currentObj) {
continue
}
movement := calcMovement(currentObj, dt, p.frameDuration)
newBounds := bounds(currentObj).Moved(movement)
for _, anotherObj := range sceneMap {
if anotherObj == currentObj {
continue
}
if !p.areColliable(currentObj, anotherObj) {
continue
}
if newBounds.Intersects(bounds(anotherObj)) {
anotherObj.OnCollide(currentObj)
currentObj.OnCollide(anotherObj)
continue loop
}
}
newPos := currentObj.GetPos().Add(movement)
currentObj.SetPos(&newPos)
}
}
func calcMovement(currentObj interfaces.SceneObject, dt, fd time.Duration) pixel.Vec {
frames := dt / fd
movement := currentObj.GetSpeed().Scaled(float64(frames))
return movement
}
func isMoving(object interfaces.SceneObject) bool {
return !object.GetSpeed().Eq(pixel.ZV)
}
func bounds(obj interfaces.SceneObject) pixel.Rect {
b := obj.GetSize().Moved(*obj.GetPos())
return b.Resized(b.Center(), pixel.V(b.W()*0.96, b.H()*0.96))
}