Added "new game" functionality.

This commit is contained in:
Sander Schobers 2020-05-11 11:53:15 +02:00
parent 9641719579
commit a6004a8ab6
2 changed files with 38 additions and 22 deletions

38
game.go
View File

@ -34,26 +34,14 @@ const simulationInterval = 120 * time.Millisecond
const fastSimulationInterval = 20 * time.Millisecond
func NewGame() *Game {
terrain := &Map{
Temp: NewNoiseMap(rand.Int63()),
Humid: NewNoiseMap(rand.Int63()),
Variant: NewRandomNoiseMap(rand.Int63()),
PlaceX: NewRandomNoiseMap(rand.Int63()),
PlaceY: NewRandomNoiseMap(rand.Int63()),
Flowers: map[Point]Flower{},
}
herbarium := NewHerbarium()
return &Game{
Speed: GameSpeedNormal,
Balance: 100,
Terrain: terrain,
Herbarium: herbarium,
game := &Game{
centerChanged: NewEvents(),
speedChanged: NewEvents(),
toolChanged: NewEvents(),
simulation: NewAnimation(time.Millisecond * 10),
}
game.Reset()
return game
}
func (g *Game) selectTool(t Tool) {
@ -129,6 +117,11 @@ func (g *Game) Dig(tile Point) {
g.Balance += desc.SellPrice
}
func (g *Game) New() {
g.Pause()
g.Reset()
}
func (g *Game) Load() {
g.CancelTool()
g.Pause()
@ -183,6 +176,21 @@ func (g *Game) PlantFlower(id string, tile Point) {
g.Terrain.AddFlower(tile, id, flower.Traits)
}
func (g *Game) Reset() {
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[Point]Flower{},
}
g.CancelTool()
g.setSpeed(GameSpeedNormal)
}
func (g *Game) Resume() { g.setSpeed(g.SpeedBeforePause) }
func (g *Game) Run() { g.setSpeed(GameSpeedNormal) }

View File

@ -65,6 +65,16 @@ func (c *GameControls) toolChanged(state interface{}) {
c.shovel.IsActive = shovel
}
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)
}
}
}
func (c *GameControls) Arrange(ctx *Context, bounds Rectangle) {
c.Bounds = bounds
c.menu.Arrange(ctx, RectSize(bounds.X, bounds.Y, buttonBarWidth, bounds.H))
@ -111,13 +121,11 @@ func (c *GameControls) Init(ctx *Context) error {
NewIconButton("control-save", func(*Context) { c.game.Save() }),
NewIconButton("control-load", func(ctx *Context) {
c.game.Load()
for _, b := range c.flowers.Buttons {
button := b.(*BuyFlowerButton)
flower, ok := c.game.Herbarium.Find(button.FlowerID)
if ok {
button.Update(ctx, flower)
}
}
c.updateFlowerControls(ctx)
}),
NewIconButton("control-new", func(ctx *Context) {
c.game.New()
c.updateFlowerControls(ctx)
}),
}