diff --git a/client/src/main.rs b/client/src/main.rs index e2228c7..3992097 100644 --- a/client/src/main.rs +++ b/client/src/main.rs @@ -23,7 +23,9 @@ use renet_visualizer::{RenetClientVisualizer, RenetVisualizerStyle}; struct MainCamera; #[derive(Component)] -struct ControlledPlayer; +struct ControlledPlayer { + aiming_at: Vec2, +} #[derive(Default)] struct NetworkMapping(HashMap); @@ -118,12 +120,15 @@ fn update_visulizer_system( } fn player_input( + controlled_player_query: Query<(&Transform, &ControlledPlayer)>, keyboard_input: Res>, mut player_input: ResMut, mouse_button_input: Res>, mut player_commands: EventWriter, most_recent_tick: Res, ) { + let (transform, controlled_player) = controlled_player_query.single(); + player_input.left = keyboard_input.pressed(KeyCode::A) || keyboard_input.pressed(KeyCode::Left); player_input.right = keyboard_input.pressed(KeyCode::D) || keyboard_input.pressed(KeyCode::Right); @@ -132,17 +137,23 @@ 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, controlled_player.aiming_at + ); + player_commands.send(PlayerCommand::BasicAttack { - fired_at: Vec2::default(), // TODO: spawn projectiles correctly + origin: Vec2::new(transform.translation.x, transform.translation.y), + direction: controlled_player.aiming_at, }); } } fn cursor_system( wnds: Res, - q_camera: Query<(&Camera, &GlobalTransform), With>, + mut q_camera: Query<(&Camera, &mut ControlledPlayer, &GlobalTransform), With>, ) { - let (camera, camera_transform) = q_camera.single(); + let (camera, mut controlled_player, camera_transform) = q_camera.single_mut(); let wnd = if let RenderTarget::Window(id) = camera.target { wnds.get(id).unwrap() @@ -158,7 +169,7 @@ fn cursor_system( let world_pos = ndc_to_world.project_point3(ndc.extend(-1.0)); let world_pos: Vec2 = world_pos.truncate(); - info!("World coords: {}/{}", world_pos.x, world_pos.y); + controlled_player.aiming_at = world_pos; } } @@ -204,7 +215,9 @@ fn client_sync_players( }); if client_id == id { - client_entity.insert(ControlledPlayer); + client_entity.insert(ControlledPlayer { + aiming_at: Vec2::new(0., 0.), + }); } let player_info = PlayerInfo { @@ -230,7 +243,7 @@ fn client_sync_players( } ServerMessages::SpawnProjectile { entity, - position, + origin, direction, } => { let projectile_entity = commands.spawn_bundle(SpriteBundle { @@ -278,5 +291,7 @@ fn client_sync_players( fn setup_camera(mut commands: Commands) { commands .spawn_bundle(Camera2dBundle::default()) - .insert(MainCamera); + .insert(ControlledPlayer { + aiming_at: Vec2::new(0., 0.), + }); } diff --git a/server/src/main.rs b/server/src/main.rs index a3e7744..4cabe38 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -167,33 +167,22 @@ fn server_update_system( let command: PlayerCommand = bincode::deserialize(&message).unwrap(); match command { - PlayerCommand::BasicAttack { mut fired_at } => { - println!( - "Received basic attack from client {}: {:?}", - client_id, fired_at + PlayerCommand::BasicAttack { origin, direction } => { + info!( + "spawned attack on server at origin {} and direction {}", + origin, direction ); - if let Some(player_entity) = lobby.players.get(&client_id) { - if let Ok((_, player)) = players.get(*player_entity) { - fired_at = Vec2::new( - player.transform.translation.x, - player.transform.translation.y, - ); + let entity = spawn_projectile(&mut commands, origin, direction); + let message = ServerMessages::SpawnProjectile { + entity, + origin, + direction, + }; - let projectile_entity = - spawn_projectile(&mut commands, fired_at, fired_at); + let message = bincode::serialize(&message).unwrap(); - let message = ServerMessages::SpawnProjectile { - entity: projectile_entity, - position: fired_at, - direction: fired_at, - }; - - let message = bincode::serialize(&message).unwrap(); - - server.broadcast_message(ServerChannel::ServerMessages.id(), message); - } - } + server.broadcast_message(ServerChannel::ServerMessages.id(), message); } } } diff --git a/shared/src/lib.rs b/shared/src/lib.rs index 4764ec1..ab62763 100644 --- a/shared/src/lib.rs +++ b/shared/src/lib.rs @@ -36,7 +36,7 @@ pub struct Jumper { #[derive(Debug, Serialize, Deserialize, Component)] pub enum PlayerCommand { - BasicAttack { fired_at: Vec2 }, + BasicAttack { origin: Vec2, direction: Vec2 }, } pub enum ClientChannel { @@ -60,7 +60,7 @@ pub enum ServerMessages { }, SpawnProjectile { entity: Entity, - position: Vec2, + origin: Vec2, direction: Vec2, }, DespawnProjectile { @@ -152,7 +152,7 @@ pub fn setup_level(mut _commands: Commands) { info!("bygger level..."); } -pub fn spawn_projectile(commands: &mut Commands, location: Vec2, direction: Vec2) -> Entity { +pub fn spawn_projectile(commands: &mut Commands, origin: Vec2, direction: Vec2) -> Entity { commands .spawn() .insert(Collider::ball(0.1)) @@ -160,7 +160,7 @@ pub fn spawn_projectile(commands: &mut Commands, location: Vec2, direction: Vec2 .insert(ActiveEvents::COLLISION_EVENTS) .insert(Projectile { duration: Timer::from_seconds(1.5, false), - location, + origin, direction, }) .id() @@ -169,6 +169,6 @@ pub fn spawn_projectile(commands: &mut Commands, location: Vec2, direction: Vec2 #[derive(Debug, Component)] pub struct Projectile { pub duration: Timer, - pub location: Vec2, + pub origin: Vec2, pub direction: Vec2, }