public abstract class DamageEvent extends ReplaceableEvent {
private MagicObject source;
private int ammount;
private boolean combat, preventable;
public DamageEvent(MagicObject affected, MagicObject source, int ammount, boolean combat, boolean preventable) {
this(you(ofInstance(affected)), source, ammount, combat, preventable);
}
public DamageEvent(Player affected, MagicObject source, int ammount, boolean combat, boolean preventable) {
this(ofInstance(affected), source, ammount, combat, preventable);
}
public DamageEvent(Supplier
super(affected);
this.source = source;
this.ammount = ammount;
this.combat = combat;
this.preventable = preventable;
}
public MagicObject getSource() {
return source;
}
/**
* This method can be used by replacement effects that prevent or increase damage.
*/
public void setAmmount(int ammount) {
this.ammount = ammount;
}
public int getAmmount() {
return ammount;
}
public boolean isCombatDamage() {
return combat;
}
public boolean isPreventable() {
return preventable;
}
}
public class DamagePlayerEvent extends DamageEvent {
private Player p;
public DamagePlayerEvent(Player p, MagicObject source, int ammount, boolean combat, boolean preventable) {
super(p, source, ammount, combat, preventable);
this.p = p;
}
public Player getPlayer() {
return p;
}
@Override
protected boolean execute0() {
CompoundEdit ed = new CompoundEdit(getGame(), true, "Deal damage");
//118.3a. Damage dealt to a player causes that player to lose that much life.
getPlayer().getLifeTotal().loseLife(getAmmount());
ed.end();
return true;
}
}
public class DamagePermanentEvent extends DamageEvent {
private static final Predicate
card(or(CREATURE, PLANESWALKER)));
private static final Predicate
//TODO implement wither and lifelink
private static final Predicate
private static final Predicate
private CardObject permanent;
public DamagePermanentEvent(CardObject affected, MagicObject source, int ammount, boolean combat, boolean preventable) {
super(affected, source, ammount, combat, preventable);
if(!legalAffected.apply(affected)) throw new IllegalArgumentException(
"affected must be a creature or planeswalker permanent: " + affected);
permanent = affected;
}
public CardObject getPermanent() {
return permanent;
}
@Override
protected boolean execute0() {
CompoundEdit ed = new CompoundEdit(getGame(), true, "Deal damage");
//118.3b. Damage dealt to a planeswalker causes that many loyalty counters to be removed from that
// planeswalker.
if(isPlaneswalker.apply(getPermanent())) {
//TODO remove counters
}
//118.3c. Damage dealt to a creature by a source with wither causes that many -1/-1 counters to be put on
// that creature.
else if(hasWither.apply(getSource())) {
//TODO add counters
}
//118.3d. Damage dealt to a creature by a source without wither causes that much damage to be marked on
// that creature.
else {
getPermanent().setMarkedDamage(getPermanent().getMarkedDamage() + getAmmount());
}
//118.3e. Damage dealt to an object or player by a source with lifelink causes that source's controller to
// gain that much life, in addition to the damage's other results.
if(hasLifelink.apply(getSource())) {
getSource().getController().getLifeTotal().gainLife(getAmmount());
}
ed.end();
return true;
}
}
I don't really like this code, because it has the effects of damage hard coded, but I think it's fine for now. extracting these effects later if it's necessary shouldn't be too hard, as the different effects of damage will only be mentioned here, so I can safely delay this task.