Extendable Checkpoints

Version:

1.0

Added on:

03 Apr 2018 18: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.
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

void onLevelLoad() {
  jjObjectPresets[OBJECT::SAVEPOST].behavior = CheckpointWrapper;
  jjObjectPresets[OBJECT::SAVEPOST].deactivates = false;
}
void onLevelReload() { //restore the state of the saved properties to how they were last time you visited a checkpoint
  for (uint i = 0; i < 32; ++i)
    jjTriggers[i] = SavedTriggers[i]; //although this example uses jjTriggers, other things should work as well
}

void CheckpointWrapper(jjOBJ@ obj) {
  if (obj.state == STATE::STOP) { //don't do anything anymore
    jjDrawSpriteFromCurFrame(obj.xPos, obj.yPos, obj.curFrame);
  } else if (obj.state == STATE::DEACTIVATE) { //due to death
    obj.deactivate();
  } else {
    obj.behave(BEHAVIOR::CHECKPOINT);
    if (obj.state == STATE::DONE) { //triggered by the player hitting it
      obj.state = STATE::STOP;
      //save the current state of some properties
      for (uint i = 0; i < 32; ++i)
        SavedTriggers[i] = jjTriggers[i];
      //OPTIONAL: this loop makes checkpoints reusable, so only the most recent checkpoint you touched is ever active
      for (int i = jjObjectCount; --i > 0;) {
        jjOBJ@ obj2 = jjObjects[i];
        if (obj2.eventID == OBJECT::CHECKPOINT && i != obj.objectID && obj2.isActive) {
          obj2.state = STATE::SLEEP;
          obj2.var[0] = 0;
        }
      }
    }
  }
}