https://github.com/developerfred/mud-rpg
https://github.com/developerfred/mud-rpg
Last synced: 8 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/developerfred/mud-rpg
- Owner: developerfred
- License: mit
- Created: 2023-05-22T19:39:15.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-08-15T23:26:53.000Z (over 2 years ago)
- Last Synced: 2025-02-17T06:42:18.105Z (11 months ago)
- Language: C#
- Size: 517 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# MUD Template Unity
## Prerequisites
1. git ([download](https://git-scm.com/downloads))
2. foundry (forge, anvil, cast) ([download](https://book.getfoundry.sh/getting-started/installation), make sure to foundryup at least once)
3. node.js (v16+) ([download](https://nodejs.org/en/download))
4. pnpm (after installing node: npm install --global pnpm)
5. Unity ([download](https://unity.com/download))
6. The .NET SDK (7.0) ([download](https://dotnet.microsoft.com/en-us/download))
If you are using Windows:
1. install git bash (gitforwindows.org)
2. install nodejs, including “native modules” (nodejs.org/en/download) (re native modules: just keep the checkmark, it’s enabled by default in the installer)
3. Install foundry via foundryup using Git bash
## Tutorial
The tutorial repo for this template can be found [here](https://github.com/emergenceland/tankmud-tutorial).
## Quickstart
Run the following command in the root:
```
pnpm install
```
Then, open two terminal tabs.
In tab 1:
```
pnpm run dev:node
```
In tab 2:
```
pnpm run dev
```
In Unity, Open the Main scene if you haven’t already `(packages/client/Assets/Scenes/Main.unity)`.
Press play. Click to increment the counter.
## Usage
MUD Template Unity autogenerates C# definitions for your Tables when you run `pnpm dev`. These should appear in `packages/client/Assets/Scripts/codegen/`.
### Getting a value
```csharp
var addressKey = net.addressKey;
// get by key
var currentPlayer = PlayerTable.GetTableValue(addressKey);
// currentPlayer is strongly typed
string name = currentPlayer.name;
```
### Subscribing to updates
```csharp
var playerSubInsert = PlayerTable.OnRecordInsert().ObserveOnMainThread().Subscribe(OnInsertPlayers);
var playerSubUpdate = PlayerTable.OnRecordUpdate().ObserveOnMainThread().Subscribe(OnDeletePlayers);
var playerSubDelete = PlayerTable.OnRecordDelete().ObserveOnMainThread().Subscribe(OnDeletePlayers);
var playerSubDeleteInsert = PlayerTable.OnRecordInsert.Merge(PlayerTable.OnRecordDelete).ObserveOnMainThread().Subscribe(OnInsertDeletePlayers);
private void OnUpdatePlayers(PlayerTableUpdate update)
{
// PlayerTableUpdate has a TypedValue, which is a Tuple of
// Item 1 is the current value, and Item 2 is the previous value.
var currentValue = update.TypedValue.Item1;
if (currentValue == null) return;
var previousValue = update.TypedValue.Item2;
// PlayerTableUpdate inherits from UniMUD's RecordUpdate
// So you can still get the key, table, and untyped values.
var playerPosition = PositionTable.GetTableValue(update.Key);
if (playerPosition == null) return;
var playerSpawnPoint = new Vector3((float)playerPosition.x, 0, (float)playerPosition.y);
var player = Instantiate(playerPrefab, playerSpawnPoint, Quaternion.identity);
if (update.Key == net.addressKey) {
Debug.Log("Is local player");
}
}
```
### Making a transaction
This template generates Nethereum bindings for your World contract, which, we can access to make a transaction.
Note that not all Unity functions can be async, so you may need to wrap your transaction in a coroutine.
```csharp
async void Spawn(NetworkManager nm)
{
var addressKey = net.addressKey;
var currentPlayer = PlayerTable.GetTableValue(addressKey);
if (currentPlayer == null)
{
// spawn the player
await nm.worldSend.TxExecute(0, 0);
}
}
// For example, with the UniTask library:
private async UniTaskVoid SendIncrementTxAsync()
{
try
{
await net.worldSend.TxExecute();
}
catch (Exception ex)
{
// Handle your exception here
Debug.LogException(ex);
}
}
```
### Deploying to a Testnet
You can deploy to any non-local chain with `cd packages/contracts && pnpm run deploy:testnet`.
Be sure to properly set the ChainID and RPC urls in the **NetworkManager** component.
UniMUD currently doesn’t implement the faucet service, so you must manually send testnet funds to your address. Your address is logged in Debug mode, but you can also create a UI component to fetch and display it from the NetworkManager.