@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; }
public static ECSAction getAction(Entity performer, String actionName) { ActionComponent actionComponent = actions.get(performer); if (actionComponent == null) { return null; } return actionComponent.getAction(actionName); }
public static List<ECSAction> getAllActions(ECSGame game) { return game.getEntitiesWithComponent(ActionComponent.class) .stream() .flatMap(entity -> entity.getComponent(ActionComponent.class) .getECSActions().stream()) .collect(Collectors.toList()); }
@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()); }
@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)); }
@Before public void before() { game = new ECSGame(); entity = game.newEntity().addComponent(new ActionComponent()); }
private void performAction(ECSAction mulliganAction) { List<Entity> chosenTargets = mulliganAction.getTargetSets().get(0).getChosenTargets(); chosenTargets.forEach(this::switchCard); Entity owner = mulliganAction.getOwner(); owner.getComponent(ActionComponent.class).removeAction(ACTION_NAME); owner.getComponent(DeckComponent.class).shuffle(); if (remainingPerforms.decrementAndGet() == 0) { phases.nextPhase(); } }
@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)); }
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)); }
protected ECSAction getAction(Entity entity, String actionName) { if (actions == null) { throw new AssertionError("actions is null"); } ActionComponent available = Objects.requireNonNull(actions.get(entity), "Entity does not have any action component"); return available.getAction(actionName); }
private static Stream<ECSAction> getAllActions(ECSGame game) { return game.getEntitiesWithComponent(ActionComponent.class) .stream() .flatMap(entity -> entity.getComponent(ActionComponent.class) .getECSActions().stream()); }
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 ECSAction actionFor(Entity player0) { return player0.getComponent(ActionComponent.class).getAction("Mulligan"); }
/** * * @param game The game to search for actions * @return A stream of action components for all entities in the game */ private static Stream<ECSAction> getAllActions(ECSGame game) { return game.getEntitiesWithComponent(ActionComponent.class) .stream() .flatMap(entity -> entity.getComponent(ActionComponent.class) .getECSActions().stream()); }
@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)); } }
private void performMulligan(Entity player, int[] cardsToChange) { HandComponent zone = player.get(hand); ECSAction action = player.getComponent(ActionComponent.class).getAction("Mulligan"); assertNotNull("Action not found on " + player); TargetSet targets = action.getTargetSets().get(0); Stream<Entity> chosenTargets = Arrays.stream(cardsToChange).mapToObj(i -> zone.stream().filter(e -> standardRetr.getFor(e) == i).findAny().get()); chosenTargets.forEach(e -> targets.addTarget(e)); assertTrue("Mulligan was not performed", action.perform(player)); }
public static List<ECSAction> getPossibleActionsOn(Entity entity, Entity performer) { return actions.required(entity).getECSActions().stream().filter(action -> action.isAllowed(performer)).collect(Collectors.toList()); }
phaseController.addPhase(playerPhase); ActionComponent actions = new ActionComponent(); player.addComponent(actions); actions.addAction(endTurnAction);