/
scene.go
105 lines (92 loc) · 2.82 KB
/
scene.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package play
import (
"fmt"
"io/fs"
"strings"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/hndada/gosu/draws"
"github.com/hndada/gosu/format/osr"
"github.com/hndada/gosu/input"
"github.com/hndada/gosu/mode"
"github.com/hndada/gosu/mode/piano"
"github.com/hndada/gosu/scene"
)
// ScenePlay: struct, PlayScene: function
// Interface declares at 'user' package.
type Scene struct {
*scene.Config
*scene.Asset
mode.ScenePlay
drawBackground func(draws.Image)
// KeyHandleXxx looks slightly better than HandleXxxByKey.
KeyHandleMusicVolume func() bool
KeyHandleSoundVolume func() bool
KeyHandleMusicOffset func() bool
KeyHandleBackgroundBrightness func() bool
KeyHandleDebugPrint func() bool
KeyHandleSpeedScale func() bool
}
func NewScene(cfg *scene.Config, asset *scene.Asset, fsys fs.FS, name string, replay *osr.Format) (s *Scene, err error) {
s = &Scene{Config: cfg, Asset: asset}
switch s.Mode {
case mode.ModePiano:
s.ScenePlay, err = piano.NewScenePlay(s.PianoConfig, s.PianoAssets, fsys, name, replay)
}
if err != nil {
return
}
ch := s.ScenePlay.ChartHeader()
s.drawBackground = scene.NewBackgroundDrawer(s.Config, s.Asset, fsys, ch.BackgroundFilename)
s.KeyHandleMusicVolume = scene.NewMusicVolumeKeyHandler(s.Config, s.Asset)
s.KeyHandleSoundVolume = scene.NewSoundVolumeKeyHandler(s.Config, s.Asset)
s.KeyHandleMusicOffset = scene.NewMusicOffsetKeyHandler(s.Config, s.Asset)
s.KeyHandleBackgroundBrightness = scene.NewBackgroundBrightnessKeyHandler(s.Config, s.Asset)
s.KeyHandleDebugPrint = scene.NewDebugPrintKeyHandler(s.Config, s.Asset)
s.KeyHandleSpeedScale = scene.NewSpeedScaleKeyHandler(s.Config, s.Asset, s.Mode)
ebiten.SetWindowTitle(s.WindowTitle())
return
}
// Changing speed might not be applied after positions are calculated.
// But this is not tested.
func (s *Scene) Update() any {
if s.KeyHandleMusicVolume() {
s.SetMusicVolume(s.MusicVolume)
}
s.KeyHandleSoundVolume()
if s.KeyHandleMusicOffset() {
s.SetMusicOffset(s.MusicOffset)
}
s.KeyHandleBackgroundBrightness()
s.KeyHandleDebugPrint()
if s.KeyHandleSpeedScale() {
s.SetSpeedScale()
}
if input.IsKeyJustPressed(input.KeyTab) {
if s.IsPaused() {
s.Resume()
} else {
s.Pause()
}
}
if input.IsKeyJustPressed(input.KeyEscape) {
return s.ScenePlay.Finish()
// os.Exit(1)
}
return s.ScenePlay.Update()
}
func (s Scene) Draw(screen draws.Image) {
s.drawBackground(screen)
s.ScenePlay.Draw(screen)
if s.DebugPrint {
f := fmt.Fprintf
var b strings.Builder
f(&b, s.Config.DebugString())
f(&b, "\n")
f(&b, s.ScenePlay.DebugString()) // interpolated ScenePlay debug string
f(&b, "\n")
f(&b, "Press TAB to pause.\n")
f(&b, "Press ESC to back to choose a song.\n")
ebitenutil.DebugPrint(screen.Image, b.String())
}
}