Skip to content

Commit

Permalink
Undo PoC
Browse files Browse the repository at this point in the history
  • Loading branch information
kaczy93 committed Nov 30, 2023
1 parent e7f33a5 commit 8b7dfc7
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 10 deletions.
11 changes: 8 additions & 3 deletions CentrED/UI/Windows/DebugWindow.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using ImGuiNET;
using Microsoft.Xna.Framework;
using static CentrED.Application;
using Vector2 = System.Numerics.Vector2;

namespace CentrED.UI.Windows;
Expand All @@ -22,11 +23,11 @@ public override void Draw()
// _graphicsDevice.PresentationParameters.BackBufferHeight / 2
// ),
// ImGuiCond.FirstUseEver);
var uiManager = Application.CEDGame.UIManager;
var mapManager = Application.CEDGame.MapManager;
var uiManager = CEDGame.UIManager;
var mapManager = CEDGame.MapManager;
ImGui.Begin(Name, ref _show, ImGuiWindowFlags.AlwaysAutoResize | ImGuiWindowFlags.NoResize);
ImGui.Text($"FPS: {uiManager._framesPerSecond:F1}");
foreach (var nameValue in Application.Metrics.Values)
foreach (var nameValue in Metrics.Values)
{
ImGui.Text($"{nameValue.Key}: {nameValue.Value.TotalMilliseconds}ms");
}
Expand All @@ -44,6 +45,10 @@ public override void Draw()
ImGui.Separator();
ImGui.InputInt("Camera x", ref _gotoX);
ImGui.InputInt("Camera y", ref _gotoY);
if (ImGui.Button("Undo"))
{
CEDClient.Undo();
}
if (ImGui.Button("Update pos"))
{
mapManager.Position = new Point(_gotoX, _gotoY);
Expand Down
7 changes: 7 additions & 0 deletions Client/CentrEDClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public sealed class CentrEDClient : BaseCentrED, IDisposable
public AccessLevel AccessLevel { get; internal set; }
public ushort X { get; private set; }
public ushort Y { get; private set; }
public Stack<Packet> UndoStack = new();
public List<String> Clients { get; } = new();
public bool Running;
private string? _status;
Expand Down Expand Up @@ -217,6 +218,12 @@ public void Flush()
NetState.Send(new ServerFlushPacket());
}

public void Undo()
{
if(UndoStack.Count > 0)
NetState.Send(UndoStack.Pop());
}

#region events

/*
Expand Down
44 changes: 37 additions & 7 deletions Client/Map/ClientLandscape.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,49 @@ public ClientLandscape(CentrEDClient client, ushort width, ushort height) : base
_client.OnBlockReleased(block);
};

LandTileReplaced += (tile, newId) => _client.Send(new DrawMapPacket(tile, newId));
LandTileElevated += (tile, newZ) => _client.Send(new DrawMapPacket(tile, newZ));
LandTileReplaced += (tile, newId) =>
{
_client.UndoStack.Push(new DrawMapPacket(tile));
_client.Send(new DrawMapPacket(tile, newId));
};
LandTileElevated += (tile, newZ) =>
{
_client.UndoStack.Push(new DrawMapPacket(tile));
_client.Send(new DrawMapPacket(tile, newZ));
};

StaticTileAdded += tile => _client.Send(new InsertStaticPacket(tile));
StaticTileRemoved += tile => _client.Send(new DeleteStaticPacket(tile));
StaticTileAdded += tile =>
{
_client.UndoStack.Push(new DeleteStaticPacket(tile));
_client.Send(new InsertStaticPacket(tile));
};
StaticTileRemoved += tile =>
{
_client.UndoStack.Push(new InsertStaticPacket(tile));
_client.Send(new DeleteStaticPacket(tile));
};
StaticTileReplaced += (tile, newId) =>
{
_client.UndoStack.Push(new InsertStaticPacket(tile.X, tile.Y, tile.Z, tile.Id, tile.Hue));
_client.UndoStack.Push(new DeleteStaticPacket(tile.X, tile.Y, tile.Z, newId, tile.Hue));
_client.Send(new DeleteStaticPacket(tile));
_client.Send(new InsertStaticPacket(tile.X, tile.Y, tile.Z, newId, tile.Hue));
};
StaticTileMoved += (tile, newX, newY) => _client.Send(new MoveStaticPacket(tile, newX, newY));
StaticTileElevated += (tile, newZ) => _client.Send(new ElevateStaticPacket(tile, newZ));
StaticTileHued += (tile, newHue) => _client.Send(new HueStaticPacket(tile, newHue));
StaticTileMoved += (tile, newX, newY) =>
{
_client.UndoStack.Push(new MoveStaticPacket(newX, newY, tile.Z, tile.Id, tile.Hue, tile.X, tile.Y));
_client.Send(new MoveStaticPacket(tile, newX, newY));
};
StaticTileElevated += (tile, newZ) =>
{
_client.UndoStack.Push(new ElevateStaticPacket(tile.X, tile.Y, newZ, tile.Id, tile.Hue, tile.Z));
_client.Send(new ElevateStaticPacket(tile, newZ));
};
StaticTileHued += (tile, newHue) =>
{
_client.UndoStack.Push(new HueStaticPacket(tile.X, tile.Y, tile.Z, tile.Id,newHue, tile.Hue));
_client.Send(new HueStaticPacket(tile, newHue));
};
}

protected override Block LoadBlock(ushort x, ushort y)
Expand Down

0 comments on commit 8b7dfc7

Please sign in to comment.