Implemented digging of flowers.

Renamed poppy to anemone.
This commit is contained in:
Sander Schobers 2020-05-10 23:00:19 +02:00
parent 243e204f48
commit 1c208152e8
11 changed files with 201 additions and 115 deletions

View File

@ -11,11 +11,7 @@ type BuyFlowerButton struct {
IconButton
FlowerID string
Name string
Price int
Description string
IsActive bool
Flower FlowerDescriptor
hoverAnimation *Animation
hoverOffset int32
@ -23,28 +19,33 @@ type BuyFlowerButton struct {
priceTexture *Texture
}
func NewBuyFlowerButton(icon, iconDisabled, flowerID, name string, price int, description string, isDisabled bool, onClick EventContextFn) *BuyFlowerButton {
func NewBuyFlowerButton(icon, iconDisabled, flowerID string, flower FlowerDescriptor, onClick EventContextFn) *BuyFlowerButton {
return &BuyFlowerButton{
IconButton: *NewIconButtonConfig(icon, onClick, func(b *IconButton) {
b.IconDisabled = iconDisabled
b.IsDisabled = isDisabled
b.IsDisabled = !flower.Unlocked
}),
FlowerID: flowerID,
Name: name,
Price: price,
Description: description,
Flower: flower,
}
}
func (b *BuyFlowerButton) animate() {
b.hoverOffset++
if b.hoverOffset > b.hoverTexture.Size().X+b.Bounds.W {
b.hoverOffset = b.priceTexture.Size().X
b.hoverOffset = 0
}
}
func (b *BuyFlowerButton) fmtTooltipText() string {
if b.Flower.Unlocked {
return fmt.Sprintf("%s - %s - %s", FmtMoney(b.Flower.BuyPrice), b.Flower.Name, "Traits are not known yet.")
}
return fmt.Sprintf("%s - %s - %s", FmtMoney(b.Flower.BuyPrice), b.Flower.Name, b.Flower.Description)
}
func (b *BuyFlowerButton) Init(ctx *Context) error {
text := fmt.Sprintf("%s - %s - %s", FmtMoney(b.Price), b.Name, b.Description)
text := fmt.Sprintf("%s - %s - %s", FmtMoney(b.Flower.BuyPrice), b.Flower.Name, b.Flower.Description)
font := ctx.Fonts.Font("small")
color := MustHexColor("#ffffff")
texture, err := font.Render(ctx.Renderer, text, color)
@ -52,7 +53,7 @@ func (b *BuyFlowerButton) Init(ctx *Context) error {
return err
}
b.hoverTexture = texture
texture, err = font.Render(ctx.Renderer, FmtMoney(b.Price), color)
texture, err = font.Render(ctx.Renderer, FmtMoney(b.Flower.BuyPrice), color)
if err != nil {
return err
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

View File

@ -4,7 +4,7 @@ control-research: images/genericItem_color_111.png
control-settings: images/gear.png
control-save: images/save.png
control-load: images/basket.png
control-load: images/import.png
control-quit: images/power.png
control-pause: images/pause.png
@ -64,11 +64,11 @@ bush-large-2: images/plant_bushLarge_NW.png
bush-large-3: images/plant_bushLarge_SW.png
bush-large-4: images/plant_bushLarge_SE.png
flower-poppy-disabled: images/flower_yellowC_NE_disabled.png
flower-poppy-1: images/flower_yellowC_NE.png
flower-poppy-2: images/flower_yellowC_NW.png
flower-poppy-3: images/flower_yellowC_SW.png
flower-poppy-4: images/flower_yellowC_SE.png
flower-anemone-disabled: images/flower_yellowC_NE_disabled.png
flower-anemone-1: images/flower_yellowC_NE.png
flower-anemone-2: images/flower_yellowC_NW.png
flower-anemone-3: images/flower_yellowC_SW.png
flower-anemone-4: images/flower_yellowC_SE.png
flower-red-c-disabled: images/flower_redC_NE_disabled.png
flower-red-c-1: images/flower_redC_NE.png

View File

@ -1,6 +1,7 @@
package tins2020
type Flower struct {
ID string
Traits FlowerTraits
}
@ -31,17 +32,16 @@ type FlowerResistance struct {
Wet float32
}
// NewPoppyTraits creates the traits of a poppy, a very generic flower that thrives in a moderate climate.
// NewPoppyTraits creates the traits of a anemone, a very generic flower that thrives in a moderate climate.
func NewPoppyTraits() FlowerTraits {
return FlowerTraits{
// Spread: 0.0011,
Spread: 0.0011,
Spread: 0.0004,
Life: 0.99993,
Resistance: FlowerResistance{
Cold: 0.5,
Hot: 0.5,
Hot: 0.7,
Dry: 0.5,
Wet: 0.5,
Wet: 0.7,
},
}
}

121
game.go
View File

@ -15,6 +15,7 @@ type Game struct {
tool Tool
toolChanged *Events
speedChanged *Events
simulation Animation
}
@ -26,29 +27,6 @@ const (
GameSpeedPaused = "paused"
)
type Map struct {
Temp NoiseMap
Humid NoiseMap
Variant NoiseMap
PlaceX NoiseMap // displacement map of props
PlaceY NoiseMap
Flowers map[Point]Flower
}
func (m *Map) AddFlower(pos Point, traits FlowerTraits) {
m.Flowers[pos] = m.NewFlower(pos, traits)
}
func (m *Map) NewFlower(pos Point, traits FlowerTraits) Flower {
flower := Flower{
Traits: traits,
}
temp, humid := float32(m.Temp.Value(pos.X, pos.Y)), float32(m.Humid.Value(pos.X, pos.Y))
flower.Traits.UpdateModifier(temp, humid)
return flower
}
const simulationInterval = 120 * time.Millisecond
const fastSimulationInterval = 40 * time.Millisecond
@ -62,11 +40,12 @@ func NewGame() *Game {
Flowers: map[Point]Flower{},
}
herbarium := NewHerbarium()
herbarium.Add("poppy", FlowerDescriptor{
Name: "Poppy",
Description: "A very generic flower that thrives in a moderate climate.",
IconTemplate: "flower-poppy-%s",
Price: 10,
herbarium.Add("anemone", FlowerDescriptor{
Name: "Anemone",
Description: "A very generic flower that thrives in a temperate climate.",
IconTemplate: "flower-anemone-%s",
BuyPrice: 10,
SellPrice: 3,
Traits: NewPoppyTraits(),
Unlocked: true,
})
@ -76,6 +55,7 @@ func NewGame() *Game {
Terrain: terrain,
Herbarium: herbarium,
speedChanged: NewEvents(),
toolChanged: NewEvents(),
simulation: NewAnimation(time.Millisecond * 10),
}
@ -86,6 +66,28 @@ func (g *Game) selectTool(t Tool) {
g.toolChanged.Notify(t)
}
func (g *Game) setSpeed(speed GameSpeed) {
if speed == g.Speed {
return
}
if speed == GameSpeedPaused {
g.SpeedBeforePause = g.Speed
}
g.Speed = speed
g.speedChanged.Notify(speed)
switch speed {
case GameSpeedPaused:
g.simulation.Pause()
case GameSpeedNormal:
g.simulation.SetInterval(simulationInterval)
g.simulation.Run()
case GameSpeedFast:
g.simulation.SetInterval(fastSimulationInterval)
g.simulation.Run()
}
}
func (g *Game) tick() {
randomNeighbor := func(pos Point) Point {
switch rand.Intn(4) {
@ -106,11 +108,11 @@ func (g *Game) tick() {
dst := randomNeighbor(pos)
if _, ok := g.Terrain.Flowers[dst]; !ok {
if _, ok := flowers[dst]; !ok {
flowers[dst] = g.Terrain.NewFlower(dst, flower.Traits)
flowers[dst] = g.Terrain.NewFlower(dst, flower.ID, flower.Traits)
}
}
}
if rand.Float32() < flower.Traits.Life*flower.Traits.LifeModifier {
if Sqr32(rand.Float32()) < flower.Traits.Life*flower.Traits.LifeModifier {
flowers[pos] = flower
}
}
@ -122,57 +124,58 @@ func (g *Game) CancelTool() {
}
func (g *Game) Dig(tile Point) {
// TODO: implement
}
func (g *Game) Pause() {
if g.Speed == GameSpeedPaused {
id := g.Terrain.DigFlower(tile)
desc, ok := g.Herbarium.Find(id)
if !ok {
return
}
g.SpeedBeforePause = g.Speed
g.Speed = GameSpeedPaused
g.simulation.Pause()
g.Balance += desc.SellPrice
}
func (g *Game) Pause() { g.setSpeed(GameSpeedPaused) }
func (g *Game) PlantFlower(id string, tile Point) {
flower, ok := g.Herbarium.Find(id)
if !ok {
log.Println("user was able to plant a flower that doesn't exist")
return
}
if flower.Price > g.Balance {
if flower.BuyPrice > g.Balance {
// TODO: notify user of insufficient balance?
return
}
g.Balance -= flower.Price
g.Terrain.AddFlower(tile, flower.Traits)
g.Balance -= flower.BuyPrice
g.Terrain.AddFlower(tile, id, flower.Traits)
}
func (g *Game) Resume() {
switch g.SpeedBeforePause {
case GameSpeedNormal:
g.Run()
case GameSpeedFast:
g.RunFast()
}
}
func (g *Game) Resume() { g.setSpeed(g.SpeedBeforePause) }
func (g *Game) Run() {
g.Speed = GameSpeedNormal
g.simulation.SetInterval(simulationInterval)
g.simulation.Run()
}
func (g *Game) Run() { g.setSpeed(GameSpeedNormal) }
func (g *Game) RunFast() {
g.Speed = GameSpeedFast
g.simulation.SetInterval(fastSimulationInterval)
g.simulation.Run()
}
func (g *Game) RunFast() { g.setSpeed(GameSpeedFast) }
func (g *Game) SelectPlantFlowerTool(id string) {
g.selectTool(&PlantFlowerTool{FlowerID: id})
}
func (g *Game) SelectShovel() {
g.selectTool(&ShovelTool{})
}
func (g *Game) SelectResearch() {
g.Pause()
}
func (g *Game) SpeedChanged() EventHandler { return g.speedChanged }
func (g *Game) TogglePause() {
if g.Speed == GameSpeedPaused {
g.Resume()
} else {
g.Pause()
}
}
func (g *Game) Tool() Tool { return g.tool }
func (g *Game) ToolChanged() EventHandler { return g.toolChanged }

View File

@ -1,5 +1,9 @@
package tins2020
import (
"github.com/veandco/go-sdl2/sdl"
)
type GameControls struct {
Container
@ -8,10 +12,14 @@ type GameControls struct {
menu ButtonBar
top ButtonBar
flowers ButtonBar
otherTools ButtonBar
pause *IconButton
run *IconButton
runFast *IconButton
shovel *IconButton
research *IconButton
}
func NewGameControls(game *Game) *GameControls {
@ -24,16 +32,23 @@ func (c *GameControls) createBuyFlowerButton(id string) *BuyFlowerButton {
flower.IconTemplate.Variant(1),
flower.IconTemplate.Disabled(),
id,
flower.Name,
flower.Price,
flower.Description,
!flower.Unlocked,
flower,
EmptyEvent(func() {
c.game.SelectPlantFlowerTool(id)
}),
)
}
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)
}
func (c *GameControls) toolChanged(state interface{}) {
tool, _ := state.(Tool)
var flowerID string
@ -44,15 +59,8 @@ func (c *GameControls) toolChanged(state interface{}) {
button := control.(*BuyFlowerButton)
button.IsActive = button.FlowerID == flowerID
}
}
func (c *GameControls) updateSpeedControls() {
disable := func(b *IconButton, speed GameSpeed) {
b.IsDisabled = speed == c.game.Speed
}
disable(c.pause, GameSpeedPaused)
disable(c.run, GameSpeedNormal)
disable(c.runFast, GameSpeedFast)
_, shovel := tool.(*ShovelTool)
c.shovel.IsActive = shovel
}
func (c *GameControls) Arrange(ctx *Context, bounds Rectangle) {
@ -60,9 +68,11 @@ func (c *GameControls) Arrange(ctx *Context, bounds Rectangle) {
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))
c.otherTools.Arrange(ctx, RectSize(bounds.Right()-buttonBarWidth, bounds.Bottom()-2*buttonBarWidth, buttonBarWidth, 2*buttonBarWidth))
}
func (c *GameControls) Init(ctx *Context) error {
c.game.SpeedChanged().RegisterItf(c.speedChanged)
c.game.ToolChanged().RegisterItf(c.toolChanged)
c.flowers.Background = MustHexColor("#356dad") // brown alternative? #4ac69a
@ -74,23 +84,20 @@ func (c *GameControls) Init(ctx *Context) error {
c.top.Orientation = OrientationHorizontal
c.pause = NewIconButtonConfig("control-pause", EmptyEvent(func() {
c.game.Pause()
c.updateSpeedControls()
}), func(b *IconButton) {
b.IconDisabled = "control-pause-disabled"
})
c.run = NewIconButtonConfig("control-run", EmptyEvent(func() {
c.game.Run()
c.updateSpeedControls()
}), func(b *IconButton) {
b.IconDisabled = "control-run-disabled"
})
c.runFast = NewIconButtonConfig("control-run-fast", EmptyEvent(func() {
c.game.RunFast()
c.updateSpeedControls()
}), func(b *IconButton) {
b.IconDisabled = "control-run-fast-disabled"
})
c.updateSpeedControls()
c.speedChanged(c.game.Speed)
c.top.Buttons = []Control{c.pause, c.run, c.runFast}
c.menu.Background = MustHexColor("#356dad")
@ -100,12 +107,45 @@ func (c *GameControls) Init(ctx *Context) error {
NewIconButton("control-load", EmptyEvent(func() {})),
}
c.shovel = NewIconButtonConfig("control-shovel", func(*Context) { c.game.SelectShovel() }, func(b *IconButton) { b.IconHeight = 48 })
c.research = NewIconButtonConfig("control-research", func(*Context) { c.game.SelectResearch() }, func(b *IconButton) { b.IconHeight = 48 })
c.otherTools.Buttons = []Control{c.shovel, c.research}
c.Container.AddChild(&c.menu)
c.Container.AddChild(&c.top)
c.Container.AddChild(&c.flowers)
c.Container.AddChild(&c.otherTools)
return c.Container.Init(ctx)
}
func (c *GameControls) Handle(ctx *Context, event sdl.Event) {
c.Container.Handle(ctx, event)
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:
c.game.SelectResearch()
case sdl.K_ESCAPE:
if c.game.Tool() == nil {
// TODO: display menu
} else {
c.game.CancelTool()
}
}
}
}
}
func (c *GameControls) Render(ctx *Context) {
topBar := MustHexColor("#0000007f")
ctx.Renderer.SetDrawColor(topBar.R, topBar.G, topBar.B, topBar.A)

View File

@ -17,7 +17,9 @@ func NewHerbarium() Herbarium {
type FlowerDescriptor struct {
Name string
Description string
Price int
BuyPrice int
SellPrice int
Unlocked bool
IconTemplate IconTemplate
Traits FlowerTraits

View File

@ -5,9 +5,11 @@ type IconButton struct {
Icon string
IconDisabled string
IconHeight int32
IconScale Scale
IconWidth int32
IsActive bool
IsDisabled bool
}
@ -51,12 +53,14 @@ func (b *IconButton) Render(ctx *Context) {
size := iconTexture.Size()
if b.IconWidth != 0 {
size = Pt(b.IconWidth, b.IconWidth*size.Y/size.X)
} else if b.IconHeight != 0 {
size = Pt(b.IconHeight*size.X/size.Y, b.IconHeight)
}
iconTexture.CopyResize(ctx.Renderer, RectSize(b.Bounds.X+(b.Bounds.W-size.X)/2, b.Bounds.Y+(b.Bounds.H-size.Y)/2, size.X, size.Y))
} else {
iconTexture.CopyResize(ctx.Renderer, b.Bounds)
}
if b.IsMouseOver && !b.IsDisabled {
if (b.IsMouseOver && !b.IsDisabled) || b.IsActive {
mouseOverTexture.CopyResize(ctx.Renderer, b.Bounds)
}
}

34
map.go Normal file
View File

@ -0,0 +1,34 @@
package tins2020
type Map struct {
Temp NoiseMap
Humid NoiseMap
Variant NoiseMap
PlaceX NoiseMap // displacement map of props
PlaceY NoiseMap
Flowers map[Point]Flower
}
func (m *Map) AddFlower(pos Point, id string, traits FlowerTraits) {
m.Flowers[pos] = m.NewFlower(pos, id, traits)
}
func (m *Map) DigFlower(pos Point) string {
flower, ok := m.Flowers[pos]
if !ok {
return ""
}
delete(m.Flowers, pos)
return flower.ID
}
func (m *Map) NewFlower(pos Point, id string, traits FlowerTraits) Flower {
flower := Flower{
ID: id,
Traits: traits,
}
temp, humid := float32(m.Temp.Value(pos.X, pos.Y)), float32(m.Humid.Value(pos.X, pos.Y))
flower.Traits.UpdateModifier(temp, humid)
return flower
}

View File

@ -50,4 +50,6 @@ func Min32(a, b float32) float32 {
func Round32(x float32) float32 { return float32(math.Round(float64(x))) }
func Sqr32(x float32) float32 { return x * x }
func Sqrt32(x float32) float32 { return float32(math.Sqrt(float64(x))) }

View File

@ -172,7 +172,7 @@ func (r *terrainRenderer) Render(ctx *Context) {
variant := r.terrain.Variant.Value(x, y)
_, ok := r.terrain.Flowers[Pt(x, y)]
if ok {
return variantToTexture("flower-poppy-%d", variant)
return variantToTexture("flower-anemone-%d", variant)
}
temp := r.terrain.Temp.Value(x, y)
humid := r.terrain.Humid.Value(x, y)