Skip to content

Commit e79a9e2

Browse files
committed
NeonSnake: stop music when snake dies or when leaving the page. Add WASM AOT related build parameters.
1 parent 7ed4055 commit e79a9e2

5 files changed

Lines changed: 41 additions & 20 deletions

File tree

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ jobs:
3434
- name: Publish .NET Project
3535
run: dotnet publish BlazorExperiments/BlazorExperiments.UI.csproj -c Release -o release --nologo
3636
- name: Publish .NET Project with AOT
37-
run: dotnet publish BlazorExperiments/BlazorExperiments.UI.csproj -c Release -p:RunAOTCompilation=true -p:WasmStripILAfterAOT=true -o release-aot --nologo
37+
run: dotnet publish BlazorExperiments/BlazorExperiments.UI.csproj -c Release -p:RunAOTCompilation=true -p:WasmStripILAfterAOT=true -p:PublishTrimmed=true -p:TrimMode=full -o release-aot --nologo
3838
- name: check current directory
3939
run: ls -la release-aot/wwwroot && pwd
4040
- name: Fix base path for Blazor AOT app

BlazorExperiments/BlazorExperiments.UI.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
<InvariantGlobalization>true</InvariantGlobalization>
88
<BlazorEnableTimeZoneSupport>false</BlazorEnableTimeZoneSupport>
99
<BlazorWebAssemblyPreserveCollationData>false</BlazorWebAssemblyPreserveCollationData>
10-
<WasmExceptionHandling>false</WasmExceptionHandling>
11-
<WasmSIMD>true</WasmSIMD>
10+
<WasmEnableExceptionHandling>true</WasmEnableExceptionHandling>
11+
<WasmEnableSIMD>true</WasmEnableSIMD>
1212
</PropertyGroup>
1313

1414
<ItemGroup>

BlazorExperiments/Layout/NavMenu.razor

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,14 @@
1414
<span class="bi bi-house-door-fill-nav-menu" aria-hidden="true"></span> Home
1515
</NavLink>
1616
</div>
17+
<div class="nav-item px-3">
18+
<NavLink class="nav-link" href="neon-snake">
19+
<span class="bi bi-list-nested-nav-menu" aria-hidden="true"></span> Neon Snake
20+
</NavLink>
21+
</div>
1722
<div class="nav-item px-3">
1823
<NavLink class="nav-link" href="snake">
19-
<span class="bi bi-list-nested-nav-menu" aria-hidden="true"></span> Snake
24+
<span class="bi bi-list-nested-nav-menu" aria-hidden="true"></span> Old Snake
2025
</NavLink>
2126
</div>
2227
<div class="nav-item px-3">

BlazorExperiments/Pages/NeonSnake.razor.cs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace BlazorExperiments.UI.Pages;
1212

