Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

This question already has an answer here:

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!

share|improve this question

marked as duplicate by Sean Middleditch, Trevor Powell, bummzack, Josh Petrie, Byte56 Feb 16 '13 at 23:52

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
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? –  Tordin Feb 14 '13 at 11:05
1  
Related/dup? –  Anko Feb 14 '13 at 13:18
    
More a duplicate of the link from @SeanMiddleditch I think, but definitely similar to both. –  Cameron Fredman Feb 15 '13 at 1:15
    
Yep, didn't see these questions, they pretty much answer my question –  roarster Feb 20 '13 at 11:03
add comment

2 Answers 2

up vote 1 down vote accepted

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

share|improve this answer
    
accepted your answer because of your clear examples, thanks! –  roarster Feb 20 '13 at 11:03
add comment

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.

share|improve this answer
add comment

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