-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.go
76 lines (60 loc) · 1.98 KB
/
game.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package foodzy
import (
"github.com/co0p/foodzy/asset"
"github.com/co0p/foodzy/internal/ecs"
"github.com/co0p/foodzy/internal/scene"
"github.com/co0p/foodzy/internal/sound"
"github.com/hajimehoshi/ebiten/v2"
"math/rand"
"os"
"time"
)
const (
ScreenWidth int = 800
ScreenHeight int = 600
GameName string = "Foodzy"
)
const (
SoundEat string = "eating"
SoundBackground = "soundtrack"
)
type ActionType func(*ecs.EntityManager)
type Game struct {
screenManager *scene.SceneManager
soundManager *sound.SoundManager
}
func NewGame() *Game {
rand.Seed(time.Now().UnixNano())
sceneManager := scene.NewSceneManager()
soundManager := sound.NewSoundManager()
// actions
exit := func(m *ecs.EntityManager) { os.Exit(0) }
startGame := func(m *ecs.EntityManager) { sceneManager.Activate(GameSceneName) }
showStartScene := func(m *ecs.EntityManager) { sceneManager.Activate(StartSceneName) }
restartGame := func(m *ecs.EntityManager) { sceneManager.Restart(GameSceneName) }
showPauseScene := func(m *ecs.EntityManager) { sceneManager.Activate(PauseSceneName) }
showGameOverScene := func(*ecs.EntityManager) { sceneManager.Restart(GameOverSceneName) }
// scenes
sceneManager.AddScene(NewStartScene(restartGame, exit))
sceneManager.AddScene(NewPauseScene(startGame))
sceneManager.AddScene(NewGameScene(soundManager, showGameOverScene, showPauseScene))
sceneManager.AddScene(NewGameOverScene(soundManager, showStartScene))
sceneManager.Activate(StartSceneName)
// sound
soundManager.Add(SoundEat, asset.Eating)
soundManager.AddLoop(SoundBackground, asset.Soundtrack)
soundManager.Play(SoundBackground)
return &Game{
soundManager: soundManager,
screenManager: sceneManager,
}
}
func (g *Game) Update() error {
return g.screenManager.Current.Update()
}
func (g *Game) Draw(screen *ebiten.Image) {
g.screenManager.Current.Draw(screen)
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
return ScreenWidth, ScreenHeight
}