Skip to content

Commit

Permalink
Merge branch 'issue11' of github.com:Cardshifter/Cardshifter into dev…
Browse files Browse the repository at this point in the history
…elop
  • Loading branch information
Zomis committed Sep 18, 2014
2 parents ebc399c + bed9507 commit 21e6ae7
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package net.zomis.cardshifter.ecs.cards;

import java.util.function.Consumer;

import net.zomis.cardshifter.ecs.base.ECSGame;
import net.zomis.cardshifter.ecs.base.ECSSystem;

public class LimitedHandSizeSystem implements ECSSystem {

private final int limit;
private final Consumer<DrawCardEvent> actionWhenFull;

public LimitedHandSizeSystem(int limit, Consumer<DrawCardEvent> actionWhenFull) {
this.limit = limit;
this.actionWhenFull = actionWhenFull;
}

@Override
public void startGame(ECSGame game) {
game.getEvents().registerHandlerBefore(DrawCardEvent.class, this::drawCard);
}

private void drawCard(DrawCardEvent event) {
if (event.getToZone().size() >= limit) {
event.setCancelled(true);
actionWhenFull.accept(event);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import net.zomis.cardshifter.ecs.cards.DeckComponent;
import net.zomis.cardshifter.ecs.cards.DrawStartCards;
import net.zomis.cardshifter.ecs.cards.HandComponent;
import net.zomis.cardshifter.ecs.cards.LimitedHandSizeSystem;
import net.zomis.cardshifter.ecs.cards.RemoveDeadEntityFromZoneSystem;
import net.zomis.cardshifter.ecs.cards.ZoneComponent;
import net.zomis.cardshifter.ecs.components.CreatureTypeComponent;
Expand Down Expand Up @@ -144,7 +145,7 @@ public static ECSGame createGame() {
game.addSystem(new DrawCardAtBeginningOfTurnSystem());
game.addSystem(new DamageConstantWhenOutOfCardsSystem(PhrancisResources.HEALTH, 1));
// game.addSystem(new DamageIncreasingWhenOutOfCardsSystem());
// TODO: game.addSystem(new LimitedHandSizeSystem(10, card -> card.destroy()));
game.addSystem(new LimitedHandSizeSystem(10, card -> card.getCardToDraw().destroy()));
// game.addSystem(new RecreateDeckSystem());

// Initial setup
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.cardshifter.core.issues;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.List;

import net.zomis.cardshifter.ecs.base.ECSGame;
import net.zomis.cardshifter.ecs.base.Entity;
import net.zomis.cardshifter.ecs.cards.DeckComponent;
import net.zomis.cardshifter.ecs.cards.DrawStartCards;
import net.zomis.cardshifter.ecs.cards.HandComponent;
import net.zomis.cardshifter.ecs.cards.LimitedHandSizeSystem;

import org.junit.Test;

public class Issue11Test {

@Test
public void handLimit() {
ECSGame game = new ECSGame();
Entity owner = game.newEntity();
DeckComponent deck = new DeckComponent(owner);
HandComponent hand = new HandComponent(owner);
owner.addComponents(deck, hand);
List<Entity> cards = new ArrayList<>();
for (int i = 0; i < 20; i++) {
Entity newCard = game.newEntity();
deck.addOnBottom(newCard);
cards.add(newCard);
}

List<Entity> denied = new ArrayList<>();
game.addSystem(new LimitedHandSizeSystem(5, event -> denied.add(event.getCardToDraw())));
game.startGame();

DrawStartCards.drawCard(owner);
DrawStartCards.drawCard(owner);
DrawStartCards.drawCard(owner);
DrawStartCards.drawCard(owner);
DrawStartCards.drawCard(owner);
assertTrue("A card has been denied prematurely", denied.isEmpty());
DrawStartCards.drawCard(owner);
assertEquals("Card was not denied", 1, denied.size());
assertEquals("Unexpected card was denied", cards.get(5), denied.get(0));
DrawStartCards.drawCard(owner);
DrawStartCards.drawCard(owner);
DrawStartCards.drawCard(owner);
assertEquals("Cards has not been denied", 4, denied.size());
}
}

0 comments on commit 21e6ae7

Please sign in to comment.