From 12a6419eaa087f34bdde49b3f9227b3cb5575341 Mon Sep 17 00:00:00 2001 From: Alexander Foremny Date: Sat, 23 Mar 2024 13:57:47 +0100 Subject: feat: add client faction --- src/client.rs | 18 +++++++++++++++++- src/main.rs | 5 +++++ src/protocol.rs | 5 +++++ src/shared/faction.rs | 21 ++++++++++++++++++++- src/shared/player.rs | 3 +++ 5 files changed, 50 insertions(+), 2 deletions(-) diff --git a/src/client.rs b/src/client.rs index 7f1220e..fc781cc 100644 --- a/src/client.rs +++ b/src/client.rs @@ -6,6 +6,7 @@ use crate::shared::area_of_effect::*; use crate::shared::buffs::*; use crate::shared::champion::*; use crate::shared::cooldown::*; +use crate::shared::faction::*; use crate::shared::health::*; use crate::shared::health_event::*; use crate::shared::imperative::*; @@ -29,6 +30,7 @@ pub fn main( client_id: u64, transport: TransportConfig, champion: Champion, + faction: Faction, ) { App::new() .add_plugins(DefaultPlugins) @@ -37,6 +39,7 @@ pub fn main( client_id, transport, champion, + faction, }) .run(); } @@ -47,6 +50,9 @@ struct ClientId(pub u64); #[derive(Resource)] struct MyChampion(pub Champion); +#[derive(Resource)] +struct MyFaction(pub Faction); + #[derive(Resource)] struct Attack(Option); @@ -55,12 +61,14 @@ struct ClientPlugin { pub client_id: u64, pub transport: TransportConfig, pub champion: Champion, + pub faction: Faction, } impl Plugin for ClientPlugin { fn build(&self, app: &mut App) { app.insert_resource(ClientId(self.client_id)) .insert_resource(MyChampion(self.champion)) + .insert_resource(MyFaction(self.faction)) .insert_resource(Attack(None)) .add_plugins(NetworkPlugin { server_addr: self.server_addr.clone(), @@ -109,7 +117,12 @@ pub struct Hotbar(AbilitySlot); #[derive(Component, PartialEq, Eq, Debug)] pub struct HotbarCooldown(AbilitySlot); -fn setup(mut client: ClientMut, mut commands: Commands, champion: Res) { +fn setup( + mut client: ClientMut, + mut commands: Commands, + champion: Res, + faction: Res, +) { commands.spawn(Camera2dBundle::default()); commands .spawn(NodeBundle { @@ -185,6 +198,9 @@ fn setup(mut client: ClientMut, mut commands: Commands, champion: Res(SelectChampion(champion.0)) .unwrap(); + client + .send_message::(SelectFaction(faction.0)) + .unwrap(); } fn render_players( diff --git a/src/main.rs b/src/main.rs index 2326310..5b6a857 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ use crate::shared::champion::*; +use crate::shared::faction::*; use clap::Parser; use lightyear::transport::io::TransportConfig; use rand::Rng; @@ -20,6 +21,8 @@ struct Cli { server: bool, #[arg(long, default_value = "meele")] champion: Champion, + #[arg(long, default_value = "blue")] + faction: Faction, } fn main() { @@ -42,6 +45,7 @@ fn main() { client_id as u64, TransportConfig::UdpSocket(client_addr), cli.champion, + cli.faction, ); } else { let (from_server_send, from_server_recv) = crossbeam_channel::unbounded(); @@ -62,6 +66,7 @@ fn main() { send: to_server_send, }, cli.champion, + cli.faction, ); } } diff --git a/src/protocol.rs b/src/protocol.rs index c881428..c04fed6 100644 --- a/src/protocol.rs +++ b/src/protocol.rs @@ -3,6 +3,7 @@ use crate::shared::area_of_effect::*; use crate::shared::buffs::*; use crate::shared::champion::*; use crate::shared::cooldown::*; +use crate::shared::faction::*; use crate::shared::health::*; use crate::shared::health_event::*; use crate::shared::imperative::*; @@ -26,12 +27,16 @@ impl UserAction for Inputs {} #[derive(Message, Serialize, Deserialize, Clone, Debug, PartialEq)] pub struct SelectChampion(pub Champion); +#[derive(Message, Serialize, Deserialize, Clone, Debug, PartialEq)] +pub struct SelectFaction(pub Faction); + #[derive(Message, Serialize, Deserialize, Clone, Debug, PartialEq)] pub struct HealthChanged(pub HealthEvent); #[message_protocol(protocol = "MyProtocol")] pub enum Messages { SelectChampion(SelectChampion), + SelectFaction(SelectFaction), HealthChanged(HealthChanged), } diff --git a/src/shared/faction.rs b/src/shared/faction.rs index 4ff4a02..7719a0f 100644 --- a/src/shared/faction.rs +++ b/src/shared/faction.rs @@ -1,6 +1,7 @@ use crate::shared::*; +use std::str::FromStr; -#[derive(Component, Clone, Copy, PartialEq, Eq)] +#[derive(Component, Clone, Copy, PartialEq, Eq, Debug, Serialize, Deserialize)] pub enum Faction { Red, Blue, @@ -14,3 +15,21 @@ impl Faction { } } } + +impl FromStr for Faction { + type Err = String; + + fn from_str(s: &str) -> Result { + match s { + "red" => Ok(Faction::Red), + "blue" => Ok(Faction::Blue), + _ => Err(format!("unknown faction: {}", s)), + } + } +} + +impl Default for Faction { + fn default() -> Self { + Faction::Blue + } +} diff --git a/src/shared/player.rs b/src/shared/player.rs index 59e8853..6b21bac 100644 --- a/src/shared/player.rs +++ b/src/shared/player.rs @@ -1,3 +1,4 @@ +use crate::shared::faction::*; use crate::shared::activation::*; use crate::shared::buffs::*; use crate::shared::shape::*; @@ -17,6 +18,7 @@ pub struct PlayerBundle { buffs: Buffs, activation: Activation, shape: Shape, + faction: Faction, replicate: Replicate, } @@ -48,6 +50,7 @@ impl PlayerBundle { buffs: Buffs::default(), activation: Activation::default(), shape: Shape::player(), + faction: Faction::default(), replicate, } } -- cgit v1.2.3