Reworked some trap cards using old AlternateCosts class.

This commit is contained in:
LevelX2 2016-02-28 08:44:14 +01:00
parent 0f1839af56
commit 996f07a4ff
2 changed files with 44 additions and 49 deletions

View file

@ -27,25 +27,24 @@
*/
package mage.sets.zendikar;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import mage.constants.CardType;
import mage.constants.ColoredManaSymbol;
import mage.constants.Rarity;
import mage.constants.WatcherScope;
import mage.constants.Zone;
import mage.abilities.Ability;
import mage.abilities.costs.AlternativeCostImpl;
import mage.abilities.costs.Cost;
import mage.abilities.costs.mana.ColoredManaCost;
import mage.abilities.condition.Condition;
import mage.abilities.costs.AlternativeCostSourceAbility;
import mage.abilities.costs.mana.ManaCostsImpl;
import mage.abilities.effects.common.CreateTokenEffect;
import mage.cards.CardImpl;
import mage.constants.CardType;
import mage.constants.Rarity;
import mage.constants.WatcherScope;
import mage.game.Game;
import mage.game.events.GameEvent;
import mage.game.events.GameEvent.EventType;
import mage.game.permanent.Permanent;
import mage.game.permanent.token.SnakeToken;
import mage.game.stack.StackObject;
import mage.players.Player;
import mage.watchers.Watcher;
/**
@ -59,11 +58,9 @@ public class CobraTrap extends CardImpl {
this.expansionSetCode = "ZEN";
this.subtype.add("Trap");
// If a noncreature permanent under your control was destroyed this turn by a spell or ability an opponent controlled, you may pay {G} rather than pay Cobra Trap's mana cost.
this.getSpellAbility().addAlternativeCost(
new CobraTrapAlternativeCost());
this.getSpellAbility().addWatcher(new CobraTrapWatcher());
this.addAbility(new AlternativeCostSourceAbility(new ManaCostsImpl("{G}"), CobraTrapCondition.getInstance()), new CobraTrapWatcher());
// Put four 1/1 green Snake creature tokens onto the battlefield.
this.getSpellAbility().addEffect(new CreateTokenEffect(new SnakeToken(), 4));
}
@ -78,10 +75,33 @@ public class CobraTrap extends CardImpl {
}
}
class CobraTrapCondition implements Condition {
private static final CobraTrapCondition fInstance = new CobraTrapCondition();
public static Condition getInstance() {
return fInstance;
}
@Override
public boolean apply(Game game, Ability source) {
CobraTrapWatcher watcher = (CobraTrapWatcher) game.getState().getWatchers().get(CobraTrapWatcher.class.getName());
return watcher != null && watcher.conditionMet(source.getControllerId());
}
@Override
public String toString() {
return "If a noncreature permanent under your control was destroyed this turn by a spell or ability an opponent controlled";
}
}
class CobraTrapWatcher extends Watcher {
Set<UUID> players = new HashSet<>();
public CobraTrapWatcher() {
super("noncreature permanent destroyed", WatcherScope.PLAYER);
super(CobraTrapWatcher.class.getName(), WatcherScope.GAME);
}
public CobraTrapWatcher(final CobraTrapWatcher watcher) {
@ -95,51 +115,26 @@ class CobraTrapWatcher extends Watcher {
@Override
public void watch(GameEvent event, Game game) {
if (condition == true) { // no need to check - condition has already occured
return;
}
Player player = game.getPlayer(controllerId);
if (player != null && event.getType() == EventType.DESTROYED_PERMANENT) {
Permanent perm = (Permanent) game.getLastKnownInformation(event.getTargetId(), Zone.BATTLEFIELD);
if (event.getType() == EventType.DESTROYED_PERMANENT) {
Permanent perm = (Permanent) game.getPermanentOrLKIBattlefield(event.getTargetId()); // can regenerate or be indestructible
if (perm != null && !perm.getCardType().contains(CardType.CREATURE)) {
if (game.getStack().size() > 0) {
StackObject spell = game.getStack().getStackObject(event.getSourceId());
if (spell != null && game.getOpponents(controllerId).contains(spell.getControllerId())) {
condition = true;
if (spell != null && game.getOpponents(perm.getControllerId()).contains(spell.getControllerId())) {
players.add(perm.getControllerId());
}
}
}
}
}
}
class CobraTrapAlternativeCost extends AlternativeCostImpl<Cost> {
public CobraTrapAlternativeCost() {
super("you may pay {G} rather than pay Cobra Trap's mana cost");
this.add(new ColoredManaCost(ColoredManaSymbol.G));
}
public CobraTrapAlternativeCost(final CobraTrapAlternativeCost cost) {
super(cost);
}
@Override
public CobraTrapAlternativeCost copy() {
return new CobraTrapAlternativeCost(this);
public void reset() {
super.reset();
players.clear();
}
@Override
public boolean isAvailable(Game game, Ability source) {
Watcher watcher = game.getState().getWatchers().get("noncreature permanent destroyed", source.getControllerId());
if (watcher != null && watcher.conditionMet()) {
return true;
}
return false;
}
@Override
public String getText() {
return "If a noncreature permanent under your control was destroyed this turn by a spell or ability an opponent controlled, you may pay {G} rather than pay Cobra Trap's mana cost";
public boolean conditionMet(UUID playerId) {
return players.contains(playerId);
}
}

View file

@ -82,7 +82,7 @@ class WhiplashTrapCondition implements Condition {
@Override
public boolean apply(Game game, Ability source) {
PermanentsEnteredBattlefieldWatcher watcher = (PermanentsEnteredBattlefieldWatcher) game.getState().getWatchers().get(PermanentsEnteredBattlefieldWatcher.BASIC_KEY);
PermanentsEnteredBattlefieldWatcher watcher = (PermanentsEnteredBattlefieldWatcher) game.getState().getWatchers().get(PermanentsEnteredBattlefieldWatcher.class.getName());
if (watcher != null) {
for (UUID opponentId : game.getOpponents(source.getControllerId())) {
List<Permanent> permanents = watcher.getThisTurnEnteringPermanents(opponentId);