Jump to content

best way to load different prefabs per level?


Mike018
 Share

Recommended Posts

Each level (8 in total) in my game has a different enemy prefab. I made it work with the following code, but having a huge if else chain doesn't seem very efficient. Any advice for a better way to do this? 

if (this.currentLevel === 1) {
    this.blade1 = new TwoBlades.Blade1(this.game, this.game.world.width * Math.random(), 0);
    this.blade2 = new TwoBlades.Blade1(this.game, this.game.world.width * Math.random(), this.game.world.height);
    this.enemySpeed = 150;
    this.enemySpeed2 = 200;

} else if (this.currentLevel === 2) {
    this.blade1 = new TwoBlades.Blade2(this.game, this.game.world.width * Math.random(), 0);
    this.blade2 = new TwoBlades.Blade2(this.game, this.game.world.width * Math.random(), this.game.world.height);
    this.enemySpeed = 200;
    this.enemySpeed2 = 150;
}

I also did the same thing in another function, changing text and images in the next level description modal after each level

else if(this.currentLevel === 4) {
    this.nextLevelName.text = 'Growth Spurt';
    this.nextLevelBlade.loadTexture('blade4');
    this.nextLevelBlade2.loadTexture('blade4');
    this.nextLevelAbilityName.text = "Grow";
    this.nextLevelAbilityImage.loadTexture('ability4');

} else if(this.currentLevel === 5) {
    this.nextLevelName.text = 'Ludicrous Speed';
    this.nextLevelBlade.loadTexture('blade5');
    this.nextLevelBlade2.loadTexture('blade5');
    this.nextLevelAbilityName.text = "Speed Burst";
    this.nextLevelAbilityImage.loadTexture('ability5');
}

 

As a side note, if I have two prefabs that do the exact same thing, but one is slightly different with some extra functions, do they have to be split into two prefabs or is there a way to use a prefab without using all it's functionality?

Link to comment
Share on other sites

Yes, I have done that. My question was on how to properly call them based on the current level, rather than having if/else statements based on the level variable. I'd imagine I couldn't do this if a game had many levels.

Link to comment
Share on other sites

My game Blaster has a very simple file format for describing each wave of the game. The game is built using webpack, so I just "require" the YAML file right in: https://github.com/drhayes/blaster/blob/master/src/data/wavesData.yaml

Each "g" is a "grunt", each "e" is an "enforcer", etc. Here's the plugin that uses that data: https://github.com/drhayes/blaster/blob/master/src/plugins/waves.js You can see in the method "loadCurrentWave" that there are a series of for-loops that build all the things.

This system could have been made MORE generic by making a map of "property in level data" to "Classname of thing to spawn" and then iterated over the keys of the map and run the for loop. e.g.:

var waveEnemies = {
  'g': Guard,
  'e': Enforcer
}

Object.keys(waveEnemies).forEach(k => {
  const clazz = waveEnemies[k];
  const thing = new clazz(this.game, this.game.world.randomX, this.game.world.randomY);
  this.enemiesGroup.add(thing);
});

Something like that.

You want this to be more data driven, essentially. Then you're not reaching in to some huge if statement to change things, you're just changing a data file. You could put the text in there, too.

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...