Extendable Checkpoints

Version:

1.0

Added on:

03 Apr 2018 20:58

Tags:

Description:
A simple wrapper for checkpoint objects that lets you save and restore additional information, e.g. the jjTriggers array. Optionally you may also have checkpoint objects be reusable, so if you use checkpoint A and then checkpoint B, checkpoint A will go back to being unused. This could be useful for less linear level design.
  1. array<bool> SavedTriggers(32, false); //you will need some way to keep track of whatever information you want saved... a global variable is the simplest choice
  2.  
  3. void onLevelLoad() {
  4.   jjObjectPresets[OBJECT::SAVEPOST].behavior = CheckpointWrapper;
  5.   jjObjectPresets[OBJECT::SAVEPOST].deactivates = false;
  6. }
  7. void onLevelReload() { //restore the state of the saved properties to how they were last time you visited a checkpoint
  8.   for (uint i = 0; i < 32; ++i)
  9.     jjTriggers[i] = SavedTriggers[i]; //although this example uses jjTriggers, other things should work as well
  10. }
  11.  
  12. void CheckpointWrapper(jjOBJ@ obj) {
  13.   if (obj.state == STATE::STOP) { //don't do anything anymore
  14.     jjDrawSpriteFromCurFrame(obj.xPos, obj.yPos, obj.curFrame);
  15.   } else if (obj.state == STATE::DEACTIVATE) { //due to death
  16.     obj.deactivate();
  17.   } else {
  18.     obj.behave(BEHAVIOR::CHECKPOINT);
  19.     if (obj.state == STATE::DONE) { //triggered by the player hitting it
  20.       obj.state = STATE::STOP;
  21.       //save the current state of some properties
  22.       for (uint i = 0; i < 32; ++i)
  23.         SavedTriggers[i] = jjTriggers[i];
  24.       //OPTIONAL: this loop makes checkpoints reusable, so only the most recent checkpoint you touched is ever active
  25.       for (int i = jjObjectCount; --i > 0;) {
  26.         jjOBJ@ obj2 = jjObjects[i];
  27.         if (obj2.eventID == OBJECT::CHECKPOINT && i != obj.objectID && obj2.isActive) {
  28.           obj2.state = STATE::SLEEP;
  29.           obj2.var[0] = 0;
  30.         }
  31.       }
  32.     }
  33.   }
  34. }