Skip to content

Commit

Permalink
Data-drive weather change notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
Gegy committed Oct 8, 2023
1 parent 48885f7 commit 5b3bd84
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 82 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package com.lovetropics.minigames.common.content.survive_the_tide.behavior;

import com.lovetropics.minigames.client.toast.NotificationIcon;
import com.lovetropics.minigames.client.toast.NotificationStyle;
import com.lovetropics.minigames.client.toast.ShowNotificationToastMessage;
import com.lovetropics.minigames.common.content.survive_the_tide.SurviveTheTide;
import com.lovetropics.minigames.common.content.survive_the_tide.SurviveTheTideWeatherConfig;
import com.lovetropics.minigames.common.core.game.IGamePhase;
import com.lovetropics.minigames.common.core.game.behavior.IGameBehavior;
Expand All @@ -13,20 +9,10 @@
import com.lovetropics.minigames.common.core.game.state.weather.GameWeatherState;
import com.lovetropics.minigames.common.core.game.weather.WeatherEvent;
import com.lovetropics.minigames.common.core.game.weather.WeatherEventType;
import com.lovetropics.minigames.common.core.network.LoveTropicsNetwork;
import com.mojang.serialization.Codec;
import net.minecraft.ChatFormatting;
import net.minecraft.SharedConstants;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.util.RandomSource;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.level.block.Blocks;

import java.util.function.Supplier;

