-
Notifications
You must be signed in to change notification settings - Fork 19
/
JSGame.js
257 lines (222 loc) · 10.1 KB
/
JSGame.js
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
load('keywords.js');
load('keywords-creatures.js');
load('keywords-enchantments.js');
load('keywords-systems.js');
load('keyword-noattack.js');
function createResource(name) {
return new com.cardshifter.modapi.resources.ECSResourceDefault(name);
}
var ATTACK = createResource("ATTACK");
var HEALTH = createResource("HEALTH");
var MAX_HEALTH = createResource("MAX_HEALTH");
var ATTACK_AVAILABLE = createResource("ATTACK_AVAILABLE");
var DENY_COUNTERATTACK = createResource("DENY_COUNTERATTACK");
var MANA = createResource("MANA");
var MANA_COST = createResource("MANA_COST");
var MANA_MAX = createResource("MANA_MAX");
var SCRAP = createResource("SCRAP");
var SCRAP_COST = createResource("SCRAP_COST");
var SICKNESS = createResource("SICKNESS");
var TAUNT = createResource("TAUNT");
var PLAY_ACTION = "Play";
var ENCHANT_ACTION = "Enchant";
var ATTACK_ACTION = "Attack";
var SCRAP_ACTION = "Scrap";
var END_TURN_ACTION = "End Turn";
var USE_ACTION = "Use";
/**
* Declare game configuration
* @param {Object} game - Game configuration data
*/
function declareConfiguration(game) {
var neutral = game.newEntity();
var zone = new com.cardshifter.modapi.cards.ZoneComponent(neutral, "Cards");
neutral.addComponent(zone);
addCards(game, zone);
/** Parameters related to DeckConfigFactory */
var maxCardsPerType = 5;
var minSize = 10;
var maxSize = 10;
/**
* Create playerComponent 0 & 1, i.e., Player1 & Player2
* Config a deck for each player
*/
for (var i = 0; i < 2; i++) {
var entity = game.newEntity();
var playerComponent = new com.cardshifter.modapi.base.PlayerComponent(i, "Player" + (i+1));
entity.addComponent(playerComponent);
var config = Java.type("net.zomis.cardshifter.ecs.config.DeckConfigFactory").create(minSize, maxSize, zone.getCards(), maxCardsPerType);
var ConfigComponent = Java.type("net.zomis.cardshifter.ecs.config.ConfigComponent");
entity.addComponent(new ConfigComponent().addConfig("Deck", config));
}
}
/**
* Contains the library of cards available for a Cardshifter game in JSON format.
* Note: Please use the 'effects' wrapper only for properties that change the *behavior* of a card.
* Keep regular/arbitrary attributes outside of effects to keep things organized.
*
* @module CardData
*/
function addCards(game, zone) {
applyCardKeywords(game, zone, {
cards: [
/** MECH CREATURES */
{
name: "Spareparts",
flavor: "Cobbled together from whatever was lying around at the time.",
creature: "Mech",
manaCost: 0,
health: 1,
attack: 0,
scrap: 3,
sickness: 0,
noAttack: true
},
/** BIO CREATURES */
{
name: "Longshot",
flavor: "Eyes and reflexes augmented for maximum deadliness.",
creature: "Bio",
manaCost: 3,
health: 1,
attack: 3,
denyCounterAttack: 1
},
/** ENCHANTMENTS */
{
name: "Bionic Arms",
flavor: "These arms will give strength to even the most puny individual.",
enchantment: true,
scrapCost: 1,
addAttack: 2,
addHealth: 0
}
]
});
}
function ownedBattlefieldCreatures(entity) {
var Cards = Java.type("com.cardshifter.modapi.cards.Cards");
return entity.hasComponent(com.cardshifter.modapi.base.CreatureTypeComponent.class)
&& Cards.isOnZone(entity, com.cardshifter.modapi.cards.BattlefieldComponent.class)
&& Cards.isOwnedByCurrentPlayer(entity)
}
function playerSetup(game) {
var phaseController = new com.cardshifter.modapi.phase.PhaseController();
game.newEntity().addComponent(phaseController);
var players = com.cardshifter.modapi.players.Players.getPlayersInGame(game);
for (var i = 0; i < 2; i++) {
var playerIndex = i;
var player = players.get(i);
var playerPhase = new com.cardshifter.modapi.phase.Phase(player, "Main");
phaseController.addPhase(playerPhase);
var actions = new com.cardshifter.modapi.actions.ActionComponent();
player.addComponent(actions);
function isPhase(phase) {
return function (act) {
var check = phaseController.getCurrentPhase() == phase;
return phaseController.getCurrentPhase() == phase;
}
}
var endTurnAction = new com.cardshifter.modapi.actions.ECSAction(player, END_TURN_ACTION,
isPhase(playerPhase), function (act) {
phaseController.nextPhase();
});
actions.addAction(endTurnAction);
com.cardshifter.modapi.resources.ECSResourceMap.createFor(player)
.set(HEALTH, 30)
.set(MAX_HEALTH, 30)
.set(MANA, 0)
.set(SCRAP, 0);
var deck = new com.cardshifter.modapi.cards.DeckComponent(player);
var hand = new com.cardshifter.modapi.cards.HandComponent(player);
var battlefield = new com.cardshifter.modapi.cards.BattlefieldComponent(player);
player.addComponents(hand, deck, battlefield);
var ConfigComponent = Java.type("net.zomis.cardshifter.ecs.config.ConfigComponent");
var config = player.getComponent(ConfigComponent.class);
var deckConf = config.getConfig(com.cardshifter.api.config.DeckConfig.class);
if (deckConf.total() < deckConf.getMinSize()) {
deckConf.generateRandom();
}
setupDeck(deck, deckConf);
deck.shuffle();
}
}
function setupDeck(deck, deckConf) {
var game = deck.owner.game;
for each (var chosen in deckConf.chosen.entrySet()) {
var entityId = chosen.key;
var count = chosen.value;
for (var i = 0; i < count; i++) {
var existing = game.getEntity(entityId);
var copy = existing.copy();
deck.addOnBottom(copy);
}
}
}
function setupGame(game) {
playerSetup(game);
var LastPlayersStandingEndsGame = Java.type("net.zomis.cardshifter.ecs.usage.LastPlayersStandingEndsGame");
var EffectActionSystem = Java.type("net.zomis.cardshifter.ecs.effects.EffectActionSystem");
var ScrapSystem = Java.type("net.zomis.cardshifter.ecs.usage.ScrapSystem");
applySystems(game, [
{ gainResource: { res: MANA_MAX, value: 1, untilMax: 10 } },
{ restoreResources: { res: MANA, value: { res: MANA_MAX } } },
// Play
{ playFromHand: PLAY_ACTION },
{ playEntersBattlefield: PLAY_ACTION },
{ useCost: { action: PLAY_ACTION, res: MANA, value: { res: MANA_COST }, whoPays: "player" } },
// Scrap
new ScrapSystem(SCRAP, function (entity) {
return com.cardshifter.modapi.resources.Resources.getOrDefault(entity, ATTACK_AVAILABLE, 0) > 0
&& com.cardshifter.modapi.resources.Resources.getOrDefault(entity, SICKNESS, 1) == 0;
}),
// Enchant
{ playFromHand: ENCHANT_ACTION },
{ useCost: { action: ENCHANT_ACTION, res: SCRAP, value: { res: SCRAP_COST }, whoPays: "player" } },
new com.cardshifter.modapi.actions.enchant.EnchantTargetCreatureTypes("Bio"),
new com.cardshifter.modapi.actions.enchant.EnchantPerform(ATTACK, HEALTH, MAX_HEALTH),
// Spell
{ useCost: { action: USE_ACTION, res: MANA, value: { res: MANA_COST }, whoPays: "player" } },
{ useCost: { action: USE_ACTION, res: SCRAP, value: { res: SCRAP_COST }, whoPays: "player" } },
{ playFromHand: USE_ACTION },
new EffectActionSystem(USE_ACTION),
new EffectActionSystem(ENCHANT_ACTION),
new EffectActionSystem(PLAY_ACTION),
{ targetFilterSystem: USE_ACTION },
{ destroyAfterUse: USE_ACTION },
// Attack
new com.cardshifter.modapi.actions.attack.AttackOnBattlefield(),
new com.cardshifter.modapi.actions.attack.AttackTargetMinionsFirstThenPlayer(TAUNT),
new com.cardshifter.modapi.actions.attack.AttackSickness(SICKNESS),
{ useCost: { action: ATTACK_ACTION, res: ATTACK_AVAILABLE, value: 1, whoPays: "self" } },
new com.cardshifter.modapi.resources.RestoreResourcesToSystem(ownedBattlefieldCreatures, ATTACK_AVAILABLE,
function (entity) { return 1; }),
new com.cardshifter.modapi.resources.RestoreResourcesToSystem(ownedBattlefieldCreatures, SICKNESS,
function (entity) { return Math.max(0, SICKNESS.getFor(entity) - 1); }),
new com.cardshifter.modapi.actions.attack.TrampleSystem(HEALTH),
// Draw cards
{ startCards: 5 },
new com.cardshifter.modapi.cards.DrawCardAtBeginningOfTurnSystem(),
new com.cardshifter.modapi.cards.DamageConstantWhenOutOfCardsSystem(HEALTH, 1),
new com.cardshifter.modapi.cards.LimitedHandSizeSystem(10, function (card) { card.getCardToDraw().destroy() }),
// General setup
new com.cardshifter.modapi.cards.MulliganSingleCards(game),
new com.cardshifter.modapi.resources.GameOverIfNoHealth(HEALTH),
new LastPlayersStandingEndsGame(),
new com.cardshifter.modapi.cards.RemoveDeadEntityFromZoneSystem(),
new com.cardshifter.modapi.phase.PerformerMustBeCurrentPlayer(),
]);
var allowCounterAttackRes = com.cardshifter.modapi.resources.ResourceRetriever.forResource(DENY_COUNTERATTACK);
var allowCounterAttack = function (attacker, defender) {
return allowCounterAttackRes.getOrDefault(attacker, 0) == 0;
}
game.addSystem(new com.cardshifter.modapi.actions.attack.AttackDamageAccumulating(ATTACK, HEALTH, allowCounterAttack));
game.addSystem(new com.cardshifter.modapi.actions.attack.AttackDamageHealAtEndOfTurn(HEALTH, MAX_HEALTH));
var ApplyAfterAttack = Java.type("net.zomis.cardshifter.ecs.usage.ApplyAfterAttack");
game.addSystem(new ApplyAfterAttack(function (entity) {
return allowCounterAttackRes.getFor(entity) > 0;
}, function (entity) {
var sickness = com.cardshifter.modapi.resources.ResourceRetriever.forResource(SICKNESS);
sickness.resFor(entity).set(2)
}));
}