diff options
author | Alexander Foremny <aforemny@posteo.de> | 2024-03-17 12:48:41 +0100 |
---|---|---|
committer | Alexander Foremny <aforemny@posteo.de> | 2024-03-17 12:57:55 +0100 |
commit | 4f9957fc6b4bdb4c7430f878101b07051b40a2ff (patch) | |
tree | fc9f2a4a4b8ef15a12ec10e6331bdc5ab3f2ef01 /src/client.rs | |
parent | 0436c10a90db450bdaf75ac43608e094bd9f0e8f (diff) |
feat: abilities
Diffstat (limited to 'src/client.rs')
-rw-r--r-- | src/client.rs | 32 |
1 files changed, 31 insertions, 1 deletions
diff --git a/src/client.rs b/src/client.rs index 3fef179..b2db950 100644 --- a/src/client.rs +++ b/src/client.rs @@ -5,6 +5,7 @@ use crate::shared::health::*; use crate::shared::imperative::*; use crate::shared::projectile::*; use crate::shared::*; +use bevy::input::keyboard::*; use bevy::input::mouse::MouseButton; use bevy::prelude::*; use bevy::sprite::{MaterialMesh2dBundle, Mesh2dHandle}; @@ -41,6 +42,9 @@ struct ClientId(pub u64); #[derive(Resource)] struct MyChampion(pub Champion); +#[derive(Resource)] +struct Attack(Option<AttackKey>); + struct ClientPlugin { pub server_addr: Option<SocketAddr>, pub client_id: u64, @@ -52,12 +56,14 @@ impl Plugin for ClientPlugin { fn build(&self, app: &mut App) { app.insert_resource(ClientId(self.client_id)) .insert_resource(MyChampion(self.champion)) + .insert_resource(Attack(None)) .add_plugins(NetworkPlugin { server_addr: self.server_addr.clone(), client_id: self.client_id, transport: self.transport.clone(), }) .add_systems(Startup, setup) + .add_systems(PreUpdate, choose_attack) .add_systems(Update, (move_players, move_projectiles)) .add_systems( Update, @@ -138,12 +144,16 @@ fn buffer_input( 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(target_player))); + 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))); @@ -152,6 +162,26 @@ fn buffer_input( } } +fn choose_attack(keyboard_input: Res<ButtonInput<KeyCode>>, mut attack_key: ResMut<Attack>) { + if keyboard_input.pressed(KeyCode::KeyA) { + attack_key.0 = Some(AttackKey::A); + } else if keyboard_input.pressed(KeyCode::KeyQ) { + attack_key.0 = Some(AttackKey::Q); + } else if keyboard_input.pressed(KeyCode::KeyW) { + attack_key.0 = Some(AttackKey::W); + } else if keyboard_input.pressed(KeyCode::KeyE) { + attack_key.0 = Some(AttackKey::E); + } else if keyboard_input.pressed(KeyCode::KeyR) { + attack_key.0 = Some(AttackKey::R); + } else if keyboard_input.pressed(KeyCode::KeyD) { + attack_key.0 = Some(AttackKey::D); + } else if keyboard_input.pressed(KeyCode::KeyF) { + attack_key.0 = Some(AttackKey::F); + } else if keyboard_input.pressed(KeyCode::Escape) { + attack_key.0 = None; + } +} + fn gizmos_hover_indicator( cameras: Query<(&Camera, &GlobalTransform)>, client_id: Res<ClientId>, |