Add `common` package with blocks module

main
copygirl 6 days ago
parent b6ac56aba4
commit ff8578fd82
  1. 8
      Cargo.lock
  2. 2
      Cargo.toml
  3. 2
      client/Cargo.toml
  4. 40
      client/src/block.rs
  5. 2
      client/src/main.rs
  6. 7
      common/Cargo.toml
  7. 26
      common/src/block.rs
  8. 1
      common/src/lib.rs

8
Cargo.lock generated

@ -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"

@ -1,6 +1,6 @@
[workspace]
resolver = "3"
members = ["client"]
members = ["client", "common"]
# Enable a small amount of optimization in the dev profile.
[profile.dev]

@ -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"

@ -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<Block>>,
}
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<IVec3> {
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<Add, Block>,
mut commands: Commands,
resources: Res<BlockResources>,
) {
commands.entity(add.entity).insert((
Mesh3d(resources.mesh.clone()),
MeshMaterial3d(resources.material.clone()),
));
}

@ -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();
}

@ -0,0 +1,7 @@
[package]
name = "bevy-bloxel-classic-common"
version = "0.1.0"
edition = "2024"
[dependencies]
bevy = { workspace = true }

@ -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<Block>>,
}
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<IVec3> {
let transform = self.blocks.get(entity).ok();
transform.map(|t| t.translation.floor().as_ivec3())
}
}

@ -0,0 +1 @@
pub mod block;
Loading…
Cancel
Save