From fee78663de0ae386d772ec32ad874ccb203fa67d Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Thu, 4 Nov 2021 09:54:38 -0400 Subject: [PATCH] Complete chapter 2.8 --- src/components.rs | 1 + src/main.rs | 4 +++- src/spawner.rs | 3 +++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/components.rs b/src/components.rs index ec07bdc..aac8516 100644 --- a/src/components.rs +++ b/src/components.rs @@ -13,6 +13,7 @@ pub struct Renderable { pub glyph: rltk::FontCharType, pub fg: RGB, pub bg: RGB, + pub render_order: i32, } #[derive(Component, Debug)] diff --git a/src/main.rs b/src/main.rs index 821758f..36442cf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -157,7 +157,9 @@ impl GameState for State { let renderables = self.ecs.read_storage::(); let map = self.ecs.fetch::(); - for (pos, render) in (&positions, &renderables).join() { + let mut data = (&positions, &renderables).join().collect::>(); + data.sort_by(|&a, &b| b.1.render_order.cmp(&a.1.render_order)); + for (pos, render) in data.iter() { let idx = map.xy_idx(pos.x, pos.y); if map.visible_tiles[idx] { diff --git a/src/spawner.rs b/src/spawner.rs index 19c21db..b8d5fd3 100644 --- a/src/spawner.rs +++ b/src/spawner.rs @@ -19,6 +19,7 @@ pub fn player(ecs: &mut World, player_x: i32, player_y: i32) -> Entity { glyph: rltk::to_cp437('@'), fg: RGB::named(rltk::YELLOW), bg: RGB::named(rltk::BLACK), + render_order: 0, }) .with(Player {}) .with(Viewshed { @@ -68,6 +69,7 @@ fn monster(ecs: &mut World, x: i32, y: i32, glyph: rltk::FontCharTy glyph, fg: RGB::named(rltk::RED), bg: RGB::named(rltk::BLACK), + render_order: 1, }) .with(Viewshed { visible_tiles: Vec::new(), @@ -152,6 +154,7 @@ fn health_potion(ecs: &mut World, x: i32, y: i32) { glyph: rltk::to_cp437('ยก'), fg: RGB::named(rltk::MAGENTA), bg: RGB::named(rltk::BLACK), + render_order: 2, }) .with(Name { name: "Health Potion".to_string(),