Merge branch 'main' into select-spawnpoint
Some checks failed
Rust Checks / checks (push) Failing after 5s

This commit is contained in:
CoCo_Sol 2024-04-03 19:24:19 +02:00
commit bd6ab4cd34
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::map::spawnpoint::SpawnPointPlugin;
@ -21,5 +22,6 @@ fn main() {
.add_plugins(MapGenerationPlugin)
.add_plugins(UiPlugin)
.add_plugins(SpawnPointPlugin)
.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;
pub mod spawnpoint;

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