drawing.rs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. use crate::components::{self, Position, Sprite};
  2. use crate::consts;
  3. use crate::game::MyGame;
  4. use ggez::{graphics, graphics::spritebatch::SpriteBatch, Context};
  5. use specs::RunNow;
  6. /// The `Draw` system is parameterized by which phase we want to draw
  7. /// in.
  8. pub struct Draw<'t, Phase> {
  9. pub ctx: &'t mut Context,
  10. //pub sprites: &'t mut SpriteBatch,
  11. pub _phase: Phase,
  12. }
  13. impl<'a, 't, Phase: specs::Component> specs::System<'a> for Draw<'t, Phase> {
  14. type SystemData = (
  15. specs::ReadStorage<'a, Position>,
  16. specs::ReadStorage<'a, Sprite>,
  17. specs::ReadStorage<'a, Phase>,
  18. specs::WriteExpect<'a, SpriteBatch>,
  19. );
  20. fn run(&mut self, (positions, sprites, phase, mut batch): Self::SystemData) {
  21. use specs::Join;
  22. for (pos, spr, _) in (&positions, &sprites, &phase).join() {
  23. let param = graphics::DrawParam {
  24. src: spr.to_rect(),
  25. dest: pos.to_point(),
  26. scale: mint::Vector2 {
  27. x: consts::SCALE,
  28. y: consts::SCALE,
  29. },
  30. ..Default::default()
  31. };
  32. batch.add(param);
  33. }
  34. let basic: graphics::DrawParam = Default::default();
  35. graphics::draw(self.ctx, &*batch, basic).unwrap();
  36. batch.clear();
  37. }
  38. }
  39. pub fn systems(game: &mut MyGame, ctx: &mut Context) -> ggez::GameResult<()> {
  40. graphics::clear(ctx, graphics::BLACK);
  41. Draw {
  42. ctx,
  43. _phase: components::Background,
  44. }
  45. .run_now(&game.world);
  46. Draw {
  47. ctx,
  48. _phase: components::Foreground,
  49. }
  50. .run_now(&game.world);
  51. Draw {
  52. ctx,
  53. _phase: components::Decoration,
  54. }
  55. .run_now(&game.world);
  56. graphics::present(ctx)
  57. }