public class SurviveTheTideWeatherControlBehavior implements IGameBehavior {
public static final Codec<SurviveTheTideWeatherControlBehavior> CODEC = SurviveTheTideWeatherConfig.CODEC.xmap(SurviveTheTideWeatherControlBehavior::new, b -> b.config);
Expand Down Expand Up @@ -77,8 +63,6 @@ public class SurviveTheTideWeatherControlBehavior implements IGameBehavior {
* - consider design to factor in worn items to negate player effects
*/

private static final Component TITLE = Component.literal("WEATHER REPORT: \n").withStyle(ChatFormatting.BOLD);

protected GameProgressionState progression;
protected GameWeatherState weather;

Expand Down Expand Up @@ -107,15 +91,15 @@ private void tick(final IGamePhase game) {
if (random.nextFloat() <= config.getRainHeavyChance(progression)) {
heavyRainfallStart(progression);
} else if (random.nextFloat() <= config.getRainAcidChance(progression)) {
acidRainStart(game, progression);
acidRainStart(progression);
} else if (random.nextFloat() <= config.getHailChance(progression)) {
hailStart(game, progression);
hailStart(progression);
} else if (random.nextFloat() <= config.getHeatwaveChance(progression)) {
heatwaveStart(game, progression);
heatwaveStart(progression);
} else if (random.nextFloat() <= config.getSandstormChance(progression)) {
sandstormStart(game, progression);
sandstormStart(progression);
} else if (random.nextFloat() <= config.getSnowstormChance(progression)) {
snowstormStart(game, progression);
snowstormStart(progression);
}
}

Expand All @@ -137,101 +121,45 @@ private void heavyRainfallStart(GameProgressionState progression) {
weather.setEvent(WeatherEvent.heavyRain(time));
}

private void acidRainStart(IGamePhase game, GameProgressionState progression) {
private void acidRainStart(GameProgressionState progression) {
int time = config.getRainAcidMinTime() + random.nextInt(config.getRainAcidExtraRandTime());
if (config.halveEventTime(progression)) {
time /= 2;
}
weather.setEvent(WeatherEvent.acidRain(time));

broadcastNotification(game,
Component.literal("Acid Rain is falling!\n")
.append("Find shelter, or make sure to carry an ")
.append(umbrellaName()),
createNotificationStyle(SurviveTheTide.ACID_REPELLENT_UMBRELLA)
);
}

private void hailStart(IGamePhase game, GameProgressionState progression) {
private void hailStart(GameProgressionState progression) {
int time = config.getRainHeavyMinTime() + random.nextInt(config.getRainHeavyExtraRandTime());
if (config.halveEventTime(progression)) {
time /= 2;
}
weather.setEvent(WeatherEvent.hail(time));

broadcastNotification(game,
Component.literal("Hail is falling!\n")
.append("Find shelter, or make sure to carry an ")
.append(umbrellaName()),
createNotificationStyle(SurviveTheTide.ACID_REPELLENT_UMBRELLA)
);
}

private void heatwaveStart(IGamePhase game, GameProgressionState progression) {
private void heatwaveStart(GameProgressionState progression) {
int time = config.getHeatwaveMinTime() + random.nextInt(config.getHeatwaveExtraRandTime());
if (config.halveEventTime(progression)) {
time /= 2;
}
weather.setEvent(WeatherEvent.heatwave(time));

broadcastNotification(game,
Component.literal("A Heat Wave is passing!\n")
.append("Stay inside, or make sure to equip ")
.append(sunscreenName()),
createNotificationStyle(SurviveTheTide.SUPER_SUNSCREEN)
);
}

private void sandstormStart(IGamePhase game, GameProgressionState progression) {
private void sandstormStart(GameProgressionState progression) {
//TODO: more config
int time = config.getHeatwaveMinTime() + random.nextInt(config.getHeatwaveExtraRandTime());
if (config.halveEventTime(progression)) {
time /= 2;
}
weather.setEvent(WeatherEvent.sandstorm(time, config.getSandstormBuildupTickRate(), config.getSandstormMaxStackable()));

broadcastNotification(game,
Component.literal("A Sandstorm is passing!\n")
.append("Find shelter!"),
createNotificationStyle(() -> Blocks.SAND)
);
}

private void snowstormStart(IGamePhase game, GameProgressionState progression) {
private void snowstormStart(GameProgressionState progression) {
//TODO: more config
int time = config.getHeatwaveMinTime() + random.nextInt(config.getHeatwaveExtraRandTime());
if (config.halveEventTime(progression)) {
time /= 2;
}
weather.setEvent(WeatherEvent.snowstorm(time, config.getSnowstormBuildupTickRate(), config.getSnowstormMaxStackable()));

broadcastNotification(game,
Component.literal("A Snowstorm is passing!\n")
.append("Find shelter!"),
createNotificationStyle(() -> Blocks.SNOW_BLOCK)
);
}

private static void broadcastNotification(IGamePhase game, Component message, NotificationStyle style) {
ShowNotificationToastMessage packet = new ShowNotificationToastMessage(Component.literal("").append(TITLE).append(message), style);
game.getAllPlayers().sendPacket(LoveTropicsNetwork.CHANNEL, packet);
game.getParticipants().playSound(SoundEvents.VILLAGER_NO, SoundSource.MASTER, 1.0f, 1.0f);
}

private static Component umbrellaName() {
return Component.translatable(SurviveTheTide.ACID_REPELLENT_UMBRELLA.get().getDescriptionId());
}

private static Component sunscreenName() {
return Component.translatable(SurviveTheTide.SUPER_SUNSCREEN.get().getDescriptionId());
}

private static NotificationStyle createNotificationStyle(final Supplier<? extends ItemLike> item) {
return new NotificationStyle(
NotificationIcon.item(new ItemStack(item.get())),
NotificationStyle.Sentiment.NEGATIVE,
NotificationStyle.Color.DARK,
5 * 1000
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
import com.lovetropics.minigames.common.core.game.behavior.instances.trigger.OnKillTrigger;
import com.lovetropics.minigames.common.core.game.behavior.instances.trigger.PhaseChangeTrigger;
import com.lovetropics.minigames.common.core.game.behavior.instances.trigger.ScheduledActionsTrigger;
import com.lovetropics.minigames.common.core.game.behavior.instances.trigger.WeatherChangeTrigger;
import com.lovetropics.minigames.common.core.game.behavior.instances.trigger.WhileInRegionTrigger;
import com.lovetropics.minigames.common.core.game.behavior.instances.tweak.CancelPlayerDamageBehavior;
import com.lovetropics.minigames.common.core.game.behavior.instances.tweak.DisableHungerBehavior;
Expand Down Expand Up @@ -160,6 +161,7 @@ public class GameBehaviorTypes {
public static final GameBehaviorEntry<GameEndEffectsBehavior> GAME_END_EFFECTS = register("game_end_effects", GameEndEffectsBehavior.CODEC);
public static final GameBehaviorEntry<TipsAndTricksBehavior> TIPS_AND_TRICKS = register("tips_and_tricks", TipsAndTricksBehavior.CODEC);
public static final GameBehaviorEntry<ProgressBarBehavior> PHASE_PROGRESS_BAR = register("phase_progress_bar", ProgressBarBehavior.CODEC);
public static final GameBehaviorEntry<WeatherChangeTrigger> WEATHER_CHANGE_TRIGGER = register("weather_change_trigger", WeatherChangeTrigger.CODEC);

public static final GameBehaviorEntry<BindObjectiveToStatisticBehavior> BIND_OBJECTIVE_TO_STATISTIC = register("bind_objective_to_statistic", BindObjectiveToStatisticBehavior.CODEC);
public static final GameBehaviorEntry<PlaceByStatisticBehavior> PLACE_BY_STATISTIC = register("place_by_statistic", PlaceByStatisticBehavior.CODEC);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.lovetropics.minigames.common.core.game.behavior.instances.trigger;

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.GameWorldEvents;
import com.lovetropics.minigames.common.core.game.weather.WeatherEventType;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;

import java.util.Map;

public class WeatherChangeTrigger implements IGameBehavior {
public static final Codec<WeatherChangeTrigger> CODEC = RecordCodecBuilder.create(i -> i.group(
Codec.unboundedMap(WeatherEventType.CODEC, GameActionList.CODEC).fieldOf("events").forGetter(c -> c.eventActions)
).apply(i, WeatherChangeTrigger::new));

private final Map<WeatherEventType, GameActionList> eventActions;

public WeatherChangeTrigger(Map<WeatherEventType, GameActionList> eventActions) {
this.eventActions = eventActions;
}

@Override
public void register(IGamePhase game, EventRegistrar events) {
for (GameActionList actions : eventActions.values()) {
actions.register(game, events);
}

events.listen(GameWorldEvents.SET_WEATHER, (lastEvent, event) -> {
if (event != null) {
GameActionList actions = eventActions.get(event.getType());
if (actions != null) {
actions.apply(game, GameActionContext.EMPTY);
}
}
});
}
}

0 comments on commit 5b3bd84

Please sign in to comment.