summaryrefslogtreecommitdiff
path: root/objects.go
diff options
context:
space:
mode:
authordavidovski <david@davidovski.xyz>2024-04-23 11:50:04 +0100
committerdavidovski <david@davidovski.xyz>2024-04-23 11:50:04 +0100
commit01d37cd3369d7b17bb42fd4e26ecdd4732d75baa (patch)
treede57d098ee84123a7d79e19161533101cfc8e2e2 /objects.go
parent40b26267b62ce9ae6673ca3ca0fed53c5e2e24f5 (diff)
add landing and jumping noises
Diffstat (limited to 'objects.go')
-rw-r--r--objects.go31
1 files changed, 24 insertions, 7 deletions
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
}
}