13-
public partial class NeonSnake {
13+
public partial class NeonSnake : IAsyncDisposable {
1414
const int KeyUp = 38;
1515
const int KeyDown = 40;
1616
const int KeyLeft = 37;
@@ -39,6 +39,7 @@ public partial class NeonSnake {
3939
string _scoreText = "Score: 0";
4040
string _bestText = "Best: 0";
4141
bool _hudDirty = true;
42+
bool _loopInProgress = false;
4243
const string BestScoreKey = "neonSnakeBestScore";
4344

4445
public async Task InitializeAsync() {
@@ -72,9 +73,11 @@ public async Task InitializeAsync() {
7273
}
7374

7475
private async ValueTask LoopAsync(ElapsedEventArgs elapsedEvent) {
76+
if (_loopInProgress) return;
77+
78+
_loopInProgress = true;
7579
var now = elapsedEvent.SignalTime;
7680
var dt = (now - _lastTick).TotalMilliseconds;
77-
if (dt < 16.67) return;
7881
_lastTick = now;
7982

8083
var scoreBefore = _snake.Score;
@@ -89,6 +92,7 @@ private async ValueTask LoopAsync(ElapsedEventArgs elapsedEvent) {
8992
if (_snake.Score > scoreBefore) await PlaySoundAsync("neonSnakeAudio.playEatSound");
9093
if (_snake.Health < healthBefore) await PlaySoundAsync("neonSnakeAudio.playHitSound");
9194
if (!deadBefore && _snake.Dead) {
95+
await PlaySoundAsync("neonSnakeAudio.stopMusic");
9296
await PlaySoundAsync("neonSnakeAudio.playDeathSound");
9397
await JS.InvokeVoidAsync("localStorage.setItem", BestScoreKey, _snake.BestScore);
9498
}
@@ -101,6 +105,7 @@ private async ValueTask LoopAsync(ElapsedEventArgs elapsedEvent) {
101105

102106
await using var batch = _canvas.Context.CreateBatch();
103107
await DrawAsync(batch);
108+
_loopInProgress = false;
104109
}
105110

106111
async ValueTask PlaySoundAsync(string fn) {
@@ -622,7 +627,8 @@ async ValueTask DrawMonstersAsync(Batch2D ctx) {
622627
await ctx.ArcAsync(ex, ey, eyeR * 0.52, 0, DoublePI);
623628
await ctx.FillAsync(FillRule.NonZero);
624629
}
625-
} else {
630+
}
631+
else {
626632
// Sleeping: closed-eye lines
627633
await ctx.StrokeStyleAsync("rgba(180,140,255,0.7)");
628634
await ctx.LineWidthAsync(2);
@@ -855,14 +861,19 @@ await ctx.FillStyleAsync(0, NeonSnakeGame.Snake.HudH, 0, _screenH,
855861
await ctx.TextAlignAsync(TextAlign.Start);
856862
}
857863

864+
async ValueTask RestartGame() {
865+
_snake = new NeonSnakeGame.Snake(_cellSize, _canvas.CellsPerRow, _visibleRows);
866+
_hudDirty = true;
867+
_lastTick = DateTime.UtcNow;
868+
_ = PlaySoundAsync("neonSnakeAudio.startMusic");
869+
}
870+
858871
#region Input Handling
859872
void HandleInput(KeyboardEventArgs e) {
860873
if (_snake is null) return;
861874

862875
if (_snake.ShowDeathScreen && (e.Code == "Space" || e.Key == " " || e.Key == "Spacebar")) {
863-
_snake = new NeonSnakeGame.Snake(_cellSize, _canvas.CellsPerRow, _visibleRows);
864-
_hudDirty = true;
865-
_lastTick = DateTime.UtcNow;
876+
_ = RestartGame();
866877
return;
867878
}
868879

@@ -887,9 +898,7 @@ void HandleInput(KeyboardEventArgs e) {
887898

888899
void HandleTouchStart(TouchEventArgs e) {
889900
if (_snake.ShowDeathScreen) {
890-
_snake = new NeonSnakeGame.Snake(_cellSize, _canvas.CellsPerRow, _visibleRows);
891-
_hudDirty = true;
892-
_lastTick = DateTime.UtcNow;
901+
_ = RestartGame();
893902
return;
894903
}
895904

@@ -918,4 +927,8 @@ void HandleTouchMove(TouchEventArgs e) {
918927
_previousTouch = e.Touches[^1];
919928
}
920929
#endregion
930+
931+
public async ValueTask DisposeAsync() {
932+
await PlaySoundAsync("neonSnakeAudio.dispose");
933+
}
921934
}

BlazorExperiments/wwwroot/js/neon-snake-audio.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
window.neonSnakeAudio = (() => {
2-
const AudioCtx = window.AudioContext || window.webkitAudioContext;
32
let audioCtx = null;
43
let masterGain = null;
54

65
const getCtx = () => {
7-
if (!AudioCtx) return null;
8-
96
if (!audioCtx) {
10-
audioCtx = new AudioCtx();
7+
audioCtx = new AudioContext();
118
masterGain = audioCtx.createGain();
129
masterGain.connect(audioCtx.destination);
1310
}
@@ -117,8 +114,6 @@ window.neonSnakeAudio = (() => {
117114
}
118115
};
119116

120-
audioCtx = getCtx();
121-
122117
// ── Monster death: electronic zap burst ─────────────────────────────────
123118
const playMonsterDeathSound = () => {
124119
try {
@@ -191,6 +186,8 @@ window.neonSnakeAudio = (() => {
191186
{ f: 220, v: 0.075 }, // A3 (accent)
192187
];
193188

189+
audioCtx = getCtx();
190+
194191
let musicPlaying = false;
195192
let musicNoteIndex = 0;
196193
let nextNoteTime = 0;
@@ -293,13 +290,19 @@ window.neonSnakeAudio = (() => {
293290
droneOscs = []; droneGains = [];
294291
};
295292

293+
const dispose = () => {
294+
audioCtx && audioCtx.close();
295+
audioCtx = null;
296+
}
297+
296298
return {
297299
playEatSound,
298300
playHitSound,
299301
playDeathSound,
300302
playMonsterDeathSound,
301303
playEggHatchSound,
302304
startMusic,
303-
stopMusic
305+
stopMusic,
306+
dispose
304307
};
305308
})();

0 commit comments

Comments
 (0)