1
\$\begingroup\$

In a turn based game each player has a set of properties, e.g attack, defence, speed and these can all be used to work out who should go first and how much damage should be dealt. That all works fine, but the problem I'm having is with edge cases where a player receives a power-up for one turn that lets them move first. How should I handle this? Should I increase the speed value and then somehow remember to reduce it next turn or should I implement some sort of object which holds modifiers that is checked every turn?

I'd also need to be able to scale this to different things like temporary poison etc.

Basically I'm very confused and help would be appreciated!

\$\endgroup\$
4
  • \$\begingroup\$ keep track if the player has a boost on it, and then chekc what kind of boost he has. and then make the logic from there? would that work? \$\endgroup\$
    – Tordin
    Feb 14 '13 at 11:05
  • 1
    \$\begingroup\$ Related/dup? \$\endgroup\$
    – Anko
    Feb 14 '13 at 13:18
  • \$\begingroup\$ More a duplicate of the link from @SeanMiddleditch I think, but definitely similar to both. \$\endgroup\$ Feb 15 '13 at 1:15
  • \$\begingroup\$ Yep, didn't see these questions, they pretty much answer my question \$\endgroup\$
    – roarster
    Feb 20 '13 at 11:03
0
\$\begingroup\$

First thing, can the user have more than one boost/powerup at a time? For example, can they have a speed boost and be poisoned at once? If not, you can simply keep an integer 'powerup' lying about, and say for example

if (powerup == 1){
  //speed boost
}else if(powerup == 2){
  //poison
}else if(powerup == 3){
  //etc. 
}

If you can have multiple modifiers at once, you'll need some sort of listing method. I use Java, so ArrayLists are what I would be using, but I think AS3 or JS arrays are similar. It would be easiest to make a powerup class with a few variables like "timeSinceActivated" or "timeLeft", and then increment or decrement them appropriately each turn.

for example:

class PowerUp{
  int timeLeft;
  int power; //if you want some poison to be stronger or whatever
  public void update(){
    timeLeft --;
  }
}
class SpeedBoost extends PowerUp{
  timeLeft = 1;
  @override
  public void update(){
    super.update();
    playerspeed = 1000;
  }
}

class Player{
  //stuff
  ArrayList<PowerUp> modifiers;
  //other stuff
  public void updatePlayer(){
    for(int i = modifiers.size() - 1; i >= 0; i --){
      PowerUp p = modifiers.get(i);
      p.update();
      if(p.timeLeft == 0){
        modifiers.remove(i);
      }
   }
}

That should be enough to get across my point

\$\endgroup\$
1
  • 1
    \$\begingroup\$ -1: This is actually very bad advice. Don't keep magic integers lying around and elseif them: use enums or at least use constants; Avoid iterating in reverse order if not strictly necessary (hurts cache); Never check timeout values by equality by zero(use <=), if you change that to a float, the code will break. \$\endgroup\$ Oct 18 '17 at 1:32
2
\$\begingroup\$

Should I increase the speed value and then somehow remember to reduce it next turn or should I implement some sort of object which holds modifiers that is checked every turn?

This is the heart of your question and you're really asking, "should I use an object oriented approach?" In general, you're likely to find more advocates than critics of the object-oriented approach, although critics exist.

My suspicion is that if you go with option one (the non-OOP approach), you're going to find yourself with an increasingly convoluted and complicated class, whereas checking status effect modifier objects each turn will lead to much cleaner, more extensible code.

Your players should hold a collection of StatusEffects. StatusEffect can be an object you extend or an interface. (I'm calling them StatusEffect rather than Power-Up, because you mentioned you'd use this for poison too, so it feels like a better name.) In your StatusEffect class you're going to want to put anything that would apply to all of them. totalDuration (number of turns it lasts), currentDuration (how many turns it has been in effect), etc.

Then you'll implement particular StatusEffects, speed-up, poison, etc. You might consider having each statuseffect implement comparable() so you can sort the collection held by the player (i.e., does poison take effect before a healing effect?). Give each effect a applyEffect() method of some sort, where the modifications to the player are applied. Also give them an isExpired() method to see if the currentDuration has reached the totalDuration. When you do a turn, you'll iterate through the effects, apply them, and remove them if necessary.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.