diff --git a/src/reskit/level/system.rs b/src/reskit/level/system.rs index 70e91bb..d22008c 100644 --- a/src/reskit/level/system.rs +++ b/src/reskit/level/system.rs @@ -1,5 +1,6 @@ use std::{error::Error, convert::TryInto, cmp::max, num::ParseIntError}; use image::GenericImageView; +use linked_hash_map::LinkedHashMap; use linked_hash_set::LinkedHashSet; use crate::reskit::{tileset::image_to_tiles, utility::{symbol_to_pascal, print_info}, cli::settings::TileOrder}; use super::{converter::{TiledTilemap, Layer, SystemPlane}, ecs::Component}; @@ -436,6 +437,7 @@ pub fn get_ecs( tilemap: &TiledTilemap ) -> Result, Box> { pub fn get_code( tilemap: &TiledTilemap, level_name: &str, path_prefix: &str ) -> Result> { let version = env!( "CARGO_PKG_VERSION" ); let level_label = symbol_to_pascal( level_name ); + let level_label_const_caps = level_label.to_uppercase(); let ( width, height ) = ( tilemap.width, tilemap.height ); let num_tiles = { let mut total_tiles = 0; @@ -446,9 +448,43 @@ pub fn get_code( tilemap: &TiledTilemap, level_name: &str, path_prefix: &str ) - total_tiles }; + + // Output IDs for each type defined in the map + let mut component_ids: LinkedHashSet = LinkedHashSet::new(); + let mut attribute_ids: LinkedHashMap> = LinkedHashMap::new(); + for entity in &tilemap.ecs { + let mut components: Vec = entity.components.keys().map( | id | id.to_lowercase() ).collect(); + components.sort(); + let components: LinkedHashSet = components.into_iter().collect(); + + component_ids.extend( components ); + + for ( component_name, component ) in &entity.components { + let mut attributes: Vec = component.attributes.keys().map( | id | id.to_lowercase() ).collect(); + attributes.sort(); + let attribues: LinkedHashSet = attributes.into_iter().collect(); + + attribute_ids.entry( component_name.to_owned() ).or_default().extend( attribues ); + } + } + + 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 ); + + let attribute_ids: Vec = attribute_ids[ component_id ].iter().map( | string | string.clone() ).collect(); + for i in 0..attribute_ids.len() { + constants += &format!( "\t{}_{}_{} = {}\n", level_label_const_caps, component_id.to_uppercase(), attribute_ids[ i ].to_uppercase(), i ); + } + + } + let file = format!( r#"; Level definition file ; Generated by reskit v{version} +{constants} {level_label}Tiles: incbin '{path_prefix}{level_name}/tiles.bin' @@ -472,5 +508,6 @@ pub fn get_code( tilemap: &TiledTilemap, level_name: &str, path_prefix: &str ) - dc.l {level_label}Collision dc.l {level_label}Objects"# ); + Ok( file ) } \ No newline at end of file