-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathChangeHeroSpell.java
More file actions
103 lines (94 loc) · 4.15 KB
/
ChangeHeroSpell.java
File metadata and controls
103 lines (94 loc) · 4.15 KB
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
package net.demilich.metastone.game.spells;
import com.hiddenswitch.spellsource.rpc.Spellsource.CardTypeMessage.CardType;
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.CardList;
import net.demilich.metastone.game.entities.Entity;
import net.demilich.metastone.game.entities.heroes.Hero;
import net.demilich.metastone.game.spells.desc.SpellArg;
import net.demilich.metastone.game.spells.desc.SpellDesc;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.Map;
/**
* Changes the hero of {@link SpellArg#TARGET_PLAYER} to the specified hero card ID in {@link SpellArg#CARD}. If {@link
* SpellArg#CARDS} or {@link SpellArg#CARD_FILTER} or {@link SpellArg#CARD_SOURCE} are specified, a random hero card
* will be chosen from the lists of cards.
* <p>
* This spell activates the {@link net.demilich.metastone.game.cards.desc.CardDesc#battlecry} by default. To disable it,
* set {@link SpellArg#EXCLUSIVE} to {@code true}.
* <p>
* For <b>example,</b> to turn the casting player's hero into Ragnaros:
* <pre>
* {
* "class": "ChangeHeroSpell",
* "card": "hero_ragnaros",
* "targetPlayer": "SELF"
* }
* </pre>
* Casts the {@link SpellArg#SPELL} sub-spell with the {@link net.demilich.metastone.game.targeting.EntityReference#OUTPUT}
* set to the new hero.
*/
public class ChangeHeroSpell extends Spell {
private static Logger logger = LoggerFactory.getLogger(ChangeHeroSpell.class);
/**
* Changes the casting player's hero to the specified card ID.
*
* @param heroCardId A hero card ({@link CardType#HERO}.
* @return The spell
*/
public static SpellDesc create(String heroCardId) {
Map<SpellArg, Object> arguments = new SpellDesc(ChangeHeroSpell.class);
arguments.put(SpellArg.CARD, heroCardId);
return new SpellDesc(arguments);
}
/**
* Changes the specified player's hero the specified card ID.
*
* @param player The player whose hero should be changed.
* @param heroCardId A hero card ({@link CardType#HERO}.
* @return The spell
*/
public static SpellDesc create(TargetPlayer player, String heroCardId) {
Map<SpellArg, Object> arguments = new SpellDesc(ChangeHeroSpell.class);
arguments.put(SpellArg.TARGET_PLAYER, player);
arguments.put(SpellArg.CARD, heroCardId);
return new SpellDesc(arguments);
}
/**
* Changes the specified player's hero with the specified card ID, resolving the battlecry if specified.
*
* @param player the player
* @param heroCardId the hero card
* @param resolveBattlecry {@code true} if the hero card should have its battlecry resolved
* @return the spell
*/
public static SpellDesc create(TargetPlayer player, String heroCardId, boolean resolveBattlecry) {
Map<SpellArg, Object> arguments = new SpellDesc(ChangeHeroSpell.class);
arguments.put(SpellArg.TARGET_PLAYER, player);
arguments.put(SpellArg.CARD, heroCardId);
arguments.put(SpellArg.EXCLUSIVE, !resolveBattlecry);
return new SpellDesc(arguments);
}
@Override
protected void onCast(GameContext context, Player player, SpellDesc desc, Entity source, Entity target) {
checkArguments(logger, context, source, desc, SpellArg.CARD, SpellArg.CARDS, SpellArg.CARD_FILTER, SpellArg.CARD_SOURCE, SpellArg.SPELL, SpellArg.EXCLUSIVE);
CardList heroCards = SpellUtils.getCards(context, player, target, source, desc);
if (heroCards.size() == 0) {
logger.error("onCast {} {}: Requires hero card ID, none specified.", context.getGameId(), source);
return;
}
Card heroCard = heroCards.get(0);
if (heroCard.getCardType() != CardType.HERO) {
throw new RuntimeException("onCast " + context.getGameId() + " " + source + ": Expected HERO card but got " + heroCard.getCardType() + " (" + heroCard.getCardId() + ")");
}
Hero hero = heroCard.hero();
context.getLogic().changeHero(player, source, hero, !(boolean) desc.getOrDefault(SpellArg.EXCLUSIVE, false));
List<SpellDesc> spellDescs = desc.subSpells(0);
for (SpellDesc subSpell : spellDescs) {
SpellUtils.castChildSpell(context, player, subSpell, source, target, hero);
}
}
}