-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenumanager.go
72 lines (62 loc) · 1.65 KB
/
menumanager.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
package main
import (
"github.com/faiface/pixel"
"github.com/faiface/pixel/pixelgl"
)
const (
MENU_ITEM_NEW_GAME int = 1
MENU_ITEM_QUIT int = 2
)
/*
MenuManager handles rendering and accepting input for the menu
*/
type MenuManager struct {
assetManager *AssetManager
window *pixelgl.Window
menuNewGameAsset *pixel.Sprite
menuQuitAsset *pixel.Sprite
currentMenuItem int
}
/*
NewMenuManager initializes the menu manager
*/
func NewMenuManager(window *pixelgl.Window, assetManager *AssetManager) *MenuManager {
return &MenuManager{
assetManager: assetManager,
window: window,
menuNewGameAsset: assetManager.LoadMenuNewGameAsset(),
menuQuitAsset: assetManager.LoadMenuQuitAsset(),
currentMenuItem: MENU_ITEM_NEW_GAME,
}
}
/*
CheckForMenuMovement will change selected menu items
*/
func (m *MenuManager) CheckForMenuMovement() {
if m.window.JustPressed(pixelgl.KeyUp) && m.currentMenuItem == MENU_ITEM_QUIT {
m.currentMenuItem = MENU_ITEM_NEW_GAME
}
if m.window.JustPressed(pixelgl.KeyDown) && m.currentMenuItem == MENU_ITEM_NEW_GAME {
m.currentMenuItem = MENU_ITEM_QUIT
}
}
/*
Draw renders the currently selected menu to the window
*/
func (m *MenuManager) Draw() {
center := pixel.IM.Moved(window.Bounds().Center())
if m.currentMenuItem == MENU_ITEM_NEW_GAME {
m.menuNewGameAsset.Draw(m.window, center)
} else if m.currentMenuItem == MENU_ITEM_QUIT {
m.menuQuitAsset.Draw(m.window, center)
}
}
/*
PressedEnter returns true if the user pressed Enter
*/
func (m *MenuManager) PressedEnter() (bool, int) {
if m.window.JustPressed(pixelgl.KeyEnter) {
return true, m.currentMenuItem
}
return false, 0
}