Bloxel sandbox game similar to Minecraft "Classic" (2009) written in Rust with Bevy
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

30 lines
860 B

use bevy::prelude::*;
use lightyear::prelude::*;
use bevy::ecs::system::SystemParam;
use serde::{Deserialize, Serialize};
#[derive(Component, PartialEq, Deserialize, Serialize)]
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.),
Replicate::to_clients(NetworkTarget::All),
));
}
/// 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())
}
}