project-ely/internal/game/entity/penguin.go

90 lines
1.6 KiB
Go

package entity
import (
"log"
"github.com/veandco/go-sdl2/sdl"
)
type penguin struct {
worldPosition *sdl.Point
currentAnimation *entityAnimation
animationStep int32
facingRight bool
}
func NewPenguin(renderer *sdl.Renderer) *penguin {
position := sdl.Point{}
p := penguin{
worldPosition: &position,
currentAnimation: penguinAnimations[PENGUIN_DEFAULT],
animationStep: 0,
facingRight: true,
}
return &p
}
func (p *penguin) Draw() error {
step := p.animationStep / p.currentAnimation.speed
err := p.currentAnimation.Draw(step, p.worldPosition)
if err != nil {
return err
}
p.animationStep = 1 + p.animationStep
return nil
}
func (p *penguin) SetPosition(point *sdl.Point) {
p.worldPosition = point
}
func (p *penguin) SetAnimation(name string) {
a, exists := penguinAnimations[name]
if !exists {
log.Printf("animation does not exist: %v", name)
a = penguinAnimations[PENGUIN_DEFAULT]
}
if a != p.currentAnimation {
p.animationStep = 0
}
p.currentAnimation = a
}
func (p *penguin) MoveRight() {
p.worldPosition.X += 1
p.SetAnimation(PENGUIN_WALK_RIGHT)
p.facingRight = true
}
func (p *penguin) MoveLeft() {
p.worldPosition.X -= 1
p.SetAnimation(PENGUIN_WALK_LEFT)
p.facingRight = false
}
func (p *penguin) MoveUp() {
// (0,0) is the top left, so negative y moves up
p.worldPosition.Y -= 1
}
func (p *penguin) MoveDown() {
// positive y moves down
p.worldPosition.Y += 1
}
func (p *penguin) StopMove() {
if p.facingRight {
p.SetAnimation(PENGUIN_STATIONARY_RIGHT)
} else {
p.SetAnimation(PENGUIN_STATIONARY_LEFT)
}
}