From bb8fc323f5a6a70e22ee557f3565a505feacd29f Mon Sep 17 00:00:00 2001 From: ashley Date: Tue, 10 Oct 2023 19:15:03 -0400 Subject: [PATCH] Adjust collision to use drawn bounding boxes --- Cargo.toml | 3 ++- src/reskit/level/converter.rs | 46 +++++++++++++++++++++++++++-------- src/reskit/level/system.rs | 25 +++++++++++-------- 3 files changed, 53 insertions(+), 21 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b5404e7..b3fdb38 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,4 +18,5 @@ linked_hash_set = "0.1.4" linked-hash-map = "0.5.6" convert_case = "0.6.0" roxmltree = "0.18.0" -regex = "1.9.5" \ No newline at end of file +regex = "1.9.5" +euclid = "0.22.9" \ No newline at end of file diff --git a/src/reskit/level/converter.rs b/src/reskit/level/converter.rs index ce2b3dd..81eb24e 100644 --- a/src/reskit/level/converter.rs +++ b/src/reskit/level/converter.rs @@ -1,4 +1,5 @@ use std::{error::Error, fs::read_to_string, path::Path, borrow::Cow}; +use euclid::default::Rect; use image::{DynamicImage, GenericImageView}; use linked_hash_map::LinkedHashMap; use roxmltree::Node; @@ -9,6 +10,7 @@ pub struct TiledTilemap { pub tileset: Vec, pub layers: Vec, pub objects: Vec, + pub collision: Vec>, pub width: usize, pub height: usize } @@ -48,9 +50,6 @@ pub enum Layer { Tile { system_plane: SystemPlane, tiles: Vec - }, - Collision { - tiles: Vec } } @@ -104,7 +103,6 @@ fn get_layer( layer: Node, map_width: usize, map_height: usize ) -> Result Ok( Some( Layer::Collision { tiles } ) ), _ => { print_warning( &format!( "on layer {}: invalid reskit-layer value {}; ignoring this layer", layer_name, layer_type ) ); Ok( None ) @@ -343,16 +341,43 @@ pub fn get_tiled_tilemap( path: &str, object_fields: &Vec<&str> ) -> Result ) -> Result Result, Box> pub fn get_collision_map( tilemap: &TiledTilemap ) -> Result, Box> { let mut result: Vec = Vec::new(); - let collision: Option<&Layer> = tilemap.layers.iter().find( | layer | matches!( layer, Layer::Collision { tiles: _ } ) ); - if let Some( collision ) = collision { - let collision = match collision { - Layer::Collision { tiles } => tiles, - _ => return Err( "internal error: invalid object type" )? - }; + // 2 bytes: Number of bounding boxes + result.extend( ( tilemap.collision.len() as u16 ).to_be_bytes() ); - for collision_data in collision { - let collision_data: u8 = if *collision_data > 0 { 1 } else { 0 }; - result.push( collision_data ); - } + // For (Number of bounding boxes): + for bounding_box in &tilemap.collision { + // 2 bytes: X dimension + result.extend( bounding_box.origin.x.to_be_bytes() ); + + // 2 bytes: Y dimension + result.extend( bounding_box.origin.y.to_be_bytes() ); + + // 2 bytes: X2 dimension (x + width) + result.extend( ( bounding_box.origin.x + bounding_box.size.width ).to_be_bytes() ); + + // 2 bytes: Y2 dimension (y + height) + result.extend( ( bounding_box.origin.y + bounding_box.size.height ).to_be_bytes() ); } Ok( result )