Skip to content

Commit

Permalink
fix hunger logic
Browse files Browse the repository at this point in the history
  • Loading branch information
nhanb committed Sep 17, 2023
1 parent 042ca8a commit 8671ddc
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions states.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,7 @@ func (sm *StateMachine) SetState(s State) {
sm.state.Enter(sm)
}
func (sm *StateMachine) Update() error {
now := time.Now()

// If enough time has passed, force enter hungry state,
// otherwise process state-specific logic as usual.
if now.Sub(sm.lastFed) >= DurationTillHungry {
sm.SetState(&StateHungry{})
sm.lastFed = now
} else {
sm.state.Update(sm)
}
sm.state.Update(sm)

// Advance to next animation frame
sm.ticks += 1
Expand Down Expand Up @@ -84,6 +75,9 @@ type StateIdle struct{}

func (s *StateIdle) Enter(sm *StateMachine) { sm.SetAnim(Idle) }
func (s *StateIdle) Update(sm *StateMachine) {
if checkHunger(sm) {
return
}
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
sm.SetState(&StateDrag{})
return
Expand Down Expand Up @@ -117,6 +111,9 @@ func (s *StateDrag) Enter(sm *StateMachine) {
s.MouseStartPos = GlobalCursorPosition()
}
func (s *StateDrag) Update(sm *StateMachine) {
if checkHunger(sm) {
return
}
if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonLeft) {
sm.SetState(&StateIdle{})
return
Expand All @@ -136,6 +133,9 @@ func (s *StateRClick) Enter(sm *StateMachine) {
sm.SetAnim(RightClick)
}
func (s *StateRClick) Update(sm *StateMachine) {
if checkHunger(sm) {
return
}
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
sm.SetState(&StateDrag{})
return
Expand All @@ -160,18 +160,31 @@ func (s *StateHungry) Update(sm *StateMachine) {
}
func (s *StateHungry) EndAnimHook(sm *StateMachine) {}

func checkHunger(sm *StateMachine) (isHungry bool) {
now := time.Now()
if now.Sub(sm.lastFed) >= DurationTillHungry {
sm.SetState(&StateHungry{})
return true
}
return false
}

type StateFeed struct{}

func (s *StateFeed) Enter(sm *StateMachine) { sm.SetAnim(Feed) }
func (s *StateFeed) Update(sm *StateMachine) {}
func (s *StateFeed) EndAnimHook(sm *StateMachine) {
sm.SetState(&StateIdle{})
sm.lastFed = time.Now()
}

type StateWalkL struct{}

func (s *StateWalkL) Enter(sm *StateMachine) { sm.SetAnim(WalkLeft) }
func (s *StateWalkL) Update(sm *StateMachine) {
if checkHunger(sm) {
return
}
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
sm.SetState(&StateDrag{})
return
Expand All @@ -191,6 +204,9 @@ type StateWalkR struct{}

func (s *StateWalkR) Enter(sm *StateMachine) { sm.SetAnim(WalkRight) }
func (s *StateWalkR) Update(sm *StateMachine) {
if checkHunger(sm) {
return
}
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
sm.SetState(&StateDrag{})
return
Expand Down

0 comments on commit 8671ddc

Please sign in to comment.