-
Notifications
You must be signed in to change notification settings - Fork 0
/
tiled_quad.rs
83 lines (74 loc) · 2.46 KB
/
tiled_quad.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use crate::util::mesh::QuadMesh;
use crate::util::tilesheet::Tilesheet;
use macroquad::{
math::vec2,
prelude::{draw_rectangle, WHITE},
texture::Texture2D,
};
use tiled::{FiniteTileLayer, Loader, ObjectData, ObjectShape};
pub struct Tile {
tile_layers: Vec<QuadMesh>,
object_layers: Vec<ObjectData>,
}
impl Tile {
pub async fn from_map(path: &str) -> Self {
let mut loader = Loader::new();
let map = loader.load_tmx_map(path).unwrap();
let tileset = map.tilesets()[0].clone();
let tilesheet = Tilesheet::from_tileset(tileset).await;
let mut tile_layers = Vec::new();
let mut object_layers = Vec::new();
for layer in map.layers() {
match &layer.layer_type() {
tiled::LayerType::TileLayer(l) => Some(tile_layers.push(generate_mesh(
match l {
tiled::TileLayer::Finite(f) => f,
tiled::TileLayer::Infinite(_) => panic!("Infinite maps not supported"),
},
&tilesheet,
tilesheet.texture(),
))),
tiled::LayerType::ObjectLayer(l) => Some(for object in l.object_data() {
object_layers.push(object.clone());
}),
_ => None,
};
}
Self {
tile_layers,
object_layers,
}
}
pub fn draw_tile_layers(&self) {
for mesh in self.tile_layers.iter() {
mesh.draw();
}
}
pub fn draw_object_layers(&self) {
for object in &self.object_layers {
match object.shape {
ObjectShape::Rect { width, height } => {
draw_rectangle(object.x, object.y, width, height, WHITE)
}
_ => {}
}
}
}
pub fn get_objects(&self) -> &Vec<ObjectData> {
&self.object_layers
}
}
fn generate_mesh(layer: &FiniteTileLayer, tilesheet: &Tilesheet, texture: &Texture2D) -> QuadMesh {
let (width, height) = (layer.width() as usize, layer.height() as usize);
let mut mesh = QuadMesh::new(texture);
let mut inds = 0;
for x in 0..width as i32 {
for y in 0..height as i32 {
if let Some(tile) = layer.get_tile(x, y) {
let uv = tilesheet.tile_rect(tile.id());
inds = mesh.add_quad(vec2(x as f32, y as f32), 16., uv, inds);
}
}
}
mesh
}