last_light/game/state/main_menu_state.go

90 lines
2.4 KiB
Go
Raw Normal View History

2024-04-24 17:11:33 +03:00
package state
import (
2024-05-06 20:43:35 +03:00
"mvvasilev/last_light/engine"
"mvvasilev/last_light/game/ui"
2024-04-24 17:11:33 +03:00
"github.com/gdamore/tcell/v2"
)
type MainMenuState struct {
2024-05-06 20:43:35 +03:00
menuTitle *engine.Raw
2024-04-24 17:11:33 +03:00
buttons []*ui.UISimpleButton
currButtonSelected int
quitGame bool
startNewGame bool
}
func NewMainMenuState() *MainMenuState {
state := new(MainMenuState)
highlightStyle := tcell.StyleDefault.Attributes(tcell.AttrBold)
2024-05-06 20:43:35 +03:00
state.menuTitle = engine.CreateRawDrawable(
2024-04-24 17:11:33 +03:00
11, 1, tcell.StyleDefault.Attributes(tcell.AttrBold).Foreground(tcell.ColorYellow),
" | | | _) | | ",
" | _` | __| __| | | _` | __ \\ __|",
" | ( | \\__ \\ | | | ( | | | | | ",
"_____| \\__,_| ____/ \\__| _____| _| \\__, | _| |_| \\__|",
" |___/ ",
)
state.buttons = make([]*ui.UISimpleButton, 0)
state.buttons = append(state.buttons, ui.CreateSimpleButton(11, 7, "New Game", tcell.StyleDefault, highlightStyle, func() {
state.startNewGame = true
}))
state.buttons = append(state.buttons, ui.CreateSimpleButton(11, 9, "Load Game", tcell.StyleDefault, highlightStyle, func() {
}))
state.buttons = append(state.buttons, ui.CreateSimpleButton(11, 11, "Quit", tcell.StyleDefault, highlightStyle, func() {
state.quitGame = true
}))
state.currButtonSelected = 0
state.buttons[state.currButtonSelected].Highlight()
return state
}
func (mms *MainMenuState) OnInput(e *tcell.EventKey) {
if e.Key() == tcell.KeyDown {
mms.buttons[mms.currButtonSelected].Unhighlight()
2024-05-06 21:47:20 +03:00
mms.currButtonSelected = engine.LimitIncrement(mms.currButtonSelected, 2)
2024-04-24 17:11:33 +03:00
mms.buttons[mms.currButtonSelected].Highlight()
}
if e.Key() == tcell.KeyUp {
mms.buttons[mms.currButtonSelected].Unhighlight()
2024-05-06 21:47:20 +03:00
mms.currButtonSelected = engine.LimitDecrement(mms.currButtonSelected, 0)
2024-04-24 17:11:33 +03:00
mms.buttons[mms.currButtonSelected].Highlight()
}
if e.Key() == tcell.KeyEnter {
mms.buttons[mms.currButtonSelected].Select()
}
}
func (mms *MainMenuState) OnTick(dt int64) GameState {
if mms.quitGame {
return &QuitState{}
}
if mms.startNewGame {
return BeginPlayingState()
}
return mms
}
2024-05-06 20:43:35 +03:00
func (mms *MainMenuState) CollectDrawables() []engine.Drawable {
arr := make([]engine.Drawable, 0)
arr = append(arr, mms.menuTitle)
2024-04-24 17:11:33 +03:00
for _, b := range mms.buttons {
arr = append(arr, b)
2024-04-24 17:11:33 +03:00
}
return arr
2024-04-24 17:11:33 +03:00
}