tins2020/research.go

121 lines
2.7 KiB
Go

package tins2020
import (
"fmt"
"math/rand"
"strconv"
"strings"
"time"
"opslag.de/schobers/geom"
"opslag.de/schobers/zntg"
"opslag.de/schobers/zntg/ui"
)
type Research struct {
ui.StackPanel
game *Game
botanist Specialist
farmer Specialist
description ui.Paragraph
specialists ui.Paragraph
dial *Dial
input ui.Label
animate zntg.Animation
closeRequested ui.Events
}
type Dialer interface {
CanUserType(int) bool
UserGaveWrongInput()
UserTyped(ui.Context, int)
}
func NewResearch(game *Game) *LargeDialog {
research := &Research{game: game}
research.animate.Interval = 20 * time.Millisecond
research.animate.Start()
research.description.Text = "Call a specialist to conduct research with."
research.dial = NewDial(research)
research.Children = []ui.Control{&research.description, &research.specialists, research.dial, &research.input}
dialog := NewLargeDialog("Research", ui.Stretch(research))
research.closeRequested.AddHandlerEmpty(func(ctx ui.Context) { dialog.closeRequested.Notify(ctx, nil) })
return dialog
}
type Specialist struct {
Cost int
Number string
}
func (r *Research) Arrange(ctx ui.Context, bounds geom.RectangleF32, offset geom.PointF32, parent ui.Control) {
r.input.TextAlignment = ui.AlignCenter
r.StackPanel.Arrange(ctx, bounds, offset, parent)
}
func (r *Research) CanUserType(digit int) bool {
typing := strconv.Itoa(digit)
return strings.HasPrefix(r.botanist.Number, r.input.Text+typing)
}
func (r *Research) Hidden() {}
func (r *Research) Render(ctx ui.Context) {
r.animate.AnimateFn(r.dial.Tick)
r.StackPanel.Render(ctx)
}
func (r *Research) Shown() {
generateNumber := func() string {
var number string
for i := 0; i < 3; i++ {
number += strconv.Itoa(rand.Intn(9) + 1)
}
return number
}
r.input.Text = ""
r.dial.Reset()
var specialists string
defer func() {
r.specialists.Text = specialists
}()
_, _, price := r.game.Herbarium.NextFlowerToUnlock()
if price == 0 {
specialists += "Botanist (unlocks next flower; unavailable)\n"
specialists += "Farmer (fertilizes land; unavailable)\n"
return
}
r.botanist.Cost = price
if r.game.Balance < r.botanist.Cost {
r.botanist.Number = "**unavailable**"
} else {
r.botanist.Number = generateNumber()
}
specialists += fmt.Sprintf("Botanist: no. %s (unlocks next flower; $ %d)\n", r.botanist.Number, r.botanist.Cost)
specialists += "Farmer: no. **unavailable** (fertilizes land; $ ---)\n"
}
func (r *Research) UserGaveWrongInput() {
r.input.Text = ""
}
func (r *Research) UserTyped(ctx ui.Context, digit int) {
r.input.Text += strconv.Itoa(digit)
if r.input.Text == r.botanist.Number {
r.game.UnlockNextFlower(ctx)
r.input.Text = ""
r.closeRequested.Notify(ctx, nil)
}
}