Skip to content

Commit

Permalink
Use separate behaviours for game end effects in Crafing Bee and Conne…
Browse files Browse the repository at this point in the history
…ct Four
  • Loading branch information
Gegy committed Nov 19, 2024
1 parent c59f10c commit 5e9fdc6
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -276,23 +276,5 @@ public State tick(IGamePhase game) {
}
return this;
}

}

record Ending(long endAt) implements State {
@Override
public State tick(IGamePhase game) {
if (game.ticks() > endAt) {
return null;
}
for (ServerPlayer player : game.participants()) {
if (!player.isSpectator() && game.random().nextInt(10) == 0) {
BlockPos fireworksPos = BlockPos.containing(player.getEyePosition()).above();
FireworkPalette.DYE_COLORS.spawn(fireworksPos, game.level());
}
}
return this;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import com.lovetropics.lib.BlockBox;
import com.lovetropics.lib.codec.MoreCodecs;
import com.lovetropics.lib.entity.FireworkPalette;
import com.lovetropics.minigames.common.content.MinigameTexts;
import com.lovetropics.minigames.common.core.game.GameException;
import com.lovetropics.minigames.common.core.game.GameStopReason;
import com.lovetropics.minigames.common.core.game.GameWinner;
import com.lovetropics.minigames.common.core.game.IGamePhase;
import com.lovetropics.minigames.common.core.game.behavior.IGameBehavior;
Expand All @@ -25,7 +22,6 @@
import com.mojang.serialization.Codec;
import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.ChatFormatting;
import net.minecraft.SharedConstants;
import net.minecraft.core.BlockPos;
import net.minecraft.core.registries.BuiltInRegistries;
Expand Down Expand Up @@ -235,19 +231,9 @@ private void onGameOver(GameWinner winner) {

if (winner instanceof GameWinner.Team(GameTeam team)) {
for (ServerPlayer winningPlayer : teams.getPlayersForTeam(team.key())) {
applyWinningPlayerEffects(winningPlayer, team);
winningPlayer.setGlowingTag(true);
}
}

game.scheduler().runAfterSeconds(5, () -> game.requestStop(GameStopReason.finished()));
}

private void applyWinningPlayerEffects(ServerPlayer winningPlayer, GameTeam team) {
game.scheduler().runAfterSeconds(game.random().nextFloat(), () -> {
BlockPos fireworksPos = BlockPos.containing(winningPlayer.getEyePosition()).above();
FireworkPalette.forDye(team.config().dye()).spawn(fireworksPos, game.level());
});
winningPlayer.setGlowingTag(true);
}

private void nextPlayer() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
import com.google.common.collect.ListMultimap;
import com.google.common.collect.Multimaps;
import com.lovetropics.lib.BlockBox;
import com.lovetropics.lib.entity.FireworkPalette;
import com.lovetropics.minigames.common.content.crafting_bee.ingredient.IngredientDecomposer;
import com.lovetropics.minigames.common.core.game.GameException;
import com.lovetropics.minigames.common.core.game.GameStopReason;
import com.lovetropics.minigames.common.core.game.GameWinner;
import com.lovetropics.minigames.common.core.game.IGamePhase;
import com.lovetropics.minigames.common.core.game.behavior.GameBehaviorType;
Expand Down Expand Up @@ -55,7 +53,6 @@
import net.minecraft.world.item.crafting.CraftingInput;
import net.minecraft.world.item.crafting.CraftingRecipe;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.level.block.BeaconBeamBlock;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.StainedGlassBlock;
Expand Down Expand Up @@ -326,20 +323,10 @@ private void onGameOver(GameWinner winner) {

if (winner instanceof GameWinner.Team(GameTeam team)) {
for (ServerPlayer winningPlayer : teams.getPlayersForTeam(team.key())) {
applyWinningPlayerEffects(winningPlayer, team);
winningPlayer.setGlowingTag(true);
}
}
});

game.scheduler().runAfterSeconds(8, () -> game.requestStop(GameStopReason.finished()));
}

