This commit is contained in:
Josh Deprez 2021-08-04 15:07:57 +10:00 committed by Josh Deprez
parent 8b3a0c78e4
commit 28d9570e1e
3 changed files with 12 additions and 56 deletions

View file

@ -3,11 +3,7 @@ package engine
import ( import (
"encoding/gob" "encoding/gob"
"image" "image"
"image/color"
"math" "math"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/inpututil"
) )
const gravity = 0.5 const gravity = 0.5
@ -24,19 +20,16 @@ type Collider interface {
CollidesWith(image.Rectangle) bool CollidesWith(image.Rectangle) bool
} }
// Actor handles basic movement.
type Actor struct { type Actor struct {
ID
Position image.Point Position image.Point
Size image.Point Size image.Point
ZPos // TODO: refactor
game *Game game *Game
xRem, yRem float64 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? // TODO: more efficient test?
hit := false hit := false
Walk(a.game, func(c interface{}) bool { Walk(a.game, func(c interface{}) bool {
@ -51,44 +44,6 @@ func (a *Actor) collidesAt(p image.Point) bool {
return hit 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()) { func (a *Actor) MoveX(dx float64, onCollide func()) {
a.xRem += dx a.xRem += dx
move := int(math.Round(a.xRem)) move := int(math.Round(a.xRem))
@ -98,7 +53,7 @@ func (a *Actor) MoveX(dx float64, onCollide func()) {
a.xRem -= float64(move) a.xRem -= float64(move)
sign := sign(move) sign := sign(move)
for move != 0 { 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 { if onCollide != nil {
onCollide() onCollide()
} }
@ -118,7 +73,7 @@ func (a *Actor) MoveY(dy float64, onCollide func()) {
a.yRem -= float64(move) a.yRem -= float64(move)
sign := sign(move) sign := sign(move)
for move != 0 { 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 { if onCollide != nil {
onCollide() onCollide()
} }
@ -131,10 +86,6 @@ func (a *Actor) MoveY(dy float64, onCollide func()) {
func (a *Actor) Build(g *Game) { func (a *Actor) Build(g *Game) {
a.game = g a.game = g
// TODO: remove hack temporary image
a.src = ebiten.NewImage(16, 16)
a.src.Fill(color.White)
} }
func sign(m int) int { func sign(m int) int {

View file

@ -31,6 +31,7 @@ func (t *Tilemap) CollidesWith(r image.Rectangle) bool {
if t.Ersatz { if t.Ersatz {
return false return false
} }
// TODO: optimise?
for j, row := range t.Map { for j, row := range t.Map {
for i, tile := range row { for i, tile := range row {
if tile == nil { if tile == nil {

10
main.go
View file

@ -82,10 +82,14 @@ func main() {
ID: "ground", ID: "ground",
Rect: image.Rect(0, 192, 320, 240), Rect: image.Rect(0, 192, 320, 240),
},*/ },*/
&engine.Actor{ &engine.Sprite{
ID: "protagonist", ID: "protagonist",
Position: image.Pt(100, 100), Actor: engine.Actor{
Size: image.Pt(16, 16), 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, ZPos: 1,
}, },
}, },