Added "new game" functionality.
This commit is contained in:
parent
9641719579
commit
a6004a8ab6
38
game.go
38
game.go
@ -34,26 +34,14 @@ const simulationInterval = 120 * time.Millisecond
|
|||||||
const fastSimulationInterval = 20 * time.Millisecond
|
const fastSimulationInterval = 20 * time.Millisecond
|
||||||
|
|
||||||
func NewGame() *Game {
|
func NewGame() *Game {
|
||||||
terrain := &Map{
|
game := &Game{
|
||||||
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,
|
|
||||||
|
|
||||||
centerChanged: NewEvents(),
|
centerChanged: NewEvents(),
|
||||||
speedChanged: NewEvents(),
|
speedChanged: NewEvents(),
|
||||||
toolChanged: NewEvents(),
|
toolChanged: NewEvents(),
|
||||||
simulation: NewAnimation(time.Millisecond * 10),
|
simulation: NewAnimation(time.Millisecond * 10),
|
||||||
}
|
}
|
||||||
|
game.Reset()
|
||||||
|
return game
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Game) selectTool(t Tool) {
|
func (g *Game) selectTool(t Tool) {
|
||||||
@ -129,6 +117,11 @@ func (g *Game) Dig(tile Point) {
|
|||||||
g.Balance += desc.SellPrice
|
g.Balance += desc.SellPrice
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (g *Game) New() {
|
||||||
|
g.Pause()
|
||||||
|
g.Reset()
|
||||||
|
}
|
||||||
|
|
||||||
func (g *Game) Load() {
|
func (g *Game) Load() {
|
||||||
g.CancelTool()
|
g.CancelTool()
|
||||||
g.Pause()
|
g.Pause()
|
||||||
@ -183,6 +176,21 @@ func (g *Game) PlantFlower(id string, tile Point) {
|
|||||||
g.Terrain.AddFlower(tile, id, flower.Traits)
|
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) Resume() { g.setSpeed(g.SpeedBeforePause) }
|
||||||
|
|
||||||
func (g *Game) Run() { g.setSpeed(GameSpeedNormal) }
|
func (g *Game) Run() { g.setSpeed(GameSpeedNormal) }
|
||||||
|
@ -65,6 +65,16 @@ func (c *GameControls) toolChanged(state interface{}) {
|
|||||||
c.shovel.IsActive = shovel
|
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) {
|
func (c *GameControls) Arrange(ctx *Context, bounds Rectangle) {
|
||||||
c.Bounds = bounds
|
c.Bounds = bounds
|
||||||
c.menu.Arrange(ctx, RectSize(bounds.X, bounds.Y, buttonBarWidth, bounds.H))
|
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-save", func(*Context) { c.game.Save() }),
|
||||||
NewIconButton("control-load", func(ctx *Context) {
|
NewIconButton("control-load", func(ctx *Context) {
|
||||||
c.game.Load()
|
c.game.Load()
|
||||||
for _, b := range c.flowers.Buttons {
|
c.updateFlowerControls(ctx)
|
||||||
button := b.(*BuyFlowerButton)
|
}),
|
||||||
flower, ok := c.game.Herbarium.Find(button.FlowerID)
|
NewIconButton("control-new", func(ctx *Context) {
|
||||||
if ok {
|
c.game.New()
|
||||||
button.Update(ctx, flower)
|
c.updateFlowerControls(ctx)
|
||||||
}
|
|
||||||
}
|
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user