115 lines
2.8 KiB
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.PenguinDefault)
|
|
|
|
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 DirLeft:
|
|
p.animation.SetAnimation(animation.PenguinStationaryLeft)
|
|
case DirRight:
|
|
p.animation.SetAnimation(animation.PenguinStationaryRight)
|
|
case DirUp:
|
|
p.animation.SetAnimation(animation.PenguinStationaryUp)
|
|
case DirDown:
|
|
p.animation.SetAnimation(animation.PenguinStationaryDown)
|
|
default:
|
|
log.Printf("unknown direction: %v", direction)
|
|
p.animation.SetAnimation(animation.PenguinDefault)
|
|
}
|
|
|
|
} else {
|
|
// Figure out which way we are facing now that we're moving
|
|
direction := determineClosestDirection(velocity)
|
|
p.direction = direction
|
|
|
|
switch direction {
|
|
case DirLeft:
|
|
p.animation.SetAnimation(animation.PenguinWalkLeft)
|
|
case DirRight:
|
|
p.animation.SetAnimation(animation.PenguinWalkRight)
|
|
case DirUp:
|
|
p.animation.SetAnimation(animation.PenguinWalkUp)
|
|
case DirDown:
|
|
p.animation.SetAnimation(animation.PenguinWalkDown)
|
|
default:
|
|
log.Printf("unknown direction: %v", direction)
|
|
p.animation.SetAnimation(animation.PenguinDefault)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (p *penguin) SetColor(color rl.Color) {
|
|
p.color = color
|
|
}
|