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 d5258aa commit fdbebda
Showing 1 changed file with 18 additions and 19 deletions.
37 changes: 18 additions & 19 deletions src/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,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 @@ -247,23 +246,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 @@ -391,11 +373,28 @@ macro_rules! generate_upgrade_list {
$(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 fdbebda

Please sign in to comment.