Skip to content

Commit

Permalink
refactor PlayerColors to be a simpler tuple struct
Browse files Browse the repository at this point in the history
implenting the suggested improvements made the tuple struct faster
than the hashmap lookup
  • Loading branch information
redbugz committed Jun 19, 2024
1 parent 5da5e64 commit e27e60f
Showing 1 changed file with 14 additions and 40 deletions.
54 changes: 14 additions & 40 deletions crates/controller/src/hud/minimap/fill.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use super::draw::DrawingParam;
use crate::ray::ScreenRay;
use ahash::AHashMap;
use bevy::{ecs::system::SystemParam, prelude::*};
use de_core::{
gamestate::GameState, objects::ObjectTypeComponent, player::PlayerComponent,
Expand All @@ -27,49 +26,23 @@ const MIN_ENTITY_SIZE: Vec2 = Vec2::splat(0.02);
const CAMERA_COLOR: Color = Color::rgb(0.9, 0.9, 0.9);

#[derive(Resource, Debug)]
struct PlayerColors {
player_colors: [Color; Player::MAX_PLAYERS],
object_colors: [AHashMap<ObjectType, Color>; Player::MAX_PLAYERS],
}
struct PlayerColors([Color; Player::MAX_PLAYERS]);
impl PlayerColors {
fn new(colors: [Color; Player::MAX_PLAYERS]) -> Self {
Self {
player_colors: colors,
object_colors: [
derive_colors(colors[0]),
derive_colors(colors[1]),
derive_colors(colors[2]),
derive_colors(colors[3]),
],
}
}

fn get_color(&self, player: Player, object_type: ObjectType) -> Color {
fn get_color(&self, player: Player, object_type: ActiveObjectType) -> Color {
let player_idx: usize = (player.to_num() - 1) as usize;
let default_color: Color = self.player_colors[player_idx];
let object_color = self.object_colors[player_idx].get(&object_type);

*object_color.unwrap_or(&default_color)
let player_color = self.0[player_idx].as_hsla();
match object_type {
ActiveObjectType::Building(_) => player_color,
ActiveObjectType::Unit(_) => player_color.with_s(0.7 * player_color.s()),
}
}
}
impl Default for PlayerColors {
fn default() -> Self {
Self::new(PLAYER_COLORS)
Self(PLAYER_COLORS)
}
}

fn derive_colors(base_color: Color) -> AHashMap<ObjectType, Color> {
let mut unit_color = base_color.clone();
unit_color.set_s(base_color.s() - 0.3);
let mut object_colors = AHashMap::new();
object_colors.insert(
ObjectType::Active(ActiveObjectType::Unit(UnitType::Attacker)),
unit_color,
);

object_colors
}

pub(super) struct FillPlugin;

impl Plugin for FillPlugin {
Expand Down Expand Up @@ -129,11 +102,12 @@ fn draw_entities_system(

for (transform, &player, &object_type) in entities.iter() {
let minimap_position = ui_coords.flat_to_rel(transform.translation.to_flat());
let color = colors.get_color(*player, *object_type);

let radius = solids.get(*object_type).ichnography().radius();
let rect_size = MIN_ENTITY_SIZE.max(ui_coords.size_to_rel(Vec2::splat(radius)));
drawing.rect(minimap_position, rect_size, color);
if let ObjectType::Active(active_object) = *object_type {
let color = colors.get_color(*player, active_object);
let radius = solids.get(*object_type).ichnography().radius();
let rect_size = MIN_ENTITY_SIZE.max(ui_coords.size_to_rel(Vec2::splat(radius)));
drawing.rect(minimap_position, rect_size, color);
}
}
}

Expand Down

0 comments on commit e27e60f

Please sign in to comment.