This commit is contained in:
Josh Deprez 2021-08-03 18:26:14 +10:00 committed by Josh Deprez
parent 75196dde75
commit adc8fc0514
2 changed files with 55 additions and 0 deletions

View file

@ -3,9 +3,14 @@ package engine
import (
"encoding/gob"
"image"
"image/color"
"math"
"github.com/hajimehoshi/ebiten/v2"
)
const gravity = 0.5
func init() {
gob.Register(Actor{})
}
@ -25,6 +30,8 @@ type Actor struct {
game *Game
xRem, yRem float64
src *ebiten.Image // TODO: refactor
vx, vy float64 // TODO: refactor
}
func (a *Actor) collidesAt(p image.Point) bool {
@ -42,6 +49,40 @@ 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 ebiten.IsKeyPressed(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, nil)
a.MoveY(a.vy, nil)
return nil
}
func (a *Actor) MoveX(dx float64, onCollide func()) {
a.xRem += dx
move := int(math.Round(a.xRem))
@ -84,6 +125,10 @@ 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 {

10
main.go
View file

@ -2,6 +2,7 @@ package main
import (
"embed"
"image"
_ "image/png"
"log"
@ -73,6 +74,15 @@ func main() {
Src: engine.ImageRef{Path: "assets/boxes.png"},
TileSize: 16,
},
&engine.SolidRect{
ID: "ground",
Rect: image.Rect(0, 192, 320, 240),
},
&engine.Actor{
ID: "protagonist",
Position: image.Pt(100, 100),
Size: image.Pt(16, 16),
},
},
},
engine.PerfDisplay{},