diff --git a/src/block_placement.rs b/src/block_placement.rs index e9adce0..aec44a3 100644 --- a/src/block_placement.rs +++ b/src/block_placement.rs @@ -1,8 +1,9 @@ -use shipyard::{UniqueViewMut, UniqueView, View, IntoIter}; +use shipyard::{UniqueViewMut, UniqueView, View, IntoIter, ViewMut, EntitiesViewMut}; use crate::{ player::MainPlayer, world::{raycast::LookingAtBlock, ChunkStorage, block::Block}, - input::{Inputs, PrevInputs} + input::{Inputs, PrevInputs}, + events::{EventComponent, player_actions::PlayerActionEvent}, }; pub fn block_placement_system( @@ -10,7 +11,10 @@ pub fn block_placement_system( raycast: View, input: UniqueView, prev_input: UniqueView, - mut world: UniqueViewMut + mut world: UniqueViewMut, + mut entities: EntitiesViewMut, + mut events: ViewMut, + mut player_events: ViewMut, ) { let action_place = input.action_b && !prev_input.0.action_b; let action_break = input.action_a && !prev_input.0.action_a; @@ -18,19 +22,27 @@ pub fn block_placement_system( //get raycast info let Some(ray) = (&main_player, &raycast).iter().next().unwrap().1/**/.0 else { return }; //update block - let place_position = if action_place { + let (place_position, place_block) = if action_place { let position = (ray.position - ray.direction * 0.5).floor().as_ivec3(); let Some(block) = world.get_block_mut(position) else { return }; *block = Block::Dirt; - position + (position, *block) } else { let Some(block) = world.get_block_mut(ray.block_position) else { return }; *block = Block::Air; - ray.block_position + (ray.block_position, *block) }; //mark chunk as dirty let (chunk_pos, _) = ChunkStorage::to_chunk_coords(place_position); let chunk = world.chunks.get_mut(&chunk_pos).unwrap(); chunk.dirty = true; + //send event + entities.add_entity( + (&mut events, &mut player_events), + (EventComponent, PlayerActionEvent::UpdatedBlock { + position: place_position, + block: place_block, + }) + ); } } diff --git a/src/events.rs b/src/events.rs index 3a37298..b7154da 100644 --- a/src/events.rs +++ b/src/events.rs @@ -2,6 +2,8 @@ use glam::UVec2; use shipyard::{World, Component, AllStoragesViewMut, SparseSet}; use glium::glutin::event::{Event, DeviceEvent, DeviceId, WindowEvent}; +pub mod player_actions; + #[derive(Component, Clone, Copy, Debug, Default)] pub struct EventComponent; diff --git a/src/events/player_actions.rs b/src/events/player_actions.rs new file mode 100644 index 0000000..09b8a0c --- /dev/null +++ b/src/events/player_actions.rs @@ -0,0 +1,36 @@ +use shipyard::{Component, View, ViewMut, EntitiesViewMut, IntoIter}; +use glam::{IVec3, Quat, Vec3}; + +use crate::{ + world::block::Block, + player::MainPlayer, + transform::Transform +}; +use super::EventComponent; + +#[derive(Component, Clone, Copy, Debug)] +pub enum PlayerActionEvent { + PositionChanged { + position: Vec3, + direction: Quat + }, + UpdatedBlock { + position: IVec3, + block: Block, + }, +} + +pub fn generate_move_events( + transforms: View, + player: View, + mut entities: EntitiesViewMut, + mut events: ViewMut, + mut actions: ViewMut, +) { + let Some((_, transform)) = (&player, transforms.inserted_or_modified()).iter().next() else { return }; + let (_, direction, position) = transform.0.to_scale_rotation_translation(); + entities.add_entity( + (&mut events, &mut actions), + (EventComponent, PlayerActionEvent::PositionChanged { position, direction }) + ); +} diff --git a/src/main.rs b/src/main.rs index 0dff5a4..791393c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,6 @@ +// allowed because systems often need a lot of arguments +#![allow(clippy::too_many_arguments)] + use shipyard::{ World, Workload, IntoWorkload, UniqueView, UniqueViewMut, @@ -44,7 +47,10 @@ use player::spawn_player; use prefabs::load_prefabs; use settings::load_settings; use camera::compute_cameras; -use events::{clear_events, process_glutin_events}; +use events::{ + clear_events, process_glutin_events, + player_actions::generate_move_events +}; use input::{init_input, process_inputs}; use fly_controller::update_controllers; use rendering::{ @@ -74,6 +80,7 @@ fn update() -> Workload { ( process_inputs, update_controllers, + generate_move_events, update_loaded_world_around_player, update_raycasts, block_placement_system,