shithub: battleship

Download patch

ref: afdfefd352f5b2ce4f3248f3581067e6496d0c38
parent: d79098840557bb4687a2e6640728dd69c62bf3a3
author: rodri <rgl@antares-labs.eu>
date: Mon Oct 2 08:56:47 EDT 2023

add more sfx and do some of their orchestration.

binary files /dev/null b/assets/sfx/cannon.mp3 differ
binary files /dev/null b/assets/sfx/water.mp3 differ
--- a/bts.c
+++ b/bts.c
@@ -155,6 +155,7 @@
 struct {
 	Image *c; /* color */
 	char *s; /* banner text */
+	AudioSource *snd; /* victory or defeat bg sfx */
 } conclusion;
 
 
@@ -242,6 +243,9 @@
 	game.state = Waiting0;
 	conclusion.s = nil;
 	csetcursor(mctl, nil);
+	audio_stop(conclusion.snd);
+	conclusion.snd = nil;
+	audio_play(playlist[SBG0]);
 }
 
 Point
@@ -544,21 +548,33 @@
 void
 initsound(void)
 {
+	struct {
+		char *path;
+		double gain;
+		int loops;
+	} sndtab[NSOUNDS] = {
+	 [SBG0]		{"assets/sfx/bg0.mp3", 1.0, 1},
+	 [SBG1]		{"assets/sfx/bg1.mp3", 1.0, 1},
+	 [SBG2]		{"assets/sfx/bg2.mp3", 1.0, 1},
+	 [SCANNON]	{"assets/sfx/cannon.mp3", 5.0, 0},
+	 [SWATER]	{"assets/sfx/water.mp3", 3.0, 0},
+	 [SVICTORY]	{"assets/sfx/victory.mp3", 1.0, 1},
+	 [SDEFEAT]	{"assets/sfx/defeat.mp3", 1.0, 1},
+	};
+	int i;
+
 	audio_init(44100);
 	audio_set_master_gain(0.5);
 
-	playlist[SBG0] = audio_new_source_from_mp3file("assets/sfx/bg0.mp3");
-	if(playlist[SBG0] == nil)
-		sysfatal("audio_new_source_from_mp3file: %r");
-	playlist[SBG1] = audio_new_source_from_mp3file("assets/sfx/bg1.mp3");
-	if(playlist[SBG1] == nil)
-		sysfatal("audio_new_source_from_mp3file: %r");
-	playlist[SBG2] = audio_new_source_from_mp3file("assets/sfx/bg2.mp3");
-	if(playlist[SBG2] == nil)
-		sysfatal("audio_new_source_from_mp3file: %r");
+	for(i = 0; i < NSOUNDS; i++){
+		playlist[i] = audio_new_source_from_file(sndtab[i].path);
+		if(playlist[i] == nil)
+			sysfatal("audio_new_source_from_file: %r");
+		audio_set_gain(playlist[i], sndtab[i].gain);
+		audio_set_loop(playlist[i], sndtab[i].loops);
+	}
 
 	audio_play(playlist[SBG0]);
-	audio_set_loop(playlist[SBG0], 1);
 }
 
 int
@@ -621,6 +637,7 @@
 		if(!ptinrect(mc->xy, alienboard.bbox))
 			break;
 
+		audio_play(playlist[SCANNON]);
 		cell = toboard(&alienboard, mc->xy);
 		chanprint(egress, "shoot %s\n", cell2coords(cell));
 		lastshot = cell;
@@ -797,6 +814,10 @@
 
 	conclusion.c = pal[PCGreen];
 	conclusion.s = s;
+	conclusion.snd = playlist[SVICTORY];
+
+	audio_stop(playlist[SBG2]);
+	audio_play(conclusion.snd);
 }
 
 void
@@ -806,6 +827,10 @@
 
 	conclusion.c = pal[PCRed];
 	conclusion.s = s;
+	conclusion.snd = playlist[SDEFEAT];
+
+	audio_stop(playlist[SBG2]);
+	audio_play(conclusion.snd);
 }
 
 void
@@ -819,6 +844,10 @@
 	snprint(s, sizeof s, "%s WON", winner);
 	conclusion.c = pal[PCGreen];
 	conclusion.s = s;
+	conclusion.snd = playlist[SVICTORY];
+
+	audio_stop(playlist[SBG2]);
+	audio_play(conclusion.snd);
 }
 
 void
@@ -868,6 +897,8 @@
 			match.bl[0] = &localboard;
 			match.bl[1] = &alienboard;
 			game.state = Watching;
+			audio_stop(playlist[SBG0]);
+			audio_play(playlist[SBG2]);
 		}
 		break;
 	case Ready:
@@ -874,6 +905,8 @@
 		if(ct->index == CMlayout){
 			game.state = Outlaying;
 			curship = &armada[0];
+			audio_stop(playlist[SBG0]);
+			audio_play(playlist[SBG2]);
 		}else if(ct->index == CMoid)
 			snprint(oid, sizeof oid, "%s", cb->f[1]);
 		break;
@@ -914,8 +947,10 @@
 			csetcursor(mctl, &waitcursor);
 		}else if(ct->index == CMwehit)
 			settile(&alienboard, lastshot, Thit);
-		else if(ct->index == CMwemiss)
+		else if(ct->index == CMwemiss){
+			audio_play(playlist[SWATER]);
 			settile(&alienboard, lastshot, Tmiss);
+		}
 		break;
 	case Waiting:
 		if(ct->index == CMplay){
--- a/credits
+++ b/credits
@@ -6,6 +6,10 @@
 	“Creepy Ambient (107 bpm).wav”, by SoundFlakes, 2017
 assets/sfx/bg2.mp3
 	“Space Emergency.mp3”, by SoundFlakes, 2019
+assets/sfx/cannon.mp3
+	“Ship artillery blast classic.wav”, by CGEffex, 2010
+assets/sfx/water.mp3
+	“Water Explosion”, by Sheyvan, 2020
 assets/sfx/victory.mp3
 	“Epic Action Theme”, by BloodPixelHero, 2022
 assets/sfx/defeat.mp3
--- a/dat.h
+++ b/dat.h
@@ -40,6 +40,10 @@
 	SBG0 = 0,
 	SBG1,
 	SBG2,
+	SCANNON,
+	SWATER,
+	SVICTORY,
+	SDEFEAT,
 	NSOUNDS,
 
 	KB = 1024,