Skip to content

Commit

Permalink
Updates examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel Schutz committed Mar 8, 2024
1 parent f7c9f89 commit 0651a50
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 8 deletions.
8 changes: 4 additions & 4 deletions examples/aware/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ func (s *BaseScene) Unload() State {
return s.count
}

func (s *BaseScene) PreTransition(toScene stagehand.Scene[State]) State {
func (s *BaseScene) PreTransition(toScene stagehand.Scene[State, *stagehand.SceneManager[State]]) State {
s.count.OnTransition = true
return s.count
}

func (s *BaseScene) PostTransition(state State, fromScene stagehand.Scene[State]) {
func (s *BaseScene) PostTransition(state State, fromScene stagehand.Scene[State, *stagehand.SceneManager[State]]) {
s.count.OnTransition = false
}

Expand All @@ -60,7 +60,7 @@ func (s *FirstScene) Update() error {
s.count.Count++
}
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonRight) {
s.sm.SwitchWithTransition(&SecondScene{}, stagehand.NewSlideTransition[State](stagehand.TopToBottom, .05))
s.sm.SwitchWithTransition(&SecondScene{}, stagehand.NewSlideTransition[State, *stagehand.SceneManager[State]](stagehand.TopToBottom, .05))
}
return nil
}
Expand All @@ -83,7 +83,7 @@ func (s *SecondScene) Update() error {
s.count.Count--
}
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonRight) {
s.sm.SwitchWithTransition(&FirstScene{}, stagehand.NewSlideTransition[State](stagehand.BottomToTop, .05))
s.sm.SwitchWithTransition(&FirstScene{}, stagehand.NewSlideTransition[State, *stagehand.SceneManager[State]](stagehand.BottomToTop, .05))
}
return nil
}
Expand Down
106 changes: 106 additions & 0 deletions examples/director/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
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 int

var (
Trigger stagehand.SceneTransitionTrigger = 1
)

type BaseScene struct {
bounds image.Rectangle
count State
sm *stagehand.SceneDirector[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.SceneDirector[State]) {
s.count = st
s.sm = sm
}

func (s *BaseScene) Unload() State {
return s.count
}

type FirstScene struct {
BaseScene
}

func (s *FirstScene) Update() error {
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
s.count++
}
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonRight) {
s.sm.ProcessTrigger(Trigger)
}
return nil
}

func (s *FirstScene) Draw(screen *ebiten.Image) {
screen.Fill(color.RGBA{255, 0, 0, 255}) // Fill Red
ebitenutil.DebugPrintAt(screen, fmt.Sprintf("Count: %v, WindowSize: %s", s.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--
}
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonRight) {
s.sm.ProcessTrigger(Trigger)
}
return nil
}

func (s *SecondScene) Draw(screen *ebiten.Image) {
screen.Fill(color.RGBA{0, 0, 255, 255}) // Fill Blue
ebitenutil.DebugPrintAt(screen, fmt.Sprintf("Count: %v, WindowSize: %s", s.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(10)

s1 := &FirstScene{}
s2 := &SecondScene{}
rs := map[stagehand.Scene[State, *stagehand.SceneDirector[State]]][]stagehand.Directive[State]{
s1: []stagehand.Directive[State]{
stagehand.Directive[State]{Dest: s2, Trigger: Trigger},
},
s2: []stagehand.Directive[State]{
stagehand.Directive[State]{Dest: s1, Trigger: Trigger},
},
}
sm := stagehand.NewSceneDirector[State](s1, state, rs)

if err := ebiten.RunGame(sm); err != nil {
log.Fatal(err)
}
}
4 changes: 2 additions & 2 deletions examples/simple/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (s *SecondScene) Update() error {
s.count--
}
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonRight) {
s.sm.SwitchWithTransition(&ThirdScene{}, stagehand.NewFadeTransition[State](.05))
s.sm.SwitchWithTransition(&ThirdScene{}, stagehand.NewFadeTransition[State, *stagehand.SceneManager[State]](.05))
}
return nil
}
Expand All @@ -86,7 +86,7 @@ func (s *ThirdScene) Update() error {
s.count *= 2
}
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonRight) {
s.sm.SwitchWithTransition(&FirstScene{}, stagehand.NewSlideTransition[State](stagehand.RightToLeft, .05))
s.sm.SwitchWithTransition(&FirstScene{}, stagehand.NewSlideTransition[State, *stagehand.SceneManager[State]](stagehand.RightToLeft, .05))
}
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions examples/timed/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (s *FirstScene) Update() error {
s.count++
}
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonRight) {
s.sm.SwitchWithTransition(&SecondScene{}, stagehand.NewTicksTimedSlideTransition[State](stagehand.LeftToRight, time.Second*time.Duration(s.count)))
s.sm.SwitchWithTransition(&SecondScene{}, stagehand.NewTicksTimedSlideTransition[State, *stagehand.SceneManager[State]](stagehand.LeftToRight, time.Second*time.Duration(s.count)))
}
return nil
}
Expand All @@ -68,7 +68,7 @@ func (s *SecondScene) Update() error {
s.count--
}
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonRight) {
s.sm.SwitchWithTransition(&FirstScene{}, stagehand.NewDurationTimedSlideTransition[State](stagehand.RightToLeft, time.Second*time.Duration(s.count)))
s.sm.SwitchWithTransition(&FirstScene{}, stagehand.NewDurationTimedSlideTransition[State, *stagehand.SceneManager[State]](stagehand.RightToLeft, time.Second*time.Duration(s.count)))
}
return nil
}
Expand Down

0 comments on commit 0651a50

Please sign in to comment.