tins2020/game.go

286 lines
6.5 KiB
Go
Raw Normal View History

2020-05-08 16:38:26 +00:00
package tins2020
import (
"log"
2020-05-08 16:38:26 +00:00
"math/rand"
"time"
"opslag.de/schobers/geom"
"opslag.de/schobers/zntg"
"opslag.de/schobers/zntg/ui"
2020-05-08 16:38:26 +00:00
)
type Game struct {
Debug bool
Balance int
Speed GameSpeed
SpeedBeforePause GameSpeed
Herbarium Herbarium
Terrain *Map
2020-05-11 01:09:01 +00:00
tool Tool
centerChanged ui.Events
toolChanged ui.Events
speedChanged ui.Events
simulation zntg.Animation
2020-05-08 16:38:26 +00:00
}
type GameSpeed string
const (
GameSpeedNormal GameSpeed = "normal"
GameSpeedFast = "fast"
GameSpeedPaused = "paused"
)
const simulationInterval = 120 * time.Millisecond
const fastSimulationInterval = 20 * time.Millisecond
2020-05-08 16:38:26 +00:00
func NewGame() *Game {
2020-05-11 09:53:15 +00:00
game := &Game{
simulation: zntg.Animation{Interval: time.Millisecond * 10},
}
2020-05-11 09:53:15 +00:00
return game
2020-05-08 16:38:26 +00:00
}
func (g *Game) selectTool(ctx ui.Context, t Tool) {
g.tool = t
g.toolChanged.Notify(ctx, t)
}
func (g *Game) setSpeed(ctx ui.Context, speed GameSpeed) {
if speed == g.Speed {
return
}
if speed == GameSpeedPaused {
g.SpeedBeforePause = g.Speed
}
g.Speed = speed
g.speedChanged.Notify(ctx, speed)
switch speed {
case GameSpeedPaused:
g.simulation.Pause()
case GameSpeedNormal:
g.simulation.Interval = simulationInterval
g.simulation.Start()
case GameSpeedFast:
g.simulation.Interval = fastSimulationInterval
g.simulation.Start()
}
}
func (g *Game) tick() {
randomNeighbor := func(pos geom.Point) geom.Point {
switch rand.Intn(4) {
case 0:
return geom.Pt(pos.X-1, pos.Y)
case 1:
return geom.Pt(pos.X, pos.Y-1)
case 2:
return geom.Pt(pos.X+1, pos.Y)
case 3:
return geom.Pt(pos.X, pos.Y+1)
}
return pos
}
flowers := map[geom.Point]Flower{}
for pos, flower := range g.Terrain.Flowers {
if rand.Float32() < flower.Traits.Spread {
dst := randomNeighbor(pos)
if _, ok := g.Terrain.Flowers[dst]; !ok {
if _, ok := flowers[dst]; !ok {
flowers[dst] = g.Terrain.NewFlower(dst, flower.ID, flower.Traits)
}
}
}
if Sqr32(rand.Float32()) < flower.Traits.Life*flower.Traits.LifeModifier {
flowers[pos] = flower
}
}
g.Terrain.Flowers = flowers
2020-05-08 16:38:26 +00:00
}
func (g *Game) CancelTool(ctx ui.Context) {
g.selectTool(ctx, nil)
}
func (g *Game) CenterChanged() ui.EventHandler { return &g.centerChanged }
2020-05-11 01:09:01 +00:00
func (g *Game) Dig(tile geom.Point) {
id := g.Terrain.DigFlower(tile)
desc, ok := g.Herbarium.Find(id)
if !ok {
return
}
adjacent := g.Terrain.FlowersOnAdjacentTiles(tile)
switch adjacent {
case 3:
g.Balance += (desc.SellPrice * 3 / 2) // 50% bonus
case 4:
g.Balance += (desc.SellPrice * 2) // 100% bonus
default:
g.Balance += desc.SellPrice
}
}
func (g *Game) New(ctx ui.Context) {
g.Pause(ctx)
g.Reset(ctx)
2020-05-11 09:53:15 +00:00
}
func (g *Game) Load(ctx ui.Context) {
g.CancelTool(ctx)
g.Pause(ctx)
2020-05-11 01:09:01 +00:00
var state GameState
err := state.Deserialize(SaveGameName())
if err != nil {
log.Println("failed to load; error:", err)
return
}
g.Herbarium = NewHerbarium()
for _, flower := range state.Herbarium.Flowers {
g.Herbarium.Update(flower.ID, func(desc *FlowerDescriptor) {
desc.Unlocked = flower.Unlocked
})
}
g.Balance = state.Balance
g.Terrain = &Map{
Temp: NewNoiseMap(state.Terrain.Temperature),
Humid: NewNoiseMap(state.Terrain.Humidity),
Variant: NewRandomNoiseMap(state.Terrain.Variant),
PlaceX: NewRandomNoiseMap(state.Terrain.PlaceX),
PlaceY: NewRandomNoiseMap(state.Terrain.PlaceY),
Flowers: map[geom.Point]Flower{},
2020-05-11 01:09:01 +00:00
}
for _, flower := range state.Terrain.Flowers {
desc, _ := g.Herbarium.Find(flower.ID)
g.Terrain.AddFlower(flower.Location, flower.ID, desc.Traits)
}
g.Terrain.Center = state.View.Center
g.centerChanged.Notify(ctx, g.Terrain.Center)
g.setSpeed(ctx, state.Speed)
2020-05-11 01:09:01 +00:00
}
func (g *Game) Pause(ctx ui.Context) { g.setSpeed(ctx, GameSpeedPaused) }
func (g *Game) PlantFlower(id string, tile geom.Point) {
if g.Terrain.HasFlower(tile) {
// TODO: notify user it tried to plant on tile with flower?
return
}
flower, ok := g.Herbarium.Find(id)
if !ok {
log.Println("user was able to plant a flower that doesn't exist")
return
}
if flower.BuyPrice > g.Balance {
// TODO: notify user of insufficient balance?
return
}
g.Balance -= flower.BuyPrice
g.Terrain.AddFlower(tile, id, flower.Traits)
}
func (g *Game) Reset(ctx ui.Context) {
2020-05-11 09:53:15 +00:00
g.Balance = 100
g.Herbarium = NewHerbarium()
g.Terrain = &Map{
Temp: NewNoiseMap(rand.Int63()),
Humid: NewNoiseMap(rand.Int63()),
Variant: NewRandomNoiseMap(rand.Int63()),
PlaceX: NewRandomNoiseMap(rand.Int63()),
PlaceY: NewRandomNoiseMap(rand.Int63()),
Flowers: map[geom.Point]Flower{},
2020-05-11 09:53:15 +00:00
}
g.CancelTool(ctx)
g.setSpeed(ctx, GameSpeedNormal)
2020-05-11 09:53:15 +00:00
}
func (g *Game) Resume(ctx ui.Context) { g.setSpeed(ctx, g.SpeedBeforePause) }
func (g *Game) Run(ctx ui.Context) { g.setSpeed(ctx, GameSpeedNormal) }
func (g *Game) RunFast(ctx ui.Context) { g.setSpeed(ctx, GameSpeedFast) }
2020-05-11 01:09:01 +00:00
func (g *Game) Save() {
state := g.State()
err := state.Serialize(SaveGameName())
if err != nil {
log.Println("failed to save; error:", err)
}
}
func (g *Game) SelectPlantFlowerTool(ctx ui.Context, id string) {
g.selectTool(ctx, &PlantFlowerTool{FlowerID: id})
}
func (g *Game) SelectShovel(ctx ui.Context) {
g.selectTool(ctx, &ShovelTool{})
}
func (g *Game) SpeedChanged() ui.EventHandler { return &g.speedChanged }
2020-05-11 01:09:01 +00:00
func (g *Game) State() GameState {
var state GameState
state.Balance = g.Balance
state.Speed = g.Speed
for _, id := range g.Herbarium.Flowers() {
flower, _ := g.Herbarium.Find(id)
state.Herbarium.Flowers = append(state.Herbarium.Flowers, HerbariumFlowerState{
ID: id,
Unlocked: flower.Unlocked,
})
}
flowers := make([]FlowerState, 0, len(g.Terrain.Flowers))
for pos, flower := range g.Terrain.Flowers {
flowers = append(flowers, FlowerState{ID: flower.ID, Location: pos})
}
state.Terrain = TerrainState{
Temperature: g.Terrain.Temp.Seed(),
Humidity: g.Terrain.Humid.Seed(),
Variant: g.Terrain.Variant.Seed(),
PlaceX: g.Terrain.PlaceX.Seed(),
PlaceY: g.Terrain.PlaceY.Seed(),
Flowers: flowers,
}
state.View.Center = g.Terrain.Center
return state
}
func (g *Game) TogglePause(ctx ui.Context) {
if g.Speed == GameSpeedPaused {
g.Resume(ctx)
} else {
g.Pause(ctx)
}
}
func (g *Game) Tool() Tool { return g.tool }
func (g *Game) ToolChanged() ui.EventHandler { return &g.toolChanged }
func (g *Game) UnlockNextFlower(ctx ui.Context) {
2020-05-11 12:41:42 +00:00
price := g.Herbarium.UnlockNext()
g.Balance -= price
g.selectTool(ctx, nil)
2020-05-11 12:41:42 +00:00
}
func (g *Game) Update() {
for g.simulation.Animate() {
g.tick()
}
}
func (g *Game) UserClickedTile(pos geom.Point) {
if g.tool == nil {
return
}
g.tool.ClickedTile(g, pos)
}