From a4c87c5ba6219bc6e148a29c43e32dac7d7fad0a Mon Sep 17 00:00:00 2001 From: Ben Frankel Date: Fri, 15 Dec 2023 15:16:10 -0800 Subject: [PATCH] Use array instead of Vec in UpgradeList --- src/upgrade.rs | 48 ++++++++++++++++++++++-------------------------- 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/src/upgrade.rs b/src/upgrade.rs index 644ec78..1822349 100644 --- a/src/upgrade.rs +++ b/src/upgrade.rs @@ -8,6 +8,8 @@ use bevy_kira_audio::prelude::*; use rand::seq::SliceRandom; use rand::thread_rng; use strum::EnumCount; +use strum::EnumIter; +use strum::IntoEnumIterator; use crate::audio::AudioAssets; #[cfg(not(feature = "web"))] @@ -41,7 +43,6 @@ impl Plugin for UpgradePlugin { app.register_type::() .register_type::() .add_event::() - .init_resource::() .init_resource::() .add_systems( OnEnter(AppState::EditorScreen), @@ -230,7 +231,7 @@ fn install_upgrades(world: &mut World, mut reader: Local()[kind].update { world.run_system(update).unwrap(); } @@ -247,23 +248,6 @@ fn run_installed_upgrades(world: &mut World) { } } -#[derive(Resource, Default)] -pub struct UpgradeList(pub Vec); - -impl Index for UpgradeList { - type Output = Upgrade; - - fn index(&self, index: UpgradeKind) -> &Self::Output { - &self.0[index as usize] - } -} - -impl IndexMut for UpgradeList { - fn index_mut(&mut self, index: UpgradeKind) -> &mut Self::Output { - &mut self.0[index as usize] - } -} - #[derive(Resource, Reflect, Default)] #[reflect(Resource)] pub struct UpgradeSequence { @@ -309,8 +293,7 @@ impl UpgradeSequence { // Filter the list of all upgrade kinds into just the ones that are unlocked // Then, (weighted) randomly choose from those upgrades for the available slots - let mut upgrades = ALL_UPGRADE_KINDS - .into_iter() + let mut upgrades = UpgradeKind::iter() .filter(|&kind| { let upgrade = &upgrade_list[kind]; // This prevents the tutorial upgrades from being offered when @@ -382,20 +365,33 @@ fn load_upgrade_sequence(mut commands: Commands) { macro_rules! generate_upgrade_list { (|$world:ident| $($enumname:ident: $upgrade:expr),+ $(,)?) => { /// Enum containing all upgrade types. - #[derive(Reflect, Clone, Copy, PartialEq, Eq, Hash, EnumCount, Debug, PartialOrd, Ord)] + #[derive(Reflect, Debug, Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Hash, EnumCount, EnumIter)] pub enum UpgradeKind { $($enumname),+ } - pub const ALL_UPGRADE_KINDS: [UpgradeKind; UpgradeKind::COUNT] = [ - $(UpgradeKind::$enumname),+ - ]; + #[derive(Resource)] + pub struct UpgradeList(pub [Upgrade; UpgradeKind::COUNT]); + + impl Index for UpgradeList { + type Output = Upgrade; + + fn index(&self, index: UpgradeKind) -> &Self::Output { + &self.0[index as usize] + } + } + + impl IndexMut for UpgradeList { + fn index_mut(&mut self, index: UpgradeKind) -> &mut Self::Output { + &mut self.0[index as usize] + } + } /// A system that initializes and inserts the UpgradeList resource. fn load_upgrade_list($world: &mut World) { use UpgradeKind::*; - let upgrade_list = UpgradeList(vec![ + let upgrade_list = UpgradeList([ $($upgrade),+ ]);