Like the title states, replays are functional. You can play through a game and upon your death a replay file will be saved that can be played back. It took a couple days to implement, but it was really fun development because I have never done anything quite like this.
The replay file is basically just a log of keypresses, with there being a couple special scenarios. When loaded the game switches out the main input class with a dummy one that looks for specific replay keys, which at this moment is just the arrow key, but there could be more in the future (fast forward, next floor, ect). When the dummy input class receives a keypress that indicates that it should advance a turn, it reads the next key from the replay and passes itself to the regular input function.
I don’t do this often, but I’ll do a little code breakdown because for some reason I thought this code was pretty cool. To start with, all input is read through the IInputProvider interface. This interface only requires one method: bool KeyPress(Keys key), that check if a key is currently pressed.
bool KeyPress(Keys key);
The game primarily uses a implementation of this interface called Controls (very original name, right?) that gets input from the keyboard. I also have a ReplayManager class that handles loading the replay and keeping all data associated with a replay. The cool thing is that this class also implements the IInputProvider interface and can be used to provide input, obviously from the replay. Here is the bit of code that handles input.
public override void HandleInput(Controls Controls)
IInputProvider C = Controls;
if (Globals.Mode == GameMode.Replay)
Globals.RecordingManager.Advance(); //Advance replay to next Key
C = Globals.RecordingManager; //Override the controller with recording manager.
return; //If no key is pressed then we do not do anything.
//Rest of the Input Code for the game (AKA The actual game)
There was a lot more code needed to get it all working, but for the most part this was the main switch that got it all working seamlessly. It was fun designing this and implementing it in a nice way that doesn’t feel like a huge hack at all.