feat: improve menu
This commit is contained in:
parent
35e73f2b37
commit
288e710b9f
2 changed files with 40 additions and 8 deletions
|
|
@ -7,22 +7,54 @@ import (
|
||||||
rl "github.com/gen2brain/raylib-go/raylib"
|
rl "github.com/gen2brain/raylib-go/raylib"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Menu struct{}
|
type entry struct {
|
||||||
|
label string
|
||||||
func (menu *Menu) Enter() {
|
state string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (menu *Menu) Exit() {
|
type Menu struct {
|
||||||
|
selected int
|
||||||
|
entries []entry
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewMenu() *Menu {
|
||||||
|
return &Menu{
|
||||||
|
selected: 0,
|
||||||
|
entries: []entry{
|
||||||
|
{"Start", "gameplay"},
|
||||||
|
{"Quit", "quit"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (menu *Menu) Enter() {
|
||||||
|
menu.selected = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (Menu) Exit() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (menu *Menu) Update(fsm state.Transitioner, delta float32) {
|
func (menu *Menu) Update(fsm state.Transitioner, delta float32) {
|
||||||
if rl.IsKeyPressed(rl.KeyEnter) {
|
if rl.IsKeyPressed(rl.KeyEnter) {
|
||||||
fsm.Switch("gameplay")
|
fsm.Switch(menu.entries[menu.selected].state)
|
||||||
|
} else if rl.IsKeyPressed(rl.KeyDown) {
|
||||||
|
menu.selected = min(menu.selected+1, len(menu.entries)-1)
|
||||||
|
} else if rl.IsKeyPressed(rl.KeyUp) {
|
||||||
|
menu.selected = max(menu.selected-1, 0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (menu *Menu) Render() {
|
func (menu Menu) Render() {
|
||||||
render.Begin(rl.Black)
|
render.Begin(rl.Black)
|
||||||
render.DrawText(250, 400, 32, "Start", rl.White)
|
y := int32(400)
|
||||||
|
for i, entry := range menu.entries {
|
||||||
|
|
||||||
|
col := rl.White
|
||||||
|
if i == menu.selected {
|
||||||
|
col = rl.Red
|
||||||
|
}
|
||||||
|
render.DrawTextCenter(340, y, 32, entry.label, col)
|
||||||
|
y += 40
|
||||||
|
}
|
||||||
render.End()
|
render.End()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
2
main.go
2
main.go
|
|
@ -35,7 +35,7 @@ func main() {
|
||||||
|
|
||||||
// Setup state machine.
|
// Setup state machine.
|
||||||
fsm := machine.New()
|
fsm := machine.New()
|
||||||
fsm.Register("menu", &handlers.Menu{})
|
fsm.Register("menu", handlers.NewMenu())
|
||||||
fsm.Register("gameover", &handlers.GameOver{})
|
fsm.Register("gameover", &handlers.GameOver{})
|
||||||
fsm.Register("gameplay", handlers.NewGamePlay())
|
fsm.Register("gameplay", handlers.NewGamePlay())
|
||||||
fsm.Start("menu")
|
fsm.Start("menu")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue