Refactor damage with power effect, added checking for wrong targets;

This commit is contained in:
Oleg Agafonov 2020-01-13 13:49:40 +04:00
parent 62530b3880
commit 4a004a27b3
16 changed files with 212 additions and 131 deletions

View file

@ -0,0 +1,73 @@
package mage.abilities.effects.common;
import mage.abilities.Ability;
import mage.abilities.Mode;
import mage.abilities.effects.OneShotEffect;
import mage.constants.Outcome;
import mage.game.Game;
import mage.game.permanent.Permanent;
import mage.players.Player;
/**
* @author JayDi85
*/
public class DamageWithPowerFromOneToAnotherTargetEffect extends OneShotEffect {
String firstTargetName;
public DamageWithPowerFromOneToAnotherTargetEffect() {
this((String) null);
}
public DamageWithPowerFromOneToAnotherTargetEffect(String firstTargetName) {
super(Outcome.Damage);
this.firstTargetName = firstTargetName;
}
public DamageWithPowerFromOneToAnotherTargetEffect(final DamageWithPowerFromOneToAnotherTargetEffect effect) {
super(effect);
this.firstTargetName = effect.firstTargetName;
}
@Override
public boolean apply(Game game, Ability source) {
if (source.getTargets().size() != 2) {
throw new IllegalStateException("It must have two targets, but found " + source.getTargets().size());
}
Permanent myPermanent = game.getPermanentOrLKIBattlefield(getTargetPointer().getFirst(game, source));
Permanent anotherPermanent = game.getPermanent(source.getTargets().get(1).getFirstTarget());
Player anotherPlayer = game.getPlayer(source.getTargets().get(1).getFirstTarget());
if (myPermanent != null && anotherPermanent != null) {
anotherPermanent.damage(myPermanent.getPower().getValue(), myPermanent.getId(), game, false, true);
return true;
} else if (myPermanent != null && anotherPlayer != null) {
anotherPlayer.damage(myPermanent.getPower().getValue(), myPermanent.getId(), game, false, true);
return true;
}
return false;
}
@Override
public DamageWithPowerFromOneToAnotherTargetEffect copy() {
return new DamageWithPowerFromOneToAnotherTargetEffect(this);
}
@Override
public String getText(Mode mode) {
if (staticText != null && !staticText.isEmpty()) {
return staticText;
}
if (mode.getTargets().size() != 2) {
throw new IllegalStateException("It must have two targets, but found " + mode.getTargets().size());
}
// Target creature you control deals damage equal to its power to target creature you don't control
String sb = (this.firstTargetName != null ? this.firstTargetName : "Target " + mode.getTargets().get(0).getTargetName()) +
" deals damage equal to its power to target " +
mode.getTargets().get(1).getTargetName();
return sb;
}
}

View file

@ -0,0 +1,70 @@
package mage.abilities.effects.common;
import mage.abilities.Ability;
import mage.abilities.Mode;
import mage.abilities.effects.OneShotEffect;
import mage.constants.Outcome;
import mage.game.Game;
import mage.game.permanent.Permanent;
import mage.players.Player;
/**
* @author JayDi85
*/
public class DamageWithPowerFromSourceToAnotherTargetEffect extends OneShotEffect {
String sourceTargetName;
public DamageWithPowerFromSourceToAnotherTargetEffect() {
this("It");
}
public DamageWithPowerFromSourceToAnotherTargetEffect(String sourceTargetName) {
super(Outcome.Damage);
this.sourceTargetName = sourceTargetName;
}
public DamageWithPowerFromSourceToAnotherTargetEffect(final DamageWithPowerFromSourceToAnotherTargetEffect effect) {
super(effect);
this.sourceTargetName = effect.sourceTargetName;
}
@Override
public boolean apply(Game game, Ability source) {
if (source.getTargets().size() != 1) {
throw new IllegalStateException("It must have one target, but found " + source.getTargets().size());
}
Permanent myPermanent = game.getPermanentOrLKIBattlefield(source.getSourceId());
Permanent anotherPermanent = game.getPermanent(source.getTargets().get(0).getFirstTarget());
Player anotherPlayer = game.getPlayer(source.getTargets().get(0).getFirstTarget());
if (myPermanent != null && anotherPermanent != null) {
anotherPermanent.damage(myPermanent.getPower().getValue(), myPermanent.getId(), game, false, true);
return true;
} else if (myPermanent != null && anotherPlayer != null) {
anotherPlayer.damage(myPermanent.getPower().getValue(), myPermanent.getId(), game, false, true);
return true;
}
return false;
}
@Override
public DamageWithPowerFromSourceToAnotherTargetEffect copy() {
return new DamageWithPowerFromSourceToAnotherTargetEffect(this);
}
@Override
public String getText(Mode mode) {
if (staticText != null && !staticText.isEmpty()) {
return staticText;
}
if (mode.getTargets().size() != 1) {
throw new IllegalStateException("It must have one targets, but found " + mode.getTargets().size());
}
// It deals damage equal to its power to target creature you don't control
return this.sourceTargetName + " deals damage equal to its power to target " + mode.getTargets().get(0).getTargetName();
}
}

View file

@ -1,45 +0,0 @@
package mage.abilities.effects.common;
import mage.abilities.Ability;
import mage.abilities.effects.OneShotEffect;
import mage.constants.Outcome;
import mage.game.Game;
import mage.game.permanent.Permanent;
import mage.players.Player;
/**
*
* @author Styxo
*/
public class DamageWithPowerTargetEffect extends OneShotEffect {
public DamageWithPowerTargetEffect() {
super(Outcome.Damage);
staticText = "Target creature you control deals damage equal to its power to target creature you don't control";
}
public DamageWithPowerTargetEffect(final DamageWithPowerTargetEffect effect) {
super(effect);
}
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
Permanent controlledCreature = game.getPermanentOrLKIBattlefield(getTargetPointer().getFirst(game, source));
Permanent targetCreature = game.getPermanent(source.getTargets().get(1).getFirstTarget());
if (controller != null) {
if (targetCreature != null && controlledCreature != null) {
targetCreature.damage(controlledCreature.getPower().getValue(), controlledCreature.getId(), game, false, true);
}
return true;
}
return false;
}
@Override
public DamageWithPowerTargetEffect copy() {
return new DamageWithPowerTargetEffect(this);
}
}

View file

@ -144,7 +144,9 @@ public final class RateCard {
return 1;
}
}
if (effect instanceof FightTargetsEffect || effect instanceof DamageWithPowerTargetEffect) {
if (effect instanceof FightTargetsEffect
|| effect instanceof DamageWithPowerFromOneToAnotherTargetEffect
|| effect instanceof DamageWithPowerFromSourceToAnotherTargetEffect) {
return 1;
}
if (effect.getOutcome() == Outcome.Damage || effect instanceof DamageTargetEffect) {