From 2df3fba33dfd6014dce0b17b15f51f33e030c6ce Mon Sep 17 00:00:00 2001 From: StarArawn Date: Sat, 1 May 2021 08:06:01 -0400 Subject: [PATCH] Added tile flip flags and updated meshers. --- src/mesher.rs | 64 +++++++++++++++++++++++++++++++++++++++++---------- src/tile.rs | 4 ++++ 2 files changed, 56 insertions(+), 12 deletions(-) diff --git a/src/mesher.rs b/src/mesher.rs index 29b5c6df..4e4eaf1b 100644 --- a/src/mesher.rs +++ b/src/mesher.rs @@ -59,10 +59,23 @@ impl TilemapChunkMesher for SquareChunkMesher { let start_v: f32 = sprite_sheet_y / chunk.texture_size.y; let end_v: f32 = (sprite_sheet_y + chunk.tile_size.y) / chunk.texture_size.y; - uvs.push([start_u, end_v]); - uvs.push([start_u, start_v]); - uvs.push([end_u, start_v]); - uvs.push([end_u, end_v]); + let mut new_uv = vec![ + [start_u, end_v], + [start_u, start_v], + [end_u, start_v], + [end_u, end_v], + ]; + + if tile.flip_x { + new_uv.reverse(); + } + if tile.flip_y { + new_uv.reverse(); + new_uv.swap(0, 2); + new_uv.swap(1, 3); + } + + uvs.extend(new_uv); indices.extend_from_slice(&[i + 0, i + 2, i + 1, i + 0, i + 3, i + 2]); i += 4; @@ -201,10 +214,24 @@ impl TilemapChunkMesher for HexChunkMesher { let start_v: f32 = sprite_sheet_y / chunk.texture_size.y; let end_v: f32 = (sprite_sheet_y + chunk.tile_size.y) / chunk.texture_size.y; - uvs.push([start_u, end_v]); - uvs.push([start_u, start_v]); - uvs.push([end_u, start_v]); - uvs.push([end_u, end_v]); + let mut new_uv = vec![ + [start_u, end_v], + [start_u, start_v], + [end_u, start_v], + [end_u, end_v], + ]; + + if tile.flip_x { + new_uv.reverse(); + } + if tile.flip_y { + new_uv.reverse(); + new_uv.swap(0, 2); + new_uv.swap(1, 3); + } + + uvs.extend(new_uv); + indices.extend_from_slice(&[i + 0, i + 2, i + 1, i + 0, i + 3, i + 2]); i += 4; @@ -288,10 +315,23 @@ impl TilemapChunkMesher for IsoChunkMesher { let start_v: f32 = sprite_sheet_y / chunk.texture_size.y; let end_v: f32 = (sprite_sheet_y + chunk.tile_size.y) / chunk.texture_size.y; - uvs.push([start_u, end_v]); - uvs.push([start_u, start_v]); - uvs.push([end_u, start_v]); - uvs.push([end_u, end_v]); + let mut new_uv = vec![ + [start_u, end_v], + [start_u, start_v], + [end_u, start_v], + [end_u, end_v], + ]; + + if tile.flip_x { + new_uv.reverse(); + } + if tile.flip_y { + new_uv.reverse(); + new_uv.swap(0, 2); + new_uv.swap(1, 3); + } + + uvs.extend(new_uv); indices.extend_from_slice(&[i + 0, i + 2, i + 1, i + 0, i + 3, i + 2]); i += 4; diff --git a/src/tile.rs b/src/tile.rs index 4c48bb85..c56989c2 100644 --- a/src/tile.rs +++ b/src/tile.rs @@ -4,6 +4,8 @@ use bevy::prelude::*; pub struct Tile { pub chunk: Entity, pub texture_index: u32, + pub flip_x: bool, + pub flip_y: bool, } impl Default for Tile { @@ -11,6 +13,8 @@ impl Default for Tile { Self { chunk: Entity::new(0), texture_index: 0, + flip_x: false, + flip_y: false, } } }