Grid/Base/GridEditorState.cs
using Sandbox;
public sealed class GridEditorState : Component
{
[Sync] public string CurrentGrid { get; private set; }
protected override void OnStart()
{
if (Networking.IsHost)
{
CurrentGrid =
"#####\n" +
"#.@.#\n" +
"#...#\n" +
"#####";
}
}
[Rpc.Host]
public void SetGrid(string newGrid)
{
CurrentGrid = newGrid;
}
[Rpc.Host]
public void SetCell(int x, int y, char value)
{
var map = new GridMap(CurrentGrid);
map.SetCell(x, y, value);
CurrentGrid = Serialize(map);
}
private string Serialize(GridMap map)
{
var lines = new string[map.Height];
for (int y = 0; y < map.Height; y++)
{
char[] row = new char[map.Width];
for (int x = 0; x < map.Width; x++)
row[x] = map.Cells[x, y];
lines[y] = new string(row);
}
return string.Join("\n", lines);
}
}