-
Notifications
You must be signed in to change notification settings - Fork 1
/
game_decorator.go
56 lines (46 loc) · 1.09 KB
/
game_decorator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main
import (
"github.com/ByteArena/box2d"
"github.com/hajimehoshi/ebiten/v2"
)
// TODO: description
// TODO: rename
// We need it because ebiten allows to run only one game
type GameDecorator struct {
g ebiten.Game
cam *Cam
ps *ParticleSystem
bg Background
}
func NewGameDecorator() *GameDecorator {
cam := NewCam()
ps := NewParticleSystem()
bg := NewBackground()
return &GameDecorator{
g: NewLevelSelector(),
cam: cam,
ps: ps,
bg: bg,
}
}
func (gd *GameDecorator) Update() error {
err := gd.g.Update()
if selectedLevel, ok := err.(*SelectedLevel); ok {
world := box2d.MakeB2World(box2d.MakeB2Vec2(0, Gravity))
level := LoadLevel(&world, gd.ps, selectedLevel.name)
game := NewGame(&world, gd.cam, gd.bg, gd.ps, level)
world.SetContactListener(game)
gd.g = game
}
switch err {
case levelComplete, levelFailed:
gd.g = NewLevelSelector()
}
return nil
}
func (gd *GameDecorator) Draw(screen *ebiten.Image) {
gd.g.Draw(screen)
}
func (gd *GameDecorator) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
return ScreenWidth, ScreenHeight
}