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 17, 2023
1 parent d5258aa commit a4c87c5
Showing 1 changed file with 22 additions and 26 deletions.
48 changes: 22 additions & 26 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 @@ -41,7 +43,6 @@ impl Plugin for UpgradePlugin {
app.register_type::<UpgradeEvent>()
.register_type::<UpgradeSequence>()
.add_event::<UpgradeEvent>()
.init_resource::<UpgradeList>()
.init_resource::<UpgradeUpdateSystems>()
.add_systems(
OnEnter(AppState::EditorScreen),
Expand Down Expand Up @@ -230,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 All @@ -247,23 +248,6 @@ fn run_installed_upgrades(world: &mut World) {
}
}

#[derive(Resource, Default)]
pub struct UpgradeList(pub Vec<Upgrade>);

impl Index<UpgradeKind> for UpgradeList {
type Output = Upgrade;

fn index(&self, index: UpgradeKind) -> &Self::Output {
&self.0[index as usize]
}
}

impl IndexMut<UpgradeKind> 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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<UpgradeKind> for UpgradeList {
type Output = Upgrade;

fn index(&self, index: UpgradeKind) -> &Self::Output {
&self.0[index as usize]
}
}

impl IndexMut<UpgradeKind> 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),+
]);

Expand Down

0 comments on commit a4c87c5

Please sign in to comment.