use crate::consts; use crate::com::{self,Position,Sprite}; use crate::game::MyGame; use ggez::{ Context, graphics, graphics::spritebatch::SpriteBatch, }; use specs::RunNow; /// The `Draw` system is parameterized by which phase we want to draw /// in. pub struct Draw<'t, Phase> { pub ctx: &'t mut Context, //pub sprites: &'t mut SpriteBatch, pub _phase: Phase, } impl<'a, 't, Phase : specs::Component> specs::System<'a> for Draw<'t, Phase> { type SystemData = ( specs::ReadStorage<'a, Position>, specs::ReadStorage<'a, Sprite>, specs::ReadStorage<'a, Phase>, specs::WriteExpect<'a, SpriteBatch>, ); fn run(&mut self, (positions, sprites, phase, mut batch): Self::SystemData) { use specs::Join; for (pos, spr, _) in (&positions, &sprites, &phase).join() { let param = graphics::DrawParam { src: spr.to_rect(), dest: pos.to_point(), scale: ggez::nalgebra::Point2::new(consts::SCALE, consts::SCALE), ..Default::default() }; batch.add(param); graphics::draw( self.ctx, &*batch, ggez::nalgebra::Point2::new(0.0, 0.0), 0.0).unwrap(); batch.clear(); } } } pub fn systems(game: &mut MyGame, ctx: &mut Context) -> ggez::GameResult<()> { graphics::set_background_color(ctx, graphics::BLACK); graphics::clear(ctx); Draw { ctx, _phase: com::Background, }.run_now(&game.world.res); Draw { ctx, _phase: com::Foreground, }.run_now(&game.world.res); Draw { ctx, _phase: com::Decoration, }.run_now(&game.world.res); graphics::present(ctx); Ok(()) }