From c6b4c64067cc25b7fa760230811a92e418a4abe3 Mon Sep 17 00:00:00 2001 From: CoCo_Sol Date: Sun, 31 Mar 2024 17:57:30 +0200 Subject: [PATCH 1/6] save --- crates/border-wars/src/lib.rs | 3 +++ crates/border-wars/src/map/ownership.rs | 9 +++++++++ 2 files changed, 12 insertions(+) create mode 100644 crates/border-wars/src/map/ownership.rs diff --git a/crates/border-wars/src/lib.rs b/crates/border-wars/src/lib.rs index 6f144ae..808e0d2 100644 --- a/crates/border-wars/src/lib.rs +++ b/crates/border-wars/src/lib.rs @@ -36,4 +36,7 @@ pub struct Player { /// The uuid of the player. pub uuid: Uuid, + + /// The color of the player. + pub color: (u8, u8, u8), } diff --git a/crates/border-wars/src/map/ownership.rs b/crates/border-wars/src/map/ownership.rs new file mode 100644 index 0000000..56dbe34 --- /dev/null +++ b/crates/border-wars/src/map/ownership.rs @@ -0,0 +1,9 @@ +//! All code related to the ownership of the tiles. + +use bevy::prelude::*; + +use crate::Player; + +/// The owner of a tile. +#[derive(Component, Clone)] +pub struct Owner(pub Player); -- 2.43.4 From e7b369eed81052a176d179463442c067fefa445c Mon Sep 17 00:00:00 2001 From: CoCo_Sol Date: Sun, 31 Mar 2024 22:40:52 +0200 Subject: [PATCH 2/6] save --- crates/border-wars/src/scenes/menu.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/border-wars/src/scenes/menu.rs b/crates/border-wars/src/scenes/menu.rs index 39a2239..809db1e 100644 --- a/crates/border-wars/src/scenes/menu.rs +++ b/crates/border-wars/src/scenes/menu.rs @@ -58,6 +58,7 @@ fn menu_ui( name: name.clone(), rank: PlayerRank::Player, uuid, + color: (0, 0, 0), }), )); } @@ -71,6 +72,7 @@ fn menu_ui( name: name.clone(), rank: PlayerRank::Admin, uuid, + color: (255, 255, 255), }); } }); -- 2.43.4 From c72137520a84ad8adf007f9d6346383dc6c13f07 Mon Sep 17 00:00:00 2001 From: CoCo_Sol Date: Mon, 1 Apr 2024 14:48:06 +0200 Subject: [PATCH 3/6] save --- crates/border-wars/src/main.rs | 2 ++ crates/border-wars/src/map/mod.rs | 1 + crates/border-wars/src/map/ownership.rs | 42 +++++++++++++++++++++++++ 3 files changed, 45 insertions(+) diff --git a/crates/border-wars/src/main.rs b/crates/border-wars/src/main.rs index 8019933..a7c9a63 100644 --- a/crates/border-wars/src/main.rs +++ b/crates/border-wars/src/main.rs @@ -3,6 +3,7 @@ use bevy::prelude::*; use border_wars::camera::CameraPlugin; use border_wars::map::generation::MapGenerationPlugin; +use border_wars::map::ownership::OwnershipPlugin; use border_wars::map::renderer::RendererPlugin; use border_wars::map::selected_tile::SelectTilePlugin; use border_wars::networking::NetworkingPlugin; @@ -19,5 +20,6 @@ fn main() { .add_plugins(NetworkingPlugin) .add_plugins(MapGenerationPlugin) .add_plugins(UiPlugin) + .add_plugins(OwnershipPlugin) .run(); } diff --git a/crates/border-wars/src/map/mod.rs b/crates/border-wars/src/map/mod.rs index 839b7e8..709fae2 100644 --- a/crates/border-wars/src/map/mod.rs +++ b/crates/border-wars/src/map/mod.rs @@ -2,6 +2,7 @@ pub mod generation; pub mod hex; +pub mod ownership; pub mod renderer; pub mod selected_tile; diff --git a/crates/border-wars/src/map/ownership.rs b/crates/border-wars/src/map/ownership.rs index 56dbe34..1321c4a 100644 --- a/crates/border-wars/src/map/ownership.rs +++ b/crates/border-wars/src/map/ownership.rs @@ -7,3 +7,45 @@ use crate::Player; /// The owner of a tile. #[derive(Component, Clone)] pub struct Owner(pub Player); + +/// The plugin to render the ownership of the tiles. +pub struct OwnershipPlugin; + +impl Plugin for OwnershipPlugin { + fn build(&self, app: &mut App) { + app.add_systems(Update, render_ownership) + .add_systems(Startup, setup_ownership_resources); + } +} + +/// The contrast of the ownership colors. +/// +/// The value is a number between 0 and 1. +#[derive(Resource)] +pub struct OwnershipColorContrast(pub f32); + +fn setup_ownership_resources(mut commands: Commands) { + commands.insert_resource(OwnershipColorContrast(0.4)); +} + +fn render_ownership( + mut query: Query<(&mut Sprite, &Owner), Changed>, + contrast: Res, +) { + for (mut sprite, owner) in query.iter_mut() { + let (r, g, b) = owner.0.color; + let target = mix_colors(Color::rgb_u8(r, g, b), sprite.color, 1. - contrast.0); + + sprite.color = target; + } +} + +// Mixes two colors. +fn mix_colors(color1: Color, color2: Color, alpha: f32) -> Color { + let [r1, g1, b1, _] = color1.as_rgba_u8(); + let [r2, g2, b2, _] = color2.as_rgba_u8(); + let mixed_r = ((1.0 - alpha) * r1 as f32 + alpha * r2 as f32).round() as u8; + let mixed_g = ((1.0 - alpha) * g1 as f32 + alpha * g2 as f32).round() as u8; + let mixed_b = ((1.0 - alpha) * b1 as f32 + alpha * b2 as f32).round() as u8; + Color::rgb_u8(mixed_r, mixed_g, mixed_b) +} -- 2.43.4 From 3f679665d98e46ae52ebe5703ed91918e26dcd54 Mon Sep 17 00:00:00 2001 From: CoCo_Sol Date: Mon, 1 Apr 2024 15:46:43 +0200 Subject: [PATCH 4/6] save --- crates/border-wars/src/map/ownership.rs | 9 +++++---- crates/border-wars/src/scenes/menu.rs | 4 ++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/crates/border-wars/src/map/ownership.rs b/crates/border-wars/src/map/ownership.rs index 1321c4a..3218215 100644 --- a/crates/border-wars/src/map/ownership.rs +++ b/crates/border-wars/src/map/ownership.rs @@ -28,6 +28,7 @@ fn setup_ownership_resources(mut commands: Commands) { commands.insert_resource(OwnershipColorContrast(0.4)); } +/// Render the ownership of the tiles by applying colors. fn render_ownership( mut query: Query<(&mut Sprite, &Owner), Changed>, contrast: Res, @@ -40,12 +41,12 @@ fn render_ownership( } } -// Mixes two colors. +/// Mixes two colors. fn mix_colors(color1: Color, color2: Color, alpha: f32) -> Color { let [r1, g1, b1, _] = color1.as_rgba_u8(); let [r2, g2, b2, _] = color2.as_rgba_u8(); - let mixed_r = ((1.0 - alpha) * r1 as f32 + alpha * r2 as f32).round() as u8; - let mixed_g = ((1.0 - alpha) * g1 as f32 + alpha * g2 as f32).round() as u8; - let mixed_b = ((1.0 - alpha) * b1 as f32 + alpha * b2 as f32).round() as u8; + let mixed_r = (1.0 - alpha).mul_add(r1 as f32, alpha * r2 as f32).round() as u8; + let mixed_g = (1.0 - alpha).mul_add(g1 as f32, alpha * g2 as f32).round() as u8; + let mixed_b = (1.0 - alpha).mul_add(b1 as f32, alpha * b2 as f32).round() as u8; Color::rgb_u8(mixed_r, mixed_g, mixed_b) } diff --git a/crates/border-wars/src/scenes/menu.rs b/crates/border-wars/src/scenes/menu.rs index 809db1e..a13de1a 100644 --- a/crates/border-wars/src/scenes/menu.rs +++ b/crates/border-wars/src/scenes/menu.rs @@ -58,7 +58,7 @@ fn menu_ui( name: name.clone(), rank: PlayerRank::Player, uuid, - color: (0, 0, 0), + color: rand::random::<(u8, u8, u8)>(), }), )); } @@ -72,7 +72,7 @@ fn menu_ui( name: name.clone(), rank: PlayerRank::Admin, uuid, - color: (255, 255, 255), + color: rand::random::<(u8, u8, u8)>(), }); } }); -- 2.43.4 From fdee1c330e539dfca103dde994623fe7c03198a1 Mon Sep 17 00:00:00 2001 From: CoCo_Sol Date: Mon, 1 Apr 2024 16:00:07 +0200 Subject: [PATCH 5/6] fix clippy --- crates/border-wars/src/map/ownership.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/border-wars/src/map/ownership.rs b/crates/border-wars/src/map/ownership.rs index 3218215..1fad8e6 100644 --- a/crates/border-wars/src/map/ownership.rs +++ b/crates/border-wars/src/map/ownership.rs @@ -24,6 +24,7 @@ impl Plugin for OwnershipPlugin { #[derive(Resource)] pub struct OwnershipColorContrast(pub f32); +/// Init resources related to the ownership of the tiles. fn setup_ownership_resources(mut commands: Commands) { commands.insert_resource(OwnershipColorContrast(0.4)); } -- 2.43.4 From 9b7c0229154fc68b42d86a5d204e8f9d9997c1aa Mon Sep 17 00:00:00 2001 From: CoCo_Sol Date: Mon, 1 Apr 2024 16:00:26 +0200 Subject: [PATCH 6/6] fix issues --- crates/border-wars/src/map/ownership.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/border-wars/src/map/ownership.rs b/crates/border-wars/src/map/ownership.rs index 1fad8e6..8a5b792 100644 --- a/crates/border-wars/src/map/ownership.rs +++ b/crates/border-wars/src/map/ownership.rs @@ -13,8 +13,8 @@ pub struct OwnershipPlugin; impl Plugin for OwnershipPlugin { fn build(&self, app: &mut App) { - app.add_systems(Update, render_ownership) - .add_systems(Startup, setup_ownership_resources); + app.add_systems(Update, render_ownership); + app.add_systems(Startup, setup_ownership_resources); } } -- 2.43.4