80 lines
1.5 KiB
Go
80 lines
1.5 KiB
Go
package entity
|
|
|
|
import (
|
|
"gitea.wisellama.rocks/Project-Ely/project-ely/internal/game/sprite"
|
|
"github.com/veandco/go-sdl2/sdl"
|
|
)
|
|
|
|
type Sprite interface {
|
|
Draw(section *sdl.Rect, placement *sdl.Rect) error
|
|
}
|
|
|
|
type penguin struct {
|
|
renderer *sdl.Renderer
|
|
position *sdl.Point
|
|
sprite Sprite
|
|
}
|
|
|
|
func NewPenguin(renderer *sdl.Renderer) *penguin {
|
|
sprite := sprite.GetSprite("assets/images/penguin.png")
|
|
|
|
pos := sdl.Point{X: 0, Y: 0}
|
|
|
|
p := penguin{
|
|
renderer: renderer,
|
|
sprite: sprite,
|
|
position: &pos,
|
|
}
|
|
|
|
return &p
|
|
}
|
|
|
|
func (p *penguin) Draw() error {
|
|
// This is where on the screen to plop that sprite sheet chunk
|
|
placement := &sdl.Rect{X: p.position.X, Y: p.position.Y, W: 200, H: 200}
|
|
|
|
// Select the section of the spritesheet to draw
|
|
// TODO this will depend on animation cycle
|
|
section := &sdl.Rect{X: 0, Y: 0, W: 100, H: 100}
|
|
|
|
err := p.sprite.Draw(section, placement)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (p *penguin) SdlDrawNoTexture() error {
|
|
sdl.Do(func() {
|
|
p.renderer.SetDrawColor(0, 0, 255, 255)
|
|
rect := &sdl.Rect{X: p.position.X, Y: p.position.Y, W: 200, H: 200}
|
|
p.renderer.DrawRect(rect)
|
|
p.renderer.FillRect(rect)
|
|
})
|
|
|
|
return nil
|
|
}
|
|
|
|
func (p *penguin) MoveTo(point *sdl.Point) {
|
|
p.position = point
|
|
}
|
|
|
|
func (p *penguin) MoveRight() {
|
|
p.position.X += 1
|
|
}
|
|
|
|
func (p *penguin) MoveLeft() {
|
|
p.position.X -= 1
|
|
}
|
|
|
|
func (p *penguin) MoveUp() {
|
|
// (0,0) is the top left, so negative y moves up
|
|
p.position.Y -= 1
|
|
}
|
|
|
|
func (p *penguin) MoveDown() {
|
|
// positive y moves down
|
|
p.position.Y += 1
|
|
}
|