Skip to content

Commit

Permalink
Merge pull request #11 from Imakoala/master
Browse files Browse the repository at this point in the history
Add support for non zero origin for the map, and fix a scaling bug
  • Loading branch information
StarArawn authored Aug 28, 2020
2 parents 8cc3f84 + 30b5c12 commit 872e31a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
17 changes: 10 additions & 7 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,17 @@ impl Map {
let y = ((-(pos.y()) / half_height) - (pos.x() / half_width)) / 2.0;
Vec2::new(x.round(), y.round())
}
pub fn center(&self) -> Translation {
pub fn center(&self, origin : &Translation) -> Translation {
let tile_size = Vec2::new(self.map.tile_width as f32, self.map.tile_height as f32);
let map_center = Vec2::new(self.map.width as f32 / 2.0, self.map.height as f32 / 2.0);
match self.map.orientation {
tiled::Orientation::Orthogonal => {
let center = Map::project_ortho(map_center, tile_size.x(), tile_size.y());
Translation::new(-center.x() * 4.0, -center.y() * 4.0, 0.0)
Translation::new(origin.x() - center.x() * 4.0, origin.y() - center.y() * 4.0, origin.z())
}
tiled::Orientation::Isometric => {
let center = Map::project_iso(map_center, tile_size.x(), tile_size.y());
Translation::new(-center.x() * 4.0, -center.y() * 4.0, 0.0)
Translation::new(origin.x() - center.x() * 4.0, origin.y() -center.y() * 4.0, origin.z())
}

_ => panic!("Unsupported orientation {:?}", self.map.orientation),
Expand All @@ -93,6 +93,7 @@ pub struct TiledMapComponents {
pub map_asset: Handle<Map>,
pub materials: HashMap<u32, Handle<ColorMaterial>>,
pub center: bool,
pub origin : Translation
}

impl Default for TiledMapComponents {
Expand All @@ -101,6 +102,7 @@ impl Default for TiledMapComponents {
map_asset: Handle::default(),
materials: HashMap::default(),
center: false,
origin : Translation::new(0., 0., 0.)
}
}
}
Expand Down Expand Up @@ -174,6 +176,7 @@ pub fn process_loaded_tile_maps(
&bool,
&Handle<Map>,
&mut HashMap<u32, Handle<ColorMaterial>>,
&Translation
)>,
) {
let mut changed_maps = HashSet::<Handle<Map>>::new();
Expand All @@ -197,7 +200,7 @@ pub fn process_loaded_tile_maps(
for changed_map in changed_maps.iter() {
let map = maps.get_mut(changed_map).unwrap();

for (_, _, _, mut materials_map) in &mut query.iter() {
for (_, _, _, mut materials_map, _) in &mut query.iter() {
for tileset in &map.map.tilesets {
if !materials_map.contains_key(&tileset.first_gid) {
let texture_path =
Expand All @@ -222,14 +225,14 @@ pub fn process_loaded_tile_maps(
}
}

for (_, center, map_handle, materials_map) in &mut query.iter() {
for (_, center, map_handle, materials_map, origin) in &mut query.iter() {
if new_meshes.contains_key(map_handle) {
let map = maps.get(map_handle).unwrap();

let translation = if *center {
map.center()
map.center(origin)
} else {
Translation::default()
*origin
};

let mesh_list = new_meshes.get_mut(map_handle).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/tile_map.vert
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ layout(set = 2, binding = 1) uniform TileMapChunk {

void main() {
v_Uv = Vertex_Uv;
vec3 position = Vertex_Position * vec3(5.0, 5.0, 1.0);
vec3 position = Vertex_Position * vec3(4.0, 4.0, 1.0);
position.z = layer_id;
gl_Position = ViewProj * Model * vec4(position, 1.0);
}

0 comments on commit 872e31a

Please sign in to comment.