1
0
Fork 0
tetris-go/game/state/handlers/menu.go

99 lines
1.9 KiB
Go

package handlers
import (
"tetris/assets"
"tetris/engine/audio"
"tetris/engine/render"
"tetris/game"
"tetris/game/state"
rl "github.com/gen2brain/raylib-go/raylib"
)
type entry struct {
label string
state string
}
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) {
if rl.IsKeyPressed(rl.KeyEnter) {
fsm.Switch(menu.entries[menu.selected].state)
audio.Play(assets.SFX_MENU_ENTER)
} else if rl.IsKeyPressed(rl.KeyDown) {
if menu.selected+1 < len(menu.entries) {
menu.selected = menu.selected + 1
audio.Play(assets.SFX_MENU_SELECT)
}
} else if rl.IsKeyPressed(rl.KeyUp) {
if menu.selected-1 >= 0 {
menu.selected = menu.selected - 1
audio.Play(assets.SFX_MENU_SELECT)
}
}
}
func (Menu) renderLogo(offset_x, offset_y int32) {
for y := range assets.LOGO_HEIGHT {
for x := range assets.LOGO_STRIDE {
index := assets.Logo[x+(y*assets.LOGO_STRIDE)]
block := game.Block(index)
if block == game.BLOCK_EMPTY {
continue
}
src := block.Tile().GetTexRect()
render.DrawTextureRec(src, rl.Rectangle{
X: float32(offset_x) + (float32(x) * src.Width * 2),
Y: float32(offset_y) + (float32(y) * src.Height * 2),
Width: src.Width * 2,
Height: src.Height * 2,
})
}
}
}
func (menu Menu) renderEntries(offset_x, offset_y int32) {
y := offset_y
for i, entry := range menu.entries {
col := rl.White
if i == menu.selected {
col = rl.Red
}
render.DrawTextCenter(offset_x, y, 32, entry.label, col)
y += 40
}
}
func (menu Menu) Render() {
render.Begin(rl.Black)
menu.renderLogo(20, 150)
menu.renderEntries(340, 400)
render.End()
}