Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by coreysnyder

Let's say I have 10 levels. In each level I have a common "Enemy Spawner" which spits out enemies. I want each level to spawn different enemies at different times. I know how to code the Enemy Spawner, what I don't know about is how you defined level specific variables which get loaded in and are accessible when the level is running.

My idea behind how to do it would be to build a config object of all the enemies I want to spawn for each level. Store them in an array or a hash like:
var enemyArray = [
   0: { 
          5: "Monster 1"
          8: "Monster 1"
        10: "Monster 2"
       },
    0: { .... }
]

Then in my init, I'll determine which level I'm on with some built in function I assume exists (share?). Based on the level, I can pull out the enemy config, and pass that to my "Enemy Spawner" using createEntity.

Is that how you'd do things? Is there a better way to store level specific variables?

1 decade ago by Joncom

Out of the box, there is no method that tells you which level is loaded.

However, you could make a function in main.js that loads the level as well as records the name of it.

changeLevel: function(map) {
	
	// Store the name of the level.
	this.currentLevel = map;

	// Change map.
	this.loadLevelDeferred( ig.global['Level' + map] );
}

You could then find out what map is currently loaded by accessing this.currentLevel or from outside main.js using ig.game.currentLevel.

1 decade ago by beardedbuddha

You could also add a void Entity to each level in WM and store level-specific data in it. This is what I do to keep my main code cleaner. For example I store the soundtrack for the level in there.

Void Entity example can be found in the Biolab Entity pack..

1 decade ago by ShawnSwander

I'd
 switch (this.currentLevel){
case 'level 1':
var spawnTime= 3
break;
case 'level 2':
var spawnTime= 6
break;
}

1 decade ago by coreysnyder

Ok here's a follow up question. I call the code below using spawnEntity. The init console.log shows up in console, but the updates never happen? What do I need to do to make sure the game calls my Entities update method?

ig.module(
    'game.entities.enemySpawner'
)
.requires(
    'impact.entity'
)
.defines(function(){

    EntityEnemySpawner = ig.Entity.extend({
        size: {x: 8, y: 8},
        _wmDrawBox: true, // Used for adding a square to the level editor
        _wmBoxColor: 'rgba(255, 0, 0 ,0.7)',

        init: function( x, y, settings ) {
            this.parent( x, y, settings );
            console.log("ENEMY SPAWNER",settings);

            this.spawnTimer = new ig.Timer();
        },
        update: function(){
            console.log("Entity UPDATE");
            this.parent();
        }

    });


});
Page 1 of 1
« first « previous next › last »