cleaner query

This commit is contained in:
Magnus von Wachenfeldt 2022-08-16 11:06:17 +02:00
parent 615d9d7080
commit 5ddbf6fc7d
Signed by: magnus
GPG Key ID: A469F7D71D09F795
3 changed files with 20 additions and 36 deletions

View File

@ -23,7 +23,10 @@ use renet_visualizer::{RenetClientVisualizer, RenetVisualizerStyle};
struct MainCamera; struct MainCamera;
#[derive(Component)] #[derive(Component)]
struct ControlledPlayer { struct ControlledPlayer;
#[derive(Component)]
struct Aimer {
aiming_at: Vec2, aiming_at: Vec2,
} }
@ -120,14 +123,14 @@ fn update_visulizer_system(
} }
fn player_input( fn player_input(
controlled_player_query: Query<(&Transform, &ControlledPlayer)>, controlled_player_query: Query<(&Transform, &Aimer)>,
keyboard_input: Res<Input<KeyCode>>, keyboard_input: Res<Input<KeyCode>>,
mut player_input: ResMut<PlayerInput>, mut player_input: ResMut<PlayerInput>,
mouse_button_input: Res<Input<MouseButton>>, mouse_button_input: Res<Input<MouseButton>>,
mut player_commands: EventWriter<PlayerCommand>, mut player_commands: EventWriter<PlayerCommand>,
most_recent_tick: Res<MostRecentTick>, most_recent_tick: Res<MostRecentTick>,
) { ) {
let (transform, controlled_player) = controlled_player_query.single(); let (transform, aimer) = controlled_player_query.single();
player_input.left = keyboard_input.pressed(KeyCode::A) || keyboard_input.pressed(KeyCode::Left); player_input.left = keyboard_input.pressed(KeyCode::A) || keyboard_input.pressed(KeyCode::Left);
player_input.right = player_input.right =
@ -139,21 +142,21 @@ fn player_input(
if mouse_button_input.just_pressed(MouseButton::Left) { if mouse_button_input.just_pressed(MouseButton::Left) {
info!( info!(
"player at origin {} fired at {}", "player at origin {} fired at {}",
transform.translation, controlled_player.aiming_at transform.translation, aimer.aiming_at
); );
player_commands.send(PlayerCommand::BasicAttack { player_commands.send(PlayerCommand::BasicAttack {
origin: Vec2::new(transform.translation.x, transform.translation.y), origin: Vec2::new(transform.translation.x, transform.translation.y),
direction: controlled_player.aiming_at, direction: aimer.aiming_at,
}); });
} }
} }
fn cursor_system( fn cursor_system(
wnds: Res<Windows>, wnds: Res<Windows>,
mut q_camera: Query<(&Camera, &mut ControlledPlayer, &GlobalTransform), With<ControlledPlayer>>, mut q_camera: Query<(&Camera, &mut Aimer, &GlobalTransform), With<ControlledPlayer>>,
) { ) {
let (camera, mut controlled_player, camera_transform) = q_camera.single_mut(); let (camera, mut aimer, camera_transform) = q_camera.single_mut();
let wnd = if let RenderTarget::Window(id) = camera.target { let wnd = if let RenderTarget::Window(id) = camera.target {
wnds.get(id).unwrap() wnds.get(id).unwrap()
@ -169,7 +172,7 @@ fn cursor_system(
let world_pos = ndc_to_world.project_point3(ndc.extend(-1.0)); let world_pos = ndc_to_world.project_point3(ndc.extend(-1.0));
let world_pos: Vec2 = world_pos.truncate(); let world_pos: Vec2 = world_pos.truncate();
controlled_player.aiming_at = world_pos; aimer.aiming_at = world_pos;
} }
} }
@ -215,7 +218,7 @@ fn client_sync_players(
}); });
if client_id == id { if client_id == id {
client_entity.insert(ControlledPlayer { client_entity.insert(ControlledPlayer).insert(Aimer {
aiming_at: Vec2::new(0., 0.), aiming_at: Vec2::new(0., 0.),
}); });
} }
@ -241,11 +244,7 @@ fn client_sync_players(
network_mapping.0.remove(&server_entity); network_mapping.0.remove(&server_entity);
} }
} }
ServerMessages::SpawnProjectile { ServerMessages::SpawnProjectile { entity } => {
entity,
origin,
direction,
} => {
let projectile_entity = commands.spawn_bundle(SpriteBundle { let projectile_entity = commands.spawn_bundle(SpriteBundle {
sprite: Sprite { sprite: Sprite {
color: Color::rgb(0.25, 0.25, 0.75), color: Color::rgb(0.25, 0.25, 0.75),
@ -291,7 +290,8 @@ fn client_sync_players(
fn setup_camera(mut commands: Commands) { fn setup_camera(mut commands: Commands) {
commands commands
.spawn_bundle(Camera2dBundle::default()) .spawn_bundle(Camera2dBundle::default())
.insert(ControlledPlayer { .insert(ControlledPlayer)
.insert(Aimer {
aiming_at: Vec2::new(0., 0.), aiming_at: Vec2::new(0., 0.),
}); });
} }

View File

@ -174,12 +174,7 @@ fn server_update_system(
); );
let entity = spawn_projectile(&mut commands, origin, direction); let entity = spawn_projectile(&mut commands, origin, direction);
let message = ServerMessages::SpawnProjectile { let message = ServerMessages::SpawnProjectile { entity };
entity,
origin,
direction,
};
let message = bincode::serialize(&message).unwrap(); let message = bincode::serialize(&message).unwrap();
server.broadcast_message(ServerChannel::ServerMessages.id(), message); server.broadcast_message(ServerChannel::ServerMessages.id(), message);

View File

@ -51,21 +51,10 @@ pub enum ServerChannel {
#[derive(Debug, Serialize, Deserialize, Component)] #[derive(Debug, Serialize, Deserialize, Component)]
pub enum ServerMessages { pub enum ServerMessages {
PlayerCreate { PlayerCreate { entity: Entity, id: u64 },
entity: Entity, PlayerRemove { id: u64 },
id: u64, SpawnProjectile { entity: Entity },
}, DespawnProjectile { entity: Entity },
PlayerRemove {
id: u64,
},
SpawnProjectile {
entity: Entity,
origin: Vec2,
direction: Vec2,
},
DespawnProjectile {
entity: Entity,
},
} }
#[derive(Debug, Serialize, Deserialize, Default)] #[derive(Debug, Serialize, Deserialize, Default)]