Skip to content

Commit

Permalink
Updates documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel Schutz committed Mar 10, 2024
1 parent 5b108e9 commit 299fe79
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 5 deletions.
68 changes: 65 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type MyState struct {

type MyScene struct {
// your scene fields
sm *stagehand.SceneManager[MyState]
}

func (s *MyScene) Update() error {
Expand All @@ -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 {
Expand Down Expand Up @@ -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

Expand All @@ -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))

// ...
}
Expand All @@ -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))

// ...
}
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions examples/director/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ const (

type State int

var (
Trigger stagehand.SceneTransitionTrigger = 1
const (
Trigger stagehand.SceneTransitionTrigger = iota
)

type BaseScene struct {
Expand Down

0 comments on commit 299fe79

Please sign in to comment.