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);

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

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

Subtracting life is similar.

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

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.

Cards like Strangling Soot will have a SpellAbility.resolve that looks like this.

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

This discards a card at random from a player.

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.

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.

Shuffling your library is a common occurrence.

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();

No comments: