-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.go
97 lines (82 loc) · 2.3 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
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"github.com/ketMix/retromancer/states"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/inpututil"
"github.com/tinne26/etxt"
)
type Game struct {
States []states.State
Flags Flags
Text *etxt.Renderer
Resources ResourceManager
Localizer Localizer
Cursor Cursor
MusicPlayer MusicPlayer
Difficulty states.Difficulty
}
func (g *Game) CreateContext() states.Context {
return states.Context{
StateMachine: g,
Cursor: &g.Cursor,
MusicPlayer: &g.MusicPlayer,
Difficulty: g.Difficulty,
L: &g.Localizer,
R: &g.Resources,
}
}
func (g *Game) State() states.State {
return g.States[len(g.States)-1]
}
func (g *Game) PushState(state states.State) {
g.States = append(g.States, state)
ctx := g.CreateContext()
state.Init(ctx)
}
func (g *Game) PopState(v interface{}) {
if len(g.States) == 0 {
return
}
ctx := g.CreateContext()
g.States[len(g.States)-1].Finalize(ctx)
g.States = g.States[:len(g.States)-1]
g.States[len(g.States)-1].Enter(ctx, v)
}
func (g *Game) Init() error {
g.Cursor.image = g.Resources.GetAs("images", "hand-normal", (*ebiten.Image)(nil)).(*ebiten.Image)
g.Cursor.Enable()
g.Text = etxt.NewRenderer()
return nil
}
func (g *Game) Update() error {
if (ebiten.IsKeyPressed(ebiten.KeyAlt) && inpututil.IsKeyJustPressed(ebiten.KeyEnter)) || inpututil.IsKeyJustPressed(ebiten.KeyF11) {
ebiten.SetFullscreen(!ebiten.IsFullscreen())
}
// Check if music needs to loop, etc.
g.MusicPlayer.Update()
if state := g.State(); state != nil {
return state.Update(g.CreateContext())
}
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
if state := g.State(); state != nil {
state.Draw(states.DrawContext{
Screen: screen,
Text: g.Text,
L: &g.Localizer,
R: &g.Resources,
})
}
if g.Cursor.Enabled() {
x, y := ebiten.CursorPosition()
opts := &ebiten.DrawImageOptions{}
opts.GeoM.Translate(float64(x), float64(y))
opts.GeoM.Translate(-float64(g.Cursor.image.Bounds().Dx())/2, -float64(g.Cursor.image.Bounds().Dy())/2)
screen.DrawImage(g.Cursor.image, opts)
}
//ebitenutil.DebugPrint(screen, fmt.Sprintf("FPS: %0.2f TPS: %0.2f", ebiten.ActualFPS(), ebiten.ActualTPS()))
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return 640, 360
}