Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for non zero origin for the map, and fix a scaling bug #11

Merged
merged 3 commits into from
Aug 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
StarArawn marked this conversation as resolved.
Show resolved Hide resolved
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);
StarArawn marked this conversation as resolved.
Show resolved Hide resolved
position.z = layer_id;
gl_Position = ViewProj * Model * vec4(position, 1.0);
}