From b4b0ff9a54e9aa2ad0ffe602d10c82d390062aaf Mon Sep 17 00:00:00 2001 From: artem Date: Fri, 26 Jan 2024 12:43:17 +0300 Subject: [PATCH] auto-build map (refactoring + build_air_island_block) --- game/src/map.rs | 227 +++++++++++++++++++++++++++--------------------- 1 file changed, 128 insertions(+), 99 deletions(-) diff --git a/game/src/map.rs b/game/src/map.rs index a9d37e7..08ad1cd 100644 --- a/game/src/map.rs +++ b/game/src/map.rs @@ -20,55 +20,13 @@ use fyrox::{ // /mnt/23154027-2dd3-43ea-93f1-02a8df7a3c5a/projects/fyrox/platformer/assets/data/tiles/ // -fn build_ground_material(resource_manager: &ResourceManager) -> MaterialResource { +fn build_material(resource_manager: &ResourceManager, path: String) -> MaterialResource { let mut material = Material::standard_2d(); material .set_property( &ImmutableString::new("diffuseTexture"), PropertyValue::Sampler { - value: Some(resource_manager.request::("assets/data/tiles/2.png")), - fallback: SamplerFallback::Normal, - }, - ) - .unwrap(); - return MaterialResource::new_ok(Default::default(), material); -} - -fn build_tree_material(resource_manager: &ResourceManager) -> MaterialResource { - let mut material = Material::standard_2d(); - material - .set_property( - &ImmutableString::new("diffuseTexture"), - PropertyValue::Sampler { - value: Some(resource_manager.request::("assets/data/objects/Tree_2.png")), - fallback: SamplerFallback::Normal, - }, - ) - .unwrap(); - return MaterialResource::new_ok(Default::default(), material); -} - -fn build_stone_material(resource_manager: &ResourceManager) -> MaterialResource { - let mut material = Material::standard_2d(); - material - .set_property( - &ImmutableString::new("diffuseTexture"), - PropertyValue::Sampler { - value: Some(resource_manager.request::("assets/data/objects/Stone.png")), - fallback: SamplerFallback::Normal, - }, - ) - .unwrap(); - return MaterialResource::new_ok(Default::default(), material); -} - -fn build_box_material(resource_manager: &ResourceManager) -> MaterialResource { - let mut material = Material::standard_2d(); - material - .set_property( - &ImmutableString::new("diffuseTexture"), - PropertyValue::Sampler { - value: Some(resource_manager.request::("assets/data/objects/Crate.png")), + value: Some(resource_manager.request::(path)), fallback: SamplerFallback::Normal, }, ) @@ -83,7 +41,7 @@ fn build_dynamic_block( scale: f32, material: MaterialResource, shape: ColliderShape, - mass: f32 + mass: f32, ) { let rb_transform = TransformBuilder::new() .with_local_position(Vector3::new(x, y, 0.0)) @@ -118,7 +76,6 @@ fn build_dynamic_block( .build(graph); } - fn build_block( graph: &mut Graph, x: f32, @@ -158,63 +115,135 @@ fn build_block( .build(graph); } +fn build_ground_block(resource_manager: &ResourceManager, graph: &mut Graph, x: f32, y: f32) { + let shape = ColliderShape::Cuboid(CuboidShape::default()); + build_block( + graph, + x as f32, + y, + 1.0, + build_material(resource_manager, "assets/data/tiles/2.png".to_owned()), + shape, + ) +} + +fn build_center_block(resource_manager: &ResourceManager, graph: &mut Graph, x: f32, y: f32) { + let shape = ColliderShape::Cuboid(CuboidShape::default()); + build_block( + graph, + x as f32, + y, + 1.0, + build_material(resource_manager, "assets/data/tiles/14.png".to_owned()), + shape, + ) +} + +fn build_left_block(resource_manager: &ResourceManager, graph: &mut Graph, x: f32, y: f32) { + let shape = ColliderShape::Cuboid(CuboidShape::default()); + build_block( + graph, + x as f32, + y, + 1.0, + build_material(resource_manager, "assets/data/tiles/13.png".to_owned()), + shape, + ) +} + +fn build_right_block(resource_manager: &ResourceManager, graph: &mut Graph, x: f32, y: f32) { + let shape = ColliderShape::Cuboid(CuboidShape::default()); + build_block( + graph, + x as f32, + y, + 1.0, + build_material(resource_manager, "assets/data/tiles/15.png".to_owned()), + shape, + ) +} + +fn build_tree_block(resource_manager: &ResourceManager, graph: &mut Graph, x: f32, y: f32) { + let tree_shape = ColliderShape::Cuboid(CuboidShape { + half_extents: Vector2::new(0.5, 1.0), + }); + + build_block( + graph, + x as f32, + y + 1.5, + 2.0, + build_material( + resource_manager, + "assets/data/objects/Tree_2.png".to_owned(), + ), + tree_shape, + ) +} + +fn build_stone_block(resource_manager: &ResourceManager, graph: &mut Graph, x: f32, y: f32) { + let shape = ColliderShape::Cuboid(CuboidShape::default()); + build_block( + graph, + x as f32, + y + 1.0, + 1.0, + build_material( + resource_manager, + "assets/data/objects/Mushroom_2.png".to_owned(), + ), + shape, + ) +} + +fn build_mushroom_block(resource_manager: &ResourceManager, graph: &mut Graph, x: f32, y: f32) { + let shape = ColliderShape::Cuboid(CuboidShape { + half_extents: Vector2::new(0.25, 0.25), + }); + build_block( + graph, + x as f32, + y + 0.5, + 0.5, + build_material( + resource_manager, + "assets/data/objects/Mushroom_2.png".to_owned(), + ), + shape, + ) +} + +fn build_box_block(resource_manager: &ResourceManager, graph: &mut Graph, x: f32, y: f32) { + let shape = ColliderShape::Cuboid(CuboidShape::default()); + build_dynamic_block( + graph, + x, + y + 1.0, + 1.0, + build_material(resource_manager, "assets/data/objects/Crate.png".to_owned()), + shape, + 3.0, + ); +} + +fn build_air_island_block(resource_manager: &ResourceManager, graph: &mut Graph, x: f32, y: f32) { + build_left_block(resource_manager, graph, x + 1.0, y); + build_center_block(resource_manager, graph, x, y); + build_right_block(resource_manager, graph, x - 1.0, y); +} + pub fn build_map(graph: &mut Graph, resource_manager: &ResourceManager) { let bottom_y: f32 = -5.0; let left_x: i32 = -50; let right_x: i32 = 50; - let box_shape = ColliderShape::Cuboid(CuboidShape::default()); - let tree_shape = ColliderShape::Cuboid(CuboidShape { - half_extents: Vector2::new(0.5, 1.0), - }); for x in left_x..right_x { - build_block( - graph, - x as f32, - bottom_y, - 1.0, - build_ground_material(resource_manager), - box_shape.clone(), - ); + build_ground_block(resource_manager, graph, x as f32, bottom_y); } - build_block( - graph, - 30.0, - bottom_y + 1.5, - 2.0, - build_tree_material(resource_manager), - tree_shape.clone(), - ); - build_block( - graph, - 25.0, - bottom_y + 1.0, - 1.0, - build_stone_material(resource_manager), - box_shape.clone(), - ); - build_block( - graph, - -25.0, - bottom_y + 1.0, - 1.0, - build_stone_material(resource_manager), - box_shape.clone(), - ); - build_block( - graph, - -30.0, - bottom_y + 1.5, - 2.0, - build_tree_material(resource_manager), - tree_shape.clone(), - ); - build_dynamic_block( - graph, - 10.0, - bottom_y + 1.0, - 1.0, - build_box_material(resource_manager), - box_shape.clone(), - 3.0 - ); + build_mushroom_block(resource_manager, graph, 25.0, bottom_y); + build_tree_block(resource_manager, graph, 30.0, bottom_y); + build_stone_block(resource_manager, graph, -25.0, bottom_y); + build_tree_block(resource_manager, graph, -30.0, bottom_y); + build_box_block(resource_manager, graph, 10.0, bottom_y); + + build_air_island_block(resource_manager, graph, 15.0, bottom_y + 2.0) }