2020-05-09 22:19:44 +00:00
|
|
|
package tins2020
|
|
|
|
|
2020-05-10 21:00:19 +00:00
|
|
|
import (
|
|
|
|
"github.com/veandco/go-sdl2/sdl"
|
|
|
|
)
|
|
|
|
|
2020-05-09 22:19:44 +00:00
|
|
|
type GameControls struct {
|
2020-05-10 15:16:18 +00:00
|
|
|
Container
|
|
|
|
|
2020-05-11 09:44:50 +00:00
|
|
|
game *Game
|
|
|
|
dialogs *Dialogs
|
2020-05-09 22:19:44 +00:00
|
|
|
|
2020-05-10 21:00:19 +00:00
|
|
|
menu ButtonBar
|
|
|
|
top ButtonBar
|
|
|
|
flowers ButtonBar
|
|
|
|
otherTools ButtonBar
|
2020-05-09 22:19:44 +00:00
|
|
|
|
2020-05-10 15:16:18 +00:00
|
|
|
pause *IconButton
|
|
|
|
run *IconButton
|
|
|
|
runFast *IconButton
|
2020-05-10 21:00:19 +00:00
|
|
|
|
|
|
|
shovel *IconButton
|
|
|
|
research *IconButton
|
2020-05-09 22:19:44 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 09:44:50 +00:00
|
|
|
func NewGameControls(game *Game, dialogs *Dialogs) *GameControls {
|
|
|
|
return &GameControls{game: game, dialogs: dialogs}
|
2020-05-09 22:19:44 +00:00
|
|
|
}
|
|
|
|
|
2020-05-10 18:44:20 +00:00
|
|
|
func (c *GameControls) createBuyFlowerButton(id string) *BuyFlowerButton {
|
|
|
|
flower, _ := c.game.Herbarium.Find(id)
|
|
|
|
return NewBuyFlowerButton(
|
|
|
|
flower.IconTemplate.Variant(1),
|
|
|
|
flower.IconTemplate.Disabled(),
|
|
|
|
id,
|
2020-05-10 21:00:19 +00:00
|
|
|
flower,
|
2020-05-10 18:44:20 +00:00
|
|
|
EmptyEvent(func() {
|
|
|
|
c.game.SelectPlantFlowerTool(id)
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-05-10 21:00:19 +00:00
|
|
|
func (c *GameControls) speedChanged(state interface{}) {
|
|
|
|
speed := state.(GameSpeed)
|
|
|
|
disable := func(b *IconButton, expected GameSpeed) {
|
|
|
|
b.IsDisabled = speed == expected
|
|
|
|
}
|
|
|
|
disable(c.pause, GameSpeedPaused)
|
|
|
|
disable(c.run, GameSpeedNormal)
|
|
|
|
disable(c.runFast, GameSpeedFast)
|
|
|
|
}
|
|
|
|
|
2020-05-10 18:44:20 +00:00
|
|
|
func (c *GameControls) toolChanged(state interface{}) {
|
|
|
|
tool, _ := state.(Tool)
|
|
|
|
var flowerID string
|
|
|
|
if tool, ok := tool.(*PlantFlowerTool); ok {
|
|
|
|
flowerID = tool.FlowerID
|
|
|
|
}
|
|
|
|
for _, control := range c.flowers.Buttons {
|
|
|
|
button := control.(*BuyFlowerButton)
|
|
|
|
button.IsActive = button.FlowerID == flowerID
|
2020-05-11 01:09:01 +00:00
|
|
|
button.IsDisabled = !c.game.Herbarium.IsUnlocked(button.FlowerID)
|
2020-05-10 18:44:20 +00:00
|
|
|
}
|
2020-05-10 21:00:19 +00:00
|
|
|
_, shovel := tool.(*ShovelTool)
|
|
|
|
c.shovel.IsActive = shovel
|
2020-05-09 22:19:44 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 09:53:15 +00:00
|
|
|
func (c *GameControls) updateFlowerControls(ctx *Context) {
|
|
|
|
for _, b := range c.flowers.Buttons {
|
|
|
|
button := b.(*BuyFlowerButton)
|
|
|
|
flower, ok := c.game.Herbarium.Find(button.FlowerID)
|
|
|
|
if ok {
|
|
|
|
button.Update(ctx, flower)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-10 15:16:18 +00:00
|
|
|
func (c *GameControls) Arrange(ctx *Context, bounds Rectangle) {
|
|
|
|
c.Bounds = bounds
|
|
|
|
c.menu.Arrange(ctx, RectSize(bounds.X, bounds.Y, buttonBarWidth, bounds.H))
|
|
|
|
c.top.Arrange(ctx, Rect(bounds.X+bounds.W/2+8, bounds.Y, bounds.Right(), bounds.Y+64))
|
|
|
|
c.flowers.Arrange(ctx, RectSize(bounds.Right()-buttonBarWidth, bounds.Y, buttonBarWidth, bounds.H))
|
2020-05-10 21:00:19 +00:00
|
|
|
c.otherTools.Arrange(ctx, RectSize(bounds.Right()-buttonBarWidth, bounds.Bottom()-2*buttonBarWidth, buttonBarWidth, 2*buttonBarWidth))
|
2020-05-09 22:19:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *GameControls) Init(ctx *Context) error {
|
2020-05-10 21:00:19 +00:00
|
|
|
c.game.SpeedChanged().RegisterItf(c.speedChanged)
|
2020-05-10 18:44:20 +00:00
|
|
|
c.game.ToolChanged().RegisterItf(c.toolChanged)
|
2020-05-11 09:44:50 +00:00
|
|
|
c.dialogs.DialogOpened().Register(func() { c.game.Pause() })
|
|
|
|
c.dialogs.DialogClosed().Register(func() { c.game.Resume() })
|
2020-05-10 18:44:20 +00:00
|
|
|
|
2020-05-11 00:49:53 +00:00
|
|
|
c.flowers.Background = MustHexColor("#356dad")
|
|
|
|
c.flowers.ButtonLength = 64
|
2020-05-10 18:44:20 +00:00
|
|
|
|
|
|
|
for _, id := range c.game.Herbarium.Flowers() {
|
|
|
|
c.flowers.Buttons = append(c.flowers.Buttons, c.createBuyFlowerButton(id))
|
2020-05-09 22:19:44 +00:00
|
|
|
}
|
|
|
|
|
2020-05-10 15:16:18 +00:00
|
|
|
c.top.Orientation = OrientationHorizontal
|
2020-05-10 15:40:56 +00:00
|
|
|
c.pause = NewIconButtonConfig("control-pause", EmptyEvent(func() {
|
2020-05-10 15:16:18 +00:00
|
|
|
c.game.Pause()
|
2020-05-10 15:40:56 +00:00
|
|
|
}), func(b *IconButton) {
|
|
|
|
b.IconDisabled = "control-pause-disabled"
|
|
|
|
})
|
|
|
|
c.run = NewIconButtonConfig("control-run", EmptyEvent(func() {
|
2020-05-10 15:16:18 +00:00
|
|
|
c.game.Run()
|
2020-05-10 15:40:56 +00:00
|
|
|
}), func(b *IconButton) {
|
|
|
|
b.IconDisabled = "control-run-disabled"
|
|
|
|
})
|
|
|
|
c.runFast = NewIconButtonConfig("control-run-fast", EmptyEvent(func() {
|
2020-05-10 15:16:18 +00:00
|
|
|
c.game.RunFast()
|
2020-05-10 15:40:56 +00:00
|
|
|
}), func(b *IconButton) {
|
|
|
|
b.IconDisabled = "control-run-fast-disabled"
|
|
|
|
})
|
2020-05-10 21:00:19 +00:00
|
|
|
c.speedChanged(c.game.Speed)
|
2020-05-10 15:16:18 +00:00
|
|
|
c.top.Buttons = []Control{c.pause, c.run, c.runFast}
|
|
|
|
|
|
|
|
c.menu.Background = MustHexColor("#356dad")
|
|
|
|
c.menu.Buttons = []Control{
|
2020-05-12 07:11:27 +00:00
|
|
|
NewIconButton("control-settings", c.dialogs.ShowSettings),
|
2020-05-11 01:09:01 +00:00
|
|
|
NewIconButton("control-save", func(*Context) { c.game.Save() }),
|
|
|
|
NewIconButton("control-load", func(ctx *Context) {
|
|
|
|
c.game.Load()
|
2020-05-11 09:53:15 +00:00
|
|
|
c.updateFlowerControls(ctx)
|
|
|
|
}),
|
|
|
|
NewIconButton("control-new", func(ctx *Context) {
|
|
|
|
c.game.New()
|
|
|
|
c.updateFlowerControls(ctx)
|
2020-05-11 01:09:01 +00:00
|
|
|
}),
|
2020-05-12 07:11:27 +00:00
|
|
|
NewIconButton("control-information", c.dialogs.ShowIntro),
|
2020-05-09 22:19:44 +00:00
|
|
|
}
|
2020-05-10 15:16:18 +00:00
|
|
|
|
2020-05-11 01:09:01 +00:00
|
|
|
c.shovel = NewIconButtonConfig("control-shovel", func(*Context) { c.game.SelectShovel() }, func(b *IconButton) { b.IconHeight = 32 })
|
2020-05-12 07:11:27 +00:00
|
|
|
c.research = NewIconButtonConfig("control-research", c.dialogs.ShowResearch, func(b *IconButton) { b.IconHeight = 32 })
|
2020-05-10 21:00:19 +00:00
|
|
|
c.otherTools.Buttons = []Control{c.shovel, c.research}
|
|
|
|
|
2020-05-10 15:16:18 +00:00
|
|
|
c.Container.AddChild(&c.menu)
|
|
|
|
c.Container.AddChild(&c.top)
|
|
|
|
c.Container.AddChild(&c.flowers)
|
2020-05-10 21:00:19 +00:00
|
|
|
c.Container.AddChild(&c.otherTools)
|
2020-05-11 09:44:50 +00:00
|
|
|
|
2020-05-10 15:16:18 +00:00
|
|
|
return c.Container.Init(ctx)
|
2020-05-09 22:19:44 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 09:44:50 +00:00
|
|
|
func (c *GameControls) Handle(ctx *Context, event sdl.Event) bool {
|
|
|
|
if c.Container.Handle(ctx, event) {
|
|
|
|
return true
|
|
|
|
}
|
2020-05-10 21:00:19 +00:00
|
|
|
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *sdl.KeyboardEvent:
|
|
|
|
if e.Type == sdl.KEYDOWN {
|
|
|
|
switch e.Keysym.Sym {
|
|
|
|
case sdl.K_SPACE:
|
|
|
|
c.game.TogglePause()
|
|
|
|
case sdl.K_1:
|
|
|
|
c.game.Run()
|
|
|
|
case sdl.K_2:
|
|
|
|
c.game.RunFast()
|
|
|
|
case sdl.K_d:
|
|
|
|
c.game.SelectShovel()
|
|
|
|
case sdl.K_r:
|
2020-05-12 07:11:27 +00:00
|
|
|
c.dialogs.ShowResearch(ctx)
|
2020-05-10 21:00:19 +00:00
|
|
|
case sdl.K_ESCAPE:
|
|
|
|
if c.game.Tool() == nil {
|
2020-05-12 07:11:27 +00:00
|
|
|
c.dialogs.ShowIntro(ctx)
|
2020-05-10 21:00:19 +00:00
|
|
|
} else {
|
|
|
|
c.game.CancelTool()
|
|
|
|
}
|
2020-05-11 09:44:50 +00:00
|
|
|
return true
|
|
|
|
case sdl.K_F3:
|
|
|
|
c.game.Debug = !c.game.Debug
|
2020-05-10 21:00:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-11 09:44:50 +00:00
|
|
|
return false
|
2020-05-10 21:00:19 +00:00
|
|
|
}
|
|
|
|
|
2020-05-09 22:19:44 +00:00
|
|
|
func (c *GameControls) Render(ctx *Context) {
|
2020-05-10 15:16:18 +00:00
|
|
|
topBar := MustHexColor("#0000007f")
|
2020-05-11 09:44:50 +00:00
|
|
|
SetDrawColor(ctx.Renderer, topBar)
|
2020-05-10 15:16:18 +00:00
|
|
|
ctx.Renderer.FillRect(Rect(c.menu.Bounds.Right(), 0, c.flowers.Bounds.X, 64).SDLPtr())
|
2020-05-10 15:40:56 +00:00
|
|
|
ctx.Fonts.Font("balance").RenderCopyAlign(ctx.Renderer, FmtMoney(c.game.Balance), Pt(c.top.Bounds.X-8, 58), MustHexColor("#4AC69A"), TextAlignmentRight)
|
2020-05-09 22:19:44 +00:00
|
|
|
|
2020-05-10 15:16:18 +00:00
|
|
|
c.Container.Render(ctx)
|
2020-05-09 22:19:44 +00:00
|
|
|
}
|