sprite
This commit is contained in:
parent
8b3a0c78e4
commit
28d9570e1e
3 changed files with 12 additions and 56 deletions
|
@ -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 {
|
||||
|
|
|
@ -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 {
|
||||
|
|
10
main.go
10
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,
|
||||
},
|
||||
},
|
||||
|
|
Loading…
Reference in a new issue