save
Some checks failed
Rust Checks / checks (push) Failing after 3m57s
Rust Checks / checks (pull_request) Failing after 3m8s

This commit is contained in:
CoCo_Sol 2024-04-01 14:48:06 +02:00
parent 2a474aafe4
commit c72137520a
3 changed files with 45 additions and 0 deletions

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

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