oh yeah
This commit is contained in:
parent
75196dde75
commit
adc8fc0514
2 changed files with 55 additions and 0 deletions
|
@ -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
10
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{},
|
||||
|
|
Loading…
Reference in a new issue