From c1b9a2cb21df181b140a449223e3bf650ab1ec68 Mon Sep 17 00:00:00 2001 From: davidovski Date: Mon, 22 Apr 2024 08:27:52 +0100 Subject: create main gameplay loop --- objects.go | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) (limited to 'objects.go') diff --git a/objects.go b/objects.go index ce7ed22..0c24beb 100644 --- a/objects.go +++ b/objects.go @@ -134,6 +134,20 @@ func (object * GameObject) Collide(other *GameObject) bool { return ! ( minX2 >= maxX1 || maxX2 <= minX1 || minY2 >= maxY1 || maxY2 <= minY1) } +func (object * GameObject) Jump() { + if object.onGround { + object.vy += -jumpHeight + } +} + +func (object * GameObject) MoveLeft() { + object.vx = -playerSpeed +} +func (object * GameObject) MoveRight() { + object.vx = playerSpeed +} + + func (object * GameObject) ToRect() image.Rectangle { width := object.image.Bounds().Dx() height := object.image.Bounds().Dy() @@ -177,6 +191,10 @@ func NewExit(game *Game, x, y float32) *GameObject{ exit := NewObject(game, x, y) exit.image = tilesImage.SubImage(image.Rect(0, 16, 32, 48)).(*ebiten.Image) + exit.onCollideUp = OnCollideExit + exit.onCollideDown = OnCollideExit + exit.onCollideLeft = OnCollideExit + exit.onCollideRight = OnCollideExit return exit } @@ -218,6 +236,13 @@ func NewLeftSpike(game *Game, x, y float32) *GameObject{ return spike } +func OnCollideExit(this, other *GameObject) bool { + if other == this.game.player { + this.game.EndLevel() + } + return false +} + func OnCollideSpring(this, other *GameObject) bool { other.vy -= SPRING_FORCE other.onGround = true @@ -226,7 +251,7 @@ func OnCollideSpring(this, other *GameObject) bool { func OnCollideSpike(this, other *GameObject) bool { if other == this.game.player { - other.game.ResetAll() + other.game.KillPlayer() } return true } -- cgit v1.2.1