Adding Checkpoints to PSK
Here's a video showing off what the checkpoint system should look like after this tutorial:
Here's the basic rundown of how I set this up. First, we'll add another tile to our list so we can add checkpoints through our text document levels. Second, we'll set up how the checkpoints should work. The most recent checkpoint you've touched will be your current checkpoint. If you know how the Sonic games have checkpoints it's very much like that. When you die you'll be returned to your most recent checkpoint.
In Tile.cs add the following in enum TileCollision:
1 | Checkpoint = 3, |
In Level.cs After this line:
1 | private Vector2 start; |
Add this:
1 | public Vector2 checkpoint; |
Now find the following:
1 2 3 4 | public void StartNewLife() { Player.Reset(start); } |
And change it to this:
1 2 3 4 5 6 7 8 9 | public void StartNewLife() { //After we die, this is called. If there IS a checkpoint saved, //we start there. Otherwise we begin at the start point. if(checkpoint != Vector2.Zero) Player.Reset(checkpoint); else Player.Reset(start); } |
In ‘private Tile LoadTile(...)’ add this near the others.
1 2 3 | //Checkpoint - A checkpoint tile can be added to levels using the 'P' character case 'P': return LoadTile("BlockA0", TileCollision.Checkpoint); |
Now in Player.cs find the following:
1 | TileCollision collision = Level.GetCollision(x, y); |
And add this after it:
1 2 3 | //If we touched a checkpoint tile, set our new checkpoint here if (collision == TileCollision.Checkpoint) level.checkpoint = new Vector2(x, y) * Tile.Size; |
Now make sure you add some 'P's to your levels so you can try out the checkpoints. Currently they just use the BlockA0 texture so you'll have to add your own to the Tiles folder and change the case 'P': ... texture argument to match your own checkpoint texture.