From a3dc1b7b5c41bb0d8a9cd441fb8c3fae7f0eae7c Mon Sep 17 00:00:00 2001 From: Sean Hickey Date: Fri, 21 Oct 2022 16:12:08 -0700 Subject: [PATCH] Change move functions --- internal/game/entity/penguin.go | 62 ++++++++++++++------------------- internal/vector/vector.go | 2 +- main.go | 15 ++++---- 3 files changed, 35 insertions(+), 44 deletions(-) diff --git a/internal/game/entity/penguin.go b/internal/game/entity/penguin.go index 4db8889..89bd88d 100644 --- a/internal/game/entity/penguin.go +++ b/internal/game/entity/penguin.go @@ -13,6 +13,7 @@ type penguin struct { currentAnimation *entityAnimation animationStep int32 facingRight bool + updateAnimation bool // Physical parameters worldPosition *vector.Vec2F @@ -75,48 +76,22 @@ func (p *penguin) SetAnimation(name string) { p.currentAnimation = a } -func (p *penguin) MoveRight() { - p.direction.X = 1 - p.facingRight = true - p.SetMoveAnimation() +func (p *penguin) MoveX(x float64) { + p.direction.X = x + p.updateAnimation = true } -func (p *penguin) MoveLeft() { - p.direction.X = -1 - p.facingRight = false - p.SetMoveAnimation() -} - -func (p *penguin) MoveUp() { +func (p *penguin) MoveY(y float64) { // (0,0) is the top left, so negative y moves up - p.direction.Y = -1 - p.SetMoveAnimation() -} - -func (p *penguin) MoveDown() { - // positive y moves down - p.direction.Y = 1 - p.SetMoveAnimation() -} - -func (p *penguin) StopMove() { - p.direction.X = 0 - p.direction.Y = 0 - p.SetMoveAnimation() -} - -func (p *penguin) StopHorizontal() { - p.direction.X = 0 - p.SetMoveAnimation() -} - -func (p *penguin) StopVertical() { - p.direction.Y = 0 - p.SetMoveAnimation() + p.direction.Y = y + p.updateAnimation = true } func (p *penguin) SetMoveAnimation() { - if p.direction.X == 0 && p.direction.Y == 0 { + x := p.direction.X + y := p.direction.Y + + if x == 0 && y == 0 { // Stationary if p.facingRight { p.SetAnimation(PENGUIN_STATIONARY_RIGHT) @@ -125,6 +100,16 @@ func (p *penguin) SetMoveAnimation() { } } else { // Moving + + // Update which direction we're facing + if x > 0 { + p.facingRight = true + } else if x < 0 { + p.facingRight = false + } + + // if x == 0, stay facing whatever direction we were previously facing + if p.facingRight { p.SetAnimation(PENGUIN_WALK_RIGHT) } else { @@ -134,6 +119,11 @@ func (p *penguin) SetMoveAnimation() { } func (p *penguin) Update() error { + if p.updateAnimation { + p.SetMoveAnimation() + p.updateAnimation = false + } + p.direction.Normalize() x := p.direction.X * p.speed y := p.direction.Y * p.speed diff --git a/internal/vector/vector.go b/internal/vector/vector.go index f38aaf6..9f03b45 100644 --- a/internal/vector/vector.go +++ b/internal/vector/vector.go @@ -3,7 +3,7 @@ package vector import "math" // This is a small-scale vector implementation for a Vec2F. -// If you need anything more complicated than just normals and dot-products, +// If you need anything more complicated than normals and dot-products, // then you should probably just switch to the glmath library instead. type Vec2F struct { diff --git a/main.go b/main.go index 8cf85d4..e9ba52b 100644 --- a/main.go +++ b/main.go @@ -126,23 +126,24 @@ func run(configMap gosimpleconf.ConfigMap) error { } if keystates[sdl.K_d] { - penguin.MoveRight() + penguin.MoveX(1) } else if keystates[sdl.K_a] { - penguin.MoveLeft() + penguin.MoveX(-1) } else { - penguin.StopHorizontal() + penguin.MoveX(0) } if keystates[sdl.K_w] { - penguin.MoveUp() + penguin.MoveY(-1) } else if keystates[sdl.K_s] { - penguin.MoveDown() + penguin.MoveY(1) } else { - penguin.StopVertical() + penguin.MoveY(0) } if !keystates[sdl.K_a] && !keystates[sdl.K_d] && !keystates[sdl.K_w] && !keystates[sdl.K_s] { - penguin.StopMove() + penguin.MoveX(0) + penguin.MoveY(0) } // Background