-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
136 lines (119 loc) · 2.55 KB
/
main.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package main
import (
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
const (
menuScreen = iota
gameScreen
)
type mainModel struct {
currentScreen int
menuModel menuModel
gameModel gameModel
}
type menuModel struct {
menuItems []string
currentItem int
}
func (m menuModel) Init() tea.Cmd {
return tea.ClearScreen
}
func (m menuModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q", "esc":
return m, tea.Quit
case "up":
if m.currentItem > 0 {
m.currentItem--
}
case "down":
if m.currentItem < len(m.menuItems)-1 {
m.currentItem++
}
case "enter":
switch m.currentItem {
case 0:
return m, tea.Quit
case 1:
return m, tea.Quit
}
}
}
return m, nil
}
func (m menuModel) View() string {
style := lipgloss.NewStyle().Foreground(lipgloss.Color("#FFF"))
v := style.Render(logo)
v += "\n\n"
v += "Select an option from the menu below:\n"
for i, item := range m.menuItems {
if i == m.currentItem {
v += "> "
} else {
v += " "
}
v += item + "\n"
}
return v
}
type gameModel struct {
playfield [21][10]int
}
func (m gameModel) Init() tea.Cmd {
return tea.ClearScreen
}
func (m gameModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, nil
}
func (m gameModel) View() string {
return ""
}
const logo = `
████ █████ █████ ████ █████ ███
█ █ █ █ █ █ █ █
█ █ █ █ █ █ █ █
████ ████ █ ████ ████ ███
█ █ █ █ █ █ █
█ █ █ █ █ █ █
█ █████ █ █ █ █████ ███
`
func (m mainModel) Init() tea.Cmd {
return tea.ClearScreen
}
func (m mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch m.currentScreen {
case menuScreen:
return m.menuModel.Update(msg)
case gameScreen:
return m.gameModel.Update(msg)
}
return m, nil
}
func (m mainModel) View() string {
switch m.currentScreen {
case menuScreen:
return m.menuModel.View()
case gameScreen:
return m.gameModel.View()
}
return ""
}
func main() {
menuModel := menuModel{
menuItems: []string{"Start", "Exit"},
currentItem: 0,
}
gameModel := gameModel{}
main := mainModel{
currentScreen: menuScreen,
menuModel: menuModel,
gameModel: gameModel,
}
p := tea.NewProgram(main)
if _, err := p.Run(); err != nil {
panic(err)
}
}