Skip to content

Commit bc6b5dc

Browse files
authored
Handle rotation mirroring (#328)
* Handle tile rotation & mirroring in ggez * formatting * formatting & refactor
1 parent 18be44f commit bc6b5dc

File tree

1 file changed

+36
-12
lines changed

1 file changed

+36
-12
lines changed

examples/ggez/map.rs

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::collections::HashMap;
1+
use std::{collections::HashMap, f32::consts::FRAC_PI_2};
22

33
use ggez::{
44
graphics::{self, Canvas, DrawParam, InstanceArray},
@@ -173,17 +173,41 @@ impl MapHandler {
173173
* 20.0;
174174
}
175175

176-
batch.push(
177-
DrawParam::default()
178-
.src(get_tile_rect(ts, tile.id(), ts_size.0, ts_size.1))
179-
.dest([dx, dy])
180-
.color(ggez::graphics::Color::from_rgba(
181-
0xFF,
182-
0xFF,
183-
0xFF,
184-
(layer.opacity * 255.0) as u8,
185-
)),
186-
);
176+
// Handle tile rotation and mirroring
177+
let offset_x = self.tile_width() as f32 / 2.0;
178+
let offset_y = self.tile_height() as f32 / 2.0;
179+
180+
let mut draw_param = DrawParam::default()
181+
.src(get_tile_rect(ts, tile.id(), ts_size.0, ts_size.1))
182+
.offset([offset_x, offset_y])
183+
.dest([dx + offset_x, dy + offset_y])
184+
.color(ggez::graphics::Color::from_rgba(
185+
0xFF,
186+
0xFF,
187+
0xFF,
188+
(layer.opacity * 255.0) as u8,
189+
));
190+
191+
let (fd, fh, fv) = (tile.flip_d, tile.flip_h, tile.flip_v);
192+
193+
draw_param = if fd {
194+
match (fh, fv) {
195+
(true, true) => {
196+
draw_param.scale([-1.0, 1.0]).rotation(FRAC_PI_2)
197+
}
198+
(true, false) => draw_param.rotation(FRAC_PI_2),
199+
(false, true) => draw_param.rotation(-FRAC_PI_2),
200+
(false, false) => {
201+
draw_param.scale([-1.0, 1.0]).rotation(-FRAC_PI_2)
202+
}
203+
}
204+
} else {
205+
let sx = if fh { -1.0 } else { 1.0 };
206+
let sy = if fv { -1.0 } else { 1.0 };
207+
draw_param.scale([sx, sy])
208+
};
209+
210+
batch.push(draw_param);
187211
}
188212
}
189213
}

0 commit comments

Comments
 (0)