Thursday, July 5, 2007

Programming Life Gain

I wanted to cover some of the basic abilities that cards do like gaining life, losing life, drawing a card, etc… AllZone is a static global variable that can be accessed from anywhere. GameAction does some high level actions like drawing cards and destroying a creature. The code snippets that I will be going over usually will go in the resolve method of SpellAbility objects. These are some common actions that are done throughout the game.

This is how you gain life.

PlayerLife p = AllZone.GameAction.getPlayerLife(Constant.Player.Computer);
p.addLife(2);

Usually I wouldn’t hardcode the player (human or computer) so the code would look like this.

PlayerLife p = AllZone.GameAction.getPlayerLife(card.getController());
p.addLife(2);

Subtracting life is similar.

PlayerLife p = AllZone.GameAction.getPlayerLife(Constant.Player.Computer);
p.subtractLife(4);

Drawing a card looks like this. If you wanted to draw multiple cards, you would use a for loop. AllZone.GameAction.drawCard(Constant.Player.Human);

Whenever a card is put into a graveyard, AllZone.GameAction.destroy(Card) is used.
AllZone.GameAction.destroy(Card);

Cards like Strangling Soot will have a SpellAbility.resolve that looks like this.
AllZone.GameAction.destroy(spell.getTargetCard());

Terror is similar but says that the card cannot be regenerated. AllZone.GameAction.destroyNoRegeneration(Card);

This discards a card at random from a player.
AllZone.GameAction.discardRandom(Constant.Player.Computer);

Regular discard is done this way. This assumes that the chosen card is already picked and this method just moves the card from a player’s hand to the graveyard.
AllZone.GameAction.discard(Card);

Sacrificing a card is similar to discarding, the chosen card is already picked. This would be called from the Input class, since it handles all the user input from the mouse. The Input class lets the user choose a card and then the code below is executed.
AllZone.GameAction.sacrifice(Card);

Shuffling your library is a common occurrence.
AllZone.GameAction.shuffle(card.getController());

Cards like Shock just add damage to a card. GameAction.checkStateEffects moves the card to the graveyard if it is needed.
Card c = spell.getTargetCard();
c.addDamage(2);

No comments: