From ca826dfbc5d0e39e2fc96a4fb13565d82ad81cf5 Mon Sep 17 00:00:00 2001
From: davidovski <david@davidovski.xyz>
Date: Mon, 22 Apr 2024 14:00:11 +0100
Subject: add ambient music

---
 assets/ambient.wav | Bin 0 -> 40219224 bytes
 level.go           |  32 ++++++++++++++++----------------
 main.go            |  27 ++++++++++++++++++++++++---
 3 files changed, 40 insertions(+), 19 deletions(-)
 create mode 100644 assets/ambient.wav

diff --git a/assets/ambient.wav b/assets/ambient.wav
new file mode 100644
index 0000000..3d1ced9
Binary files /dev/null and b/assets/ambient.wav differ
diff --git a/level.go b/level.go
index eee46fb..3e7ca2a 100644
--- a/level.go
+++ b/level.go
@@ -102,22 +102,22 @@ func StartLevel4(g *Game) {
     g.ClearAll()
     tilemap := NewTilemap([][]int{
             {
-                5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
-                5, 5, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 5, 5,
-                5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 5,
-                5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 5,
-                5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 5,
-                5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 5,
-                5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 5,
-                5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 5,
-                5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 5, 5,
-                5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
-                5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
-                5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
-                5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
-                5, 5, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
-                5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
-                5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
+                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 0,
+                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 5, 5, 5, 5, 5, 5, 5, 5, 6, 0,
+                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 5, 5, 5, 5, 5, 5, 5, 5, 6, 0,
+                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 5, 5, 5, 5, 5, 5, 5, 5, 6, 0,
+                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 5, 5, 5, 5, 5, 5, 5, 5, 6, 0,
+                0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 0,
+                0, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 0,
+                0, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 0,
 			},
 		}, 25)
 
diff --git a/main.go b/main.go
index 8c32678..500d82a 100644
--- a/main.go
+++ b/main.go
@@ -33,6 +33,7 @@ const (
     exitTransitionWeight = 0.9
     ghostAlpha = 0.5
     hightlightBorder = 2
+    audioFadeIn = 0.999
 
     sampleRate = 44100
 )
@@ -57,6 +58,9 @@ var (
 
 	//go:embed assets/start.wav
 	startWav_src []byte
+
+	//go:embed assets/ambient.wav
+	ambientWav_src []byte
 )
 
 var (
@@ -87,6 +91,7 @@ type AudioPlayer struct {
     rewindAudio  *audio.Player
     stopAudio  *audio.Player
     startAudio  *audio.Player
+    ambientAudio  *audio.Player
 }
 
 type Game struct {
@@ -209,6 +214,7 @@ func (g *Game) Init() {
 
     g.ResetAll()
     StartLevel1(g)
+    g.audioPlayer.ambientAudio.SetVolume(0)
 
 
 	ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
@@ -222,6 +228,16 @@ func (g *Game) Init() {
 func (g *Game) Update() error {
     g.time += 1
 
+
+    if g.state == IN_GAME || g.state == PLACING {
+        if ! g.audioPlayer.ambientAudio.IsPlaying() {
+            g.audioPlayer.ambientAudio.Play()
+        }
+        volume := g.audioPlayer.ambientAudio.Volume()
+        volume = 1-((1-volume)*audioFadeIn)
+        g.audioPlayer.ambientAudio.SetVolume(volume)
+    }
+
     if g.state == IN_GAME {
         if inpututil.IsKeyJustPressed(ebiten.KeyR) {
             g.SetReversing()
@@ -443,7 +459,7 @@ func (g *Game) EndLevel() {
     if g.state == IN_GAME {
         g.state = END
         g.TransitionState()
-    } 
+    }
     if g.state == PLACING {
         g.ResetPlayerAi()
     }
@@ -473,6 +489,7 @@ func (g *Game) SetReversing() {
     g.player.alpha = 1.0
     g.audioPlayer.rewindAudio.Rewind()
     g.audioPlayer.rewindAudio.Play()
+    g.audioPlayer.ambientAudio.Pause()
 }
 
 func (g *Game) StopRewinding() {
@@ -481,6 +498,7 @@ func (g *Game) StopRewinding() {
         g.audioPlayer.rewindAudio.Pause()
         g.audioPlayer.startAudio.Rewind()
         g.audioPlayer.startAudio.Play()
+        g.audioPlayer.ambientAudio.Play()
     }
 
 }
@@ -520,6 +538,11 @@ func (g *Game) LoadAudio() {
     g.audioPlayer.rewindAudio = loadAudio(rewindWav_src, g.audioPlayer.audioContext)
     g.audioPlayer.stopAudio = loadAudio(stopWav_src, g.audioPlayer.audioContext)
     g.audioPlayer.startAudio = loadAudio(startWav_src, g.audioPlayer.audioContext)
+    g.audioPlayer.ambientAudio = loadAudio(ambientWav_src, g.audioPlayer.audioContext)
+
+    if g.audioPlayer.ambientAudio == nil {
+        fmt.Printf("AUDIO NUL")
+    }
 }
 
 func (g *Game) LoadImages() {
@@ -537,13 +560,11 @@ func (g *Game) LoadImages() {
 	}
 
     tilesImage = ebiten.NewImageFromImage(img)
-
 }
 
 func main() {
     LoadShaders()
 
-
 	ebiten.SetWindowTitle("Hello, World!")
     game := &Game{}
     game.LoadAudio()
-- 
cgit v1.2.1