From 28d9570e1e7394c56ac0123aa809af3a169977b3 Mon Sep 17 00:00:00 2001 From: Josh Deprez Date: Wed, 4 Aug 2021 15:07:57 +1000 Subject: [PATCH] sprite --- engine/actor.go | 57 ++++--------------------------------------------- engine/tiles.go | 1 + main.go | 10 ++++++--- 3 files changed, 12 insertions(+), 56 deletions(-) diff --git a/engine/actor.go b/engine/actor.go index 6dbbab9..b95c4fd 100644 --- a/engine/actor.go +++ b/engine/actor.go @@ -3,11 +3,7 @@ package engine import ( "encoding/gob" "image" - "image/color" "math" - - "github.com/hajimehoshi/ebiten/v2" - "github.com/hajimehoshi/ebiten/v2/inpututil" ) const gravity = 0.5 @@ -24,19 +20,16 @@ type Collider interface { CollidesWith(image.Rectangle) bool } +// Actor handles basic movement. type Actor struct { - ID Position image.Point Size image.Point - ZPos // TODO: refactor game *Game xRem, yRem float64 - src *ebiten.Image // TODO: refactor - vx, vy float64 // TODO: refactor } -func (a *Actor) collidesAt(p image.Point) bool { +func (a *Actor) CollidesAt(p image.Point) bool { // TODO: more efficient test? hit := false Walk(a.game, func(c interface{}) bool { @@ -51,44 +44,6 @@ func (a *Actor) collidesAt(p image.Point) bool { return hit } -func (a *Actor) Draw(screen *ebiten.Image, geom ebiten.GeoM) { - // TODO: delegate drawing to something else - var op ebiten.DrawImageOptions - op.GeoM.Translate(float64(a.Position.X), float64(a.Position.Y)) - op.GeoM.Concat(geom) - screen.DrawImage(a.src, &op) -} - -func (a *Actor) Update() error { - // TODO: delegate updating to something else - if a.collidesAt(a.Position.Add(image.Pt(0, 1))) { - // Not falling - a.vy = 0 - if inpututil.IsKeyJustPressed(ebiten.KeySpace) { - // Jump? - a.vy = -7 - } - } else { - // Falling - a.vy += gravity - } - switch { - case ebiten.IsKeyPressed(ebiten.KeyLeft): - a.vx = -3 - case ebiten.IsKeyPressed(ebiten.KeyRight): - a.vx = 3 - default: - a.vx = 0 - } - a.MoveX(a.vx, func() { - a.vx = 0 - }) - a.MoveY(a.vy, func() { - a.vy = 0 - }) - return nil -} - func (a *Actor) MoveX(dx float64, onCollide func()) { a.xRem += dx move := int(math.Round(a.xRem)) @@ -98,7 +53,7 @@ func (a *Actor) MoveX(dx float64, onCollide func()) { a.xRem -= float64(move) sign := sign(move) for move != 0 { - if a.collidesAt(a.Position.Add(image.Pt(sign, 0))) { + if a.CollidesAt(a.Position.Add(image.Pt(sign, 0))) { if onCollide != nil { onCollide() } @@ -118,7 +73,7 @@ func (a *Actor) MoveY(dy float64, onCollide func()) { a.yRem -= float64(move) sign := sign(move) for move != 0 { - if a.collidesAt(a.Position.Add(image.Pt(0, sign))) { + if a.CollidesAt(a.Position.Add(image.Pt(0, sign))) { if onCollide != nil { onCollide() } @@ -131,10 +86,6 @@ func (a *Actor) MoveY(dy float64, onCollide func()) { func (a *Actor) Build(g *Game) { a.game = g - - // TODO: remove hack temporary image - a.src = ebiten.NewImage(16, 16) - a.src.Fill(color.White) } func sign(m int) int { diff --git a/engine/tiles.go b/engine/tiles.go index e9fbc94..2d37e56 100644 --- a/engine/tiles.go +++ b/engine/tiles.go @@ -31,6 +31,7 @@ func (t *Tilemap) CollidesWith(r image.Rectangle) bool { if t.Ersatz { return false } + // TODO: optimise? for j, row := range t.Map { for i, tile := range row { if tile == nil { diff --git a/main.go b/main.go index 03ff234..b9f5e1f 100644 --- a/main.go +++ b/main.go @@ -82,10 +82,14 @@ func main() { ID: "ground", Rect: image.Rect(0, 192, 320, 240), },*/ - &engine.Actor{ + &engine.Sprite{ ID: "protagonist", - Position: image.Pt(100, 100), - Size: image.Pt(16, 16), + Actor: engine.Actor{ + Position: image.Pt(100, 100), + Size: image.Pt(16, 16), + }, + AnimRef: engine.AnimRef{Key: "green_tiles"}, + Src: engine.ImageRef{Path: "assets/boxes.png"}, ZPos: 1, }, },