Change move functions

main
Sean Hickey 2022-10-21 16:12:08 -07:00
parent ddd9ace4b6
commit a3dc1b7b5c
3 changed files with 35 additions and 44 deletions

View File

@ -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

View File

@ -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 {

15
main.go
View File

@ -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