Hello everyone, I recently got back into making a game along the lines of Advance Wars, and I’m finding that I’m making quick progress after a long break. I don’t want to bore you all with the details there though, so let’s jump to it.
I am setting up the following situation in preparation to make my first test of dijkstra’s algorithm. As I’m sure anyone familiar with the AW series or any turn-based strategy game would know. I have units and terrain I am dealing with. The units have movement types that do better or worse on certain terrain. Therefore, I have to implement a movement cost for each unit on each terrain type.
Ultimately, I’m looking for a critique of this code from more experience programmers. I am playing this by ear with no formal training. I’ve heard that a large quantity of if statements is supposed to be bad, and I have 13, with switch statements nested inside.
Here is the code itself:
public class TerrainData : MonoBehaviour {
/*I have about 13 terrain tiles. Each one will have this script component "TerrainData". The terrain type will be set in the editor for the prefab terrain game objects, which should take care of any terrain instance that is created.*/
enum movementType { foot, mech, tireA, tireB, tread, flying, naval };
public enum terrainType { plain, forest, river, mountain, sea, road, shore, fog, city, unitBase, airport, port, headquarters };
public terrainType terrain = terrainType.plain;
public bool occupied = false;
/*Dijkstra's algorithm will call the function below to determine the movement cost of the currently selected unit. The movement type parameter from the unit will be set within the algorithm*/
int GetMovementCostA(movementType m)
{
int movementCost=1;
/*I am defining only the plain terrain for this code snippet.*/
/*I elected to use this method where every terrain shares the same Data script because the Dijkstra script can then make one call to the function "GetMovementCostA()" and this will work for any terrain instead of having unique call functions for each terrain*/
if (terrain == terrainType.plain)
{
switch (m)
{
case movementType.foot:
movementCost = 1;
break;
case movementType.mech:
movementCost = 1;
break;
case movementType.tireA:
movementCost = 1;
break;
case movementType.tireB:
movementCost = 2;
break;
case movementType.tread:
movementCost = 1;
break;
case movementType.flying:
movementCost = 1;
break;
case movementType.naval:
movementCost = 9999;
break;
default:
movementCost = 1;
break;
}
/*As a side question, I noticed that I can't use "else if" statements in my code without generating error (sorry I don't have it infront of me); so the if statement is repeated 13 times, and if my understanding is correct, that means there will be 13 checks for terrain type even if the first condition is met. This seems somewhat bad, but this function isn't called on an update function, so I guess this is okay?*/
}
...
return movementCost;
}
Thanks for any help!