From a47c0d7ebac607aff7314edeb07cf09324bb5826 Mon Sep 17 00:00:00 2001 From: Logan Allred Date: Wed, 19 Jun 2024 11:10:54 -0600 Subject: [PATCH] refactor PlayerColors to be a simpler tuple struct implenting the suggested improvements made the tuple struct faster than the hashmap lookup --- crates/controller/src/hud/minimap/fill.rs | 54 ++++++----------------- 1 file changed, 14 insertions(+), 40 deletions(-) diff --git a/crates/controller/src/hud/minimap/fill.rs b/crates/controller/src/hud/minimap/fill.rs index 5af24641..b8c6158f 100644 --- a/crates/controller/src/hud/minimap/fill.rs +++ b/crates/controller/src/hud/minimap/fill.rs @@ -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, @@ -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; 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 { - 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 { @@ -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); + } } }