aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLibravatar Alexander Foremny <aforemny@posteo.de>2024-03-23 13:57:47 +0100
committerLibravatar Alexander Foremny <aforemny@posteo.de>2024-03-23 13:57:47 +0100
commit12a6419eaa087f34bdde49b3f9227b3cb5575341 (patch)
tree8b8dbadc50e81cef1386c2711edf0479e37d35ec /src
parent23d32213ff8832d2c4360618eb511a0098153818 (diff)
feat: add client faction
Diffstat (limited to 'src')
-rw-r--r--src/client.rs18
-rw-r--r--src/main.rs5
-rw-r--r--src/protocol.rs5
-rw-r--r--src/shared/faction.rs21
-rw-r--r--src/shared/player.rs3
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();
}
@@ -48,6 +51,9 @@ struct ClientId(pub u64);
struct MyChampion(pub Champion);
#[derive(Resource)]
+struct MyFaction(pub Faction);
+
+#[derive(Resource)]
struct Attack(Option<AbilitySlot>);
struct ClientPlugin {
@@ -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<MyChampion>) {
+fn setup(
+ mut client: ClientMut,
+ mut commands: Commands,
+ champion: Res<MyChampion>,
+ faction: Res<MyFaction>,
+) {
commands.spawn(Camera2dBundle::default());
commands
.spawn(NodeBundle {
@@ -185,6 +198,9 @@ fn setup(mut client: ClientMut, mut commands: Commands, champion: Res<MyChampion
client
.send_message::<Channel1, SelectChampion>(SelectChampion(champion.0))
.unwrap();
+ client
+ .send_message::<Channel1, SelectFaction>(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::*;
@@ -27,11 +28,15 @@ impl UserAction for Inputs {}
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<Faction, String> {
+ 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,
}
}