alternate tile functionality; move some things around

This commit is contained in:
2020-07-20 22:34:07 +01:00
parent 225316cfb0
commit 33f2552003
7 changed files with 109 additions and 14 deletions

View File

@@ -22,7 +22,15 @@ fn tile_name(prefix: &str, index: &u32) -> Option<String> {
/// image is a base64-encoded string
/// prefix will be ignored if empty
#[wasm_bindgen]
pub fn add_tiles(game_data: String, image: String, prefix: String) -> String {
pub fn add_tiles(
game_data: String,
image: String,
prefix: String,
invert: bool,
flip: bool,
mirror: bool,
rotate: bool
) -> String {
let game = Game::from(game_data);
if game.is_err() {
return format!("Couldn't parse game data");
@@ -43,10 +51,10 @@ pub fn add_tiles(game_data: String, image: String, prefix: String) -> String {
}
let image = image.unwrap();
let width = image.width();
let height = image.height();
let columns = (width as f64 / 8.).floor() as u32;
let rows = (height as f64 / 8.).floor() as u32;
let width = image.width();
let height = image.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;
@@ -73,10 +81,75 @@ pub fn add_tiles(game_data: String, image: String, prefix: String) -> String {
};
if !game.tiles.contains(&tile) {
game.add_tile(tile);
game.add_tile(tile.clone());
tile_index += 1;
}
if invert {
let mut inverted = tile.clone();
inverted.invert();
if inverted.name.is_some() {
inverted.name = Some(format!("{} inverted", inverted.name.unwrap()));
}
if !game.tiles.contains(&inverted) {
game.add_tile(inverted);
tile_index += 1;
}
}
if flip {
let mut flipped = tile.clone();
flipped.flip();
if flipped.name.is_some() {
flipped.name = Some(format!("{} flipped", flipped.name.unwrap()));
}
if !game.tiles.contains(&flipped) {
game.add_tile(flipped);
tile_index += 1;
}
}
if mirror {
let mut mirrored = tile.clone();
mirrored.mirror();
if mirrored.name.is_some() {
mirrored.name = Some(format!("{} mirrored", mirrored.name.unwrap()));
}
if !game.tiles.contains(&mirrored) {
game.add_tile(mirrored);
tile_index += 1;
}
}
if rotate {
for i in 1..4 {
let mut rotated = tile.clone();
for _ in 0..i {
rotated.rotate();
}
if rotated.name.is_some() {
rotated.name = Some(format!("{} {}°", rotated.name.unwrap(), i * 90));
}
if !game.tiles.contains(&rotated) {
game.add_tile(rotated);
tile_index += 1;
}
}
}
}
}
@@ -92,7 +165,17 @@ mod test {
fn example() {
let game_data = bitsy_parser::mock::game_default().to_string();
let image = include_str!("test-resources/test.png.base64").to_string();
let output = add_tiles(game_data, image, "".to_string());
let output = add_tiles(
game_data,
image,
"".to_string(),
false,
false,
false,
false
);
let expected = include_str!("test-resources/expected.bitsy");
assert_eq!(output, expected);
}