diff --git a/client/src/main.rs b/client/src/main.rs
index 58cf919..0e774b5 100644
--- a/client/src/main.rs
+++ b/client/src/main.rs
@@ -13,8 +13,8 @@ use bevy_renet::{
};
use daggmask_shared::{
- client_connection_config, setup_level, ClientChannel, NetworkFrame, PlayerCommand, PlayerInput,
- ServerChannel, ServerMessages, PROTOCOL_ID,
+ client_connection_config, setup_level, ClientChannel, NetworkFrame, PlayerCommand, PlayerId,
+ PlayerInput, ServerChannel, ServerMessages, PROTOCOL_ID,
};
use renet_visualizer::{RenetClientVisualizer, RenetVisualizerStyle};
@@ -123,14 +123,14 @@ fn update_visulizer_system(
}
fn player_input(
- controlled_player_query: Query<(&Transform, &Aimer)>,
+ controlled_player_query: Query<&Aimer>,
keyboard_input: Res>,
mut player_input: ResMut,
mouse_button_input: Res>,
mut player_commands: EventWriter,
most_recent_tick: Res,
) {
- let (transform, aimer) = controlled_player_query.single();
+ let aimer = controlled_player_query.single();
player_input.left = keyboard_input.pressed(KeyCode::A) || keyboard_input.pressed(KeyCode::Left);
player_input.right =
@@ -140,13 +140,9 @@ fn player_input(
player_input.most_recent_tick = most_recent_tick.0;
if mouse_button_input.just_pressed(MouseButton::Left) {
- info!(
- "player at origin {} fired at {}",
- transform.translation, aimer.aiming_at
- );
+ info!("player fired at {}", aimer.aiming_at);
player_commands.send(PlayerCommand::BasicAttack {
- origin: Vec2::new(transform.translation.x, transform.translation.y),
direction: aimer.aiming_at,
});
}
@@ -218,9 +214,12 @@ fn client_sync_players(
});
if client_id == id {
- client_entity.insert(ControlledPlayer).insert(Aimer {
- aiming_at: Vec2::new(0., 0.),
- });
+ client_entity
+ .insert(ControlledPlayer)
+ .insert(Aimer {
+ aiming_at: Vec2::new(0., 0.),
+ })
+ .insert(PlayerId { id });
}
let player_info = PlayerInfo {
diff --git a/server/src/main.rs b/server/src/main.rs
index 03def18..5945c48 100644
--- a/server/src/main.rs
+++ b/server/src/main.rs
@@ -84,6 +84,7 @@ fn main() {
#[allow(clippy::too_many_arguments)]
fn server_update_system(
mut server_events: EventReader,
+ pos_query: Query<(&Transform, &Player)>,
mut commands: Commands,
mut lobby: ResMut,
mut server: ResMut,
@@ -124,10 +125,7 @@ fn server_update_system(
.insert(ActiveEvents::COLLISION_EVENTS)
.insert(PlayerInput::default())
.insert(Velocity::default())
- .insert(Player {
- id: *id,
- transform: Transform::from_xyz(10., 10., 0.),
- })
+ .insert(Player { id: *id })
.insert(Jumper {
jump_impulse: 80.,
is_jumping: false,
@@ -167,17 +165,24 @@ fn server_update_system(
let command: PlayerCommand = bincode::deserialize(&message).unwrap();
match command {
- PlayerCommand::BasicAttack { origin, direction } => {
- info!(
- "spawned attack on server at origin {} and direction {}",
- origin, direction
- );
+ PlayerCommand::BasicAttack { direction } => {
+ for (transform, player) in pos_query.iter() {
+ if player.id == client_id {
+ let origin =
+ Vec2::new(transform.translation.x, transform.translation.y);
- let entity = spawn_projectile(&mut commands, origin, direction);
- let message = ServerMessages::SpawnProjectile { entity };
- let message = bincode::serialize(&message).unwrap();
+ info!(
+ "spawned attack on server at origin {} with direction {}",
+ origin, direction
+ );
- server.broadcast_message(ServerChannel::ServerMessages.id(), message);
+ let entity = spawn_projectile(&mut commands, origin, direction);
+ let message = ServerMessages::SpawnProjectile { entity };
+ let message = bincode::serialize(&message).unwrap();
+
+ server.broadcast_message(ServerChannel::ServerMessages.id(), message);
+ }
+ }
}
}
}
diff --git a/shared/src/lib.rs b/shared/src/lib.rs
index 79ecc5e..2cc912c 100644
--- a/shared/src/lib.rs
+++ b/shared/src/lib.rs
@@ -17,7 +17,11 @@ pub const PROTOCOL_ID: u64 = 7;
#[derive(Debug, Component)]
pub struct Player {
pub id: u64,
- pub transform: Transform,
+}
+
+#[derive(Debug, Component)]
+pub struct PlayerId {
+ pub id: u64,
}
#[derive(Debug, Default, Clone, Copy, Serialize, Deserialize, Component)]
@@ -36,7 +40,7 @@ pub struct Jumper {
#[derive(Debug, Serialize, Deserialize, Component)]
pub enum PlayerCommand {
- BasicAttack { origin: Vec2, direction: Vec2 },
+ BasicAttack { direction: Vec2 },
}
pub enum ClientChannel {
@@ -144,12 +148,15 @@ pub fn setup_level(mut _commands: Commands) {
pub fn spawn_projectile(commands: &mut Commands, origin: Vec2, direction: Vec2) -> Entity {
commands
.spawn()
+ .insert(Transform {
+ translation: Vec3::new(origin.x, origin.y, 0.),
+ ..Default::default()
+ })
.insert(Collider::ball(0.1))
.insert(Velocity::linear(direction * 10.))
.insert(ActiveEvents::COLLISION_EVENTS)
.insert(Projectile {
- duration: Timer::from_seconds(1.5, false),
- origin,
+ duration: Timer::from_seconds(3.5, false),
direction,
})
.id()
@@ -158,6 +165,5 @@ pub fn spawn_projectile(commands: &mut Commands, origin: Vec2, direction: Vec2)
#[derive(Debug, Component)]
pub struct Projectile {
pub duration: Timer,
- pub origin: Vec2,
pub direction: Vec2,
}