Skip to content

Commit

Permalink
added DenyActionForName, and denying ATTACK for some creatures
Browse files Browse the repository at this point in the history
  • Loading branch information
Zomis committed Jan 30, 2015
1 parent 59d28f9 commit 8638081
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 3 deletions.
@@ -0,0 +1,41 @@
package net.zomis.cardshifter.ecs.usage;

import com.cardshifter.modapi.actions.ActionAllowedCheckEvent;
import com.cardshifter.modapi.attributes.AttributeRetriever;
import com.cardshifter.modapi.attributes.Attributes;
import com.cardshifter.modapi.base.ECSGame;
import com.cardshifter.modapi.base.ECSSystem;

import java.util.HashSet;
import java.util.Set;

/**
* A system that denies a specific action on all entities with a specific name
*/
public class DenyActionForNames implements ECSSystem {

private final HashSet<String> names;
private final String action;

private AttributeRetriever name = AttributeRetriever.forAttribute(Attributes.NAME);

public DenyActionForNames(String action, Set<String> deniedNames) {
this.names = new HashSet<>(deniedNames);
this.action = action;
}

@Override
public void startGame(ECSGame game) {
game.getEvents().registerHandlerAfter(this, ActionAllowedCheckEvent.class, this::allowCheck);
}

private void allowCheck(ActionAllowedCheckEvent event) {
if (!action.equals(event.getAction().getName())) {
return;
}
if (names.contains(name.getOrDefault(event.getEntity(), ""))) {
event.setAllowed(false);
}
}

}
@@ -1,11 +1,13 @@
package net.zomis.cardshifter.ecs.usage;

import java.util.HashSet;
import java.util.Map.Entry;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.ToIntFunction;
import java.util.function.UnaryOperator;

import com.cardshifter.modapi.attributes.AttributeRetriever;
import com.cardshifter.modapi.phase.*;
import com.cardshifter.modapi.players.Players;
import net.zomis.cardshifter.ecs.config.ConfigComponent;
Expand Down Expand Up @@ -98,6 +100,9 @@ public void addCards(ZoneComponent zone) {
Consumer<Entity> noSickness = e -> sickness.resFor(e).set(0);
BiFunction<Entity, Integer, Integer> restoreHealth = (e, value) ->
Math.max(Math.min(healthMax.getFor(e) - health.getFor(e), value), 0);
AttributeRetriever name = AttributeRetriever.forAttribute(Attributes.NAME);
Set<String> noAttackNames = new HashSet<>();
Consumer<Entity> noAttack = e -> noAttackNames.add(name.getFor(e));

// Mechs (ManaCost, zone, Attack, Health, "Type", ScrapValue, "CardName")
createCreature(0, zone, 0, 1, "Mech", 3, "Spareparts");
Expand All @@ -108,7 +113,7 @@ public void addCards(ZoneComponent zone) {
createCreature(3, zone, 4, 2, "Mech", 2, "Assassinatrix");
createCreature(3, zone, 2, 4, "Mech", 2, "Fortimech");
createCreature(3, zone, 5, 1, "Mech", 2, "Scout Mech").apply(noSickness);
createCreature(3, zone, 0, 5, "Mech", 3, "Supply Mech");
createCreature(3, zone, 0, 5, "Mech", 3, "Supply Mech").apply(noAttack);
createCreature(5, zone, 5, 3, "Mech", 3, "Modleg Ambusher").apply(noSickness);
createCreature(5, zone, 3, 6, "Mech", 3, "Heavy Mech");
createCreature(5, zone, 4, 4, "Mech", 3, "Waste Runner");
Expand All @@ -128,7 +133,7 @@ public void addCards(ZoneComponent zone) {
createCreature(6, zone, 3, 5, "Bio", 0, "Cyberpimp");
createCreature(7, zone, 5, 5, "Bio", 0, "Cyborg");
createCreature(8, zone, 6, 6, "Bio", 0, "Web Boss");
createCreature(8, zone, 2, 6, "Bio", 0, "Inside Man");
createCreature(8, zone, 2, 6, "Bio", 0, "Inside Man").apply(noAttack);

// Enchantments: (zone, AttackModify, HealthModify, ScrapCost, "CardName")
createEnchantment(zone, 2, 0, 1, "Bionic Arms");
Expand All @@ -140,6 +145,9 @@ public void addCards(ZoneComponent zone) {
createEnchantment(zone, 0, 3, 2, "Exoskeleton");
createEnchantment(zone, 2, 2, 3, "Artificial Intelligence Implants");
createEnchantment(zone, 3, 3, 5, "Full-body Cybernetics Upgrade");

ECSGame game = zone.getComponentEntity().getGame();
game.addSystem(new DenyActionForNames(PhrancisGame.ATTACK_ACTION, noAttackNames));
}

public Entity createTargetSpell(String name, ZoneComponent zone, int manaCost, int scrapCost, EffectComponent effect, FilterComponent filter) {
Expand Down
Expand Up @@ -39,6 +39,7 @@ public class PhrancisTest extends GameTest {
private final ResourceRetriever manaCost = ResourceRetriever.forResource(PhrancisResources.MANA_COST);
private final ResourceRetriever health = ResourceRetriever.forResource(PhrancisResources.HEALTH);
private final ResourceRetriever attackPoints = ResourceRetriever.forResource(PhrancisResources.ATTACK_AVAILABLE);
private final ResourceRetriever attack = ResourceRetriever.forResource(PhrancisResources.ATTACK);
private final ResourceRetriever scrapCost = ResourceRetriever.forResource(PhrancisResources.SCRAP_COST);
private final ResourceRetriever scrap = ResourceRetriever.forResource(PhrancisResources.SCRAP);

Expand Down Expand Up @@ -71,12 +72,27 @@ protected void setupGame(ECSGame game) {
addCard(deckConf, isCreature.and(manaCost(1)));
addCard(deckConf, isCreatureType("Bio").and(health(4)));
addCard(deckConf, hasName("Field Medic"));
addCard(deckConf, hasName("Supply Mech"));
addCard(deckConf, e -> scrapCost.getFor(e) == 1 && health.getFor(e) == 1);
}

mod.setupGame(game);
}

@Test
public void noAttackCard() {
while (mana.getFor(currentPlayer()) < 5) {
nextPhase();
}

Entity entity = cardToHand(hasName("Supply Mech"));
useAction(entity, PhrancisGame.PLAY_ACTION);
nextPhase();
nextPhase();
attack.resFor(entity).set(10);
useFail(entity, PhrancisGame.ATTACK_ACTION);
}

@Test
public void healEndOfTurn() {
while (mana.getFor(currentPlayer()) < 5) {
Expand Down

0 comments on commit 8638081

Please sign in to comment.