-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
114 lines (93 loc) · 2.61 KB
/
main.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
106
107
108
109
110
111
112
113
114
package main
import (
"fmt"
"image"
"image/color"
"log"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/hajimehoshi/ebiten/v2/inpututil"
"github.com/joelschutz/stagehand"
)
const (
screenWidth = 640
screenHeight = 480
)
type State struct {
Count int
OnTransition bool
}
type BaseScene struct {
bounds image.Rectangle
count State
sm *stagehand.SceneManager[State]
}
func (s *BaseScene) Layout(w, h int) (int, int) {
s.bounds = image.Rect(0, 0, w, h)
return w, h
}
func (s *BaseScene) Load(st State, sm stagehand.SceneController[State]) {
s.count = st
s.sm = sm.(*stagehand.SceneManager[State])
}
func (s *BaseScene) Unload() State {
return s.count
}
func (s *BaseScene) PreTransition(toScene stagehand.Scene[State]) State {
s.count.OnTransition = true
return s.count
}
func (s *BaseScene) PostTransition(state State, fromScene stagehand.Scene[State]) {
s.count.OnTransition = false
}
type FirstScene struct {
BaseScene
}
func (s *FirstScene) Update() error {
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
s.count.Count++
}
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonRight) {
s.sm.SwitchWithTransition(&SecondScene{}, stagehand.NewSlideTransition[State](stagehand.TopToBottom, .05))
}
return nil
}
func (s *FirstScene) Draw(screen *ebiten.Image) {
if s.count.OnTransition {
screen.Fill(color.RGBA{0, 0, 0, 255}) // Fill Black
} else {
screen.Fill(color.RGBA{255, 0, 0, 255}) // Fill Red
}
ebitenutil.DebugPrintAt(screen, fmt.Sprintf("Count: %v, WindowSize: %s", s.count.Count, s.bounds.Max), s.bounds.Dx()/2, s.bounds.Dy()/2)
}
type SecondScene struct {
BaseScene
}
func (s *SecondScene) Update() error {
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
s.count.Count--
}
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonRight) {
s.sm.SwitchWithTransition(&FirstScene{}, stagehand.NewSlideTransition[State](stagehand.BottomToTop, .05))
}
return nil
}
func (s *SecondScene) Draw(screen *ebiten.Image) {
if s.count.OnTransition {
screen.Fill(color.RGBA{255, 255, 255, 255}) // Fill White
} else {
screen.Fill(color.RGBA{0, 0, 255, 255}) // Fill Blue
}
ebitenutil.DebugPrintAt(screen, fmt.Sprintf("Count: %v, WindowSize: %s", s.count.Count, s.bounds.Max), s.bounds.Dx()/2, s.bounds.Dy()/2)
}
func main() {
ebiten.SetWindowSize(screenWidth, screenHeight)
ebiten.SetWindowTitle("My Game")
ebiten.SetWindowResizable(true)
state := State{Count: 10}
s := &FirstScene{}
sm := stagehand.NewSceneManager[State](s, state)
if err := ebiten.RunGame(sm); err != nil {
log.Fatal(err)
}
}