draw_system.rs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. use crate::components::{Position, Renderable};
  2. use crate::map;
  3. use rltk::{Console, Rltk};
  4. use specs::{Join, ReadExpect, ReadStorage, System};
  5. pub struct DrawRenderables<'r> {
  6. pub ctx: &'r mut Rltk,
  7. }
  8. impl<'a, 'r> System<'a> for DrawRenderables<'r> {
  9. type SystemData = (
  10. ReadStorage<'a, Position>,
  11. ReadStorage<'a, Renderable>,
  12. ReadExpect<'a, map::Map>,
  13. );
  14. fn run(&mut self, (positions, renderables, map): Self::SystemData) {
  15. let mut y = 0;
  16. let mut x = 0;
  17. for (idx, tile) in map.tiles.iter().enumerate() {
  18. // Render a tile depending upon the tile type
  19. if map.revealed_tiles[idx] {
  20. let glyph;
  21. let mut fg;
  22. match tile {
  23. map::TileType::Floor => {
  24. glyph = rltk::to_cp437('.');
  25. fg = rltk::RGB::from_f32(0.0, 0.5, 0.5);
  26. }
  27. map::TileType::Wall => {
  28. glyph = rltk::to_cp437('#');
  29. fg = rltk::RGB::from_f32(0., 1.0, 0.);
  30. }
  31. }
  32. if !map.visible_tiles[idx] {
  33. fg = fg.to_greyscale()
  34. }
  35. self.ctx
  36. .set(x, y, fg, rltk::RGB::from_f32(0., 0., 0.), glyph);
  37. }
  38. // Move the coordinates
  39. x += 1;
  40. if x > map::MAPWIDTH as i32 - 1 {
  41. x = 0;
  42. y += 1;
  43. }
  44. }
  45. for (pos, render) in (&positions, &renderables).join() {
  46. let idx = map.xy_idx(pos.x, pos.y);
  47. if map.visible_tiles[idx] {
  48. self.ctx
  49. .set(pos.x, pos.y, render.fg, render.bg, render.glyph);
  50. }
  51. }
  52. }
  53. }