From ff8578fd828bb1f8a43c30f6ed239f09eb6201f1 Mon Sep 17 00:00:00 2001 From: copygirl Date: Wed, 22 Oct 2025 22:19:48 +0200 Subject: [PATCH] Add `common` package with blocks module --- Cargo.lock | 8 ++++++++ Cargo.toml | 2 +- client/Cargo.toml | 2 ++ client/src/block.rs | 40 ++++++++++++---------------------------- client/src/main.rs | 2 ++ common/Cargo.toml | 7 +++++++ common/src/block.rs | 26 ++++++++++++++++++++++++++ common/src/lib.rs | 1 + 8 files changed, 59 insertions(+), 29 deletions(-) create mode 100644 common/Cargo.toml create mode 100644 common/src/block.rs create mode 100644 common/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 0aa186d..9d52600 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -328,9 +328,17 @@ name = "bevy-bloxel-classic" version = "0.1.0" dependencies = [ "bevy", + "bevy-bloxel-classic-common", "bevy_fix_cursor_unlock_web", ] +[[package]] +name = "bevy-bloxel-classic-common" +version = "0.1.0" +dependencies = [ + "bevy", +] + [[package]] name = "bevy_a11y" version = "0.17.2" diff --git a/Cargo.toml b/Cargo.toml index 6028d7c..61c3a5e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [workspace] resolver = "3" -members = ["client"] +members = ["client", "common"] # Enable a small amount of optimization in the dev profile. [profile.dev] diff --git a/client/Cargo.toml b/client/Cargo.toml index 5d48029..bb0c611 100644 --- a/client/Cargo.toml +++ b/client/Cargo.toml @@ -4,6 +4,8 @@ version = "0.1.0" edition = "2024" [dependencies] +common = { package = "bevy-bloxel-classic-common", path = "../common" } + bevy = { workspace = true } bevy_fix_cursor_unlock_web = "0.2" diff --git a/client/src/block.rs b/client/src/block.rs index 00b675b..c64ceb0 100644 --- a/client/src/block.rs +++ b/client/src/block.rs @@ -1,33 +1,6 @@ -use bevy::ecs::system::SystemParam; use bevy::prelude::*; -#[derive(Component)] -pub struct Block; - -#[derive(SystemParam)] -pub struct Blocks<'w, 's> { - commands: Commands<'w, 's>, - block_resources: Res<'w, BlockResources>, - blocks: Query<'w, 's, &'static Transform, With>, -} - -impl Blocks<'_, '_> { - pub fn spawn(&mut self, pos: IVec3) { - self.commands.spawn(( - Block, - Mesh3d(self.block_resources.mesh.clone()), - MeshMaterial3d(self.block_resources.material.clone()), - Transform::from_translation(pos.as_vec3() + Vec3::ONE / 2.), - )); - } - - /// Gets the position of a block entity, or `None` - /// if the given entity is not alive or not a block. - pub fn position(&self, entity: Entity) -> Option { - let transform = self.blocks.get(entity).ok(); - transform.map(|t| t.translation.floor().as_ivec3()) - } -} +pub use common::block::*; #[derive(Resource)] pub struct BlockResources { @@ -45,3 +18,14 @@ pub fn setup_blocks( material: materials.add(Color::srgb_u8(124, 144, 255)), }); } + +pub fn add_block_visuals( + add: On, + mut commands: Commands, + resources: Res, +) { + commands.entity(add.entity).insert(( + Mesh3d(resources.mesh.clone()), + MeshMaterial3d(resources.material.clone()), + )); +} diff --git a/client/src/main.rs b/client/src/main.rs index c8931f4..41fe3e9 100644 --- a/client/src/main.rs +++ b/client/src/main.rs @@ -28,6 +28,8 @@ fn main() { // For a most up-to-date value, run it after that's been updated. .add_systems(PostUpdate, place_break_blocks.after(TransformSystems::Propagate)) + .add_observer(add_block_visuals) + .run(); } diff --git a/common/Cargo.toml b/common/Cargo.toml new file mode 100644 index 0000000..57c76f8 --- /dev/null +++ b/common/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "bevy-bloxel-classic-common" +version = "0.1.0" +edition = "2024" + +[dependencies] +bevy = { workspace = true } diff --git a/common/src/block.rs b/common/src/block.rs new file mode 100644 index 0000000..566061b --- /dev/null +++ b/common/src/block.rs @@ -0,0 +1,26 @@ +use bevy::ecs::system::SystemParam; +use bevy::prelude::*; + +#[derive(Component)] +pub struct Block; + +#[derive(SystemParam)] +pub struct Blocks<'w, 's> { + commands: Commands<'w, 's>, + blocks: Query<'w, 's, &'static Transform, With>, +} + +impl Blocks<'_, '_> { + pub fn spawn(&mut self, pos: IVec3) { + self.commands.spawn(( + Block, + Transform::from_translation(pos.as_vec3() + Vec3::ONE / 2.), + )); + } + + /// Gets the position of a block entity, or `None` if not a block. + pub fn position(&self, entity: Entity) -> Option { + let transform = self.blocks.get(entity).ok(); + transform.map(|t| t.translation.floor().as_ivec3()) + } +} diff --git a/common/src/lib.rs b/common/src/lib.rs new file mode 100644 index 0000000..a863eaa --- /dev/null +++ b/common/src/lib.rs @@ -0,0 +1 @@ +pub mod block;