-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.go
60 lines (47 loc) · 1.34 KB
/
game.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
package engine
import "golang.org/x/mobile/event/key"
// Game is a struct that defines a game and the window that contains it
type Game struct {
Title string
Width int
Height int
ScaleFactor int
TargetFrameRate int
FramePainter FramePainter
KeyListener KeyListener
Levels []*Level
CurrentLevelID int
CurrentFrame int
}
// CreateGame sets up a game and its window
func CreateGame(title string, width int, height int, scaleFactor int, targetFrameRate int, framePainter FramePainter, keyListener KeyListener, levels []*Level) *Game {
game := Game{
Title: title,
Width: width,
Height: height,
ScaleFactor: scaleFactor,
TargetFrameRate: targetFrameRate,
FramePainter: framePainter,
KeyListener: keyListener,
Levels: levels,
CurrentLevelID: 0,
CurrentFrame: 0,
}
for _, level := range levels {
level.Game = &game
}
createWindow(&game)
return &game
}
// CurrentLevel gets the current level object
func (game *Game) CurrentLevel() *Level {
return game.Levels[game.CurrentLevelID]
}
// BroadCastInput sends the game input to the current level's object if they are controllable
func (game *Game) BroadcastInput(event key.Event) {
for _, gameObject := range game.CurrentLevel().GameObjects {
if gameObject.IsControllable == true {
game.KeyListener(event, gameObject)
}
}
}