@Override public Component copy(Entity copyTo) { ActionComponent copy = new ActionComponent(); for (Entry<String, ECSAction> entry : actions.entrySet()) { copy.addAction(entry.getValue().copy(copyTo)); } return copy; }
private void createCards(ZoneComponent hand) { for (int i = 0; i < 5; i++) { Entity entity = hand.getOwner().getGame().newEntity(); ECSResourceMap.createFor(entity) .set(CyborgChroniclesGame.CyborgChroniclesResources.HEALTH, 3) .set(CyborgChroniclesGame.CyborgChroniclesResources.MAX_HEALTH, 3); ECSAttributeMap.createFor(entity).set(Attributes.NAME, "Test"); ActionComponent action = new ActionComponent(); entity.addComponent(action); action.addAction(moveAction("Field", entity, BattlefieldComponent.class, false)); action.addAction(moveAction("Hand", entity, HandComponent.class, false)); action.addAction(moveAction("Deck", entity, DeckComponent.class, false)); action.addAction(moveAction("2-Field", entity, BattlefieldComponent.class, true)); action.addAction(moveAction("2-Hand", entity, HandComponent.class, true)); action.addAction(moveAction("2-Deck", entity, DeckComponent.class, true)); action.addAction(damageAction(entity)); hand.addOnBottom(entity); } }
private void giveReplayControls(Entity player) { ActionComponent actions = player.getComponent(ActionComponent.class); if (actions == null) { actions = new ActionComponent(); player.addComponent(actions); } actions.addAction(new ECSAction(player, NEXT_STEP, e -> true, this::nextStep)); }
@Override public void startGame(ECSGame game) { ComponentRetriever<ActionComponent> actions = ComponentRetriever.retreiverFor(ActionComponent.class); game.getEvents().registerHandlerAfter(this, TargetableCheckEvent.class, this::targetAllowed); game.getEvents().registerHandlerAfter(this, ActionAllowedCheckEvent.class, this::actionAllowed); Set<Entity> players = game.getEntitiesWithComponent(PlayerComponent.class); remainingPerforms.set(players.size()); for (Entity player : players) { ActionComponent playerActions = player.get(actions); if (playerActions == null) { playerActions = new ActionComponent(); player.addComponent(playerActions); } playerActions.addAction(createMulliganAction(player)); } }
@Test public void testPerformAllowedAction() { game.startGame(); ActionComponent actions = retriever.get(entity); assertEquals(Collections.<String>emptySet(), actions.getActions()); @SuppressWarnings("unchecked") Consumer<ECSAction> perform = (Consumer<ECSAction>) mock(Consumer.class); String name = "Use"; actions.addAction(new ECSAction(entity, name, action -> true, perform)); actions.getAction(name).copy().perform(entity); verify(perform).accept(Mockito.any(ECSAction.class)); }
@Test public void deniedActionWithSystem() { String name = "Use"; game.addSystem(new SpecificActionSystem(name) { @Override protected void isAllowed(ActionAllowedCheckEvent event) { event.setAllowed(false); } @Override protected void onPerform(ActionPerformEvent event) { } }); game.startGame(); ActionComponent actions = retriever.get(entity); assertEquals(Collections.<String>emptySet(), actions.getActions()); @SuppressWarnings("unchecked") Consumer<ECSAction> perform = (Consumer<ECSAction>) mock(Consumer.class); actions.addAction(new ECSAction(entity, name, action -> true, perform)); actions.getAction(name).copy().perform(entity); verifyNoMoreInteractions(perform); assertFalse(actions.getAction(name).isAllowed(entity)); }
@Test public void removeSystemTest() { ECSGame game = new ECSGame(); ActionComponent act = new ActionComponent(); Entity entity = game.newEntity().addComponent(act); act.addAction(new ECSAction(entity, "Test", e -> true, e -> {})); system = new SpecificActionSystem("Test") { @Override protected void onPerform(ActionPerformEvent event) { value.getAndIncrement(); event.getEntity().getGame().removeSystem(system); } }; game.addSystem(system); game.startGame(); assertEquals(0, value.get()); act.getAction("Test").perform(entity); assertEquals(1, value.get()); act.getAction("Test").perform(entity); assertEquals(1, value.get()); game.addSystem(system); act.getAction("Test").perform(entity); assertEquals(2, value.get()); }
actions.addAction(endTurnAction);