From 1f1c464f84a169ea5146d360ae6b1211d8a7074f Mon Sep 17 00:00:00 2001 From: davidovski Date: Sun, 21 Apr 2024 13:47:41 +0100 Subject: Initial Commit --- player.go | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 player.go (limited to 'player.go') diff --git a/player.go b/player.go new file mode 100644 index 0000000..0b313ed --- /dev/null +++ b/player.go @@ -0,0 +1,53 @@ +package main + +import ( + "github.com/hajimehoshi/ebiten/v2" +) + +const ( + gravity = 0.01 + friction = 0.6 +) + +type GameObject struct { + x, y float32 + vx, vy float32 + image *ebiten.Image + onGround bool +} + + +type Player struct { + GameObject +} + +func (o * GameObject) Update(tilemap Tilemap) { + o.vy += gravity + + o.x += o.vx + if tilemap.CollideObject(o) { + o.x -= o.vx + o.vx = 0 + } + + o.y += o.vy + if (tilemap.CollideObject(o)) { + + o.onGround = true; + o.vx *= friction + + + o.y -= o.vy + o.vy = 0 + } else { + o.onGround = false; + } +} + +func (o * GameObject) Draw(screen *ebiten.Image, tilemap Tilemap) { + op := &ebiten.DrawImageOptions{} + op.GeoM.Translate(float64(o.x * float32(tilemap.tileSize)), float64(o.y * float32(tilemap.tileSize))) + screen.DrawImage(o.image, op) +} + + -- cgit v1.2.1