cleaner query
This commit is contained in:
parent
615d9d7080
commit
5ddbf6fc7d
@ -23,7 +23,10 @@ use renet_visualizer::{RenetClientVisualizer, RenetVisualizerStyle};
|
||||
struct MainCamera;
|
||||
|
||||
#[derive(Component)]
|
||||
struct ControlledPlayer {
|
||||
struct ControlledPlayer;
|
||||
|
||||
#[derive(Component)]
|
||||
struct Aimer {
|
||||
aiming_at: Vec2,
|
||||
}
|
||||
|
||||
@ -120,14 +123,14 @@ fn update_visulizer_system(
|
||||
}
|
||||
|
||||
fn player_input(
|
||||
controlled_player_query: Query<(&Transform, &ControlledPlayer)>,
|
||||
controlled_player_query: Query<(&Transform, &Aimer)>,
|
||||
keyboard_input: Res<Input<KeyCode>>,
|
||||
mut player_input: ResMut<PlayerInput>,
|
||||
mouse_button_input: Res<Input<MouseButton>>,
|
||||
mut player_commands: EventWriter<PlayerCommand>,
|
||||
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.right =
|
||||
@ -139,21 +142,21 @@ fn player_input(
|
||||
if mouse_button_input.just_pressed(MouseButton::Left) {
|
||||
info!(
|
||||
"player at origin {} fired at {}",
|
||||
transform.translation, controlled_player.aiming_at
|
||||
transform.translation, aimer.aiming_at
|
||||
);
|
||||
|
||||
player_commands.send(PlayerCommand::BasicAttack {
|
||||
origin: Vec2::new(transform.translation.x, transform.translation.y),
|
||||
direction: controlled_player.aiming_at,
|
||||
direction: aimer.aiming_at,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
fn cursor_system(
|
||||
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 {
|
||||
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: 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 {
|
||||
client_entity.insert(ControlledPlayer {
|
||||
client_entity.insert(ControlledPlayer).insert(Aimer {
|
||||
aiming_at: Vec2::new(0., 0.),
|
||||
});
|
||||
}
|
||||
@ -241,11 +244,7 @@ fn client_sync_players(
|
||||
network_mapping.0.remove(&server_entity);
|
||||
}
|
||||
}
|
||||
ServerMessages::SpawnProjectile {
|
||||
entity,
|
||||
origin,
|
||||
direction,
|
||||
} => {
|
||||
ServerMessages::SpawnProjectile { entity } => {
|
||||
let projectile_entity = commands.spawn_bundle(SpriteBundle {
|
||||
sprite: Sprite {
|
||||
color: Color::rgb(0.25, 0.25, 0.75),
|
||||
@ -291,7 +290,8 @@ fn client_sync_players(
|
||||
fn setup_camera(mut commands: Commands) {
|
||||
commands
|
||||
.spawn_bundle(Camera2dBundle::default())
|
||||
.insert(ControlledPlayer {
|
||||
.insert(ControlledPlayer)
|
||||
.insert(Aimer {
|
||||
aiming_at: Vec2::new(0., 0.),
|
||||
});
|
||||
}
|
||||
|
@ -174,12 +174,7 @@ fn server_update_system(
|
||||
);
|
||||
|
||||
let entity = spawn_projectile(&mut commands, origin, direction);
|
||||
let message = ServerMessages::SpawnProjectile {
|
||||
entity,
|
||||
origin,
|
||||
direction,
|
||||
};
|
||||
|
||||
let message = ServerMessages::SpawnProjectile { entity };
|
||||
let message = bincode::serialize(&message).unwrap();
|
||||
|
||||
server.broadcast_message(ServerChannel::ServerMessages.id(), message);
|
||||
|
@ -51,21 +51,10 @@ pub enum ServerChannel {
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Component)]
|
||||
pub enum ServerMessages {
|
||||
PlayerCreate {
|
||||
entity: Entity,
|
||||
id: u64,
|
||||
},
|
||||
PlayerRemove {
|
||||
id: u64,
|
||||
},
|
||||
SpawnProjectile {
|
||||
entity: Entity,
|
||||
origin: Vec2,
|
||||
direction: Vec2,
|
||||
},
|
||||
DespawnProjectile {
|
||||
entity: Entity,
|
||||
},
|
||||
PlayerCreate { entity: Entity, id: u64 },
|
||||
PlayerRemove { id: u64 },
|
||||
SpawnProjectile { entity: Entity },
|
||||
DespawnProjectile { entity: Entity },
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Default)]
|
||||
|
Loading…
Reference in New Issue
Block a user