project-ely/internal/entity/penguin.go

115 lines
2.8 KiB
Go

package entity
import (
"log"
"math"
"git.wisellama.rocks/Project-Ely/project-ely/internal/animation"
"git.wisellama.rocks/Project-Ely/project-ely/internal/physics"
rl "github.com/gen2brain/raylib-go/raylib"
)
type penguin struct {
animation AnimationTracker
object physics.Object
direction int
staticAnimation bool
color rl.Color
}
func NewPenguin() *penguin {
object := physics.NewObject()
anim := animation.NewAnimationTracker()
anim.SetAnimation(animation.PENGUIN_DEFAULT)
return &penguin{
animation: anim,
object: object,
staticAnimation: false,
color: rl.White,
}
}
func (p *penguin) Draw() error {
position := p.object.GetPosition()
// TODO?
//windowPosition := worldPosToWindowPos()
windowPosition := rl.Vector2{
X: float32(math.Round(float64(position.X))),
Y: float32(-1 * math.Round(float64(position.Y))),
}
return p.animation.Draw(windowPosition, p.color)
}
func (p *penguin) Update() error {
p.SetMoveAnimation()
return p.object.Update()
}
func (p *penguin) GetObject() physics.Object {
return p.object
}
func (p *penguin) GetAnimationTracker() AnimationTracker {
return p.animation
}
// ToggleStaticAnimation will set whether or not this entity should ever update its animation loop or not.
// True means this will never update, it is static.
// False means this will update the animation loop based on physical properties of this entity.
func (p *penguin) ToggleStaticAnimation() {
p.staticAnimation = !p.staticAnimation
}
func (p *penguin) SetMoveAnimation() {
if p.staticAnimation {
return
}
velocity := p.object.GetVelocity()
if velocity.X == 0 && velocity.Y == 0 {
// Stay facing whatever direction we were facing
direction := p.direction
switch direction {
case DIR_LEFT:
p.animation.SetAnimation(animation.PENGUIN_STATIONARY_LEFT)
case DIR_RIGHT:
p.animation.SetAnimation(animation.PENGUIN_STATIONARY_RIGHT)
case DIR_UP:
p.animation.SetAnimation(animation.PENGUIN_STATIONARY_UP)
case DIR_DOWN:
p.animation.SetAnimation(animation.PENGUIN_STATIONARY_DOWN)
default:
log.Printf("unknown direction: %v", direction)
p.animation.SetAnimation(animation.PENGUIN_DEFAULT)
}
} else {
// Figure out which way we are facing now that we're moving
direction := determineClosestDirection(velocity)
p.direction = direction
switch direction {
case DIR_LEFT:
p.animation.SetAnimation(animation.PENGUIN_WALK_LEFT)
case DIR_RIGHT:
p.animation.SetAnimation(animation.PENGUIN_WALK_RIGHT)
case DIR_UP:
p.animation.SetAnimation(animation.PENGUIN_WALK_UP)
case DIR_DOWN:
p.animation.SetAnimation(animation.PENGUIN_WALK_DOWN)
default:
log.Printf("unknown direction: %v", direction)
p.animation.SetAnimation(animation.PENGUIN_DEFAULT)
}
}
}
func (p *penguin) SetColor(color rl.Color) {
p.color = color
}