diff options
-rw-r--r-- | src/client.rs | 72 | ||||
-rw-r--r-- | src/main.rs | 4 | ||||
-rw-r--r-- | src/protocol.rs | 15 | ||||
-rw-r--r-- | src/server.rs | 41 | ||||
-rw-r--r-- | src/shared.rs | 12 | ||||
-rw-r--r-- | src/shared/ability.rs | 7 | ||||
-rw-r--r-- | src/shared/activation.rs | 6 | ||||
-rw-r--r-- | src/shared/area_of_effect.rs | 5 | ||||
-rw-r--r-- | src/shared/buffs.rs | 4 | ||||
-rw-r--r-- | src/shared/champion.rs | 7 | ||||
-rw-r--r-- | src/shared/cooldown.rs | 8 | ||||
-rw-r--r-- | src/shared/damage.rs | 2 | ||||
-rw-r--r-- | src/shared/faction.rs | 6 | ||||
-rw-r--r-- | src/shared/health.rs | 4 | ||||
-rw-r--r-- | src/shared/health_event.rs | 3 | ||||
-rw-r--r-- | src/shared/immovable.rs | 2 | ||||
-rw-r--r-- | src/shared/imperative.rs | 6 | ||||
-rw-r--r-- | src/shared/minion.rs | 9 | ||||
-rw-r--r-- | src/shared/name.rs | 6 | ||||
-rw-r--r-- | src/shared/nexus.rs | 10 | ||||
-rw-r--r-- | src/shared/player.rs | 14 | ||||
-rw-r--r-- | src/shared/projectile.rs | 5 | ||||
-rw-r--r-- | src/shared/shape.rs | 4 | ||||
-rw-r--r-- | src/shared/stats.rs | 4 | ||||
-rw-r--r-- | src/shared/tower.rs | 9 |
25 files changed, 175 insertions, 90 deletions
diff --git a/src/client.rs b/src/client.rs index b71b83c..aa073ab 100644 --- a/src/client.rs +++ b/src/client.rs @@ -10,6 +10,7 @@ use crate::shared::faction::*; use crate::shared::health::*; use crate::shared::health_event::*; use crate::shared::imperative::*; +use crate::shared::name::*; use crate::shared::player::*; use crate::shared::projectile::*; use crate::shared::shape::*; @@ -36,6 +37,7 @@ pub fn main( champion: Champion, faction: Faction, scale_factor: f32, + name: String, ) { App::new() .add_plugins(DefaultPlugins.set(WindowPlugin { @@ -51,6 +53,7 @@ pub fn main( transport, champion, faction, + name, }) .run(); } @@ -65,6 +68,9 @@ struct MyChampion(pub Champion); struct MyFaction(pub Faction); #[derive(Resource)] +struct MyName(String); + +#[derive(Resource)] struct Attack(Option<AbilitySlot>); #[derive(Resource, Default)] @@ -79,6 +85,7 @@ struct ClientPlugin { pub transport: TransportConfig, pub champion: Champion, pub faction: Faction, + pub name: String, } impl Plugin for ClientPlugin { @@ -86,6 +93,7 @@ impl Plugin for ClientPlugin { app.insert_resource(MyClientId(self.client_id)) .insert_resource(MyChampion(self.champion)) .insert_resource(MyFaction(self.faction)) + .insert_resource(MyName(self.name.clone())) .insert_resource(Attack(None)) .insert_resource(CameraControls::default()) .add_plugins(NetworkPlugin { @@ -150,6 +158,7 @@ fn setup( mut commands: Commands, champion: Res<MyChampion>, faction: Res<MyFaction>, + name: Res<MyName>, mut connection_manager: ResMut<ConnectionManager<MyProtocol>>, ) { commands.spawn(Camera2dBundle::default()); @@ -225,10 +234,11 @@ fn setup( }); client.connect().unwrap(); connection_manager - .send_message::<Input, SelectChampion>(SelectChampion(champion.0)) - .unwrap(); - connection_manager - .send_message::<Input, SelectFaction>(SelectFaction(faction.0)) + .send_message::<Input, Spawn>(Spawn { + champion: champion.0, + faction: faction.0, + name: name.0.clone(), + }) .unwrap(); } @@ -236,17 +246,51 @@ fn render_players( mut commands: Commands, mut materials: ResMut<Assets<ColorMaterial>>, mut meshes: ResMut<Assets<Mesh>>, - players: Query<(Entity, &PlayerPosition, &PlayerColor, &Shape), Added<PlayerId>>, + players: Query< + ( + Entity, + &PlayerPosition, + &PlayerColor, + &Shape, + &Faction, + Option<&Name_>, + ), + Added<PlayerId>, + >, ) { - for (entity, position, color, shape) in players.iter() { - commands.entity(entity).insert(MaterialMesh2dBundle { - mesh: Mesh2dHandle(meshes.add(Circle { - radius: shape.radius, - })), - material: materials.add(color.0), - transform: Transform::from_xyz(position.0.x, position.0.y, 0.), - ..Default::default() - }); + for (entity, position, color, shape, faction, name) in players.iter() { + commands + .entity(entity) + .insert(SpatialBundle { + transform: Transform::from_xyz(position.0.x, position.0.y, 0.), + ..Default::default() + }) + .with_children(|root| { + root.spawn(MaterialMesh2dBundle { + mesh: Mesh2dHandle(meshes.add(Circle { + radius: shape.radius, + })), + material: materials.add(color.0), + ..Default::default() + }); + match name { + Some(name) => { + root.spawn(Text2dBundle { + text: Text::from_section( + name.0.clone(), + TextStyle { + font_size: 10., + color: faction.to_color(), + ..Default::default() + }, + ), + transform: Transform::from_xyz(0., shape.radius + 12., 0.), + ..Default::default() + }); + } + None => {} + } + }); } } diff --git a/src/main.rs b/src/main.rs index 4852856..893b151 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,6 +26,8 @@ struct Cli { faction: Faction, #[arg(long, default_value = "1.0")] scale_factor: f32, + #[arg(long, default_value = "unknown")] + name: String, } fn main() { @@ -50,6 +52,7 @@ fn main() { cli.champion, cli.faction, cli.scale_factor, + cli.name, ); } else { let (from_server_send, from_server_recv) = crossbeam_channel::unbounded(); @@ -72,6 +75,7 @@ fn main() { cli.champion, cli.faction, cli.scale_factor, + cli.name, ); } } diff --git a/src/protocol.rs b/src/protocol.rs index f75cfab..bdc2098 100644 --- a/src/protocol.rs +++ b/src/protocol.rs @@ -7,12 +7,12 @@ use crate::shared::faction::*; use crate::shared::health::*; use crate::shared::health_event::*; use crate::shared::imperative::*; +use crate::shared::name::*; use crate::shared::player::*; use crate::shared::projectile::*; use crate::shared::shape::*; use crate::shared::stats::*; use crate::shared::tower::*; -use bevy::prelude::*; use lightyear::prelude::*; use serde::Deserialize; use serde::Serialize; @@ -24,18 +24,18 @@ pub enum Inputs { 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); +pub struct Spawn { + pub champion: Champion, + pub faction: Faction, + pub name: String, +} #[derive(Message, Serialize, Deserialize, Clone, Debug, PartialEq)] pub struct HealthChanged(pub HealthEvent); #[message_protocol(protocol = "MyProtocol")] pub enum Messages { - SelectChampion(SelectChampion), - SelectFaction(SelectFaction), + Spawn(Spawn), HealthChanged(HealthChanged), Imperative(Imperative), Activation(Activation), @@ -57,6 +57,7 @@ pub enum Components { Shape(Shape), Tower(Tower), Faction(Faction), + Name(Name_), } #[derive(Channel)] diff --git a/src/server.rs b/src/server.rs index b6c23c6..6b339d0 100644 --- a/src/server.rs +++ b/src/server.rs @@ -13,6 +13,7 @@ use crate::shared::health_event::*; use crate::shared::immovable::*; use crate::shared::imperative::*; use crate::shared::minion::*; +use crate::shared::name::*; use crate::shared::nexus::*; use crate::shared::player::*; use crate::shared::projectile::*; @@ -61,13 +62,7 @@ impl Plugin for ServerPlugin { }) .add_systems(Startup, setup) .add_systems(Update, (connects, disconnects)) - .add_systems( - Update, - ( - receive_message_select_champion, - receive_message_select_faction, - ), - ) + .add_systems(Update, receive_message_spawn) .add_systems( Update, (receive_message_imperative, receive_message_activation), @@ -179,38 +174,30 @@ fn connects( 50. * rng.gen_range(-2..=2) as f32, ); let color = Color::hsl(360. * rng.gen_range(0..=15) as f32 / 16., 0.95, 0.7); - let entity = commands.spawn(PlayerBundle::new(*client_id, position, color)); + let entity = commands.spawn(PlayerBundle::new( + *client_id, + "unknown".into(), + position, + color, + )); entity_map.0.insert(*client_id, entity.id()); } } -fn receive_message_select_champion( - entity_map: Res<EntityMap>, - mut commands: Commands, - mut reader: EventReader<MessageEvent<SelectChampion>>, -) { - for event in reader.read() { - let client_id = event.context(); - let SelectChampion(champion) = event.message(); - let Some(entity) = entity_map.0.get(client_id) else { - continue; - }; - commands.entity(*entity).insert(*champion); - } -} - -fn receive_message_select_faction( +fn receive_message_spawn( entity_map: Res<EntityMap>, mut commands: Commands, - mut reader: EventReader<MessageEvent<SelectFaction>>, + mut reader: EventReader<MessageEvent<Spawn>>, ) { for event in reader.read() { let client_id = event.context(); - let SelectFaction(faction) = event.message(); + let spawn = event.message(); let Some(entity) = entity_map.0.get(client_id) else { continue; }; - commands.entity(*entity).insert(*faction); + commands + .entity(*entity) + .insert((spawn.champion, spawn.faction, Name_(spawn.name.clone()))); } } diff --git a/src/shared.rs b/src/shared.rs index 172ce6b..546ef4f 100644 --- a/src/shared.rs +++ b/src/shared.rs @@ -1,14 +1,3 @@ -use crate::protocol::Replicate; -use crate::shared::champion::*; -use crate::shared::cooldown::*; -use crate::shared::health::*; -use crate::shared::imperative::*; -use bevy::prelude::*; -use lightyear::prelude::*; -use serde::Deserialize; -use serde::Serialize; -use std::default::Default; - pub mod ability; pub mod activation; pub mod area_of_effect; @@ -22,6 +11,7 @@ pub mod health_event; pub mod immovable; pub mod imperative; pub mod minion; +pub mod name; pub mod nexus; pub mod player; pub mod projectile; diff --git a/src/shared/ability.rs b/src/shared/ability.rs index 66d236f..63780ff 100644 --- a/src/shared/ability.rs +++ b/src/shared/ability.rs @@ -2,12 +2,15 @@ use crate::server::entity_map::*; use crate::shared::area_of_effect::*; use crate::shared::buffs::*; use crate::shared::damage::*; +use crate::shared::health::*; +use crate::shared::imperative::*; use crate::shared::player::*; use crate::shared::projectile::*; use crate::shared::shape::*; -use crate::shared::*; use bevy::ecs::system::*; -use bevy::utils::Duration; +use bevy::prelude::*; +use bevy::utils::*; +use serde::*; use std::ops::*; #[derive(Copy, Clone, PartialEq, Debug, Deserialize, Serialize)] diff --git a/src/shared/activation.rs b/src/shared/activation.rs index 3ca6956..e2ff304 100644 --- a/src/shared/activation.rs +++ b/src/shared/activation.rs @@ -1,7 +1,7 @@ use crate::shared::ability::*; -use crate::shared::*; -use serde::Deserialize; -use serde::Serialize; +use bevy::prelude::*; +use lightyear::prelude::*; +use serde::*; #[derive(Component, Message, Copy, Clone, PartialEq, Debug, Deserialize, Serialize)] pub enum Activation { diff --git a/src/shared/area_of_effect.rs b/src/shared/area_of_effect.rs index dcdc86c..11608ad 100644 --- a/src/shared/area_of_effect.rs +++ b/src/shared/area_of_effect.rs @@ -1,8 +1,11 @@ +use crate::protocol::*; use crate::server::entity_map::*; use crate::shared::buffs::*; use crate::shared::player::*; -use crate::shared::*; use bevy::ecs::system::*; +use bevy::prelude::*; +use lightyear::prelude::*; +use serde::*; #[derive(Bundle)] pub struct AreaOfEffectBundle { diff --git a/src/shared/buffs.rs b/src/shared/buffs.rs index 868f3a3..be8ca33 100644 --- a/src/shared/buffs.rs +++ b/src/shared/buffs.rs @@ -1,4 +1,6 @@ -use crate::shared::*; +use bevy::prelude::*; +use lightyear::prelude::*; +use serde::*; #[derive(Clone, Message, Component, Default, Debug, PartialEq, Serialize, Deserialize)] pub struct Buffs { diff --git a/src/shared/champion.rs b/src/shared/champion.rs index c082ba3..9a1c7e2 100644 --- a/src/shared/champion.rs +++ b/src/shared/champion.rs @@ -1,10 +1,13 @@ use crate::shared::ability::*; +use crate::shared::cooldown::*; use crate::shared::damage::*; use crate::shared::shape::*; use crate::shared::stats::*; -use crate::shared::*; +use bevy::prelude::*; use bevy::utils::*; -use std::str::FromStr; +use lightyear::prelude::*; +use serde::*; +use std::str::*; #[derive(Component, Message, Clone, Copy, Serialize, Deserialize, PartialEq, Debug)] pub enum Champion { diff --git a/src/shared/cooldown.rs b/src/shared/cooldown.rs index 6995941..e2c3318 100644 --- a/src/shared/cooldown.rs +++ b/src/shared/cooldown.rs @@ -1,8 +1,8 @@ -use crate::shared::*; +use bevy::prelude::*; use bevy::utils::Duration; -use serde::Deserialize; -use serde::Serialize; -use std::default::Default; +use lightyear::prelude::*; +use serde::*; +use std::default::*; // TODO `AbilitySlot::A`'s cooldown is unused // diff --git a/src/shared/damage.rs b/src/shared/damage.rs index c336d87..5d975cc 100644 --- a/src/shared/damage.rs +++ b/src/shared/damage.rs @@ -1,5 +1,5 @@ use crate::shared::stats::*; -use crate::shared::*; +use serde::*; #[derive(Clone, Copy, PartialEq, Debug, Serialize, Deserialize)] pub enum Damage { diff --git a/src/shared/faction.rs b/src/shared/faction.rs index d3086d8..be9aafc 100644 --- a/src/shared/faction.rs +++ b/src/shared/faction.rs @@ -1,5 +1,7 @@ -use crate::shared::*; -use std::str::FromStr; +use bevy::prelude::*; +use lightyear::prelude::*; +use serde::*; +use std::str::*; #[derive(Message, Component, Clone, Copy, PartialEq, Eq, Debug, Serialize, Deserialize)] pub enum Faction { diff --git a/src/shared/health.rs b/src/shared/health.rs index f9cc300..03c0822 100644 --- a/src/shared/health.rs +++ b/src/shared/health.rs @@ -1,4 +1,6 @@ -use crate::shared::*; +use bevy::prelude::*; +use lightyear::prelude::*; +use serde::*; #[derive(Component, Message, Serialize, Deserialize, Clone, Copy, Debug, PartialEq)] pub struct Health { diff --git a/src/shared/health_event.rs b/src/shared/health_event.rs index 10af4ed..e41bb42 100644 --- a/src/shared/health_event.rs +++ b/src/shared/health_event.rs @@ -1,5 +1,6 @@ use crate::shared::player::*; -use crate::shared::*; +use lightyear::prelude::*; +use serde::*; #[derive(Message, Serialize, Deserialize, PartialEq, Clone, Debug)] pub struct HealthEvent { diff --git a/src/shared/immovable.rs b/src/shared/immovable.rs index ef4b3f0..bf3c7df 100644 --- a/src/shared/immovable.rs +++ b/src/shared/immovable.rs @@ -1,4 +1,4 @@ -use crate::shared::*; +use bevy::prelude::*; #[derive(Component)] pub struct Immovable; diff --git a/src/shared/imperative.rs b/src/shared/imperative.rs index 4291368..d11f6ee 100644 --- a/src/shared/imperative.rs +++ b/src/shared/imperative.rs @@ -1,8 +1,8 @@ use crate::shared::ability::*; use crate::shared::player::*; -use crate::shared::*; -use serde::Deserialize; -use serde::Serialize; +use bevy::prelude::*; +use lightyear::prelude::*; +use serde::*; #[derive(Component, Message, Copy, Clone, PartialEq, Debug, Deserialize, Serialize)] pub enum Imperative { diff --git a/src/shared/minion.rs b/src/shared/minion.rs index 4aa824e..3b58d37 100644 --- a/src/shared/minion.rs +++ b/src/shared/minion.rs @@ -1,10 +1,17 @@ +use crate::protocol::*; use crate::shared::activation::*; use crate::shared::buffs::*; +use crate::shared::champion::*; +use crate::shared::cooldown::*; use crate::shared::faction::*; +use crate::shared::health::*; +use crate::shared::imperative::*; use crate::shared::player::*; use crate::shared::shape::*; use crate::shared::stats::*; -use crate::shared::*; +use bevy::prelude::*; +use lightyear::prelude::*; +use serde::*; #[derive(Bundle)] pub struct MinionBundle { diff --git a/src/shared/name.rs b/src/shared/name.rs new file mode 100644 index 0000000..acf9db5 --- /dev/null +++ b/src/shared/name.rs @@ -0,0 +1,6 @@ +use bevy::prelude::*; +use lightyear::prelude::*; +use serde::*; + +#[derive(Component, Message, Clone, Serialize, Deserialize, Debug, PartialEq, Eq)] +pub struct Name_(pub String); diff --git a/src/shared/nexus.rs b/src/shared/nexus.rs index df91db9..e1f846a 100644 --- a/src/shared/nexus.rs +++ b/src/shared/nexus.rs @@ -1,12 +1,18 @@ +use crate::protocol::*; use crate::shared::activation::*; use crate::shared::buffs::*; +use crate::shared::champion::*; +use crate::shared::cooldown::*; use crate::shared::faction::*; +use crate::shared::health::*; use crate::shared::immovable::*; +use crate::shared::imperative::*; use crate::shared::player::*; use crate::shared::shape::*; use crate::shared::stats::*; -use crate::shared::*; -use bevy::utils::Duration; +use bevy::prelude::*; +use bevy::utils::*; +use lightyear::prelude::*; #[derive(Bundle)] pub struct NexusBundle { diff --git a/src/shared/player.rs b/src/shared/player.rs index 606a3b2..f65851d 100644 --- a/src/shared/player.rs +++ b/src/shared/player.rs @@ -1,9 +1,17 @@ +use crate::protocol::*; use crate::shared::activation::*; use crate::shared::buffs::*; +use crate::shared::champion::*; +use crate::shared::cooldown::*; use crate::shared::faction::*; +use crate::shared::health::*; +use crate::shared::imperative::*; +use crate::shared::name::*; use crate::shared::shape::*; use crate::shared::stats::*; -use crate::shared::*; +use bevy::prelude::*; +use lightyear::prelude::*; +use serde::*; #[derive(Bundle)] pub struct PlayerBundle { @@ -20,11 +28,12 @@ pub struct PlayerBundle { shape: Shape, faction: Faction, player: Player, + name: Name_, replicate: Replicate, } impl PlayerBundle { - pub fn new(id: ClientId, position: Vec2, color: Color) -> Self { + pub fn new(id: ClientId, name: String, position: Vec2, color: Color) -> Self { let mut replicate = Replicate { replication_group: ReplicationGroup::default().set_priority(10.), ..Default::default() @@ -53,6 +62,7 @@ impl PlayerBundle { shape: Shape::player(), faction: Faction::default(), player: Player::default(), + name: Name_(name), replicate, } } diff --git a/src/shared/projectile.rs b/src/shared/projectile.rs index 8fd0070..74dc260 100644 --- a/src/shared/projectile.rs +++ b/src/shared/projectile.rs @@ -1,6 +1,9 @@ +use crate::protocol::*; use crate::shared::damage::*; use crate::shared::player::*; -use crate::shared::*; +use bevy::prelude::*; +use lightyear::prelude::*; +use serde::*; #[derive(Bundle)] pub struct ProjectileBundle { diff --git a/src/shared/shape.rs b/src/shared/shape.rs index d77403d..6cbc901 100644 --- a/src/shared/shape.rs +++ b/src/shared/shape.rs @@ -1,4 +1,6 @@ -use crate::shared::*; +use bevy::prelude::*; +use lightyear::prelude::*; +use serde::*; #[derive(Component, Message, Clone, Copy, Serialize, Deserialize, Debug, PartialEq)] pub struct Shape { diff --git a/src/shared/stats.rs b/src/shared/stats.rs index 98c26aa..45f3352 100644 --- a/src/shared/stats.rs +++ b/src/shared/stats.rs @@ -1,4 +1,6 @@ -use crate::shared::*; +use bevy::prelude::*; +use lightyear::prelude::*; +use serde::*; #[derive(Clone, Copy, Serialize, Deserialize, PartialEq, Debug)] pub struct Stats { diff --git a/src/shared/tower.rs b/src/shared/tower.rs index 28ccc13..d19f272 100644 --- a/src/shared/tower.rs +++ b/src/shared/tower.rs @@ -1,11 +1,18 @@ +use crate::protocol::*; use crate::shared::activation::*; use crate::shared::buffs::*; +use crate::shared::champion::*; +use crate::shared::cooldown::*; use crate::shared::faction::*; +use crate::shared::health::*; use crate::shared::immovable::*; +use crate::shared::imperative::*; use crate::shared::player::*; use crate::shared::shape::*; use crate::shared::stats::*; -use crate::shared::*; +use bevy::prelude::*; +use lightyear::prelude::*; +use serde::*; #[derive(Bundle)] pub struct TowerBundle { |