Skip to content

Commit

Permalink
implement walk states
Browse files Browse the repository at this point in the history
  • Loading branch information
nhanb committed Sep 17, 2023
1 parent 5c694ce commit 5cfebd8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
11 changes: 5 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type Anim struct {
type Position struct{ x, y int }

var DurationTillHungry time.Duration
var WalkChance, StopChance int

type Game struct {
CurrentAnim *Anim
Expand All @@ -60,8 +61,6 @@ type Game struct {
MouseStartPos Vector
Size int
LastFed time.Time
WalkChance int
StopChance int
X int
Y int
MaxX int
Expand Down Expand Up @@ -145,15 +144,15 @@ func (g *Game) Update() error {
}

if g.CurrentAnim == Idle {
if randBool(g.WalkChance) {
if randBool(WalkChance) {
if randBool(50) {
g.CurrentAnim = WalkLeft
} else {
g.CurrentAnim = WalkRight
}
}
} else if g.CurrentAnim == WalkLeft || g.CurrentAnim == WalkRight {
if randBool(g.StopChance) {
if randBool(StopChance) {
g.CurrentAnim = Idle
}
}
Expand Down Expand Up @@ -268,8 +267,8 @@ func main() {
game.LastFed = time.Now()
DurationTillHungry = time.Duration(secondsUntilHungryFlag) * 1_000_000_000
game.Size = sizeFlag
game.WalkChance = walkChanceFlag
game.StopChance = stopChanceFlag
WalkChance = walkChanceFlag
StopChance = stopChanceFlag
game.X = xFlag
game.Y = yFlag

Expand Down
34 changes: 30 additions & 4 deletions states.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,6 @@ type State interface {
EndAnimHook(sm *StateMachine)
}

type StateWalkL struct{}
type StateWalkR struct{}

type StateIdle struct{}

func (s *StateIdle) Enter(sm *StateMachine) { sm.SetAnim(Idle) }
Expand All @@ -94,7 +91,16 @@ func (s *StateIdle) Update(sm *StateMachine) {
return
}
}
func (s *StateIdle) EndAnimHook(sm *StateMachine) {}
func (s *StateIdle) EndAnimHook(sm *StateMachine) {
if randBool(WalkChance) {
if randBool(50) {
sm.SetState(&StateWalkL{})
} else {
sm.SetState(&StateWalkR{})
}
return
}
}

type StateDrag struct {
PreviousMousePos Vector
Expand Down Expand Up @@ -150,3 +156,23 @@ func (s *StateFeed) Update(sm *StateMachine) {}
func (s *StateFeed) EndAnimHook(sm *StateMachine) {
sm.SetState(&StateIdle{})
}

type StateWalkL struct{}

func (s *StateWalkL) Enter(sm *StateMachine) { sm.SetAnim(WalkLeft) }
func (s *StateWalkL) Update(sm *StateMachine) {}
func (s *StateWalkL) EndAnimHook(sm *StateMachine) {
if randBool(StopChance) {
sm.SetState(&StateIdle{})
}
}

type StateWalkR struct{}

func (s *StateWalkR) Enter(sm *StateMachine) { sm.SetAnim(WalkRight) }
func (s *StateWalkR) Update(sm *StateMachine) {}
func (s *StateWalkR) EndAnimHook(sm *StateMachine) {
if randBool(StopChance) {
sm.SetState(&StateIdle{})
}
}

0 comments on commit 5cfebd8

Please sign in to comment.