-
Notifications
You must be signed in to change notification settings - Fork 0
/
motionObject.go
135 lines (115 loc) · 3.28 KB
/
motionObject.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"time"
)
type Moving struct {
Speed Point
Direction Point
}
type Motioner interface {
GetSpeed() *Point
GetDirection() *Point
}
type Accelerator interface {
GetMaxSpeed() *Point
GetMinSpeed() *Point
}
type MotionObject struct {
*Object
Moving
moving bool
AccelDuration time.Duration
AccelTimeFunc timeFunction
speedAccelerator Point
MinSpeed Point
MaxSpeed Point
currAccelDuration time.Duration
alignToGrid bool
accelerate bool
}
func (receiver *MotionObject) Update(timeLeft time.Duration) error {
if receiver.destroyed {
return nil
}
receiver.Object.Update(timeLeft)
if receiver.moving {
deltaX := receiver.Moving.Direction.X * (receiver.Moving.Speed.X * receiver.speedAccelerator.X) * (float64(timeLeft) / float64(time.Second))
deltaY := receiver.Moving.Direction.Y * (receiver.Moving.Speed.Y * receiver.speedAccelerator.Y) * (float64(timeLeft) / float64(time.Second))
receiver.RelativeMove(deltaX, deltaY)
if receiver.AccelDuration > 0 {
fraction := receiver.AccelTimeFunc(float64(receiver.currAccelDuration) / float64(receiver.AccelDuration))
receiver.Moving.Speed.X = receiver.MinSpeed.X + ((receiver.MaxSpeed.X - receiver.MinSpeed.X) * fraction)
receiver.Moving.Speed.Y = receiver.MinSpeed.Y + ((receiver.MaxSpeed.Y - receiver.MinSpeed.Y) * fraction)
receiver.currAccelDuration += timeLeft
if receiver.currAccelDuration > receiver.AccelDuration {
receiver.currAccelDuration = receiver.AccelDuration
}
}
} else {
receiver.currAccelDuration = 0
}
return nil
}
func (receiver *MotionObject) Accelerate(factorX, factorY float64) {
receiver.speedAccelerator.X, receiver.speedAccelerator.Y = factorX, factorY
}
func (receiver *MotionObject) GetSpeed() *Point {
return &receiver.Speed
}
func (receiver *MotionObject) GetMaxSpeed() *Point {
return &receiver.MaxSpeed
}
func (receiver *MotionObject) GetMinSpeed() *Point {
return &receiver.MinSpeed
}
func (receiver *MotionObject) GetDirection() *Point {
return &receiver.Direction
}
func (receiver *MotionObject) Destroy(nemesis ObjectInterface) error {
receiver.Object.Destroy(nemesis)
receiver.moving = false
return nil
}
func (receiver *MotionObject) Reset() error {
receiver.Object.Reset()
receiver.Speed = receiver.MinSpeed
receiver.currAccelDuration = 0
receiver.accelerate = true
receiver.moving = false
receiver.speedAccelerator.X, receiver.speedAccelerator.Y = 1.0, 1.0
return nil
}
func (receiver *MotionObject) Copy() *MotionObject {
instance := *receiver
instance.Object = instance.Object.Copy()
instance.Moving = Moving{
Speed: Point{
X: receiver.Moving.Speed.X,
Y: receiver.Moving.Speed.Y,
},
Direction: Point{
X: receiver.Moving.Direction.X,
Y: receiver.Moving.Direction.Y,
},
}
return &instance
}
func NewMotionObject(obj *Object, direction Point, speed Point) (*MotionObject, error) {
instance := MotionObject{
Object: obj,
Moving: Moving{
Speed: speed,
Direction: direction,
},
MinSpeed: speed,
MaxSpeed: speed,
AccelTimeFunc: LinearTimeFunction,
AccelDuration: 0,
currAccelDuration: 0,
accelerate: true,
moving: false,
alignToGrid: false,
speedAccelerator: Point{1.0, 1.0},
}
return &instance, nil
}