-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'issue11' of github.com:Cardshifter/Cardshifter into dev…
…elop
- Loading branch information
Showing
3 changed files
with
83 additions
and
1 deletion.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
cardshifter-core/src/main/java/net/zomis/cardshifter/ecs/cards/LimitedHandSizeSystem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
cardshifter-core/src/test/java/com/cardshifter/core/issues/Issue11Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |