-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgamebackground.go
99 lines (83 loc) · 2.81 KB
/
gamebackground.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
package main
import (
"time"
"github.com/faiface/pixel"
"github.com/faiface/pixel/pixelgl"
)
/*
GameBackground handles the scrolling background during the game
*/
type GameBackground struct {
assetManager *AssetManager
window *pixelgl.Window
starsTile1 *pixel.Sprite
starsTile2 *pixel.Sprite
closerStarsTile1 *pixel.Sprite
closerStarsTile2 *pixel.Sprite
starsTile1Pos pixel.Vec
starsTile2Pos pixel.Vec
closerStarsTile1Pos pixel.Vec
closerStarsTile2Pos pixel.Vec
starsHeight float64
t <-chan time.Time
}
/*
NewGameBackground creates a new background manager
*/
func NewGameBackground(window *pixelgl.Window, assetManager *AssetManager) *GameBackground {
var pic1 pixel.Picture
var pic2 pixel.Picture
pic1 = assetManager.GetStarsAsset()
pic2 = assetManager.GetCloserStarsAsset()
centerVector := window.Bounds().Center()
topVector := window.Bounds().Center()
topVector = topVector.Add(pixel.V(0.0, 768.0))
return &GameBackground{
assetManager: assetManager,
window: window,
starsTile1: pixel.NewSprite(pic1, pic1.Bounds()),
starsTile2: pixel.NewSprite(pic1, pic1.Bounds()),
closerStarsTile1: pixel.NewSprite(pic2, pic2.Bounds()),
closerStarsTile2: pixel.NewSprite(pic2, pic2.Bounds()),
starsTile1Pos: topVector,
starsTile2Pos: centerVector,
closerStarsTile1Pos: topVector,
closerStarsTile2Pos: centerVector,
starsHeight: pic1.Bounds().Size().Y,
t: time.Tick(time.Millisecond * 30),
}
}
/*
Draw renders the stars to the window
*/
func (b *GameBackground) Draw() {
b.starsTile1.Draw(b.window, pixel.IM.Moved(b.starsTile1Pos))
b.starsTile2.Draw(b.window, pixel.IM.Moved(b.starsTile2Pos))
b.closerStarsTile1.Draw(b.window, pixel.IM.Moved(b.closerStarsTile1Pos))
b.closerStarsTile2.Draw(b.window, pixel.IM.Moved(b.closerStarsTile2Pos))
}
/*
Update scrolls the background
*/
func (b *GameBackground) Update(dt float64) {
select {
case <-b.t:
if b.starsTile2Pos.Y <= -384 {
b.starsTile2Pos = b.starsTile1Pos.Add(pixel.V(0.0, b.starsHeight))
} else if b.starsTile1Pos.Y <= -384 {
b.starsTile1Pos = b.starsTile2Pos.Add(pixel.V(0.0, b.starsHeight))
}
if b.closerStarsTile2Pos.Y <= -384 {
b.closerStarsTile2Pos = b.closerStarsTile1Pos.Add(pixel.V(0.0, b.starsHeight))
} else if b.closerStarsTile1Pos.Y <= -384 {
b.closerStarsTile1Pos = b.closerStarsTile2Pos.Add(pixel.V(0.0, b.starsHeight))
}
starsMovement := 1.0
b.starsTile1Pos = b.starsTile1Pos.Sub(pixel.V(0.0, starsMovement))
b.starsTile2Pos = b.starsTile2Pos.Sub(pixel.V(0.0, starsMovement))
closerStarsMovement := 2.0
b.closerStarsTile1Pos = b.closerStarsTile1Pos.Sub(pixel.V(0.0, closerStarsMovement))
b.closerStarsTile2Pos = b.closerStarsTile2Pos.Sub(pixel.V(0.0, closerStarsMovement))
default:
}
}