1 decade ago by Dropkick
How exactly can I save a level state so that any entities killed, power-ups collected, etc stay the same when the player returns to the level?
This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact
main.js
add the following code:// important that you keep this accurate between levels // can be a number or string current_level: '', killed_entities: {},
kill: function() { // Create an object for the current level if it does not exist yet. if(typeof ig.game.killed_entities[ ig.game.current_level ] === 'undefined') { ig.game.killed_entities[ ig.game.current_level ] = {}; } // Store the ID of the current entity. ig.game.killed_entities[ ig.game.current_level ][ this.id ] = true; // Kill the entity. this.parent(); }
main.js
you want to customize your loadLevel
method.loadLevel: function(data) { // Load the level, as usual. this.parent(data); // Quickly kill all the entities that are supposed to be dead. // Is there "persistently dead" data to check against? if(typeof this.killed_entities[ this.current_level ] !== 'undefined') { // Check all entities... for(var i=0; i<this.entities.length; i++) { // Against "persistently dead" data. for(var id in this.killed_entities[ this.current_level ]) { // Is this entity supposed to be "persistently dead"? if(this.entities[ i ].id === id) { // Kill the entity. this.entities[ i ].kill(); } } } } }
LevelExample.data.entities
and splice out the ones that should stay dead before you call loadLevel
. Quote from Joncom
By doing it manually.
PlayerInfo
class that gets instantiated in main.js once. It maintains two lists: a list of game states that have been checkpointed, and a list of game states that haven&039;t been checkpointed yet. When the player runs over a checkpoint, I concat the latter list onto the former. If the player dies before checkpointing, I iterate over the "current" game states and #unapply
them.GameState
looks like this:ig.module( 'game.system.gameState' ) .requires( 'impact.impact' ) .defines(function() { // As the player moves around in a level, we need to track // what they have done. If the player dies before checkpointing, // either by touching a checkpoint or by entering a level portal, // then we need to undo everything that the player has affected // on the level, and any effects those things might have had on // the player. GameState = ig.Class.extend({ init: function() {}, // Called when the gamestate should be applied. If this // is the state of an entity, that entity should be removed // from the world. apply: function() { // By default, remove the entity from the level. ig.game.removeEntity(this.entityInstance); }, // Called to give this frame a chance to modify the // player behaviors in whatever way. modifyPlayer: function(player) {}, // Called to give this frame a chance to undo the // player behavior changes it made earlier. unmodifyPlayer: function(player) {}, // Called when the object is removed from player, // whether dropped or because of player death. unapply: function() { // By default, spawn another one in the level. var x = this.entityInstance.pos.x; var y = this.entityInstance.pos.y; ig.game.spawnEntity(this.entityType, x, y); } }); });
modifyPlayer
to make any changes to how the player works (player can now fly, etc). The default implementations of apply
and unapply
take care of adding and removing the entity from the level. There&039;s an override of #GameState
for entities that have to be added in/removed if the player hasn't checkpointed yet.unmodifyPlayer
handles removing the new behaviors and unapply
handles resetting the entity.id
which evidently changes, set a unique name
value for each entity in Weltmeister. Then change up the one line from:// Store the ID of the current entity. ig.game.killed_entities[ ig.game.current_level ][ this.id ] = true;
// Store the name of the current entity. ig.game.killed_entities[ ig.game.current_level ][ this.name ] = true;