I’m in the middle of adding a co-op mode into my iPad game Bomber Command.
Basically I have a spawn manager which keeps track of the spawns and difficultly level of the spawned enemy. I then have a prefab for each type of enemy which when it’s instantiated checks the spawn manager to see what difficulty level the player has reached. From this info I want the enemy to set itself up deciding whether it has a sheild, which weapon it has, hit points, damage etc.
The problem is I have potentially a 100 levels of difficulty and I don’t want to write an if statement for each differrent difficulty level, for example:
I’m fairly new to writing code and was wondering if someone could help or point me in the right direction as there must be a better way of achieving this result without 100’s of if statements!
Everything depends on how your game is balanced, but if your game is rather procedural, you may think about some of your values indexed on the level, like :
Whether your current solution is a good or bad idea really depends on the context.
Here’s one (untested) example where you use a GameManager script to keep track of the difficulty settings. These can either be tweaked in code or in the inspector of it. Please excuse the C#, I’m not much of a JavaScript baker.
// From GameSettings.cs
public class LevelSettings
{
public bool shield;
public bool macineGun;
public bool laserGun;
public int hitPoints;
public int damage;
}
public LevelSettings[] levelSettings = null;
void Awake ()
{
if (levelSettings == null)
{
levelSettings = new LevelSettings[3];
levelSettings[0] = new LevelSettings ()
{
shield = false,
machineGun = true,
laserGun = false,
hitPoints = 100,
damage = 20
};
levelSettings[1] = new LevelSettings ()
{
shield = true,
machineGun = true,
laserGun = false,
hitPoints = 100,
damage = 40
};
levelSettings[2] = new LevelSettings ()
{
shield = true,
machineGun = true,
laserGun = true,
hitPoints = 200,
damage = 60
};
}
}
// Your class using it
void Awake ()
{
GameSettings settings = (GameSettings)FindObjectOfType (typeof (GameSettings));
shield = settings.levelSettings[spawnManager.difficultyLevel].shield;
machineGun = settings.levelSettings[spawnManager.difficultyLevel].machineGun;
laserGun = settings.levelSettings[spawnManager.difficultyLevel].laserGun;
hitpoints = settings.levelSettings[spawnManager.difficultyLevel].hitpoints;
damage = settings.levelSettings[spawnManager.difficultyLevel].damage;
}
@Kemical
I was planning to to be able to mix and match the enemies abilities but your method seems like it would be much easier to control the balance of the difficulty with less play testing.
@Angry Ant
I’m not really up to speed on how classes work so please correct me if I’m wrong but with your method I would be setting all the variables up in a class and then my enemy prefab would inherit the variables from the class based on the difficulty? Am I correct?
@iceshaft07
I don’t really understand what your doing it looks like your storing the information in an array could you elaborate a bit more for me please my coding is not the greatest and I start to get lost with array’s?
My example would have the GameSettings class or component be where you configure possible stats per level. The spawner would then query this central component for what to apply, given a certain level. The idea is basically to centralize these settings - rather than having them spread out per spawnable.
Don’t forget the switch/case… tho’ I’ve not looked closely at any of the code on this thread (bad bad angel), too many if’s can often be replaced with:
////////////// SWITCH CASE EXAMPLES///////////
switch (subjectVar) {
case subjectVarValue:
DoSomething();
break;
case subjectVarValue:
DoSomething();
break;
case subjectVarValue:
DoSomething();
break;
}
switch (axis) {
case ConstraintAxis.X:
rotateAround = Vector3.right;
break;
case ConstraintAxis.Y:
rotateAround = Vector3.up;
break;
case ConstraintAxis.Z:
rotateAround = Vector3.forward;
break;
}
switch (currentInitial) {
case 1:
currentCharacter = PlayerPrefs.GetInt("Initial1") + newValue;
if (currentCharacter > 47)
currentCharacter = 0;
if (currentCharacter < 0)
currentCharacter = 47;
PlayerPrefs.SetInt("Initial1", currentCharacter);
break;
case 2:
currentCharacter = PlayerPrefs.GetInt("Initial2") + newValue;
if (currentCharacter > 47)
currentCharacter = 0;
if (currentCharacter < 0)
currentCharacter = 47;
PlayerPrefs.SetInt("Initial2", currentCharacter);
break;
case 3:
currentCharacter = PlayerPrefs.GetInt("Initial3") + newValue;
if (currentCharacter > 47)
currentCharacter = 0;
if (currentCharacter < 0)
currentCharacter = 47;
PlayerPrefs.SetInt("Initial3", currentCharacter);
break;
}
////////////// END SWITCH CASE EXAMPLES END ///////////
This allows you to switch straight to the particular case you have at the mo’.