From adc8fc0514027563237c2b39b5bbeb32851bbcf7 Mon Sep 17 00:00:00 2001 From: Josh Deprez Date: Tue, 3 Aug 2021 18:26:14 +1000 Subject: [PATCH] oh yeah --- engine/actor.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ main.go | 10 ++++++++++ 2 files changed, 55 insertions(+) diff --git a/engine/actor.go b/engine/actor.go index f0e0ce0..960b92f 100644 --- a/engine/actor.go +++ b/engine/actor.go @@ -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 { diff --git a/main.go b/main.go index 16fc880..0a9d01e 100644 --- a/main.go +++ b/main.go @@ -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{},