diff options
-rw-r--r-- | src/client.rs | 32 | ||||
-rw-r--r-- | src/server.rs | 4 | ||||
-rw-r--r-- | src/shared/imperative.rs | 13 |
3 files changed, 45 insertions, 4 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>, diff --git a/src/server.rs b/src/server.rs index d9f2653..e3d5a72 100644 --- a/src/server.rs +++ b/src/server.rs @@ -198,7 +198,7 @@ fn imperative_attack_approach( ) { for (id, mut imperative) in players.iter_mut() { match *imperative { - Imperative::Attack(target_player) => { + Imperative::Attack(AttackKey::A, target_player) => { let Some(entity) = entity_map.0.get(&id.0) else { *imperative = Imperative::Idle; return; @@ -242,7 +242,7 @@ fn imperative_attack_attack( ) { for (id, mut imperative) in players.iter_mut() { match *imperative { - Imperative::Attack(target_player) => { + Imperative::Attack(AttackKey::A, target_player) => { let Some(entity) = entity_map.0.get(&id.0) else { *imperative = Imperative::Idle; return; diff --git a/src/shared/imperative.rs b/src/shared/imperative.rs index d9d727f..256ea1a 100644 --- a/src/shared/imperative.rs +++ b/src/shared/imperative.rs @@ -6,5 +6,16 @@ use serde::Serialize; pub enum Imperative { Idle, WalkTo(Vec2), - Attack(PlayerId), + Attack(AttackKey, PlayerId), +} + +#[derive(Resource, Copy, Clone, PartialEq, Debug, Deserialize, Serialize)] +pub enum AttackKey { + A, + Q, + W, + E, + R, + D, + F, } |