Change move functions
parent
ddd9ace4b6
commit
a3dc1b7b5c
|
@ -13,6 +13,7 @@ type penguin struct {
|
||||||
currentAnimation *entityAnimation
|
currentAnimation *entityAnimation
|
||||||
animationStep int32
|
animationStep int32
|
||||||
facingRight bool
|
facingRight bool
|
||||||
|
updateAnimation bool
|
||||||
|
|
||||||
// Physical parameters
|
// Physical parameters
|
||||||
worldPosition *vector.Vec2F
|
worldPosition *vector.Vec2F
|
||||||
|
@ -75,48 +76,22 @@ func (p *penguin) SetAnimation(name string) {
|
||||||
p.currentAnimation = a
|
p.currentAnimation = a
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *penguin) MoveRight() {
|
func (p *penguin) MoveX(x float64) {
|
||||||
p.direction.X = 1
|
p.direction.X = x
|
||||||
p.facingRight = true
|
p.updateAnimation = true
|
||||||
p.SetMoveAnimation()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *penguin) MoveLeft() {
|
func (p *penguin) MoveY(y float64) {
|
||||||
p.direction.X = -1
|
|
||||||
p.facingRight = false
|
|
||||||
p.SetMoveAnimation()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *penguin) MoveUp() {
|
|
||||||
// (0,0) is the top left, so negative y moves up
|
// (0,0) is the top left, so negative y moves up
|
||||||
p.direction.Y = -1
|
p.direction.Y = y
|
||||||
p.SetMoveAnimation()
|
p.updateAnimation = true
|
||||||
}
|
|
||||||
|
|
||||||
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()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *penguin) SetMoveAnimation() {
|
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
|
// Stationary
|
||||||
if p.facingRight {
|
if p.facingRight {
|
||||||
p.SetAnimation(PENGUIN_STATIONARY_RIGHT)
|
p.SetAnimation(PENGUIN_STATIONARY_RIGHT)
|
||||||
|
@ -125,6 +100,16 @@ func (p *penguin) SetMoveAnimation() {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Moving
|
// 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 {
|
if p.facingRight {
|
||||||
p.SetAnimation(PENGUIN_WALK_RIGHT)
|
p.SetAnimation(PENGUIN_WALK_RIGHT)
|
||||||
} else {
|
} else {
|
||||||
|
@ -134,6 +119,11 @@ func (p *penguin) SetMoveAnimation() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *penguin) Update() error {
|
func (p *penguin) Update() error {
|
||||||
|
if p.updateAnimation {
|
||||||
|
p.SetMoveAnimation()
|
||||||
|
p.updateAnimation = false
|
||||||
|
}
|
||||||
|
|
||||||
p.direction.Normalize()
|
p.direction.Normalize()
|
||||||
x := p.direction.X * p.speed
|
x := p.direction.X * p.speed
|
||||||
y := p.direction.Y * p.speed
|
y := p.direction.Y * p.speed
|
||||||
|
|
|
@ -3,7 +3,7 @@ package vector
|
||||||
import "math"
|
import "math"
|
||||||
|
|
||||||
// This is a small-scale vector implementation for a Vec2F.
|
// 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.
|
// then you should probably just switch to the glmath library instead.
|
||||||
|
|
||||||
type Vec2F struct {
|
type Vec2F struct {
|
||||||
|
|
15
main.go
15
main.go
|
@ -126,23 +126,24 @@ func run(configMap gosimpleconf.ConfigMap) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
if keystates[sdl.K_d] {
|
if keystates[sdl.K_d] {
|
||||||
penguin.MoveRight()
|
penguin.MoveX(1)
|
||||||
} else if keystates[sdl.K_a] {
|
} else if keystates[sdl.K_a] {
|
||||||
penguin.MoveLeft()
|
penguin.MoveX(-1)
|
||||||
} else {
|
} else {
|
||||||
penguin.StopHorizontal()
|
penguin.MoveX(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
if keystates[sdl.K_w] {
|
if keystates[sdl.K_w] {
|
||||||
penguin.MoveUp()
|
penguin.MoveY(-1)
|
||||||
} else if keystates[sdl.K_s] {
|
} else if keystates[sdl.K_s] {
|
||||||
penguin.MoveDown()
|
penguin.MoveY(1)
|
||||||
} else {
|
} else {
|
||||||
penguin.StopVertical()
|
penguin.MoveY(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !keystates[sdl.K_a] && !keystates[sdl.K_d] && !keystates[sdl.K_w] && !keystates[sdl.K_s] {
|
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
|
// Background
|
||||||
|
|
Loading…
Reference in New Issue