From 299fe79cc5986a9911a537e942d42f283e4a0ee9 Mon Sep 17 00:00:00 2001 From: Joel Schutz Date: Sun, 10 Mar 2024 08:59:57 -0300 Subject: [PATCH] Updates documentation --- README.md | 68 +++++++++++++++++++++++++++++++++++++-- examples/director/main.go | 4 +-- 2 files changed, 67 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index d57952a..1479f39 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,7 @@ type MyState struct { type MyScene struct { // your scene fields + sm *stagehand.SceneManager[MyState] } func (s *MyScene) Update() error { @@ -44,8 +45,9 @@ func (s *MyScene) Draw(screen *ebiten.Image) { // your draw code } -func (s *MyScene) Load(state MyState ,manager *stagehand.SceneManager) { +func (s *MyScene) Load(state MyState ,manager stagehand.SceneController[MyState]) { // your load code + s.sm = manager.(*stagehand.SceneManager[MyState]) // This type assertion is important } func (s *MyScene) Unload() MyState { @@ -75,6 +77,7 @@ We provide some example code so you can start fast: - [Simple Example](https://github.com/joelschutz/stagehand/blob/master/examples/simple/main.go) - [Timed Transition Example](https://github.com/joelschutz/stagehand/blob/master/examples/timed/main.go) - [Transition Awareness Example](https://github.com/joelschutz/stagehand/blob/master/examples/aware/main.go) +- [Scene Director Example](https://github.com/joelschutz/stagehand/blob/master/examples/director/main.go) ## Transitions @@ -100,7 +103,7 @@ The `FadeTransition` will fade out the current scene while fading in the new sce func (s *MyScene) Update() error { // ... scene2 := &OtherScene{} - s.manager.SwitchWithTransition(scene2. stagehand.NewFadeTransition(.05)) + s.manager.SwitchWithTransition(scene2, stagehand.NewFadeTransition(.05)) // ... } @@ -116,7 +119,7 @@ The `SlideTransition` will slide out the current scene and slide in the new scen func (s *MyScene) Update() error { // ... scene2 := &OtherScene{} - s.manager.SwitchWithTransition(scene2. stagehand.NewSlideTransition(stagehand.LeftToRight, .05)) + s.manager.SwitchWithTransition(scene2, stagehand.NewSlideTransition(stagehand.LeftToRight, .05)) // ... } @@ -183,6 +186,65 @@ Unload Called on old scene PostTransition Called on new scene ``` +## SceneDirector + +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`: + +```go +type MyScene struct { + // your scene fields + director *stagehand.SceneDirector[MyState] +} + +func (s *MyScene) Load(state MyState ,director stagehand.SceneController[MyState]) { + // your load code + s.director = director.(*stagehand.SceneDirector[MyState]) // This type assertion is important +} +``` + +Then define a ruleSet of `Directive` and `SceneTransitionTrigger` for the game. + +```go +// Triggers are int type underneath +const ( + Trigger1 stagehand.SceneTransitionTrigger = iota + Trigger2 +) + +func main() { + // ... + scene1 := &MyScene{} + scene2 := &OtherScene{} + + // Create a rule set for transitioning between scenes based on Triggers + ruleSet := make(map[stagehand.Scene[MyState]][]Directive[MyState]) + directive1 := Directive[MyState]{Dest: scene2, Trigger: Trigger1} + directive2 := Directive[MyState]{Dest: scene1, Trigger: Trigger2, Transition: stagehand.NewFadeTransition(.05)} // Add transitions inside the directive + + // Directives are mapped to each Scene pointer and can be shared + ruleSet[scene1] = []Directive[MyState]{directive1, directive2} + ruleSet[scene2] = []Directive[MyState]{directive2} + + state := MyState{} + manager := stagehand.NewSceneDirector[MyState](scene1, state, ruleSet) + + if err := ebiten.RunGame(sm); err != nil { + log.Fatal(err) + } +} +``` + +Now you can now notify the `SceneDirector` about activated `SceneTransitionTrigger`, if no `Directive` match, the code will still run without errors. + +```go +func (s *MyScene) Update() error { + // ... + s.manager.ProcessTrigger(Trigger) + + // ... +} +``` + ## Contribution 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. diff --git a/examples/director/main.go b/examples/director/main.go index 704ed7c..294086d 100644 --- a/examples/director/main.go +++ b/examples/director/main.go @@ -19,8 +19,8 @@ const ( type State int -var ( - Trigger stagehand.SceneTransitionTrigger = 1 +const ( + Trigger stagehand.SceneTransitionTrigger = iota ) type BaseScene struct {