forked from ranchblt/geometry-jumper
/
triangle.go
67 lines (58 loc) · 1.85 KB
/
triangle.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
package game
type Triangle struct {
*BaseShape
// this is expected to be degrees.
TravelAngle float64
DestinationTrack int
swapState int
midwayPoint int
}
func NewTriangle(base *BaseShape) *Triangle {
var t = &Triangle{
BaseShape: base,
TravelAngle: DefaultTriangleAngleOfDescent,
DestinationTrack: SubsequentTracks[base.Track],
swapState: TriangleBeforeSwap,
midwayPoint: int(base.Center.x / 2),
}
return t
}
func NewTriangleNonStandardAngle(base *BaseShape, travelAngle float64) *Triangle {
var t = &Triangle{
BaseShape: base,
TravelAngle: DefaultTriangleAngleOfDescent,
DestinationTrack: SubsequentTracks[base.Track],
swapState: TriangleBeforeSwap,
midwayPoint: int(base.Center.x / 2),
}
return t
}
func (t *Triangle) Update() {
if t.swapState == TriangleBeforeSwap || t.swapState == TriangleAfterSwap {
// before and after swap, just slide along the track
t.Center.x = t.Center.x - t.BaseSpeed
if t.swapState == TriangleBeforeSwap && int(t.Center.x) <= t.midwayPoint {
t.swapState = TriangleDuringSwap
}
} else {
t.updateWithTrackSwitchingMovement()
}
t.crossedLeftEdge()
}
// this is the circle's up / down logic! wooo!
func (t *Triangle) updateWithTrackSwitchingMovement() {
var xVelocity, yVelocity = getVelocityComponents(t.BaseSpeed, t.TravelAngle)
if t.Track < t.DestinationTrack {
yVelocity = yVelocity * -1
}
t.Center.x = t.Center.x - xVelocity
t.Center.y = t.Center.y - yVelocity
if (t.Track < t.DestinationTrack && t.Center.y >= TrackMappings[t.DestinationTrack]) ||
(t.Track > t.DestinationTrack && t.Center.y <= TrackMappings[t.DestinationTrack]) {
// then set the track to the destination
t.Track = t.DestinationTrack
// and snap the centerY to the new track
t.Center.y = TrackMappings[t.Track]
t.swapState = TriangleAfterSwap
}
}