By the power of macros, reduce the amount of syntax required to register components

This commit is contained in:
Timothy Warren 2021-11-04 11:27:44 -04:00
parent cb475708c7
commit 7070fb0357

View File

@ -27,6 +27,17 @@ use player::*;
pub use rect::Rect; pub use rect::Rect;
use visibility_system::VisibilitySystem; 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; pub const MAP_SIZE: usize = 80 * 50;
#[derive(PartialEq, Copy, Clone)] #[derive(PartialEq, Copy, Clone)]
@ -183,22 +194,25 @@ fn main() -> rltk::BError {
let mut gs = State { ecs: World::new() }; let mut gs = State { ecs: World::new() };
gs.ecs.register::<Position>(); register!(
gs.ecs.register::<Renderable>(); gs,
gs.ecs.register::<Player>(); Position,
gs.ecs.register::<Viewshed>(); Renderable,
gs.ecs.register::<Monster>(); Player,
gs.ecs.register::<Name>(); Viewshed,
gs.ecs.register::<BlocksTile>(); Monster,
gs.ecs.register::<CombatStats>(); Name,
gs.ecs.register::<WantsToMelee>(); BlocksTile,
gs.ecs.register::<SufferDamage>(); CombatStats,
gs.ecs.register::<Item>(); WantsToMelee,
gs.ecs.register::<Potion>(); SufferDamage,
gs.ecs.register::<InBackpack>(); Item,
gs.ecs.register::<WantsToPickupItem>(); Potion,
gs.ecs.register::<WantsToDrinkPotion>(); InBackpack,
gs.ecs.register::<WantsToDropItem>(); WantsToPickupItem,
WantsToDrinkPotion,
WantsToDropItem,
);
let map: Map = Map::new_map_rooms_and_corridors(); let map: Map = Map::new_map_rooms_and_corridors();
let (player_x, player_y) = map.rooms[0].center(); let (player_x, player_y) = map.rooms[0].center();