-
Notifications
You must be signed in to change notification settings - Fork 5
/
game.go
85 lines (72 loc) · 1.9 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
77
78
79
80
81
82
83
84
85
package gosu
import (
"fmt"
"io/fs"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/hndada/gosu/draws"
"github.com/hndada/gosu/game/piano"
"github.com/hndada/gosu/scene"
"github.com/hndada/gosu/scene/play"
"github.com/hndada/gosu/scene/selects"
)
type Game struct {
resources *scene.Resources
options *scene.Options
currentScene scene.Scene
sceneSelect *selects.Scene
scenePlay *play.Scene
}
func NewGame(fsys fs.FS) *Game {
g := &Game{}
g.resources = scene.NewResources(fsys)
g.loadOptions()
sceneSelect, err := selects.NewScene(g.resources, g.options)
if err != nil {
panic(err)
}
g.sceneSelect = sceneSelect
g.currentScene = sceneSelect
// issue: It jitters when Vsync is enabled.
// ebiten.SetVsyncEnabled(false)
ebiten.SetTPS(ebiten.SyncWithFPS)
ebiten.SetWindowSize(g.screenSize().IntValues())
ebiten.SetWindowTitle("gosu")
return g
}
func (g *Game) Update() error {
sc := g.currentScene
switch args := sc.Update().(type) {
case error:
fmt.Println("play scene error:", args)
g.currentScene = g.sceneSelect
case piano.Scorer:
ebiten.SetWindowTitle("gosu")
g.currentScene = g.sceneSelect
// debug.SetGCPercent(100)
case scene.PlayArgs:
scenePlay, err := play.NewScene(g.resources, g.options, args)
if err != nil {
fmt.Println("play scene error:", args)
g.currentScene = g.sceneSelect
return nil
}
ebiten.SetWindowTitle(scenePlay.WindowTitle())
g.scenePlay = scenePlay
g.currentScene = scenePlay
// debug.SetGCPercent(0)
}
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
g.currentScene.Draw(draws.Image{Image: screen})
if g.options.DebugPrint {
ebitenutil.DebugPrint(screen, g.options.DebugString())
}
}
func (g Game) screenSize() draws.XY {
return g.options.Resolution
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
return g.screenSize().IntValues()
}