diff options
Diffstat (limited to 'src/client.rs')
-rw-r--r-- | src/client.rs | 89 |
1 files changed, 60 insertions, 29 deletions
diff --git a/src/client.rs b/src/client.rs index b2db950..7f70549 100644 --- a/src/client.rs +++ b/src/client.rs @@ -15,7 +15,6 @@ use std::net::SocketAddr; mod network; -const PLAYER_RADIUS: f32 = 10.; const PLAYER_HOVER_INDICATOR_RADIUS: f32 = 13.; const PLAYER_HOVER_RADIUS: f32 = 20.; @@ -111,23 +110,35 @@ fn render_projectiles( mut commands: Commands, mut materials: ResMut<Assets<ColorMaterial>>, mut meshes: ResMut<Assets<Mesh>>, - projectiles: Query<(Entity, &ProjectilePosition), Added<Projectile>>, + projectiles: Query<(Entity, &Projectile), Added<Projectile>>, ) { - for (entity, position) in projectiles.iter() { + for (entity, projectile) in projectiles.iter() { + let Some(position) = (match projectile.type_ { + ProjectileType::Free(FreeProjectile { position, .. }) => Some(position), + ProjectileType::Instant(InstantProjectile { .. }) => None, + ProjectileType::Targeted(TargetedProjectile { position, .. }) => Some(position), + }) else { + continue; + }; commands.entity(entity).insert(MaterialMesh2dBundle { mesh: Mesh2dHandle(meshes.add(Circle { radius: 2. })), material: materials.add(Color::RED), - transform: Transform::from_xyz(position.0.x, position.0.y, 1.), + transform: Transform::from_xyz(position.x, position.y, 1.), ..Default::default() }); } } -fn move_projectiles( - mut projectiles: Query<(&mut Transform, &ProjectilePosition), Changed<ProjectilePosition>>, -) { - for (mut transform, projectile_position) in projectiles.iter_mut() { - transform.translation = Vec3::new(projectile_position.0.x, projectile_position.0.y, 0.); +fn move_projectiles(mut projectiles: Query<(&mut Transform, &Projectile), Changed<Projectile>>) { + for (mut transform, projectile) in projectiles.iter_mut() { + let Some(position) = (match projectile.type_ { + ProjectileType::Free(FreeProjectile { position, .. }) => Some(position), + ProjectileType::Instant(InstantProjectile { .. }) => None, + ProjectileType::Targeted(TargetedProjectile { position, .. }) => Some(position), + }) else { + continue; + }; + transform.translation = Vec3::new(position.x, position.y, 0.); } } @@ -138,46 +149,66 @@ fn move_players(mut players: Query<(&mut Transform, &PlayerPosition), Changed<Pl } fn buffer_input( - attackables: Query<(&PlayerId, &PlayerPosition)>, + players: Query<(&PlayerId, &PlayerPosition)>, + mut attack: ResMut<Attack>, cameras: Query<(&Camera, &GlobalTransform)>, client_id: Res<ClientId>, mouse_input: Res<ButtonInput<MouseButton>>, mut client: ClientMut, windows: Query<&Window>, - attack: Res<Attack>, ) { if mouse_input.just_pressed(MouseButton::Left) { - if let Some((target_player, _)) = - hovered_other_player(&cameras, &client_id, &attackables, &windows) - { - client.add_input(Inputs::Imperative(Imperative::Attack( - attack.0.unwrap_or(AttackKey::A), - target_player, - ))); - } else { - if let Some(world_position) = cursor_world_position(&windows, &cameras) { - client.add_input(Inputs::Imperative(Imperative::WalkTo(world_position))); + match attack.0 { + Some(AttackKey::Q) => { + let Some(world_position) = cursor_world_position(&windows, &cameras) else { + return; + }; + let Some(position) = player_position(&client_id, &players) else { + return; + }; + let Some(direction) = (world_position - position.0).try_normalize() else { + return; + }; + client.add_input(Inputs::Imperative(Imperative::AttackDirection( + AttackKey::Q, + direction, + ))); + attack.0 = None; + } + _ => { + if let Some((target_player, _)) = + hovered_other_player(&cameras, &client_id, &players, &windows) + { + client.add_input(Inputs::Imperative(Imperative::AttackTarget( + attack.0.unwrap_or(AttackKey::A), + target_player, + ))); + } else { + if let Some(world_position) = cursor_world_position(&windows, &cameras) { + client.add_input(Inputs::Imperative(Imperative::WalkTo(world_position))); + } + } } } } } fn choose_attack(keyboard_input: Res<ButtonInput<KeyCode>>, mut attack_key: ResMut<Attack>) { - if keyboard_input.pressed(KeyCode::KeyA) { + if keyboard_input.just_pressed(KeyCode::KeyA) { attack_key.0 = Some(AttackKey::A); - } else if keyboard_input.pressed(KeyCode::KeyQ) { + } else if keyboard_input.just_pressed(KeyCode::KeyQ) { attack_key.0 = Some(AttackKey::Q); - } else if keyboard_input.pressed(KeyCode::KeyW) { + } else if keyboard_input.just_pressed(KeyCode::KeyW) { attack_key.0 = Some(AttackKey::W); - } else if keyboard_input.pressed(KeyCode::KeyE) { + } else if keyboard_input.just_pressed(KeyCode::KeyE) { attack_key.0 = Some(AttackKey::E); - } else if keyboard_input.pressed(KeyCode::KeyR) { + } else if keyboard_input.just_pressed(KeyCode::KeyR) { attack_key.0 = Some(AttackKey::R); - } else if keyboard_input.pressed(KeyCode::KeyD) { + } else if keyboard_input.just_pressed(KeyCode::KeyD) { attack_key.0 = Some(AttackKey::D); - } else if keyboard_input.pressed(KeyCode::KeyF) { + } else if keyboard_input.just_pressed(KeyCode::KeyF) { attack_key.0 = Some(AttackKey::F); - } else if keyboard_input.pressed(KeyCode::Escape) { + } else if keyboard_input.just_pressed(KeyCode::Escape) { attack_key.0 = None; } } |