-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathConditionalEffectSpell.java
67 lines (59 loc) · 2.62 KB
/
ConditionalEffectSpell.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package net.demilich.metastone.game.spells;
import net.demilich.metastone.game.GameContext;
import net.demilich.metastone.game.Player;
import net.demilich.metastone.game.entities.Entity;
import net.demilich.metastone.game.spells.desc.SpellArg;
import net.demilich.metastone.game.spells.desc.SpellDesc;
import net.demilich.metastone.game.spells.desc.condition.Condition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Casts {@link SpellArg#SPELL1} and evaluates the {@link SpellArg#CONDITION}: if it is fulfilled, casts {@link
* SpellArg#SPELL2}. When {@link SpellArg#EXCLUSIVE} is {@code true}, behaves like an {@link EitherOrSpell}.
* <p>
* Use this spell to evaluate a condition after {@link SpellArg#SPELL1} is evaluated.
* <p>
* For example, "Deal 3 damage. If the target dies, draw a card.":
* <pre>
* "spell": {
* "class": "ConditionalEffectSpell",
* "condition": {
* "class": "IsDeadCondition"
* },
* "spell1": {
* "class": "DamageSpell",
* "value": 3
* },
* "spell2": {
* "class": "DrawCardSpell"
* }
* }
* </pre>
*
* @see EitherOrSpell for a spell that does one thing when true or another when false (i.e., this spell with {@link
* SpellArg#EXCLUSIVE} set to true.
* @see ConditionalSpell for a spell that executes its subspell only if its condition is met.
*/
public class ConditionalEffectSpell extends Spell {
private static Logger logger = LoggerFactory.getLogger(ConditionalEffectSpell.class);
protected boolean isConditionFulfilled(GameContext context, Player player, SpellDesc desc, Entity source, Entity target) {
Condition condition = (Condition) desc.get(SpellArg.CONDITION);
return condition.isFulfilled(context, player, source, target);
}
@Override
protected void onCast(GameContext context, Player player, SpellDesc desc, Entity source, Entity target) {
checkArguments(logger, context, source, desc, SpellArg.SPELL1, SpellArg.SPELL2, SpellArg.CONDITION, SpellArg.EXCLUSIVE, SpellArg.VALUE, SpellArg.SECONDARY_VALUE);
boolean exclusive = desc.getBool(SpellArg.EXCLUSIVE);
SpellDesc primarySpell = (SpellDesc) desc.get(SpellArg.SPELL1);
SpellDesc secondarySpell = (SpellDesc) desc.get(SpellArg.SPELL2);
if (exclusive) {
SpellUtils.castChildSpell(context, player, isConditionFulfilled(context, player, desc, source, target) ? secondarySpell : primarySpell,
source, target);
} else {
SpellUtils.castChildSpell(context, player, primarySpell, source, target);
if (isConditionFulfilled(context, player, desc, source, target)) {
SpellUtils.castChildSpell(context, player, secondarySpell, source, target);
}
}
}
}