-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathChooseOneSpell.java
133 lines (120 loc) · 4.26 KB
/
ChooseOneSpell.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package net.demilich.metastone.game.spells;
import net.demilich.metastone.game.GameContext;
import net.demilich.metastone.game.Player;
import net.demilich.metastone.game.cards.Card;
import net.demilich.metastone.game.cards.CardArrayList;
import net.demilich.metastone.game.cards.CardList;
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;
/**
* Provides a choice between {@link SpellArg#SPELL1} and {@link SpellArg#SPELL2}, using the {@link SpellArg#NAME} and
* {@link SpellArg#DESCRIPTION} in those spells to generate the choice cards.
* <p>
* The sub-spells should be {@link ChooseOneOptionSpell} spells.
* <p>
* The {@link SpellArg#CONDITION}, if specified, casts {@link SpellArg#SPELL} (if specified) instead of giving choices
* (or does nothing).
* <p>
* For <b>example,</b> this text gives the player two choices if their player has more than 1 imbue charge:
* <pre>
* {@code
* {
* "class": "ChooseOneSpell",
* "condition": {
* "class": "AttributeCondition",
* "target": "FRIENDLY_PLAYER",
* "attribute": "IMBUE",
* "value": 1,
* "operation": "GREATER_OR_EQUAL"
* },
* "spell1": {
* "class": "ChooseOneOptionSpell",
* "name": "Normal",
* "description": "Don't Imbue."
* },
* "spell2": {
* "class": "ChooseOneOptionSpell",
* "name": "Imbue",
* "description": "Give this unit Spellpower +1.",
* "spells": [
* {
* "class": "ModifyAttributeSpell",
* "value": 1,
* "attribute": "SPELL_DAMAGE",
* "target": "SELF"
* },
* {
* "class": "ModifyAttributeSpell",
* "value": -1,
* "attribute": "IMBUE",
* "target": "FRIENDLY_PLAYER"
* }
* ]
* }
* }
* }
* </pre>
*/
public class ChooseOneSpell extends Spell {
@Override
protected void onCast(GameContext context, Player player, SpellDesc desc, Entity source, Entity target) {
if (desc.containsKey(SpellArg.CONDITION)) {
Condition condition = (Condition) desc.get(SpellArg.CONDITION);
if (!condition.isFulfilled(context, player, source, target)) {
if (desc.containsKey(SpellArg.SPELL)) {
SpellUtils.castChildSpell(context, player, desc.getSpell(), source, target);
}
return;
}
}
var spell1 = (SpellDesc) desc.get(SpellArg.SPELL1);
var spell2 = (SpellDesc) desc.get(SpellArg.SPELL2);
var card1 = getTempCard(context, spell1, source.getSourceCard());
var card2 = getTempCard(context, spell2, source.getSourceCard());
CardList cards = new CardArrayList();
if (spell1.containsKey(SpellArg.CONDITION)) {
var condition = (Condition) spell1.get(SpellArg.CONDITION);
if (condition.isFulfilled(context, player, source, target)) {
cards.add(card1);
}
} else {
cards.add(card1);
}
if (spell2.containsKey(SpellArg.CONDITION)) {
var condition = (Condition) spell2.get(SpellArg.CONDITION);
if (condition.isFulfilled(context, player, source, target)) {
cards.add(card2);
}
} else {
cards.add(card2);
}
cards.removeIf(card -> shouldRemoveCard(card, player, context));
if (cards.isEmpty()) {
if (desc.containsKey(SpellArg.SPELL)) {
SpellUtils.castChildSpell(context, player, desc.getSpell(), source, target);
}
return;
}
var clone = desc.clone();
clone.put(SpellArg.SPELL, NullSpell.create());
var discoverAction = SpellUtils.discoverCard(context, player, source, clone, cards);
SpellUtils.castChildSpell(context, player, discoverAction.getCard().getSpell(), source, target);
}
public boolean shouldRemoveCard(Card card, Player player, GameContext context) {
return false;
}
/**
* Generates a temporary card. Used by {@link ChooseOneOptionSpell} to actually generate the card definitions for the
* choice cards.
*
* @param context
* @param spellDesc
* @param sourceCard
* @return
*/
public Card getTempCard(GameContext context, SpellDesc spellDesc, Card sourceCard) {
return ChooseOneOptionSpell.getTempCard(context, spellDesc, sourceCard, "option_");
}
}