diff --git a/assets/level01.tmx b/assets/level01.tmx index a738270..c928a2f 100644 --- a/assets/level01.tmx +++ b/assets/level01.tmx @@ -1,5 +1,5 @@ - + @@ -198,5 +198,18 @@ + + + + + + + + + + + + Hello World + diff --git a/src/ground_platforms.rs b/src/ground_platforms.rs index 6ee5e87..59afabc 100644 --- a/src/ground_platforms.rs +++ b/src/ground_platforms.rs @@ -3,6 +3,7 @@ use bevy_rapier2d::{ dynamics::{Ccd, ExternalImpulse, GravityScale, RigidBody}, geometry::{Collider, Restitution}, }; +use tiled::ObjectShape; use crate::{ helpers::tiled::TiledMap, @@ -32,18 +33,62 @@ pub struct Ground; #[derive(Component)] pub struct Platform; -fn get_ground_layer(tiled_map: &TiledMap) { - for layer in tiled_map.map.layers() { - if layer.name == "Ground" { - info!("Found Ground layer"); - if let tiled::LayerType::Objects(object_data) = layer.layer_type() { - for object in object_data.objects() { - info!("Found object {:?}", object.name); - info!("Found object {:?}", object.shape); - } - } +fn get_ground_layer(commands: &mut Commands, tiled_map: &TiledMap, level: &Level) { + tiled_map.map.layers().for_each(|layer| { + if layer.name != "Ground" { + return; } - } + + info!("Found Ground layer"); + + let object_data = match layer.layer_type() { + tiled::LayerType::Objects(object_data) => object_data, + _ => return, + }; + + object_data.objects().for_each(|object| { + info!("Found object {:?}", object.name); + debug!("Found object {:?}", object.shape); + + // match &object.shape { + // ObjectShape::Rect { width, height } => { + // todo!(); + // } + // ObjectShape::Polygon { points } => { + // todo!(); + // } + // ObjectShape::Polyline { points } => { + // let points: Vec = points + // .iter() + // .map(|(x, y)| { + // level.map.tiled_to_bevy_coord( + // Vec2::new(*x, *y) + Vec2::new(object.x, object.y), + // ) + // }) + // .collect(); + // + // debug!("points: {:?}", points); + // + // commands + // .spawn(( + // SpatialBundle::default(), + // Collider::polyline(points, None), + // Ground, + // )) + // .insert(Ccd::enabled()); + // } + // ObjectShape::Text { .. } => { + // todo!(); + // } + // ObjectShape::Ellipse { .. } => { + // todo!(); + // } + // ObjectShape::Point { .. } => { + // todo!(); + // } + // } + }) + }) } fn setup_ground_platforms( @@ -52,102 +97,109 @@ fn setup_ground_platforms( current_level: Res, levels: Query<&Level, With>, ) { - for level in levels.iter() { - if level.id == current_level.id { - if let Some(tiled_map) = assets.get(&level.handle) { - // dbg!(&tiled_map.map); - get_ground_layer(tiled_map); - } + info!("setup_ground_platforms"); + + levels.iter().for_each(|level| { + if level.id != current_level.id { + return; } - } - /* Create the ground. */ - - info!("setup_physics"); - let points = vec![ - Vec2::new(-1280.0 * 2.0 * 3.0, 128.0), - // Vec2::new(48.0, -8.0 - 224.0), - // Vec2::new(48.0 + 14.0 * 16.0, -8.0 - (224.0 - 7.0 * 16.0)), - // Vec2::new(272.0 + 3.0 * 16.0, -8.0 - (224.0 - 7.0 * 16.0)), - // Vec2::new(320.0 + 7.0 * 16.0, -8.0 - (224.0)), - Vec2::new(1280.0 / 2.0 * 2.0 + 30.0, 128.0), - ]; - - commands - .spawn(( - SpatialBundle::default(), - Collider::polyline(points, None), - Ground, - )) - .insert(Ccd::enabled()) - // .insert(TransformBundle::from(Transform::from_xyz(0.0, 0.0, 0.0))) - .with_children(|parent| { - // Create 2 x test platforms - parent - .spawn((Collider::cuboid(60.0, 5.0), Platform)) - .insert(TransformBundle::from(Transform::from_xyz( - 1280.0 / 2.0 * 2.0, - -324.0 + 5.0 * 16.0, // 8.0 is hard to climb, 9.0 can not be climbed - 0.0, - ))); - - parent - .spawn((Collider::cuboid(60.0, 5.0), Platform)) - .insert(TransformBundle::from(Transform::from_xyz( - 1280.0 / 2.0 * 2.0, - -724.0 + 5.0 * 16.0, // 8.0 is hard to climb, 9.0 can not be climbed - 0.0, - ))); - - parent - .spawn((Collider::cuboid(60.0, 5.0), Platform)) - .insert(TransformBundle::from(Transform::from_xyz( - 1280.0 / 2.0 * 2.3, - -224.0 + 5.0 * 16.0, // 8.0 is hard to climb, 9.0 can not be climbed - 0.0, - ))); - - parent - .spawn((Collider::cuboid(60.0, 5.0), Platform)) - .insert(TransformBundle::from(Transform::from_xyz( - 1280.0 / 2.0 * 2.5, - -124.0 + 3.0 * 16.0, // 8.0 is hard to climb, 9.0 can not be climbed - 0.0, - ))); - - parent - .spawn((Collider::cuboid(60.0, 5.0), Platform)) - .insert(TransformBundle::from(Transform::from_xyz( - 1280.0 / 2.0 * 2.4, - -024.0 + 5.0 * 16.0, // 8.0 is hard to climb, 9.0 can not be climbed - 0.0, - ))); - - parent - .spawn((Collider::cuboid(60.0, 5.0), Platform)) - .insert(TransformBundle::from(Transform::from_xyz( - -1280.0 / 2.0 + 380.0, // 270 Gap is reachable, 290 seems not - -224.0 + 10.0 * 16.0, // 8.0 is hard to climb, 9.0 can not be climbed - 0.0, - ))); - - /* Create the bouncing ball. */ - parent - .spawn(RigidBody::Dynamic) - .insert(GravityScale(20.0)) - .insert(Collider::ball(20.0)) - .insert(Restitution::coefficient(0.0)) - // .insert(ColliderMassProperties::Density(20.0)) - .insert(ExternalImpulse { - // impulse: Vec2::new(100.0, 200.0), - // torque_impulse: 14.0, - ..default() - }) - // .insert(Damping { - // linear_damping: 100.0, - // angular_damping: 0.0, - // }) - .insert(TransformBundle::from(Transform::from_xyz(0.0, 400.0, 0.0))); - }); + + let tiled_map = match assets.get(&level.handle) { + Some(tiled_map) => tiled_map, + None => return, + }; + + get_ground_layer(&mut commands, tiled_map, level); + }); + + // /* Create the ground. */ + // + // info!("setup_physics"); + // let points = vec![ + // Vec2::new(-1280.0 * 2.0 * 3.0, 128.0), + // // Vec2::new(48.0, -8.0 - 224.0), + // // Vec2::new(48.0 + 14.0 * 16.0, -8.0 - (224.0 - 7.0 * 16.0)), + // // Vec2::new(272.0 + 3.0 * 16.0, -8.0 - (224.0 - 7.0 * 16.0)), + // // Vec2::new(320.0 + 7.0 * 16.0, -8.0 - (224.0)), + // Vec2::new(1280.0 / 2.0 * 2.0 + 30.0, 128.0), + // ]; + // + // commands + // .spawn(( + // SpatialBundle::default(), + // Collider::polyline(points, None), + // Ground, + // )) + // .insert(Ccd::enabled()) + // // .insert(TransformBundle::from(Transform::from_xyz(0.0, 0.0, 0.0))) + // .with_children(|parent| { + // // Create 2 x test platforms + // parent + // .spawn((Collider::cuboid(60.0, 5.0), Platform)) + // .insert(TransformBundle::from(Transform::from_xyz( + // 1280.0 / 2.0 * 2.0, + // -324.0 + 5.0 * 16.0, // 8.0 is hard to climb, 9.0 can not be climbed + // 0.0, + // ))); + // + // parent + // .spawn((Collider::cuboid(60.0, 5.0), Platform)) + // .insert(TransformBundle::from(Transform::from_xyz( + // 1280.0 / 2.0 * 2.0, + // -724.0 + 5.0 * 16.0, // 8.0 is hard to climb, 9.0 can not be climbed + // 0.0, + // ))); + // + // parent + // .spawn((Collider::cuboid(60.0, 5.0), Platform)) + // .insert(TransformBundle::from(Transform::from_xyz( + // 1280.0 / 2.0 * 2.3, + // -224.0 + 5.0 * 16.0, // 8.0 is hard to climb, 9.0 can not be climbed + // 0.0, + // ))); + // + // parent + // .spawn((Collider::cuboid(60.0, 5.0), Platform)) + // .insert(TransformBundle::from(Transform::from_xyz( + // 1280.0 / 2.0 * 2.5, + // -124.0 + 3.0 * 16.0, // 8.0 is hard to climb, 9.0 can not be climbed + // 0.0, + // ))); + // + // parent + // .spawn((Collider::cuboid(60.0, 5.0), Platform)) + // .insert(TransformBundle::from(Transform::from_xyz( + // 1280.0 / 2.0 * 2.4, + // -024.0 + 5.0 * 16.0, // 8.0 is hard to climb, 9.0 can not be climbed + // 0.0, + // ))); + // + // parent + // .spawn((Collider::cuboid(60.0, 5.0), Platform)) + // .insert(TransformBundle::from(Transform::from_xyz( + // -1280.0 / 2.0 + 380.0, // 270 Gap is reachable, 290 seems not + // -224.0 + 10.0 * 16.0, // 8.0 is hard to climb, 9.0 can not be climbed + // 0.0, + // ))); + // + // /* Create the bouncing ball. */ + // parent + // .spawn(RigidBody::Dynamic) + // .insert(GravityScale(20.0)) + // .insert(Collider::ball(20.0)) + // .insert(Restitution::coefficient(0.0)) + // // .insert(ColliderMassProperties::Density(20.0)) + // .insert(ExternalImpulse { + // // impulse: Vec2::new(100.0, 200.0), + // // torque_impulse: 14.0, + // ..default() + // }) + // // .insert(Damping { + // // linear_damping: 100.0, + // // angular_damping: 0.0, + // // }) + // .insert(TransformBundle::from(Transform::from_xyz(0.0, 400.0, 0.0))); + // }); } fn print_ball_altitude(positions: Query<&Transform, With>) { diff --git a/src/screen_map.rs b/src/screen_map.rs index 2c51798..81eac45 100644 --- a/src/screen_map.rs +++ b/src/screen_map.rs @@ -98,7 +98,7 @@ impl Map { pub fn tiled_to_bevy_coord(&self, tiled_coord: Vec2) -> Vec2 { Vec2::new( tiled_coord.x - (self.width / 2) as f32, - (tiled_coord.y - (self.height / 2) as f32) + 1f32, + -(tiled_coord.y - (self.height / 2) as f32) - 1f32, ) } @@ -330,19 +330,19 @@ mod tests { assert_eq!( map.tiled_to_bevy_coord(Vec2::new(0.0, 0.0)), - Vec2::new(-1920.0, -1079.0) + Vec2::new(-1920.0, 1079.0) ); assert_eq!( map.tiled_to_bevy_coord(Vec2::new(1919.0, 0.0)), - Vec2::new(-1.0, -1079.0) + Vec2::new(-1.0, 1079.0) ); assert_eq!( map.tiled_to_bevy_coord(Vec2::new(0.0, 719.0)), - Vec2::new(-1920.0, -360.0) + Vec2::new(-1920.0, 360.0) ); assert_eq!( map.tiled_to_bevy_coord(Vec2::new(3839.0, 2159.0)), - Vec2::new(1919.0, 1080.0) + Vec2::new(1919.0, -1080.0) ); } @@ -356,19 +356,19 @@ mod tests { assert_eq!( map.tiled_to_bevy_coord(Vec2::new(0.0, 0.0)), - Vec2::new(-1280.0, -359.0) + Vec2::new(-1280.0, 359.0) ); assert_eq!( map.tiled_to_bevy_coord(Vec2::new(1280.0, 360.0)), - Vec2::new(0.0, 1.0) + Vec2::new(0.0, -1.0) ); assert_eq!( map.tiled_to_bevy_coord(Vec2::new(0.0, 719.0)), - Vec2::new(-1280.0, 360.0) + Vec2::new(-1280.0, -360.0) ); assert_eq!( map.tiled_to_bevy_coord(Vec2::new(2559.0, 719.0)), - Vec2::new(1279.0, 360.0) + Vec2::new(1279.0, -360.0) ); }