165 lines
3.7 KiB
Go
165 lines
3.7 KiB
Go
package types
|
|
|
|
import (
|
|
"log"
|
|
"math"
|
|
"sync"
|
|
|
|
"git.wisellama.rocks/Project-Ely/project-ely/internal/game/entity/animation"
|
|
"git.wisellama.rocks/Project-Ely/project-ely/internal/vector"
|
|
"github.com/veandco/go-sdl2/sdl"
|
|
)
|
|
|
|
type penguin struct {
|
|
mx sync.RWMutex
|
|
|
|
// Animation parameters
|
|
currentAnimation EntityAnimation // pointer to current animation loop
|
|
animationStep int // index of animation loop
|
|
direction int // direction facing for sprite animations
|
|
updateAnimation bool // if false, don't change the animation
|
|
|
|
// Physical parameters
|
|
worldPosition vector.Vec2F // where is the center of this object
|
|
velocity vector.Vec2F // movement direction to be applied each tick
|
|
speed float64 // movement magnitude to multiply with the velocity
|
|
}
|
|
|
|
func NewPenguin(renderer *sdl.Renderer) penguin {
|
|
position := vector.Vec2F{}
|
|
velocity := vector.Vec2F{}
|
|
speed := 1.0
|
|
|
|
return penguin{
|
|
currentAnimation: animation.PenguinAnimations[animation.PENGUIN_DEFAULT],
|
|
animationStep: 0,
|
|
direction: DIR_RIGHT,
|
|
|
|
worldPosition: position,
|
|
speed: speed,
|
|
velocity: velocity,
|
|
}
|
|
}
|
|
|
|
func (p *penguin) Draw() error {
|
|
step := p.animationStep / p.currentAnimation.GetSpeed()
|
|
|
|
// TODO
|
|
//windowPosition := worldPosToWindowPos()
|
|
windowPosition := sdl.Point{
|
|
X: int32(math.Round(p.worldPosition.X)),
|
|
Y: int32(math.Round(-1 * p.worldPosition.Y)),
|
|
}
|
|
|
|
err := p.currentAnimation.Draw(step, windowPosition)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
p.animationStep = 1 + p.animationStep
|
|
|
|
return nil
|
|
}
|
|
|
|
func (p *penguin) SetPosition(vec vector.Vec2F) {
|
|
p.worldPosition = vec
|
|
}
|
|
|
|
func (p *penguin) SetDirection(dir int) {
|
|
p.direction = dir
|
|
}
|
|
|
|
func (p *penguin) SetAnimation(id int) {
|
|
a, exists := animation.PenguinAnimations[id]
|
|
|
|
if !exists {
|
|
log.Printf("animation does not exist: %v", id)
|
|
a = animation.PenguinAnimations[animation.PENGUIN_DEFAULT]
|
|
}
|
|
|
|
if a != p.currentAnimation {
|
|
p.animationStep = 0
|
|
}
|
|
|
|
p.currentAnimation = a
|
|
}
|
|
|
|
func (p *penguin) MoveX(x float64) {
|
|
p.mx.Lock()
|
|
defer p.mx.Unlock()
|
|
|
|
p.velocity.X = x
|
|
p.velocity = p.velocity.Normalized()
|
|
p.updateAnimation = true
|
|
}
|
|
|
|
func (p *penguin) MoveY(y float64) {
|
|
p.mx.Lock()
|
|
defer p.mx.Unlock()
|
|
|
|
// (0,0) is the top left, so negative y moves up
|
|
p.velocity.Y = y
|
|
p.velocity.Normalized()
|
|
p.updateAnimation = true
|
|
}
|
|
|
|
func (p *penguin) SetSpeed(s float64) {
|
|
p.speed = s
|
|
}
|
|
|
|
func (p *penguin) GetSpeed() float64 {
|
|
return p.speed
|
|
}
|
|
|
|
func (p *penguin) SetMoveAnimation() {
|
|
if p.velocity.Zero() {
|
|
// Stay facing whatever direction we were facing
|
|
switch p.direction {
|
|
case DIR_LEFT:
|
|
p.SetAnimation(animation.PENGUIN_STATIONARY_LEFT)
|
|
case DIR_RIGHT:
|
|
p.SetAnimation(animation.PENGUIN_STATIONARY_RIGHT)
|
|
case DIR_UP:
|
|
p.SetAnimation(animation.PENGUIN_STATIONARY_UP)
|
|
case DIR_DOWN:
|
|
p.SetAnimation(animation.PENGUIN_STATIONARY_DOWN)
|
|
default:
|
|
log.Printf("unknown direction: %v", p.direction)
|
|
p.SetAnimation(animation.PENGUIN_DEFAULT)
|
|
}
|
|
|
|
} else {
|
|
// Figure out which way we are facing now that we're moving
|
|
p.direction = determineClosestDirection(p.velocity)
|
|
|
|
switch p.direction {
|
|
case DIR_LEFT:
|
|
p.SetAnimation(animation.PENGUIN_WALK_LEFT)
|
|
case DIR_RIGHT:
|
|
p.SetAnimation(animation.PENGUIN_WALK_RIGHT)
|
|
case DIR_UP:
|
|
p.SetAnimation(animation.PENGUIN_WALK_UP)
|
|
case DIR_DOWN:
|
|
p.SetAnimation(animation.PENGUIN_WALK_DOWN)
|
|
default:
|
|
log.Printf("unknown direction: %v", p.direction)
|
|
p.SetAnimation(animation.PENGUIN_DEFAULT)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (p *penguin) Update() error {
|
|
if p.updateAnimation {
|
|
p.SetMoveAnimation()
|
|
p.updateAnimation = false
|
|
}
|
|
|
|
x := p.velocity.X * p.speed
|
|
y := p.velocity.Y * p.speed
|
|
|
|
p.worldPosition.X += x
|
|
p.worldPosition.Y += y
|
|
|
|
return nil
|
|
}
|