Skip to content

Commit 299fe79

Browse files
author
Joel Schutz
committed
Updates documentation
1 parent 5b108e9 commit 299fe79

File tree

2 files changed

+67
-5
lines changed

2 files changed

+67
-5
lines changed

README.md

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type MyState struct {
3434

3535
type MyScene struct {
3636
// your scene fields
37+
sm *stagehand.SceneManager[MyState]
3738
}
3839

3940
func (s *MyScene) Update() error {
@@ -44,8 +45,9 @@ func (s *MyScene) Draw(screen *ebiten.Image) {
4445
// your draw code
4546
}
4647

47-
func (s *MyScene) Load(state MyState ,manager *stagehand.SceneManager) {
48+
func (s *MyScene) Load(state MyState ,manager stagehand.SceneController[MyState]) {
4849
// your load code
50+
s.sm = manager.(*stagehand.SceneManager[MyState]) // This type assertion is important
4951
}
5052

5153
func (s *MyScene) Unload() MyState {
@@ -75,6 +77,7 @@ We provide some example code so you can start fast:
7577
- [Simple Example](https://github.com/joelschutz/stagehand/blob/master/examples/simple/main.go)
7678
- [Timed Transition Example](https://github.com/joelschutz/stagehand/blob/master/examples/timed/main.go)
7779
- [Transition Awareness Example](https://github.com/joelschutz/stagehand/blob/master/examples/aware/main.go)
80+
- [Scene Director Example](https://github.com/joelschutz/stagehand/blob/master/examples/director/main.go)
7881

7982
## Transitions
8083

@@ -100,7 +103,7 @@ The `FadeTransition` will fade out the current scene while fading in the new sce
100103
func (s *MyScene) Update() error {
101104
// ...
102105
scene2 := &OtherScene{}
103-
s.manager.SwitchWithTransition(scene2. stagehand.NewFadeTransition(.05))
106+
s.manager.SwitchWithTransition(scene2, stagehand.NewFadeTransition(.05))
104107

105108
// ...
106109
}
@@ -116,7 +119,7 @@ The `SlideTransition` will slide out the current scene and slide in the new scen
116119
func (s *MyScene) Update() error {
117120
// ...
118121
scene2 := &OtherScene{}
119-
s.manager.SwitchWithTransition(scene2. stagehand.NewSlideTransition(stagehand.LeftToRight, .05))
122+
s.manager.SwitchWithTransition(scene2, stagehand.NewSlideTransition(stagehand.LeftToRight, .05))
120123

121124
// ...
122125
}
@@ -183,6 +186,65 @@ Unload Called on old scene
183186
PostTransition Called on new scene
184187
```
185188

189+
## SceneDirector
190+
191+
The `SceneDirector` is an alternative way to manage the transitions between scenes. It provides transitioning between scenes based on a set of rules just like a FSM. The `Scene` implementation is the same, with only a feel differences, first you need to assert the `SceneDirector` instead of the `SceneManager`:
192+
193+
```go
194+
type MyScene struct {
195+
// your scene fields
196+
director *stagehand.SceneDirector[MyState]
197+
}
198+
199+
func (s *MyScene) Load(state MyState ,director stagehand.SceneController[MyState]) {
200+
// your load code
201+
s.director = director.(*stagehand.SceneDirector[MyState]) // This type assertion is important
202+
}
203+
```
204+
205+
Then define a ruleSet of `Directive` and `SceneTransitionTrigger` for the game.
206+
207+
```go
208+
// Triggers are int type underneath
209+
const (
210+
Trigger1 stagehand.SceneTransitionTrigger = iota
211+
Trigger2
212+
)
213+
214+
func main() {
215+
// ...
216+
scene1 := &MyScene{}
217+
scene2 := &OtherScene{}
218+
219+
// Create a rule set for transitioning between scenes based on Triggers
220+
ruleSet := make(map[stagehand.Scene[MyState]][]Directive[MyState])
221+
directive1 := Directive[MyState]{Dest: scene2, Trigger: Trigger1}
222+
directive2 := Directive[MyState]{Dest: scene1, Trigger: Trigger2, Transition: stagehand.NewFadeTransition(.05)} // Add transitions inside the directive
223+
224+
// Directives are mapped to each Scene pointer and can be shared
225+
ruleSet[scene1] = []Directive[MyState]{directive1, directive2}
226+
ruleSet[scene2] = []Directive[MyState]{directive2}
227+
228+
state := MyState{}
229+
manager := stagehand.NewSceneDirector[MyState](scene1, state, ruleSet)
230+
231+
if err := ebiten.RunGame(sm); err != nil {
232+
log.Fatal(err)
233+
}
234+
}
235+
```
236+
237+
Now you can now notify the `SceneDirector` about activated `SceneTransitionTrigger`, if no `Directive` match, the code will still run without errors.
238+
239+
```go
240+
func (s *MyScene) Update() error {
241+
// ...
242+
s.manager.ProcessTrigger(Trigger)
243+
244+
// ...
245+
}
246+
```
247+
186248
## Contribution
187249

188250
Contributions are welcome! If you find a bug or have a feature request, please open an issue on GitHub. If you would like to contribute code, please fork the repository and submit a pull request.

examples/director/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ const (
1919

2020
type State int
2121

22-
var (
23-
Trigger stagehand.SceneTransitionTrigger = 1
22+
const (
23+
Trigger stagehand.SceneTransitionTrigger = iota
2424
)
2525

2626
type BaseScene struct {

0 commit comments

Comments
 (0)