I'm trying to implement, in C# / Unity, an ability to decloak cloaked ships in a turn-based game for a certain number of turns, then a cooldown for a certain number of turns, and then go back into decloaking again. The script for the ability includes a TakeTurn function that gets called every turn. The code I've come up with is rather convoluted and not ideal. Does anyone have any ideas on how to make this simpler/better?
public void TakeTurn()
{
if (nTurnsCooling < N_TURNS_COOLDOWN && !active)
{
nTurnsCooling++;
}
else
{
active = true;
if (nTurnsActive < N_TURNS_TO_REVEAL_CLOAKED_SHIPS)
{
foreach (var ship in shipsBeingAffected)
{
ship.DecloakShip();
}
nTurnsActive++;
}
else
{
active = false;
nTurnsActive = 0;
}
nTurnsCooling = 0;
}
}
I'm trying to implement, in C# / Unity, an ability to decloak cloaked ships in a turn-based game for a certain number of turns, then a cooldown for a certain number of turns, and then go back into decloaking again. The script for the ability includes a TakeTurn function that gets called every turn. The code I've come up with is rather convoluted and not ideal. Does anyone have any ideas on how to make this simpler/better?
public void TakeTurn()
{
if (nTurnsCooling < N_TURNS_COOLDOWN && !active)
{
nTurnsCooling++;
}
else
{
active = true;
if (nTurnsActive < N_TURNS_TO_REVEAL_CLOAKED_SHIPS)
{
foreach (var ship in shipsBeingAffected)
{
ship.DecloakShip();
}
nTurnsActive++;
}
else
{
active = false;
nTurnsActive = 0;
}
nTurnsCooling = 0;
}
}
Keep the cooldown / cloak state in variables in each ship's class. Implement a ProcessTurn() function to update that state before or after each turn. This way, each ship can have its own cloak / cooldown status. This keeps information about ships with ships and doesn't complicate the turn processing.