Skip to content

Commit

Permalink
explicitly track window position
Browse files Browse the repository at this point in the history
This solves a bug where quick calls to ebiten.(Set)WindowPosition()
result in lost progress.

But now we have a bug when our window tries to walk agains a screen
boundary...
  • Loading branch information
nhanb committed Sep 17, 2023
1 parent 5feac3e commit 4375d5a
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions states.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ type StateMachine struct {
animFrameCount int // number of frames in current anim
frameIdx int // frame index in current animation
ticks int // ticks since last animation frame change
lastFed time.Time
x, y int // logical window position

lastFed time.Time
isXYInit bool
}

func NewStateMachine() *StateMachine {
Expand All @@ -39,8 +41,15 @@ func (sm *StateMachine) SetState(s State) {
sm.state.Enter(sm)
}
func (sm *StateMachine) Update() error {
if !sm.isXYInit {
sm.x, sm.y = ebiten.WindowPosition()
sm.isXYInit = true
}

sm.state.Update(sm)

ebiten.SetWindowPosition(sm.x, sm.y)

// Advance to next animation frame
sm.ticks += 1
if sm.ticks < 10 {
Expand Down Expand Up @@ -117,7 +126,7 @@ func (s *StateDrag) Update(sm *StateMachine) {
mousePos := GlobalCursorPosition()
if mousePos != s.PreviousMousePos {
winPos := s.WinStartPos.Add(mousePos.Subtract(s.MouseStartPos))
ebiten.SetWindowPosition(winPos.x, winPos.y)
sm.x, sm.y = winPos.x, winPos.y
}
s.PreviousMousePos = mousePos
}
Expand Down Expand Up @@ -198,15 +207,13 @@ func (s *StateWalk) Update(sm *StateMachine) {
return
}

x, y := ebiten.WindowPosition()
newX := x
oldX := sm.x
if s.isLeft {
newX -= 2
sm.x -= 1
} else {
newX += 2
sm.x += 1
}
fmt.Println(x, "->", newX)
ebiten.SetWindowPosition(newX, y)
fmt.Println(oldX, "->", sm.x)
}
func (s *StateWalk) EndAnimHook(sm *StateMachine) {
if randBool(StopChance) {
Expand Down

0 comments on commit 4375d5a

Please sign in to comment.