magarena/src/magic/model/action/DestroyAction.java

98 lines
3.3 KiB
Java
Raw Normal View History

2013-04-12 19:32:25 -07:00
package magic.model.action;
2020-01-15 12:02:42 -08:00
import java.util.ArrayList;
import java.util.Collection;
2013-04-12 19:32:25 -07:00
import magic.model.MagicAbility;
import magic.model.MagicGame;
import magic.model.MagicLocationType;
2020-01-15 12:02:42 -08:00
import magic.model.MagicMessage;
2013-04-12 19:32:25 -07:00
import magic.model.MagicPermanent;
import magic.model.MagicPermanentState;
public class DestroyAction extends MagicAction {
2013-06-23 18:33:35 -07:00
private final Collection<MagicPermanent> targets = new ArrayList<>();
private int numDestroyed = 0;
2013-06-23 18:33:35 -07:00
public DestroyAction(final MagicPermanent permanent) {
2013-04-12 19:32:25 -07:00
this.targets.add(permanent);
}
2013-06-23 18:33:35 -07:00
public DestroyAction(final Collection<MagicPermanent> targets) {
2013-04-12 19:32:25 -07:00
this.targets.addAll(targets);
}
2013-06-23 18:33:35 -07:00
2013-04-12 19:32:25 -07:00
@Override
public void doAction(final MagicGame game) {
final Collection<MagicPermanent> toBeDestroyed = new ArrayList<>();
2013-04-12 19:32:25 -07:00
for (final MagicPermanent permanent : targets) {
boolean destroy = true;
2013-06-23 18:33:35 -07:00
2013-04-12 19:32:25 -07:00
// Indestructible
if (destroy && permanent.hasAbility(MagicAbility.Indestructible)) {
destroy = false;
}
2013-06-23 18:33:35 -07:00
2013-04-12 19:32:25 -07:00
// Regeneration
if (destroy && permanent.isRegenerated()) {
game.logAppendMessage(
permanent.getController(),
MagicMessage.format("%s is regenerated.", permanent)
);
2015-04-14 07:54:29 -07:00
game.doAction(new TapAction(permanent));
game.doAction(new RemoveAllDamageAction(permanent));
game.doAction(new RemoveFromCombatAction(permanent));
game.doAction(ChangeStateAction.Clear(permanent,MagicPermanentState.Regenerated));
2013-04-12 19:32:25 -07:00
destroy = false;
2013-06-23 18:33:35 -07:00
}
2013-04-12 19:32:25 -07:00
// Totem armor
if (destroy && permanent.isEnchanted()) {
for (final MagicPermanent aura : permanent.getAuraPermanents()) {
if (aura.hasAbility(MagicAbility.TotemArmor)) {
game.logAppendMessage(
permanent.getController(),
MagicMessage.format("Remove all damage from %s.", permanent)
);
game.doAction(new RemoveAllDamageAction(permanent));
2013-04-12 19:32:25 -07:00
toBeDestroyed.add(aura);
destroy = false;
//Only the first aura with totem armor will be
//destroyed. If there are multiple auras with totem
//armor, player can choose the one to be destroyed, but
//this is not implemented
break;
}
}
}
2013-06-23 18:33:35 -07:00
2013-04-12 19:32:25 -07:00
if (destroy) {
toBeDestroyed.add(permanent);
}
}
2013-06-23 18:33:35 -07:00
numDestroyed = toBeDestroyed.size();
2013-04-12 19:32:25 -07:00
for (final MagicPermanent permanent : toBeDestroyed) {
game.logAppendMessage(
permanent.getController(),
MagicMessage.format("%s is destroyed.", permanent)
);
2013-06-23 18:33:35 -07:00
}
game.doAction(new RemoveAllFromPlayAction(toBeDestroyed, MagicLocationType.Graveyard));
2013-04-12 19:32:25 -07:00
}
public int getNumDestroyed() {
return numDestroyed;
}
public boolean isDestroyed() {
return numDestroyed == 1;
}
2013-04-12 19:32:25 -07:00
@Override
public void undoAction(final MagicGame game) {}
}