This commit is contained in:
Josh Deprez 2021-08-04 17:19:17 +10:00 committed by Josh Deprez
parent 0fce733d56
commit 4c8e8bf4a7
4 changed files with 45 additions and 13 deletions

BIN
assets/aw.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 388 B

View file

@ -6,8 +6,6 @@ import (
"math" "math"
) )
const gravity = 0.5
func init() { func init() {
gob.Register(Actor{}) gob.Register(Actor{})
} }

View file

@ -7,16 +7,20 @@ import (
"github.com/hajimehoshi/ebiten/v2/inpututil" "github.com/hajimehoshi/ebiten/v2/inpututil"
) )
const dampen = 0.5
const gravity = 0.2
// Sprite combines an Actor with the ability to Draw... // Sprite combines an Actor with the ability to Draw...
type Sprite struct { type Sprite struct {
Actor Actor
AnimRef *Anim // TODO: better
Hidden bool Hidden bool
ID ID
Src ImageRef Src ImageRef
ZPos ZPos
vx, vy float64 // TODO: refactor vx, vy float64 // TODO: refactor
animIdle, animWalkLeft, animWalkRight *Anim
} }
func (s *Sprite) Draw(screen *ebiten.Image, geom ebiten.GeoM) { func (s *Sprite) Draw(screen *ebiten.Image, geom ebiten.GeoM) {
@ -27,7 +31,7 @@ func (s *Sprite) Draw(screen *ebiten.Image, geom ebiten.GeoM) {
op.GeoM.Translate(float64(s.Actor.Position.X), float64(s.Actor.Position.Y)) op.GeoM.Translate(float64(s.Actor.Position.X), float64(s.Actor.Position.Y))
op.GeoM.Concat(geom) op.GeoM.Concat(geom)
frame := s.Anim().CurrentFrame() frame := s.Anim.CurrentFrame()
src := s.Src.Image() src := s.Src.Image()
w, _ := src.Size() w, _ := src.Size()
sx, sy := (frame * s.Actor.Size.X) % w, ((frame * s.Actor.Size.X) / w) * s.Actor.Size.Y sx, sy := (frame * s.Actor.Size.X) % w, ((frame * s.Actor.Size.X) / w) * s.Actor.Size.Y
@ -46,7 +50,7 @@ func (s *Sprite) Update() error {
s.vy = 0 s.vy = 0
if inpututil.IsKeyJustPressed(ebiten.KeySpace) { if inpututil.IsKeyJustPressed(ebiten.KeySpace) {
// Jump? // Jump?
s.vy = -7 s.vy = -5
} }
} else { } else {
// Falling // Falling
@ -54,13 +58,23 @@ func (s *Sprite) Update() error {
} }
switch { switch {
case ebiten.IsKeyPressed(ebiten.KeyLeft): case ebiten.IsKeyPressed(ebiten.KeyLeft):
s.vx = -3 s.vx = -2
s.Anim = s.animWalkLeft
case ebiten.IsKeyPressed(ebiten.KeyRight): case ebiten.IsKeyPressed(ebiten.KeyRight):
s.vx = 3 s.vx = 2
s.Anim = s.animWalkRight
default: default:
s.vx = 0 s.vx = 0
s.Anim = s.animIdle
} }
s.Actor.MoveX(s.vx, func() { s.vx = 0 }) s.Actor.MoveX(s.vx, func() { s.vx = -s.vx * dampen })
s.Actor.MoveY(s.vy, func() { s.vy = 0 }) s.Actor.MoveY(s.vy, func() { s.vy = -s.vy * dampen })
return s.Anim().Update() return s.Anim.Update()
}
func (s *Sprite) Build(g *Game) {
// TODO: better than this
s.animWalkLeft = &Anim{Def: AnimDefs["aw_walk_left"]}
s.animWalkRight = &Anim{Def: AnimDefs["aw_walk_right"]}
s.animIdle = &Anim{Def: AnimDefs["aw_idle"]}
} }

26
main.go
View file

@ -36,6 +36,27 @@ func main() {
{Frame: 6, Duration: 12}, {Frame: 6, Duration: 12},
}, },
}, },
"aw_idle": {
Frames: []engine.AnimFrame{
{Frame: 0, Duration: 60},
},
},
"aw_walk_right": {
Frames: []engine.AnimFrame{
{Frame: 1, Duration: 6},
{Frame: 2, Duration: 6},
{Frame: 3, Duration: 6},
{Frame: 4, Duration: 6},
},
},
"aw_walk_left": {
Frames: []engine.AnimFrame{
{Frame: 5, Duration: 6},
{Frame: 6, Duration: 6},
{Frame: 7, Duration: 6},
{Frame: 8, Duration: 6},
},
},
} }
tiles := [][]engine.Tile{ tiles := [][]engine.Tile{
@ -86,10 +107,9 @@ func main() {
ID: "protagonist", ID: "protagonist",
Actor: engine.Actor{ Actor: engine.Actor{
Position: image.Pt(100, 100), Position: image.Pt(100, 100),
Size: image.Pt(16, 16), Size: image.Pt(8, 16),
}, },
AnimRef: engine.AnimRef{Key: "green_tiles"}, Src: engine.ImageRef{Path: "assets/aw.png"},
Src: engine.ImageRef{Path: "assets/boxes.png"},
ZPos: 1, ZPos: 1,
}, },
}, },