Switch to Percy penguin animations

main
Sean Hickey 2022-10-23 22:31:51 -07:00
parent 0368193061
commit b16a27cc09
6 changed files with 39 additions and 14 deletions

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 KiB

View File

@ -10,14 +10,19 @@ var PenguinAnimations map[int]*entityAnimation
const (
PENGUIN_WALK_RIGHT int = iota
PENGUIN_WALK_LEFT
PENGUIN_WALK_UP
PENGUIN_WALK_DOWN
PENGUIN_STATIONARY_RIGHT
PENGUIN_STATIONARY_LEFT
)
PENGUIN_DEFAULT = PENGUIN_STATIONARY_RIGHT
const (
PENGUIN_NUM_ANIMS = 6
PENGUIN_DEFAULT = PENGUIN_STATIONARY_RIGHT
)
func DefinePenguinAnimations() {
filename := sprite.PLATFORMER_FOREST_CHARACTERS
filename := sprite.PERCYWALKING
var (
dimensions sdl.Point
@ -25,17 +30,19 @@ func DefinePenguinAnimations() {
center *sdl.Point
length int32
speed int32
border int32
)
dimensions = sdl.Point{X: 32, Y: 32}
dimensions = sdl.Point{X: 13, Y: 17}
PenguinAnimations = make(map[int]*entityAnimation)
// Walking Right is in the spritesheet.
speed = 5
offset = sdl.Point{X: 0, Y: 2}
length = 4
offset = sdl.Point{X: 0, Y: 1}
length = 5
border = 1 // optional border around each sprite
center = nil // center is for rotation, nil will default to w/2 h/2
walkRight := sprite.NewAnimation(filename, dimensions, offset, length)
walkRight := sprite.NewAnimation(filename, dimensions, offset, length, border)
PenguinAnimations[PENGUIN_WALK_RIGHT] = NewEntityAnimation(walkRight, speed, length, 0, center, sdl.FLIP_NONE)
// Walking Left is just that flipped.
@ -43,7 +50,19 @@ func DefinePenguinAnimations() {
// Stationary is just the first frame.
length = 1
stationaryRight := sprite.NewAnimation(filename, dimensions, offset, length)
stationaryRight := sprite.NewAnimation(filename, dimensions, offset, length, border)
PenguinAnimations[PENGUIN_STATIONARY_RIGHT] = NewEntityAnimation(stationaryRight, speed, length, 0, center, sdl.FLIP_NONE)
PenguinAnimations[PENGUIN_STATIONARY_LEFT] = NewEntityAnimation(stationaryRight, speed, length, 0, center, sdl.FLIP_HORIZONTAL)
// Walk Up
length = 5
offset = sdl.Point{X: 0, Y: 3}
walkUp := sprite.NewAnimation(filename, dimensions, offset, length, border)
PenguinAnimations[PENGUIN_WALK_UP] = NewEntityAnimation(walkUp, speed, length, 0, center, sdl.FLIP_NONE)
// Walk Down
length = 5
offset = sdl.Point{X: 0, Y: 0}
walkDown := sprite.NewAnimation(filename, dimensions, offset, length, border)
PenguinAnimations[PENGUIN_WALK_DOWN] = NewEntityAnimation(walkDown, speed, length, 0, center, sdl.FLIP_NONE)
}

View File

@ -108,7 +108,7 @@ func Run(ctx context.Context, configMap gosimpleconf.ConfigMap) error {
entityCmd := command.NewCommandHandler(ctx, entity)
randomPos := vector.Vec2F{X: rand.Float64() * 500, Y: rand.Float64() * 500}
entity.SetPosition(&randomPos)
entity.SetAnimation(animation.PENGUIN_WALK_LEFT)
entity.SetAnimation(rand.Intn(animation.PENGUIN_NUM_ANIMS))
entityList = append(entityList, entityCmd)
wg.Add(1)

View File

@ -13,6 +13,7 @@ type spriteAnimation struct {
dimensions sdl.Point
offset sdl.Point
length int32
border int32
}
func NewAnimation(
@ -20,6 +21,7 @@ func NewAnimation(
dimensions sdl.Point,
offset sdl.Point,
length int32,
border int32,
) *spriteAnimation {
spritesheet := GetSpritesheet(filename)
@ -29,6 +31,7 @@ func NewAnimation(
dimensions: dimensions,
offset: offset,
length: length,
border: border,
}
return &a
@ -46,14 +49,15 @@ func (a *spriteAnimation) Draw(
height := a.dimensions.Y
base := sdl.Point{
X: width * a.offset.X,
Y: height * a.offset.Y,
X: (width+a.border)*a.offset.X + a.border,
Y: (height+a.border)*a.offset.Y + a.border,
}
// Assuming all frames are horizontal going left to right
// Assuming all frames are horizontal going left to right.
// Potentially with a border offset as well.
f := frame % a.length
section := sdl.Rect{
X: base.X + f*width,
X: base.X + f*(width+a.border),
Y: base.Y,
W: width,
H: height,
@ -67,8 +71,8 @@ func (a *spriteAnimation) Draw(
placement := sdl.Rect{
X: windowPosition.X,
Y: windowPosition.Y,
W: width,
H: height,
W: width * 2, // TODO just testing with x2
H: height * 2,
}
err = a.spritesheet.Draw(&section, &placement, angle, center, flip)

View File

@ -8,11 +8,13 @@ import (
const (
PENGUIN = "assets/images/penguin.png"
PERCYWALKING = "assets/images/percywalking.png"
PLATFORMER_FOREST_CHARACTERS = "assets/images/a-platformer-in-the-forest/characters.png"
)
var fileList []string = []string{
PENGUIN,
PERCYWALKING,
PLATFORMER_FOREST_CHARACTERS,
}