private void applyWinningPlayerEffects(ServerPlayer winningPlayer, GameTeam team) {
game.scheduler().runAfterSeconds(game.random().nextFloat(), () -> {
BlockPos fireworksPos = BlockPos.containing(winningPlayer.getEyePosition()).above();
FireworkPalette.forDye(team.config().dye()).spawn(fireworksPos, game.level());
});
winningPlayer.setGlowingTag(true);
}

private void tickRunning(IGamePhase game) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.lovetropics.minigames.common.core.game;

import com.lovetropics.minigames.common.content.MinigameTexts;
import com.lovetropics.minigames.common.core.game.player.PlayerSet;
import com.lovetropics.minigames.common.core.game.state.statistics.PlayerKey;
import com.lovetropics.minigames.common.core.game.state.team.GameTeam;
import com.lovetropics.minigames.common.core.game.state.team.TeamState;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;

Expand All @@ -14,27 +16,48 @@ static GameWinner byPlayerKey(IGamePhase game, PlayerKey key) {

Component name();

PlayerSet resolvePlayers(IGamePhase game);

record Nobody() implements GameWinner {
@Override
public Component name() {
return MinigameTexts.NOBODY;
}

@Override
public PlayerSet resolvePlayers(IGamePhase game) {
return PlayerSet.EMPTY;
}
}

record Player(ServerPlayer player) implements GameWinner {
@Override
public Component name() {
return player.getDisplayName();
}

@Override
public PlayerSet resolvePlayers(IGamePhase game) {
return PlayerSet.of(player);
}
}

record OfflinePlayer(PlayerKey playerKey, Component name) implements GameWinner {
@Override
public PlayerSet resolvePlayers(IGamePhase game) {
return PlayerSet.EMPTY;
}
}

record Team(GameTeam team) implements GameWinner {
@Override
public Component name() {
return team.config().styledName();
}

@Override
public PlayerSet resolvePlayers(IGamePhase game) {
return game.instanceState().getOrThrow(TeamState.KEY).getPlayersForTeam(team.key());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import com.lovetropics.minigames.common.core.game.behavior.instances.action.CloseGameAction;
import com.lovetropics.minigames.common.core.game.behavior.instances.action.CountdownAction;
import com.lovetropics.minigames.common.core.game.behavior.instances.action.DamagePlayerAction;
import com.lovetropics.minigames.common.core.game.behavior.instances.action.DelayedAction;
import com.lovetropics.minigames.common.core.game.behavior.instances.action.KillEntitiesAction;
import com.lovetropics.minigames.common.core.game.behavior.instances.action.SetPlayerRoleAction;
import com.lovetropics.minigames.common.core.game.behavior.instances.action.EndGameAction;
Expand Down Expand Up @@ -273,6 +274,7 @@ public class GameBehaviorTypes {
public static final GameBehaviorEntry<PackageCostModifierBehavior> PACKAGE_COST_MODIFIER = register("package_cost_modifier", PackageCostModifierBehavior.CODEC);
public static final GameBehaviorEntry<WeatherEventAction> WEATHER_EVENT = register("weather_event", WeatherEventAction.CODEC);
public static final GameBehaviorEntry<CountdownAction<?>> COUNTDOWN_ACTION = register("countdown_action", CountdownAction.CODEC);
public static final GameBehaviorEntry<DelayedAction<?>> DELAYED = register("delayed", DelayedAction.CODEC);
public static final GameBehaviorEntry<TargetPlayerAction> TARGET_PLAYER = register("target_player", TargetPlayerAction.CODEC);
public static final GameBehaviorEntry<SpawnFireworksAction> SPAWN_FIREWORKS = register("spawn_fireworks", SpawnFireworksAction.CODEC);
public static final GameBehaviorEntry<RunCommandsAction> RUN_COMMANDS = register("run_commands", RunCommandsAction.CODEC);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.lovetropics.minigames.common.core.game.behavior.instances.action;

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.ActionTarget;
import com.lovetropics.minigames.common.core.game.behavior.action.GameActionList;
import com.lovetropics.minigames.common.core.game.behavior.action.NoneActionTarget;
import com.lovetropics.minigames.common.core.game.behavior.event.EventRegistrar;
import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.util.valueproviders.IntProvider;

public record DelayedAction<T>(
IntProvider delay,
GameActionList<?> actions,
ActionTarget<T> target
) implements IGameBehavior {
public static final MapCodec<DelayedAction<?>> CODEC = RecordCodecBuilder.mapCodec(i -> i.group(
IntProvider.POSITIVE_CODEC.fieldOf("delay").forGetter(b -> b.delay),
GameActionList.MAP_CODEC.forGetter(c -> c.actions),
ActionTarget.CODEC.optionalFieldOf("target", NoneActionTarget.INSTANCE).forGetter(c -> c.target)
).apply(i, DelayedAction::new));

@Override
public void register(IGamePhase game, EventRegistrar events) {
actions.register(game, events);

target.listenAndCaptureSource(events, (context, targets) -> {
int ticks = delay.sample(game.random());
game.scheduler().runAfterTicks(ticks, () -> {
if (actions.target.type() == target.type()) {
actions.applyIf(target::type, game, context, targets);
} else {
actions.apply(game, context);
}
});
return true;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,40 @@
import com.lovetropics.minigames.common.core.game.behavior.IGameBehavior;
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.state.team.GameTeamKey;
import com.lovetropics.minigames.common.core.game.state.team.TeamState;
import com.mojang.serialization.Codec;
import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.level.levelgen.Heightmap;

public final class SpawnFireworksAction implements IGameBehavior {
public static final MapCodec<SpawnFireworksAction> CODEC = MapCodec.unit(SpawnFireworksAction::new);
public record SpawnFireworksAction(
boolean useTeamColor
) implements IGameBehavior {
public static final MapCodec<SpawnFireworksAction> CODEC = RecordCodecBuilder.mapCodec(i -> i.group(
Codec.BOOL.optionalFieldOf("use_team_color", true).forGetter(SpawnFireworksAction::useTeamColor)
).apply(i, SpawnFireworksAction::new));

@Override
public void register(IGamePhase game, EventRegistrar events) {
events.listen(GameActionEvents.APPLY_TO_PLAYER, (context, player) -> {
BlockPos fireworkPos = player.level().getHeightmapPos(Heightmap.Types.MOTION_BLOCKING, player.blockPosition());
FireworkPalette.DYE_COLORS.spawn(fireworkPos, player.level());
FireworkPalette palette = selectPalette(game, player);
palette.spawn(fireworkPos, player.level());
return true;
});
}

private FireworkPalette selectPalette(IGamePhase game, ServerPlayer player) {
if (useTeamColor) {
TeamState teams = game.instanceState().getOrNull(TeamState.KEY);
GameTeamKey team = teams != null ? teams.getTeamForPlayer(player) : null;
if (team != null) {
return FireworkPalette.forDye(teams.getTeamOrThrow(team).config().dye());
}
}
return FireworkPalette.DYE_COLORS;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.lovetropics.minigames.common.core.game.behavior.instances.trigger.phase;

import com.lovetropics.minigames.common.core.game.GameException;
import com.lovetropics.minigames.common.core.game.GameWinner;
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;
Expand All @@ -12,7 +11,9 @@
import com.mojang.serialization.MapCodec;
import net.minecraft.server.level.ServerPlayer;

public record GameOverTrigger(GameActionList<ServerPlayer> actions) implements IGameBehavior {
public record GameOverTrigger(
GameActionList<ServerPlayer> actions
) implements IGameBehavior {
public static final MapCodec<GameOverTrigger> CODEC = GameActionList.PLAYER_MAP_CODEC
.xmap(GameOverTrigger::new, GameOverTrigger::actions);

Expand All @@ -21,11 +22,7 @@ public void register(IGamePhase game, EventRegistrar events) throws GameExceptio
actions.register(game, events);
events.listen(GameLogicEvents.GAME_OVER, winner -> {
GameActionContext context = GameActionContext.builder().set(GameActionParameter.WINNER, winner.name()).build();
if (winner instanceof GameWinner.Player(ServerPlayer player)) {
actions.apply(game, context, player);
} else {
actions.apply(game, context);
}
actions.apply(game, context, winner.resolvePlayers(game));
});
}
}

0 comments on commit 5e9fdc6

Please sign in to comment.