/// 测试System use futures::future::{BoxFuture, FutureExt}; use pi_ecs::{prelude::{Query, With,Res, World, Local, ResMut, Entity, StageBuilder, SingleDispatcher, Dispatcher}, sys::system::IntoSystem}; use pi_async::rt::{multi_thread::MultiTaskRuntimeBuilder, AsyncRuntime}; use std::{sync::Arc, io::Result}; /// 定义一个名为Node原型类型 pub struct Node; #[derive(Debug)] /// 定义一个组件类型 pub struct Position(pub usize); #[derive(Debug)] /// 定义一个组件类型 pub struct Velocity(pub usize); #[derive(Debug)] /// 定义一个资源类型 pub struct Resource1(pub usize); #[derive(Debug)] /// 定义一个资源类型 pub struct Resource2(pub usize); /// 定义一个系统的本地数据类型 #[derive(Default, Debug)] pub struct DirtyMark(pub usize); #[derive(Default)] pub struct Local1(pub u32); // 同步系统 fn _sync_sys( _query1: Query, _query2: Query>, // Query> _local: Local, _res: Res, _res_mut: ResMut, ) { println!("run _sync_sys"); } // 异步系统() fn _async_sys1( _query1: Query, mut query2: Query>, // Query> local: Local, res: Res, res_mut: ResMut, ) -> BoxFuture<'static, Result<()>> { async move { // let r1 = &*res; // let r2 = &*res_mut; // let r2 = &*local; println!("run _async_sys1, res1: {:?}, {:?}, {:?}", &*res, &*res_mut, &*local); println!("run _async_sys1, res1: {:?}, {:?}, {:?}", 1, 2,3); for (entity, position) in query2.iter_mut() { println!("run _async_sys1, entity: {:?}, position:{:?}",entity, &*position); } Ok(()) }.boxed() } #[test] fn test() { // 创建world let mut world = World::new(); // 创建一个名为Node的原型,为该原型注册组件类型(一旦注册,不可修改) world.new_archetype::() .register::() .register::() .create(); world.insert_resource(Resource1(1)); world.insert_resource(Resource2(2)); // 创建原型为Node的实体,并为该实体添加组件(必须是在Node中注册过的组件, 否则无法插入) for i in 0..5 { let _id = world.spawn::() .insert(Position(i)) .id(); } let mut vec = Vec::new(); for i in 0..5 { let id = world.spawn::() .insert(Position(i + 5)) .insert(Velocity(i + 5)) .id(); vec.push(id); } // 创建查询,并迭代查询 let mut query = world.query::(); for (entity, velocity, position) in query.iter_mut(&mut world) { println!("iter_mut:{:?}, {:?}, {:?}", entity, velocity, position); } let mut query = world.query::(); for (entity, position) in query.iter(&mut world) { println!("iter_mut1:{:?}, {:?}", entity, position); } test_system(&mut world); std::thread::sleep(std::time::Duration::from_secs(5)); } fn test_system(world: &mut World) { let rt = AsyncRuntime::Multi(MultiTaskRuntimeBuilder::default().build()); let sync_system = _sync_sys.system(world); // let async_system = _async_sys.system(world.clone()); let async_system = _async_sys1.system(world); let mut stage = StageBuilder::new(); stage.add_node(sync_system); stage.add_node(async_system); let mut stages = Vec::new(); stages.push(Arc::new(stage.build())); let dispatcher = SingleDispatcher::new(stages, world, rt); dispatcher.run(); }