c# - What is the best way to implement both a cooldown and length of effect for an ability in a Turn-Based Strategy Game? - Stac

admin2025-04-17  2

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;
        }
    }
Share Improve this question edited Mar 16 at 1:22 Christoph Rackwitz 15.9k5 gold badges39 silver badges51 bronze badges asked Feb 1 at 3:01 Jacob JacksonJacob Jackson 3643 silver badges13 bronze badges 1
  • Use "count-down" counters for each object that needs to maintain a certain state for a certain number of frames, turns or time span. Initialize a counter and an initial state. "Count down" on each turn. When you hit "zero", transition to the new state, set the counter to the new set of "turns", and count down again to the next state. – Gerry Schmitz Commented Feb 1 at 5:06
Add a comment  | 

1 Answer 1

Reset to default 1

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.

转载请注明原文地址:http://anycun.com/QandA/1744840929a88361.html