Skip to content

Commit

Permalink
Final River Race zone and win condition
Browse files Browse the repository at this point in the history
  • Loading branch information
Gegy committed Nov 19, 2024
1 parent b88cb8f commit c59f10c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import com.lovetropics.minigames.common.core.game.GameException;
import com.lovetropics.minigames.common.core.game.IGamePhase;
import com.lovetropics.minigames.common.core.game.behavior.IGameBehavior;
import com.lovetropics.minigames.common.core.game.behavior.action.GameActionContext;
import com.lovetropics.minigames.common.core.game.behavior.action.GameActionList;
import com.lovetropics.minigames.common.core.game.behavior.event.EventRegistrar;
import com.lovetropics.minigames.common.core.game.behavior.event.GameActionEvents;
import com.lovetropics.minigames.common.core.game.behavior.event.SubGameEvents;
import com.lovetropics.minigames.common.core.game.config.GameConfig;
import com.lovetropics.minigames.common.core.game.config.GameConfigs;
import com.lovetropics.minigames.common.core.game.impl.MultiGamePhase;
Expand All @@ -14,20 +17,28 @@
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.ExtraCodecs;
import org.apache.commons.lang3.mutable.MutableBoolean;

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

public record StartMicrogamesAction(List<ResourceLocation> gameConfigIds, int gamesPerRound) implements IGameBehavior {
public record StartMicrogamesAction(
List<ResourceLocation> gameConfigIds,
int gamesPerRound,
GameActionList<Void> onComplete
) implements IGameBehavior {

public static final MapCodec<StartMicrogamesAction> CODEC = RecordCodecBuilder.mapCodec(i -> i.group(
ExtraCodecs.nonEmptyList(ResourceLocation.CODEC.listOf()).fieldOf("games").forGetter(StartMicrogamesAction::gameConfigIds),
Codec.INT.optionalFieldOf("games_per_round", 1).forGetter(c -> c.gamesPerRound)
Codec.INT.optionalFieldOf("games_per_round", 1).forGetter(c -> c.gamesPerRound),
GameActionList.VOID_CODEC.optionalFieldOf("on_complete", GameActionList.EMPTY_VOID).forGetter(StartMicrogamesAction::onComplete)
).apply(i, StartMicrogamesAction::new));

@Override
public void register(IGamePhase game, EventRegistrar events) throws GameException {
onComplete.register(game, events);

List<GameConfig> gameConfigs = new ArrayList<>(gameConfigIds.size());
for (ResourceLocation configId : gameConfigIds) {
GameConfig config = GameConfigs.REGISTRY.get(configId);
Expand All @@ -36,11 +47,21 @@ public void register(IGamePhase game, EventRegistrar events) throws GameExceptio
}
gameConfigs.add(config);
}

MutableBoolean scheduled = new MutableBoolean();
events.listen(GameActionEvents.APPLY, context -> {
queueMicrogames(game, gameConfigs);
startQueuedMicrogame(game);
scheduled.setTrue();
return true;
});

events.listen(SubGameEvents.RETURN_TO_TOP, () -> {
if (scheduled.isTrue()) {
scheduled.setFalse();
onComplete.apply(game, GameActionContext.EMPTY);
}
});
}

public void queueMicrogames(IGamePhase game, List<GameConfig> gameConfigs) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,6 @@ public boolean apply(IGamePhase phase, GameActionContext context, Iterable<T> so
}

private boolean isEmpty() {
return this == EMPTY;
return behavior == IGameBehavior.EMPTY;
}
}

0 comments on commit c59f10c

Please sign in to comment.