I'm working on an undo and redo system map editor and have come across a major issue. If I place a tile down, move right a tile, and then move back to that tile I previously placed everything works fine. Until I try and Undo, Redo, and then Undo again.
This is the state when they're placed:
Executing:
X: 0
Y: 0
Tiles: {X:32 Y:160 Width:32 Height:32}
Previous Tiles:
Executing:
X: 1
Y: 0
Tiles: {X:32 Y:160 Width:32 Height:32}
Previous Tiles:
Executing:
X: 0
Y: 0
Tiles: {X:32 Y:160 Width:32 Height:32}
Previous Tiles:
The undo redo stack looks something like:
Unexecuting:
X: 0
Y: 0
Tiles: {X:32 Y:160 Width:32 Height:32}
Previous Tiles:
41
Unexecuting:
X: 1
Y: 0
Tiles: {X:32 Y:160 Width:32 Height:32}
Previous Tiles:
-1
Unexecuting:
X: 0
Y: 0
Tiles: {X:32 Y:160 Width:32 Height:32}
Previous Tiles:
-1
Executing:
X: 0
Y: 0
Tiles: {X:32 Y:160 Width:32 Height:32}
Previous Tiles:
Executing:
X: 1
Y: 0
Tiles: {X:32 Y:160 Width:32 Height:32}
Previous Tiles:
Executing:
X: 0
Y: 0
Tiles: {X:32 Y:160 Width:32 Height:32}
Previous Tiles:
Unexecuting:
X: 0
Y: 0
Tiles: {X:32 Y:160 Width:32 Height:32}
Previous Tiles:
-1
Unexecuting:
X: 1
Y: 0
Tiles: {X:32 Y:160 Width:32 Height:32}
Previous Tiles:
-1
Unexecuting:
X: 0
Y: 0
Tiles: {X:32 Y:160 Width:32 Height:32}
Previous Tiles:
41
I don't want this to be a code dump or "debug my code for me". I understand the problem is located at the bottom most segment where it assigns the tile an ID of 41 once again - since that's what it's previous value was. This works the first time - but not the second as seen. I'm confused to why the algorithm fails. If anyone has written something similar, feedback would be appreciated. I don't need code-fixed - just hints as to where I can find a working algorithim or a rational explanationa s to why this happens. It's just a simple pencil tool that should write tiles the grid.
public new void Execute(GameMap gameMap)
{
Console.WriteLine("Executing: ");
PrintState();
_previousTiles.Clear();
// Get vertical portion
var curTexture = MapEditorGlobals.CurrentActiveTexture;
// We need to loop over the width of the editor
for (int w = 0; w < SelectedTiles.Width / 32; w++)
{
for (int h = 0; h < SelectedTiles.Height / 32; h++)
{
var gX = SelectedTiles.X / 32 + w;
var gY = SelectedTiles.Y / 32 + h;
var tY = (gY) * curTexture.Width / 32;
var tX = gX;
var tileID = tY + tX;
_previousTiles.Add(gameMap.Layers[Layer].MapTiles[X + w][Y + h].TileId);
gameMap.Layers[Layer].MapTiles[X + w][Y + h].TileId = tileID;
}
}
}
public new void UnExecute(GameMap gameMap)
{
Console.WriteLine("Unexecuting: ");
PrintState();
List<int> newTiles = new List<int>();
// Get vertical portion
var curTexture = MapEditorGlobals.CurrentActiveTexture;
// We need to loop over the width of the editor
for (int w = 0; w < SelectedTiles.Width / 32; w++)
{
for (int h = 0; h < SelectedTiles.Height / 32; h++)
{
var gX = SelectedTiles.X / 32 + w;
var gY = SelectedTiles.Y / 32 + h;
var tY = (gY) * curTexture.Width / 32;
var tX = gX;
var tileID = tY + tX;
newTiles.Add(gameMap.Layers[Layer].MapTiles[X + w][Y + h].TileId);
gameMap.Layers[Layer].MapTiles[X + w][Y + h].TileId = _previousTiles.Last();
_previousTiles.Remove(_previousTiles.Last());
}
}
}