From 5d520c917f711b9476977ef459eb6892bafee53d Mon Sep 17 00:00:00 2001 From: CoCo_Sol Date: Wed, 3 Apr 2024 15:44:17 +0000 Subject: [PATCH] Add the ownership system (#96) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit closes: #67 Reviewed-on: https://git.tipragot.fr/fish-cannard/border-wars/pulls/96 Reviewed-by: Raphaƫl Co-authored-by: CoCo_Sol Co-committed-by: CoCo_Sol --- crates/border-wars/src/lib.rs | 3 ++ crates/border-wars/src/main.rs | 2 + crates/border-wars/src/map/mod.rs | 1 + crates/border-wars/src/map/ownership.rs | 53 +++++++++++++++++++++++++ crates/border-wars/src/scenes/menu.rs | 2 + 5 files changed, 61 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 16b1b2d..fc115d4 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/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 new file mode 100644 index 0000000..8a5b792 --- /dev/null +++ b/crates/border-wars/src/map/ownership.rs @@ -0,0 +1,53 @@ +//! 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); + +/// 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); + app.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); + +/// Init resources related to the ownership of the tiles. +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, +) { + 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).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 39a2239..a13de1a 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: rand::random::<(u8, u8, u8)>(), }), )); } @@ -71,6 +72,7 @@ fn menu_ui( name: name.clone(), rank: PlayerRank::Admin, uuid, + color: rand::random::<(u8, u8, u8)>(), }); } });