https://github.com/benpollarduk/netaf.blazor
A Blazor implementation of NetAF.
https://github.com/benpollarduk/netaf.blazor
blazor csharp example framework netaf text-adventure tutorial
Last synced: about 2 months ago
JSON representation
A Blazor implementation of NetAF.
- Host: GitHub
- URL: https://github.com/benpollarduk/netaf.blazor
- Owner: benpollarduk
- License: mit
- Created: 2025-01-31T16:00:49.000Z (3 months ago)
- Default Branch: main
- Last Pushed: 2025-03-09T19:22:03.000Z (about 2 months ago)
- Last Synced: 2025-03-09T20:24:33.492Z (about 2 months ago)
- Topics: blazor, csharp, example, framework, netaf, text-adventure, tutorial
- Language: HTML
- Homepage:
- Size: 128 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# NetAF.Blazor
A Blazor implementation of [NetAF](http://www.github.com/benpollarduk/netaf). This repo demonstrates how NetAF can be used in a Blazor app by making use of the various classes in NetAF.Targets.Html.## Getting Started
### Running the Example
Getting started running NetAF in Blazor is easy!* Clone the repo.
* Build and run NetAF.Blazor.
* The app will be served at https://localhost:7295/. Navigate to https://localhost:7295/ in your broswer to start the game.
### How it Works
The Blazor app provides a single [page](NetAF.Blazor/Components/Pages/Home.razor) for rendering and interacting with the game. NetAF provides frame builders to generate the HTML for your game, which is then displayed directly in the page using:
```
@((MarkupString)frameAsHtml)
```
The page itself is an implementation of *IFramePresenter*, when an update occurs in the game the *Present* method is called, which allows the page to receive the generated HTML content.Besides the generated content the page displays a simple acknowledge button and a input box that allow the user to interact with the game. Based on the games current mode these elements are either visible or hidden.
The game itself is executed as a background task.
```
@page "/"
@using NetAF.Logic.Modes
@using NetAF.Rendering.FrameBuilders
@implements IFramePresenter
@rendermode @(new InteractiveServerRenderMode(prerender:false))NetAF
@((MarkupString)frameAsHtml)
@if (showInput)
{
}@if (showAcknowledge)
{
OK
}@code {
private HtmlAdapter? htmlAdapter;
private string frameAsHtml = string.Empty;
private string text = string.Empty;
private bool showInput = false;
private bool showAcknowledge = false;
private ElementReference acknowledgeButton;
private ElementReference textInput;private void Acknowledge()
{
GameExecutor.Update();
}private void HandleInput(KeyboardEventArgs e)
{
if (e.Key == "Enter" && !string.IsNullOrWhiteSpace(text))
{
GameExecutor.Update(text);
text = string.Empty;
}
}protected override void OnInitialized()
{
if (GameExecutor.IsExecuting)
return;htmlAdapter = new HtmlAdapter(this);
GameConfiguration configuration = new(htmlAdapter, FrameBuilderCollections.Html, new(80, 50));
GameExecutor.Execute(ExampleGame.Create(configuration));
}public async void Present(string frame)
{
frameAsHtml = frame;
showInput = htmlAdapter?.Game?.Mode?.Type == GameModeType.Interactive;
showAcknowledge = htmlAdapter?.Game?.Mode?.Type == GameModeType.Information;await InvokeAsync(StateHasChanged);
if (showInput && textInput.Context != null)
await textInput.FocusAsync();if (showAcknowledge && acknowledgeButton.Context != null)
await acknowledgeButton.FocusAsync();
}
}
```
### Example Game
The [ExampleGame](NetAF.Blazor/ExampleGame.cs) is included in the repo.## Documentation
Please visit [https://benpollarduk.github.io/NetAF-docs/](https://benpollarduk.github.io/NetAF-docs/) to view the NetAF documentation.## For Open Questions
Visit https://github.com/benpollarduk/NetAF.Blazor/issues