From 01d37cd3369d7b17bb42fd4e26ecdd4732d75baa Mon Sep 17 00:00:00 2001 From: davidovski Date: Tue, 23 Apr 2024 11:50:04 +0100 Subject: add landing and jumping noises --- objects.go | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) (limited to 'objects.go') diff --git a/objects.go b/objects.go index 11d52ca..fe3028a 100644 --- a/objects.go +++ b/objects.go @@ -5,6 +5,7 @@ import ( "image" "image/color" + "math/rand/v2" "github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2/vector" ) @@ -138,7 +139,7 @@ func (o * GameObject) Update(tilemap Tilemap, others []*GameObject) { o.x += o.vx - if o.vx > 0 { + if o.vx < 0 { direction = LEFT } else { direction = RIGHT @@ -152,12 +153,15 @@ func (o * GameObject) Update(tilemap Tilemap, others []*GameObject) { o.y += o.vy if o.vy > 0 { - direction = UP - } else { direction = DOWN + } else { + direction = UP } if o.HasCollision(tilemap, others, direction) { + if ! o.onGround && o.vy > gravity*10 { + o.playLand() + } o.onGround = true; o.vx *= o.friction @@ -194,13 +198,13 @@ func (o * GameObject) HasCollision(tilemap Tilemap, others []*GameObject, dir Di if obj.Collide(o) { var f func(this, other *GameObject) bool switch dir { - case UP: - f = obj.onCollideUp case DOWN: + f = obj.onCollideUp + case UP: f = obj.onCollideDown - case LEFT: - f = obj.onCollideLeft case RIGHT: + f = obj.onCollideLeft + case LEFT: f = obj.onCollideRight } @@ -256,8 +260,21 @@ func (object * GameObject) Collide(other *GameObject) bool { return ! ( minX2 >= maxX1 || maxX2 <= minX1 || minY2 >= maxY1 || maxY2 <= minY1) } +func (object *GameObject) playLand() { + jumpid := rand.IntN(2) + object.game.audioPlayer.landAudio[jumpid].Rewind() + object.game.audioPlayer.landAudio[jumpid].Play() +} + +func (object *GameObject) playJump() { + jumpid := rand.IntN(2) + object.game.audioPlayer.jumpAudio[jumpid].Rewind() + object.game.audioPlayer.jumpAudio[jumpid].Play() +} + func (object * GameObject) Jump() { if object.onGround { + object.playJump() object.vy += -jumpHeight } } -- cgit v1.2.1