Skip to content

Commit

Permalink
Use array instead of Vec in UpgradeList
Browse files Browse the repository at this point in the history
  • Loading branch information
benfrankel committed Dec 15, 2023
1 parent fdbebda commit e33dda2
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions src/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))]
Expand Down Expand Up @@ -229,7 +231,7 @@ fn install_upgrades(world: &mut World, mut reader: Local<ManualEventReader<Upgra
}

// Update all upgrades
for kind in ALL_UPGRADE_KINDS {
for kind in UpgradeKind::iter() {
if let Some(update) = world.resource::<UpgradeList>()[kind].update {
world.run_system(update).unwrap();
}
Expand Down Expand Up @@ -291,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
Expand Down Expand Up @@ -364,15 +365,11 @@ 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]);

Expand Down

0 comments on commit e33dda2

Please sign in to comment.