This is something I've been putting off (for literally years) for quite a few reasons: I didn't know how to portray the "death", I didn't know whether I wanted lives or health or both, I didn't know how to handle puzzle and item state across a character death. And it's not that I've necessarily resolved all those problems - but I figured I just needed to make a start on it.
The first thing I did is punted on the death animation/rag doll bit - and for now, I just catapult the little cow into space when he hits something deadly. In the test moovie below, the "Spikes" model has a "Toxic" behaviour attached to it which sends a "Damage" message to the cow, and for now at least, the cow loses a life rather than some hitpoints. I might look at adding health-style damage later - but my immediate goal is just getting the spawn-play-die-restart cycle working.
Next, I had to handle resetting the current area/map/room. This turned out to be a really interesting problem - and a lot harder to solve that just saving/reloading the current game state (which is already solved via the built in object serialisation). You see, the plan is for the cow to get lot of little puzzles to solve: How do you reach the key up on that platform? How do you open that door? Etc. So what happens when the player goes into a room and destroys some block that they actually need to finish the puzzle? Or they push the key off the cliff? I need a way of resetting all the elements of a given puzzle back to a solvable state.
Before I waffle on about the boring behind the scenes work, here's a little moovie of it working in a test scene: there's a switch which is setup to remove a block from the room, and there's a gun that rests on the block (so flipping the switch lets the gun fall to the ground where you could pick it up). In the vid, you can see the cow flips the switch, and then suicides on the spikes. When the character respawns, not only is the cow put back as he was, but the switch flips back, the block re-appears, and the gun falls back down from its starting point.
To implement this, I've added a Snapshot object. Whenever you enter a new area, the game populates the Snapshot with all the puzzle elements for that area and takes a snapshot of their current state. Then, whenever you die or re-enter the area, it restores the snapshot. The cool thing about the snapshot is that it's built on top of the exiting serialisation mechanism, but modified so that it restores values on the existing objects, rather than replacing them with a new object. This allows it to "undo" literally any change (e.g. not only can you move things around and have them put back, but you can remove the physics from an object and it will put it back, you can destroy an object completely and it will resurrect it for you, you can change the state of a state machine and it will put it back, you can turn off gravity and collisions on an object and it will turn them back on, you can change the colour of the shader ... you get the picture). And because it restores the current objects, it won't break any relationships to other objects in the gameworld (for bigger quests, etc). As a bonus, because it's a layer on top of the serialisation code - I can choose to use XML (for dev/debugging) or binary (for speed/size) to implement the snapshot - which has already saved me hours in debugging the feature to start with.
Next I need to think more about how many Snapshots the game will actually use to properly handle independently resetting the room vs specific parts of the player's character (e.g. right now, the snapshot restores the character's life count ... which kind of defeats the purpose of letting him lose a life in the first place). Still, I can now start a new game (from my spiffy menus of last entry), walk around until I lose a life, then restart the current room ... which is getting much closer to actually having a very simple end-to-end game cycle working.
Cheers!