player move events

This commit is contained in:
griffi-gh 2023-01-30 00:21:03 +01:00
parent 71154b07bc
commit f5a4bd9603
4 changed files with 64 additions and 7 deletions

View file

@ -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<LookingAtBlock>,
input: UniqueView<Inputs>,
prev_input: UniqueView<PrevInputs>,
mut world: UniqueViewMut<ChunkStorage>
mut world: UniqueViewMut<ChunkStorage>,
mut entities: EntitiesViewMut,
mut events: ViewMut<EventComponent>,
mut player_events: ViewMut<PlayerActionEvent>,
) {
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,
})
);
}
}

View file

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

View file

@ -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<Transform>,
player: View<MainPlayer>,
mut entities: EntitiesViewMut,
mut events: ViewMut<EventComponent>,
mut actions: ViewMut<PlayerActionEvent>,
) {
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 })
);
}

View file

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