Skip to content

Commit c99febd

Browse files
committed
redo and undo twice
1 parent 75dc1e4 commit c99febd

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

Assets/Patterns/1. Command/Rebind keys/Scripts/GameController.cs

+34
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,23 @@ void Update()
9494
redoCommands.Push(lastCommand);
9595
}
9696
}
97+
//Undo with Q
98+
else if (Input.GetKeyDown(KeyCode.Q))
99+
{
100+
if (undoCommands.Count < 2)
101+
{
102+
Debug.Log("Can't undo twice because we are back where we started or there is only one undo command");
103+
}
104+
else
105+
{
106+
Command lastCommand = undoCommands.Pop();
107+
lastCommand.Undo();
108+
redoCommands.Push(lastCommand);
109+
lastCommand = undoCommands.Pop();
110+
lastCommand.Undo();
111+
redoCommands.Push(lastCommand);
112+
}
113+
}
97114
//Redo with r
98115
else if (Input.GetKeyDown(KeyCode.R))
99116
{
@@ -111,6 +128,23 @@ void Update()
111128
undoCommands.Push(nextCommand);
112129
}
113130
}
131+
//Redo twice
132+
else if (Input.GetKeyDown(KeyCode.Alpha2))
133+
{
134+
if (redoCommands.Count < 2)
135+
{
136+
Debug.Log("Can't redo twice because we are at the end or there is only one left");
137+
}
138+
else
139+
{
140+
Command nextCommand = redoCommands.Pop();
141+
nextCommand.Execute();
142+
undoCommands.Push(nextCommand);
143+
nextCommand = redoCommands.Pop();
144+
nextCommand.Execute();
145+
undoCommands.Push(nextCommand);
146+
}
147+
}
114148

115149

116150
//Rebind keys by just swapping A and D buttons

0 commit comments

Comments
 (0)