From 7070fb035712afec12d63a3ded718002d809e8f9 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Thu, 4 Nov 2021 11:27:44 -0400 Subject: [PATCH] By the power of macros, reduce the amount of syntax required to register components --- src/main.rs | 46 ++++++++++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/src/main.rs b/src/main.rs index 0c05fd3..a5879af 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,6 +27,17 @@ use player::*; pub use rect::Rect; use visibility_system::VisibilitySystem; +/// Cut down on the amount of syntax to register components +macro_rules! register { + // $gs is needed to get the scope at the usage point + // $Type is the Component type that is being registered + ($gs: ident, $( $Type: ty ),*,) => { + $( + $gs.ecs.register::<$Type>(); + )* + } +} + pub const MAP_SIZE: usize = 80 * 50; #[derive(PartialEq, Copy, Clone)] @@ -183,22 +194,25 @@ fn main() -> rltk::BError { let mut gs = State { ecs: World::new() }; - gs.ecs.register::(); - gs.ecs.register::(); - gs.ecs.register::(); - gs.ecs.register::(); - gs.ecs.register::(); - gs.ecs.register::(); - gs.ecs.register::(); - gs.ecs.register::(); - gs.ecs.register::(); - gs.ecs.register::(); - gs.ecs.register::(); - gs.ecs.register::(); - gs.ecs.register::(); - gs.ecs.register::(); - gs.ecs.register::(); - gs.ecs.register::(); + register!( + gs, + Position, + Renderable, + Player, + Viewshed, + Monster, + Name, + BlocksTile, + CombatStats, + WantsToMelee, + SufferDamage, + Item, + Potion, + InBackpack, + WantsToPickupItem, + WantsToDrinkPotion, + WantsToDropItem, + ); let map: Map = Map::new_map_rooms_and_corridors(); let (player_x, player_y) = map.rooms[0].center();