diff --git a/src/main.rs b/src/main.rs index ce03a336..9c9ccd14 100644 --- a/src/main.rs +++ b/src/main.rs @@ -59,13 +59,19 @@ impl Component for Draws { type Storage = VecStorage; } -struct Vel(f32); +struct Vel { + x: f32, + y: f32, +} impl Component for Vel { type Storage = VecStorage; } -struct Pos(f32); +struct Pos { + x: f32, + y: f32 +} impl Component for Pos { type Storage = VecStorage; @@ -75,6 +81,7 @@ impl Component for Pos { struct PersistentState { surface: Option>>, window_size: (u32, u32), + delta_time: f32, canvas_frame: CanvasFrame, compu_frame: CompuFrame, } @@ -91,21 +98,23 @@ impl<'a> System<'a> for RenderSystem { Write<'a, VkProcessor>, ); - fn run(&mut self, (mut pos, vel, draw, mut state, mut vk_processor): Self::SystemData) { + fn run(&mut self, (mut pos, vel, mut draw, mut state, mut vk_processor): Self::SystemData) { state.canvas_frame = CanvasFrame::new(state.window_size); state.compu_frame = CompuFrame::new(state.window_size); // compu_frame.add_with_image_swap(compute_buffer.clone(), compute_kernel.clone(), &compu_sprite1); // compu_frame.add(compute_buffer.clone(), compute_kernel.clone()); - // The `.join()` combines multiple components, - // so we only access those entities which have - // both of them. - // This joins the component storages for Position - // and Velocity together; it's also possible to do this - // in parallel using rayon's `ParallelIterator`s. - // See `ParJoin` for more. + + for (vel, pos, draw) in (&vel, &mut pos, &mut draw).join() { + pos.x += vel.x * state.delta_time; + pos.y += vel.y * state.delta_time; + + draw.0.position = (pos.x, pos.y); + + } + for draw_data in (&draw).join() { let size = state.window_size.clone(); @@ -118,29 +127,6 @@ impl<'a> System<'a> for RenderSystem { } } -struct SysA; - -impl<'a> System<'a> for SysA { - type SystemData = ( - WriteStorage<'a, Pos>, - ReadStorage<'a, Vel> - ); - - fn run(&mut self, (mut pos, vel): Self::SystemData) { - // The `.join()` combines multiple components, - // so we only access those entities which have - // both of them. - - // This joins the component storages for Position - // and Velocity together; it's also possible to do this - // in parallel using rayon's `ParallelIterator`s. - // See `ParJoin` for more. - for (pos, vel) in (&mut pos, &vel).join() { - pos.0 += vel.0; - } - } -} - pub fn main() { hprof::start_frame(); @@ -234,14 +220,15 @@ pub fn main() { world.insert::(PersistentState { surface: Some(surface.clone()), window_size: (0, 0), + delta_time, canvas_frame: CanvasFrame::new((0, 0)), compu_frame: CompuFrame::new((0, 0)), }); // An entity may or may not contain some component. world.create_entity() - .with(Vel(2.0)) - .with(Pos(0.0)) + .with(Vel { x: 0.0, y: 0.0 }) + .with(Pos{ x: 0.0, y: 0.0 }) .with(Draws( Sprite::new( (200.0, 200.0), @@ -249,7 +236,7 @@ pub fn main() { .build(); let mut dispatcher = DispatcherBuilder::new() - .with(SysA, "sys_a", &[]) + // .with(SysA, "sys_a", &[]) .with(RenderSystem, "render_s", &[]).build(); @@ -338,6 +325,7 @@ pub fn main() { } accumulator_time += delta_time; // This dispatches all the systems in parallel (but blocking). + world.write_resource::().delta_time = delta_time; dispatcher.dispatch(&mut world); } Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => {