Add the ownership system (#96)
All checks were successful
Rust Checks / checks (push) Successful in 2m20s

closes: #67
Reviewed-on: #96
Reviewed-by: Raphaël <r.lauray@outlook.fr>
Co-authored-by: CoCo_Sol <solois.corentin@gmail.com>
Co-committed-by: CoCo_Sol <solois.corentin@gmail.com>
This commit is contained in:
CoCo_Sol 2024-04-03 15:44:17 +00:00 committed by Raphaël
parent 45b47947de
commit 5d520c917f
5 changed files with 61 additions and 0 deletions

View file

@ -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),
}

View file

@ -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();
}

View file

@ -2,6 +2,7 @@
pub mod generation;
pub mod hex;
pub mod ownership;
pub mod renderer;
pub mod selected_tile;

View file

@ -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<Owner>>,
contrast: Res<OwnershipColorContrast>,
) {
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)
}

View file

@ -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)>(),
});
}
});