From ef8d51de1f62aa151a44d4086bdb56e7d6a7034c Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Fri, 5 Nov 2021 10:41:47 -0400 Subject: [PATCH] Cut down on some struct boilerplate --- src/components.rs | 18 ++++++++++++++++++ src/spawner.rs | 20 ++++---------------- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/components.rs b/src/components.rs index cfd0a2a..71bdfe4 100644 --- a/src/components.rs +++ b/src/components.rs @@ -26,6 +26,16 @@ pub struct Viewshed { pub dirty: bool, } +impl Default for Viewshed { + fn default() -> Self { + Viewshed { + visible_tiles: Vec::new(), + range: 8, + dirty: true, + } + } +} + #[derive(Component, Debug)] pub struct Monster {} @@ -34,6 +44,14 @@ pub struct Name { pub name: String, } +impl Name { + pub fn new(s: S) -> Self { + Name { + name: s.to_string(), + } + } +} + #[derive(Component, Debug)] pub struct BlocksTile {} diff --git a/src/spawner.rs b/src/spawner.rs index 5383dad..37814b4 100644 --- a/src/spawner.rs +++ b/src/spawner.rs @@ -22,14 +22,8 @@ pub fn player(ecs: &mut World, player_x: i32, player_y: i32) -> Entity { render_order: 0, }) .with(Player {}) - .with(Viewshed { - visible_tiles: Vec::new(), - range: 8, - dirty: true, - }) - .with(Name { - name: "Player".to_string(), - }) + .with(Viewshed::default()) + .with(Name::new("Player")) .with(CombatStats { max_hp: 30, hp: 30, @@ -71,15 +65,9 @@ fn monster(ecs: &mut World, x: i32, y: i32, glyph: rltk::FontCharTy bg: RGB::named(rltk::BLACK), render_order: 1, }) - .with(Viewshed { - visible_tiles: Vec::new(), - range: 8, - dirty: true, - }) + .with(Viewshed::default()) .with(Monster {}) - .with(Name { - name: name.to_string(), - }) + .with(Name::new(name)) .with(BlocksTile {}) .with(CombatStats { max_hp: 16,