Skip to content

Commit

Permalink
Merge pull request #6 from StarArawn/tile_flipping
Browse files Browse the repository at this point in the history
Added tile flip flags and updated meshers.
  • Loading branch information
StarArawn authored May 1, 2021
2 parents 9d48b50 + 2df3fba commit ea19bdf
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 12 deletions.
64 changes: 52 additions & 12 deletions src/mesher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions src/tile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ 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 {
fn default() -> Self {
Self {
chunk: Entity::new(0),
texture_index: 0,
flip_x: false,
flip_y: false,
}
}
}

0 comments on commit ea19bdf

Please sign in to comment.