moves
This commit is contained in:
60
src/main.rs
60
src/main.rs
@@ -59,13 +59,19 @@ impl Component for Draws {
|
|||||||
type Storage = VecStorage<Self>;
|
type Storage = VecStorage<Self>;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Vel(f32);
|
struct Vel {
|
||||||
|
x: f32,
|
||||||
|
y: f32,
|
||||||
|
}
|
||||||
|
|
||||||
impl Component for Vel {
|
impl Component for Vel {
|
||||||
type Storage = VecStorage<Self>;
|
type Storage = VecStorage<Self>;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Pos(f32);
|
struct Pos {
|
||||||
|
x: f32,
|
||||||
|
y: f32
|
||||||
|
}
|
||||||
|
|
||||||
impl Component for Pos {
|
impl Component for Pos {
|
||||||
type Storage = VecStorage<Self>;
|
type Storage = VecStorage<Self>;
|
||||||
@@ -75,6 +81,7 @@ impl Component for Pos {
|
|||||||
struct PersistentState {
|
struct PersistentState {
|
||||||
surface: Option<Arc<Surface<Window>>>,
|
surface: Option<Arc<Surface<Window>>>,
|
||||||
window_size: (u32, u32),
|
window_size: (u32, u32),
|
||||||
|
delta_time: f32,
|
||||||
canvas_frame: CanvasFrame,
|
canvas_frame: CanvasFrame,
|
||||||
compu_frame: CompuFrame,
|
compu_frame: CompuFrame,
|
||||||
}
|
}
|
||||||
@@ -91,21 +98,23 @@ impl<'a> System<'a> for RenderSystem {
|
|||||||
Write<'a, VkProcessor>,
|
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.canvas_frame = CanvasFrame::new(state.window_size);
|
||||||
state.compu_frame = CompuFrame::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_with_image_swap(compute_buffer.clone(), compute_kernel.clone(), &compu_sprite1);
|
||||||
// compu_frame.add(compute_buffer.clone(), compute_kernel.clone());
|
// 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
|
for (vel, pos, draw) in (&vel, &mut pos, &mut draw).join() {
|
||||||
// in parallel using rayon's `ParallelIterator`s.
|
pos.x += vel.x * state.delta_time;
|
||||||
// See `ParJoin` for more.
|
pos.y += vel.y * state.delta_time;
|
||||||
|
|
||||||
|
draw.0.position = (pos.x, pos.y);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
for draw_data in (&draw).join() {
|
for draw_data in (&draw).join() {
|
||||||
let size = state.window_size.clone();
|
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() {
|
pub fn main() {
|
||||||
hprof::start_frame();
|
hprof::start_frame();
|
||||||
@@ -234,14 +220,15 @@ pub fn main() {
|
|||||||
world.insert::<PersistentState>(PersistentState {
|
world.insert::<PersistentState>(PersistentState {
|
||||||
surface: Some(surface.clone()),
|
surface: Some(surface.clone()),
|
||||||
window_size: (0, 0),
|
window_size: (0, 0),
|
||||||
|
delta_time,
|
||||||
canvas_frame: CanvasFrame::new((0, 0)),
|
canvas_frame: CanvasFrame::new((0, 0)),
|
||||||
compu_frame: CompuFrame::new((0, 0)),
|
compu_frame: CompuFrame::new((0, 0)),
|
||||||
});
|
});
|
||||||
|
|
||||||
// An entity may or may not contain some component.
|
// An entity may or may not contain some component.
|
||||||
world.create_entity()
|
world.create_entity()
|
||||||
.with(Vel(2.0))
|
.with(Vel { x: 0.0, y: 0.0 })
|
||||||
.with(Pos(0.0))
|
.with(Pos{ x: 0.0, y: 0.0 })
|
||||||
.with(Draws(
|
.with(Draws(
|
||||||
Sprite::new(
|
Sprite::new(
|
||||||
(200.0, 200.0),
|
(200.0, 200.0),
|
||||||
@@ -249,7 +236,7 @@ pub fn main() {
|
|||||||
.build();
|
.build();
|
||||||
|
|
||||||
let mut dispatcher = DispatcherBuilder::new()
|
let mut dispatcher = DispatcherBuilder::new()
|
||||||
.with(SysA, "sys_a", &[])
|
// .with(SysA, "sys_a", &[])
|
||||||
.with(RenderSystem, "render_s", &[]).build();
|
.with(RenderSystem, "render_s", &[]).build();
|
||||||
|
|
||||||
|
|
||||||
@@ -338,6 +325,7 @@ pub fn main() {
|
|||||||
}
|
}
|
||||||
accumulator_time += delta_time;
|
accumulator_time += delta_time;
|
||||||
// This dispatches all the systems in parallel (but blocking).
|
// This dispatches all the systems in parallel (but blocking).
|
||||||
|
world.write_resource::<PersistentState>().delta_time = delta_time;
|
||||||
dispatcher.dispatch(&mut world);
|
dispatcher.dispatch(&mut world);
|
||||||
}
|
}
|
||||||
Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => {
|
Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => {
|
||||||
|
|||||||
Reference in New Issue
Block a user