Created
January 20, 2020 04:46
-
-
Save devfans/ca9bedd4be78236e2b5961bce1bf2d56 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| impl NpcCreation { | |
| pub fn new(state: ServerState, num: usize, config: &Value) -> Self { | |
| let report_hosts = if let Some(host) = config["reporter_host"].as_str() { | |
| vec!(host.to_string()) | |
| } else { Vec::new() }; | |
| let mut px = state.world.physics.lock().unwrap(); | |
| macro_rules! new_plane { | |
| ($pos: expr, $shape: expr) => { | |
| { | |
| let shape_cuboid = nc::shape::Cuboid::new($shape); | |
| let collider_desc = ColliderDesc::new(nc::shape::ShapeHandle::new(shape_cuboid)) | |
| .density(1.0) | |
| .position($pos); | |
| let id = state.world.create_entity(); | |
| let mut rigid_body = RigidBodyDesc::new() | |
| .gravity_enabled(false) | |
| .build(); | |
| rigid_body.set_status(BodyStatus::Static); | |
| // let rigid_body = Ground::new(); | |
| let parent_handle = px.bodies.insert(rigid_body); | |
| let collider = collider_desc.build(BodyPartHandle(parent_handle, 0)); | |
| let collider_handle = px.colliders.insert(collider); | |
| assert!(state.world.bind_component(id, RigidBodyComponent { handle: parent_handle })); | |
| assert!(state.world.bind_component(id, ColliderComponent { handle: collider_handle })); | |
| } | |
| } | |
| } | |
| let size = 100.; | |
| let edge = size * 1.2; | |
| new_plane!(Isometry3::translation(0., size, 0.), Vector3::new(edge, 2., edge)); | |
| new_plane!(Isometry3::translation(0., -size, 0.), Vector3::new(edge, 2., edge)); | |
| new_plane!(Isometry3::translation(size, 0., 0.), Vector3::new(2., edge, edge)); | |
| new_plane!(Isometry3::translation(-size, 0., 0.), Vector3::new(2., edge, edge)); | |
| new_plane!(Isometry3::translation(0., 0., size), Vector3::new(edge, edge, 2.)); | |
| new_plane!(Isometry3::translation(0., 0., -size), Vector3::new(edge, edge, 2.)); | |
| /* | |
| new_plane!((-300., 0., 0.), Vector3::new(2., 300., 300.)); | |
| new_plane!((0., 300., 0.), Vector3::new(300., 2., 300.)); | |
| new_plane!((0., -300., 0.), Vector3::new(300., 2., 300.)); | |
| new_plane!((0., 0., 300.), Vector3::new(300., 300., 2.)); | |
| new_plane!((0., 0., -300.), Vector3::new(300., 300., 2.)); | |
| */ | |
| Self { | |
| cs_npc: state.world.get_components::<NpcComponent>(), | |
| cs_player: state.world.get_components::<PlayerComponent>(), | |
| cs_transform: state.world.get_components::<TransformComponent>(), | |
| state: state.clone(), | |
| num, | |
| step: config["npc_addition_step"].as_u64().unwrap() as usize, | |
| reporter: GraphiteClient::new(report_hosts), | |
| prefix: config["reporter_prefix"].as_str().unwrap().to_string(), | |
| } | |
| } | |
| } | |
| impl ecs::System for NpcCreation { | |
| fn tick(&self) -> AsyncFut<'_, ()> { | |
| async_fut!({ | |
| let mut rng = rand::thread_rng(); | |
| let count = (self.num - self.cs_npc.read().unwrap().len()).min(self.step); | |
| if count > 0 { | |
| let mut px = self.state.world.physics.lock().unwrap(); | |
| let rigid_body_desc = RigidBodyDesc::new() | |
| .gravity_enabled(false); | |
| let shape_cuboid = nc::shape::Ball::new(1.); | |
| let collider_desc = ColliderDesc::new(nc::shape::ShapeHandle::new(shape_cuboid)) | |
| .density(1.0); | |
| for _ in 0..count { | |
| let id = self.state.world.create_entity(); | |
| let mut pos = Transform3::default(); | |
| pos.set_translation_xyz( | |
| rng.gen_range(0, 180) as u32 as f32 - 90., | |
| rng.gen_range(0, 180) as u32 as f32 - 90., | |
| rng.gen_range(0, 180) as u32 as f32 - 90. | |
| ); | |
| let mut rigid_body = rigid_body_desc.build(); | |
| rigid_body.set_position(pos.isometry().clone()); | |
| rigid_body.set_velocity(np::math::Velocity::linear( | |
| rng.gen_range(0, 200) as u32 as f32 - 100., | |
| rng.gen_range(0, 200) as u32 as f32 - 100., | |
| rng.gen_range(0, 200) as u32 as f32 - 100., | |
| )); | |
| let transform = components::TransformComponent { | |
| transform: pos | |
| }; | |
| let parent_handle = px.bodies.insert(rigid_body); | |
| let collider = collider_desc.build(BodyPartHandle(parent_handle, 0)); | |
| let collider_handle = px.colliders.insert(collider); | |
| assert!(self.state.world.bind_component(id, RigidBodyComponent { handle: parent_handle })); | |
| assert!(self.state.world.bind_component(id, ColliderComponent { handle: collider_handle })); | |
| assert!(self.state.world.bind_component(id, transform)); | |
| assert!(self.state.world.bind_component(id, NpcComponent {})); | |
| } | |
| } | |
| }) | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment