From 512f386c25446e5b863dad41e4a4d923b4c73775 Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Sun, 8 Nov 2020 12:30:46 +0000 Subject: [PATCH] first attempt at creating room --- Cargo.toml | 2 +- TODO.md | 1 + src/lib.rs | 58 ++++++++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 52 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 16227c3..0414973 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pixsy" -version = "0.72.5" +version = "0.72.6" description = "convert images to Bitsy rooms" authors = ["Max Bradbury "] edition = "2018" diff --git a/TODO.md b/TODO.md index 682e0e7..0a59acd 100644 --- a/TODO.md +++ b/TODO.md @@ -8,3 +8,4 @@ * if "create new bitsy game", add new room as room 0 * actually create room * stats for added room (number of tiles) +* dedupe "palette from custom colours" functionality diff --git a/src/lib.rs b/src/lib.rs index 269ce73..4d43ac0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -242,18 +242,40 @@ pub fn add_room() -> String { return "No game data loaded".to_string(); } + match &state.palette { + SelectedPalette::None => { return "No palette selected".to_string(); }, + _ => {} + }; + let mut game = state.game.clone().unwrap(); if state.image.is_none() { return "No image loaded".to_string(); } + let palette_id = Some(match &state.palette { + SelectedPalette::None => bitsy_parser::mock::game_default().palettes[0].id.clone(), + SelectedPalette::Existing { id } => id.clone(), + SelectedPalette::New { background, foreground } => { + game.add_palette(bitsy_parser::Palette { + id: "0".to_string(), + name: None, + colours: vec![ + background.clone(), + foreground.clone(), + bitsy_parser::Colour { red: 0, green: 0, blue: 0 } + ], + }) + }, + }); + let width = state.image.as_ref().unwrap().width(); let height = state.image.as_ref().unwrap().height(); let columns = (width as f64 / SD as f64).floor() as u32; let rows = (height as f64 / SD as f64).floor() as u32; - let mut tile_index = 1; + let mut tile_ids = Vec::new(); + let mut new_tile_count: u32 = 0; for column in 0..columns { for row in 0..rows { @@ -271,25 +293,45 @@ pub fn add_room() -> String { let tile = Tile { // "0" will get overwritten to a new, safe tile ID id: "0".to_string(), - name: tile_name(&state.room_name, &tile_index), + name: tile_name(&state.room_name, &new_tile_count), wall: None, animation_frames: vec![Image { pixels }], colour_id: None }; - if !game.tiles.contains(&tile) { - game.add_tile(tile.clone()); - - tile_index += 1; - } + tile_ids.push( + if game.tiles.contains(&tile) { + game.tiles.iter().find(|&t| t == &tile).unwrap().id.clone() + } else { + new_tile_count += 1; + game.add_tile(tile.clone()) + } + ); } } + // todo if player selected "create new game", delete room 0 here + + game.add_room(bitsy_parser::Room { + id: "0".to_string(), + palette_id, + name: None, + tiles: tile_ids, + items: vec![], + exits: vec![], + endings: vec![], + walls: None + }); + game.dedupe_tiles(); state.game = Some(game.to_owned()); - "OK".to_string() + format!( + "Added room \"{}\" with {} new tiles", + &state.room_name.as_ref().unwrap_or(&"untitled".to_string()), + new_tile_count + ) } #[wasm_bindgen]