From 5ba27514e73e653f08dd06d7d5eaa466a61747c3 Mon Sep 17 00:00:00 2001 From: ashley Date: Sat, 23 Sep 2023 12:44:56 -0400 Subject: [PATCH] Attribute export --- src/reskit/level/system.rs | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/reskit/level/system.rs b/src/reskit/level/system.rs index 4721300..b3a3e30 100644 --- a/src/reskit/level/system.rs +++ b/src/reskit/level/system.rs @@ -449,9 +449,12 @@ pub fn get_code( tilemap: &TiledTilemap, level_name: &str, path_prefix: &str ) - total_tiles }; + let mut constants: String = String::new(); + // Output IDs for each type defined in the map let mut component_ids: LinkedHashSet = LinkedHashSet::new(); let mut attribute_ids: LinkedHashMap> = LinkedHashMap::new(); + let mut attribute_string_values: LinkedHashSet = LinkedHashSet::new(); for entity in &tilemap.ecs { let mut components: Vec = entity.components.keys().map( | id | id.to_lowercase() ).collect(); components.sort(); @@ -460,6 +463,18 @@ pub fn get_code( tilemap: &TiledTilemap, level_name: &str, path_prefix: &str ) - component_ids.extend( components ); for ( component_name, component ) in &entity.components { + for ( _, attribute_value ) in &component.attributes { + match attribute_value.as_str() { + "true" | "false" => { /* nothing */ }, + string => { + let try_as_u16: Result = string.parse(); + if let Err( _ ) = try_as_u16 { + attribute_string_values.insert_if_absent( attribute_value.to_owned() ); + } + } + } + } + let mut attributes: Vec = component.attributes.keys().map( | id | id.to_lowercase() ).collect(); attributes.sort(); let attribues: LinkedHashSet = attributes.into_iter().collect(); @@ -468,8 +483,21 @@ pub fn get_code( tilemap: &TiledTilemap, level_name: &str, path_prefix: &str ) - } } + let attribute_string_values: Vec = attribute_string_values.into_iter().collect(); + for i in 0..attribute_string_values.len() { + let attr_val = &attribute_string_values[ i ]; + match attr_val.as_str() { + "true" | "false" => { /* nothing */ }, + string => { + let try_as_u16: Result = string.parse(); + if let Err( _ ) = try_as_u16 { + constants += &format!( "{}_ATTR_{} = {}\n", level_label_const_caps, attribute_string_values[ i ].to_uppercase(), i ); + } + } + } + } + let component_ids: Vec = component_ids.into_iter().collect(); - let mut constants: String = String::new(); for i in 0..component_ids.len() { let component_id = &component_ids[ i ]; constants += &format!( "{}_{} = {}\n", level_label_const_caps, component_id.to_uppercase(), i );