spacetea/mainscreen.go

163 lines
3.8 KiB
Go
Raw Normal View History

2022-05-19 12:58:37 -04:00
package main
import (
"fmt"
2022-05-25 17:52:42 -04:00
"sort"
2022-05-19 12:58:37 -04:00
"strconv"
"time"
sim "git.saintnet.tech/stryan/spacetea/simulator"
2022-05-28 17:46:28 -04:00
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
2022-05-19 12:58:37 -04:00
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
var style = lipgloss.NewStyle().
2022-05-28 17:46:28 -04:00
Width(32).
2022-05-19 12:58:37 -04:00
Align(lipgloss.Center).
2022-05-28 17:46:28 -04:00
Foreground(lipgloss.Color("#F0FFFF")).
2022-05-19 12:58:37 -04:00
BorderStyle(lipgloss.NormalBorder()).
BorderForeground(lipgloss.Color("63"))
type model struct {
s *sim.Simulator
input textinput.Model
help help.Model
keys keyMap
next string
lastSize tea.WindowSizeMsg
2022-05-19 12:58:37 -04:00
}
type beat struct{}
type simCmd string
func heartbeat() tea.Cmd {
return tea.Tick(time.Second*1, func(time.Time) tea.Msg {
return beat{}
})
}
func (m model) simCommand() tea.Msg {
if m.next != "" {
tmp := m.next
m.next = ""
return simCmd(tmp)
}
return nil
}
2022-08-22 18:39:41 -04:00
func initMainscreen(sim *simulator) model {
2022-05-19 12:58:37 -04:00
ti := textinput.New()
ti.Placeholder = "input command"
ti.CharLimit = 156
ti.Width = 20
return model{
2022-08-22 18:39:41 -04:00
s: sim,
2022-05-19 12:58:37 -04:00
input: ti,
2022-05-28 17:46:28 -04:00
help: help.New(),
keys: keys,
2022-05-19 12:58:37 -04:00
}
}
func (m model) Init() tea.Cmd {
return heartbeat()
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case beat:
//heartbeat
return m, heartbeat()
2022-05-28 17:46:28 -04:00
case tea.WindowSizeMsg:
m.help.Width = msg.Width
m.lastSize = msg
2022-05-19 12:58:37 -04:00
case placeMsg:
simc := fmt.Sprintf("place %v", string(msg))
m.s.Input(simc)
return m, nil
2022-05-24 18:26:34 -04:00
case craftMsg:
simc := fmt.Sprintf("craft %v", string(msg))
m.s.Input(simc)
return m, nil
case readMsg:
pid, err := strconv.Atoi(string(msg))
if err != nil {
panic(err)
}
return newJView(sim.GlobalPages[pid].Title, sim.GlobalPages[pid].Content), m.GetSize
2022-05-19 12:58:37 -04:00
case tea.KeyMsg:
2022-05-28 17:46:28 -04:00
switch {
case key.Matches(msg, m.keys.Help):
m.help.ShowAll = !m.help.ShowAll
return m, nil
case key.Matches(msg, m.keys.Quit):
2022-05-19 12:58:37 -04:00
m.s.Stop()
return m, tea.Quit
2022-05-28 17:46:28 -04:00
case key.Matches(msg, m.keys.Right):
2022-05-19 12:58:37 -04:00
m.s.Input("right")
return m, nil
2022-05-28 17:46:28 -04:00
case key.Matches(msg, m.keys.Left):
2022-05-19 12:58:37 -04:00
m.s.Input("left")
return m, nil
2022-05-28 17:46:28 -04:00
case key.Matches(msg, m.keys.Up):
2022-05-19 12:58:37 -04:00
m.s.Input("up")
return m, nil
2022-05-28 17:46:28 -04:00
case key.Matches(msg, m.keys.Down):
2022-05-19 12:58:37 -04:00
m.s.Input("down")
return m, nil
2022-05-28 17:46:28 -04:00
case key.Matches(msg, m.keys.Gather):
m.s.Input("get")
case key.Matches(msg, m.keys.Place):
var res entrylist
for _, k := range sim.GlobalItems {
if m.s.Player.Resources[k.ID()] != 0 {
res = append(res, k.(sim.ItemEntry))
2022-05-19 12:58:37 -04:00
}
}
2022-05-28 17:46:28 -04:00
sort.Sort(res)
return newMenuModel(res, placeMenu), m.GetSize
2022-05-28 17:46:28 -04:00
case key.Matches(msg, m.keys.Pickup):
m.s.Input("pickup")
case key.Matches(msg, m.keys.Destroy):
m.s.Input("destroy")
case key.Matches(msg, m.keys.Craft):
var res entrylist
for k := range m.s.Player.Craftables {
res = append(res, sim.GlobalItems[k].(sim.ItemEntry))
}
sort.Sort(res)
return newMenuModel(res, craftMenu), m.GetSize
case key.Matches(msg, m.keys.Journal):
var res pagelist
for k := range m.s.Player.Pages {
res = append(res, sim.GlobalPages[k])
}
sort.Sort(res)
return newJMenuModel(res), m.GetSize
2022-05-19 12:58:37 -04:00
}
}
m.input, cmd = m.input.Update(msg)
return m, cmd
}
func (m model) View() string {
var render string
2022-05-28 17:46:28 -04:00
if m.help.ShowAll != true {
playerAndLog := lipgloss.JoinVertical(0, style.Render(fmt.Sprintf("%v", m.s.Player.String())), style.Render(fmt.Sprintf("%v", m.s.Player.Log())))
placeAndInput := lipgloss.JoinVertical(0, style.Render(m.s.Place.String()), style.Render(m.help.View(m.keys)))
display := lipgloss.JoinHorizontal(0, placeAndInput, playerAndLog)
render = fmt.Sprintf("%v\n%v\n", style.Render(fmt.Sprintf("Current Time: %v", strconv.Itoa(m.s.Time))), display)
} else {
render = style.Render(m.help.View(m.keys))
}
2022-05-19 12:58:37 -04:00
return render
}
func (m model) GetSize() tea.Msg {
return m.lastSize
}