valence/crates/valence_client/src/weather.rs

225 lines
6.5 KiB
Rust
Raw Normal View History

Reorganize Project (#321) ## Description - `valence` and `valence_protocol` have been divided into smaller crates in order to parallelize the build and improve IDE responsiveness. In the process, code architecture has been made clearer by removing circular dependencies between modules. `valence` is now just a shell around the other crates. - `workspace.packages` and `workspace.dependencies` are now used. This makes dependency managements and crate configuration much easier. - `valence_protocol` is no more. Most things from `valence_protocol` ended up in `valence_core`. We won't advertise `valence_core` as a general-purpose protocol library since it contains too much valence-specific stuff. Closes #308. - Networking code (login, initial TCP connection handling, etc.) has been extracted into the `valence_network` crate. The API has been expanded and improved with better defaults. Player counts and initial connections to the server are now tracked separately. Player counts function by default without any user configuration. - Some crates like `valence_anvil`, `valence_network`, `valence_player_list`, `valence_inventory`, etc. are now optional. They can be enabled/disabled with feature flags and `DefaultPlugins` just like bevy. - Whole-server unit tests have been moved to `valence/src/tests` in order to avoid [cyclic dev-dependencies](https://github.com/rust-lang/cargo/issues/4242). - Tools like `valence_stresser` and `packet_inspector` have been moved to a new `tools` directory. Renamed `valence_stresser` to `stresser`. Closes #241. - Moved all benches to `valence/benches/` to make them easier to run and organize. Ignoring transitive dependencies and `valence_core`, here's what the dependency graph looks like now: ```mermaid graph TD network --> client client --> instance biome --> registry dimension --> registry instance --> biome instance --> dimension instance --> entity player_list --> client inventory --> client anvil --> instance entity --> block ``` ### Issues - Inventory tests inspect many private implementation details of the inventory module, forcing us to mark things as `pub` and `#[doc(hidden)]`. It would be ideal if the tests only looked at observable behavior. - Consider moving packets in `valence_core` elsewhere. `Particle` wants to use `BlockState`, but that's defined in `valence_block`, so we can't use it without causing cycles. - Unsure what exactly should go in `valence::prelude`. - This could use some more tests of course, but I'm holding off on that until I'm confident this is the direction we want to take things. ## TODOs - [x] Update examples. - [x] Update benches. - [x] Update main README. - [x] Add short READMEs to crates. - [x] Test new schedule to ensure behavior is the same. - [x] Update tools. - [x] Copy lints to all crates. - [x] Fix docs, clippy, etc.
2023-04-22 07:43:59 +10:00
//! The weather system.
//!
//! This module contains the systems and components needed to handle
//! weather.
//!
//! # Components
//!
//! The components may be attached to clients or instances.
//!
//! - [`Rain`]: When attached, raining begin and rain level set events are
//! emitted. When removed, the end raining event is emitted.
//! - [`Thunder`]: When attached, thunder level set event is emitted. When
//! removed, the thunder level set to zero event is emitted.
//!
//! New joined players are handled, so that they are get weather events from
//! the instance.
use super::*;
use crate::packet::{GameEventKind, GameStateChangeS2c};
Reorganize Project (#321) ## Description - `valence` and `valence_protocol` have been divided into smaller crates in order to parallelize the build and improve IDE responsiveness. In the process, code architecture has been made clearer by removing circular dependencies between modules. `valence` is now just a shell around the other crates. - `workspace.packages` and `workspace.dependencies` are now used. This makes dependency managements and crate configuration much easier. - `valence_protocol` is no more. Most things from `valence_protocol` ended up in `valence_core`. We won't advertise `valence_core` as a general-purpose protocol library since it contains too much valence-specific stuff. Closes #308. - Networking code (login, initial TCP connection handling, etc.) has been extracted into the `valence_network` crate. The API has been expanded and improved with better defaults. Player counts and initial connections to the server are now tracked separately. Player counts function by default without any user configuration. - Some crates like `valence_anvil`, `valence_network`, `valence_player_list`, `valence_inventory`, etc. are now optional. They can be enabled/disabled with feature flags and `DefaultPlugins` just like bevy. - Whole-server unit tests have been moved to `valence/src/tests` in order to avoid [cyclic dev-dependencies](https://github.com/rust-lang/cargo/issues/4242). - Tools like `valence_stresser` and `packet_inspector` have been moved to a new `tools` directory. Renamed `valence_stresser` to `stresser`. Closes #241. - Moved all benches to `valence/benches/` to make them easier to run and organize. Ignoring transitive dependencies and `valence_core`, here's what the dependency graph looks like now: ```mermaid graph TD network --> client client --> instance biome --> registry dimension --> registry instance --> biome instance --> dimension instance --> entity player_list --> client inventory --> client anvil --> instance entity --> block ``` ### Issues - Inventory tests inspect many private implementation details of the inventory module, forcing us to mark things as `pub` and `#[doc(hidden)]`. It would be ideal if the tests only looked at observable behavior. - Consider moving packets in `valence_core` elsewhere. `Particle` wants to use `BlockState`, but that's defined in `valence_block`, so we can't use it without causing cycles. - Unsure what exactly should go in `valence::prelude`. - This could use some more tests of course, but I'm holding off on that until I'm confident this is the direction we want to take things. ## TODOs - [x] Update examples. - [x] Update benches. - [x] Update main README. - [x] Add short READMEs to crates. - [x] Test new schedule to ensure behavior is the same. - [x] Update tools. - [x] Copy lints to all crates. - [x] Fix docs, clippy, etc.
2023-04-22 07:43:59 +10:00
#[derive(SystemSet, Copy, Clone, PartialEq, Eq, Hash, Debug)]
struct UpdateWeatherPerInstanceSet;
#[derive(SystemSet, Copy, Clone, PartialEq, Eq, Hash, Debug)]
struct UpdateWeatherPerClientSet;
pub(super) fn build(app: &mut App) {
app.configure_set(
UpdateWeatherPerInstanceSet
.in_base_set(CoreSet::PostUpdate)
.before(WriteUpdatePacketsToInstancesSet),
)
.configure_set(
UpdateWeatherPerClientSet
.in_base_set(CoreSet::PostUpdate)
.before(FlushPacketsSet),
)
.add_systems(
(
handle_rain_begin_per_instance,
handle_rain_change_per_instance,
handle_rain_end_per_instance,
handle_thunder_change_per_instance,
handle_thunder_end_per_instance,
)
.chain()
.in_set(UpdateWeatherPerInstanceSet)
.before(UpdateWeatherPerClientSet),
)
.add_systems(
(
handle_rain_begin_per_client,
handle_rain_change_per_client,
handle_rain_end_per_client,
handle_thunder_change_per_client,
handle_thunder_end_per_client,
)
.chain()
.in_set(UpdateWeatherPerClientSet),
)
.add_system(
handle_weather_for_joined_player
.before(UpdateWeatherPerClientSet)
.in_base_set(CoreSet::PostUpdate),
);
}
/// Contains the rain level.
///
/// Valid values are within `0.0..=1.0`.
#[derive(Component)]
pub struct Rain(pub f32);
/// Contains the thunder level.
///
/// Valid values are within `0.0..=1.0`.
#[derive(Component)]
pub struct Thunder(pub f32);
fn handle_weather_for_joined_player(
mut clients: Query<(&mut Client, &Location), Added<Client>>,
weathers: Query<(Option<&Rain>, Option<&Thunder>), With<Instance>>,
) {
for (mut client, loc) in &mut clients {
if let Ok((rain, thunder)) = weathers.get(loc.0) {
if let Some(level) = rain {
client.write_packet(&GameStateChangeS2c {
kind: GameEventKind::BeginRaining,
value: 0.0,
});
client.write_packet(&GameStateChangeS2c {
kind: GameEventKind::RainLevelChange,
value: level.0,
});
}
if let Some(level) = thunder {
client.write_packet(&GameStateChangeS2c {
kind: GameEventKind::ThunderLevelChange,
value: level.0,
});
}
}
}
}
fn handle_rain_begin_per_instance(mut instances: Query<&mut Instance, Added<Rain>>) {
for mut instance in &mut instances {
instance.write_packet(&GameStateChangeS2c {
kind: GameEventKind::BeginRaining,
value: f32::default(),
});
}
}
fn handle_rain_change_per_instance(mut instances: Query<(&mut Instance, &Rain), Changed<Rain>>) {
for (mut instance, rain) in &mut instances {
instance.write_packet(&GameStateChangeS2c {
kind: GameEventKind::RainLevelChange,
value: rain.0,
});
}
}
fn handle_rain_end_per_instance(
mut instances: Query<&mut Instance>,
mut removed: RemovedComponents<Rain>,
) {
for entity in &mut removed {
if let Ok(mut instance) = instances.get_mut(entity) {
instance.write_packet(&GameStateChangeS2c {
kind: GameEventKind::EndRaining,
value: 0.0,
});
}
}
}
fn handle_thunder_change_per_instance(
mut instances: Query<(&mut Instance, &Thunder), Changed<Thunder>>,
) {
for (mut instance, thunder) in &mut instances {
instance.write_packet(&GameStateChangeS2c {
kind: GameEventKind::ThunderLevelChange,
value: thunder.0,
});
}
}
fn handle_thunder_end_per_instance(
mut instances: Query<&mut Instance>,
mut removed: RemovedComponents<Thunder>,
) {
for entity in &mut removed {
if let Ok(mut instance) = instances.get_mut(entity) {
instance.write_packet(&GameStateChangeS2c {
kind: GameEventKind::ThunderLevelChange,
value: 0.0,
});
}
}
}
fn handle_rain_begin_per_client(mut clients: Query<&mut Client, (Added<Rain>, Without<Instance>)>) {
for mut client in &mut clients {
client.write_packet(&GameStateChangeS2c {
kind: GameEventKind::BeginRaining,
value: 0.0,
});
}
}
#[allow(clippy::type_complexity)]
fn handle_rain_change_per_client(
mut clients: Query<(&mut Client, &Rain), (Changed<Rain>, Without<Instance>)>,
) {
for (mut client, rain) in &mut clients {
client.write_packet(&GameStateChangeS2c {
kind: GameEventKind::RainLevelChange,
value: rain.0,
});
}
}
fn handle_rain_end_per_client(
mut clients: Query<&mut Client>,
mut removed: RemovedComponents<Rain>,
) {
for entity in &mut removed {
if let Ok(mut client) = clients.get_mut(entity) {
client.write_packet(&GameStateChangeS2c {
kind: GameEventKind::EndRaining,
value: f32::default(),
});
}
}
}
#[allow(clippy::type_complexity)]
fn handle_thunder_change_per_client(
mut clients: Query<(&mut Client, &Thunder), (Changed<Thunder>, Without<Instance>)>,
) {
for (mut client, thunder) in &mut clients {
client.write_packet(&GameStateChangeS2c {
kind: GameEventKind::ThunderLevelChange,
value: thunder.0,
});
}
}
fn handle_thunder_end_per_client(
mut clients: Query<&mut Client, Without<Instance>>,
mut removed: RemovedComponents<Thunder>,
) {
for entity in &mut removed {
if let Ok(mut client) = clients.get_mut(entity) {
client.write_packet(&GameStateChangeS2c {
kind: GameEventKind::ThunderLevelChange,
value: 0.0,
});
}
}
}