Skip to content

Commit

Permalink
add a typing sound effect
Browse files Browse the repository at this point in the history
  • Loading branch information
erinnmclaughlin committed Nov 24, 2023
1 parent 7596bda commit 71b0a13
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 16 deletions.
62 changes: 46 additions & 16 deletions PersonalWebsite/Components/TerminalCommands.razor
Original file line number Diff line number Diff line change
@@ -1,16 +1,34 @@
@for (int i = 0; i <= ExecutedCommandCount - 1; i++)
@if (CurrentState is State.Waiting)
{
<TerminalCommand Command="@Commands[i]" OnAnimationComplete="AnimateNextCommand" />
<button id="start" type="button" @onclick="Start">> <TerminalCursor /></button>
}

@if (!IsAnimating && ExecutedCommandCount != Commands.Length)
else
{
<button id="start" type="button" @onclick="Start">> <TerminalCursor /></button>
for (int i = 0; i <= ExecutedCommandCount - 1; i++)
{
<TerminalCommand Command="@Commands[i]" OnAnimationComplete="OnCommandCompleted" />
}

@if (ShowCursor)
{
<div>> <TerminalCursor /></div>
}

@if (IsTyping)
{
<audio autoplay loop>
<source src="sfx/keyboard-typing.mp3" type="audio/mpeg" />
</audio>
}
}

@code {

private bool IsAnimating { get; set; } = false;
private State CurrentState { get; set; } = State.Waiting;

private bool IsTyping { get; set; }
private bool ShowCursor => HasNext() && !IsTyping;

private int ExecutedCommandCount { get; set; } = 0;

[Parameter]
Expand All @@ -19,26 +37,38 @@
[Parameter]
public EventCallback OnComplete { get; set; }

private void Start()
private async Task OnCommandCompleted()
{
IsAnimating = true;
ExecutedCommandCount++;
}

private async Task AnimateNextCommand()
{
IsAnimating = false;
IsTyping = false;
StateHasChanged();

await Task.Delay(600);

if (ExecutedCommandCount == Commands.Length)
if (!HasNext())
{
await OnComplete.InvokeAsync();
return;
}

IsAnimating = true;
EnterNextCommand();
}

private bool HasNext()
{
return ExecutedCommandCount < Commands.Length;
}

private void EnterNextCommand()
{
IsTyping = true;
ExecutedCommandCount++;
}

private void Start()
{
CurrentState = State.Running;
EnterNextCommand();
}

private enum State { Waiting, Running, Completed }
}
Binary file added PersonalWebsite/wwwroot/sfx/keyboard-typing.mp3
Binary file not shown.

0 comments on commit 71b0a13

Please sign in to comment.