ldtk-codegen 0.1.1

Generate typed rust code from LDtk Project
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
pub mod math {
    SERDE_USE!();
    #[derive([SERDE]Clone, Copy, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
    pub struct Vec2<T> {
        pub x: T,
        pub y: T,
    }

    impl<T> Vec2<T> {
        pub const fn new(x: T, y: T) -> Self {
            Self { x, y }
        }

        pub fn casted<I>(self) -> I
        where
            Self: Into<I>,
        {
            self.into()
        }

        pub fn min<I: Into<Self>>(self, other: I) -> Self
        where
            T: Ord,
        {
            let other = other.into();
            Self::new(
                std::cmp::min(self.x, other.x),
                std::cmp::min(self.y, other.y),
            )
        }

        pub fn max<I: Into<Self>>(self, other: I) -> Self
        where
            T: Ord,
        {
            let other = other.into();
            Self::new(
                std::cmp::max(self.x, other.x),
                std::cmp::max(self.y, other.y),
            )
        }
    }

    macro_rules! implement_value {
        ($($type: ty),+) => {
            $(
                impl Vec2<$type> {
                    pub const fn zero() -> Self {
                        Self::new(0 as $type, 0 as $type)
                    }

                    pub const fn one() -> Self {
                        Self::new(1 as $type, 1 as $type)
                    }
                }
            )+
        };
    }

    implement_value!(i8, u8, i16, u16, i32, u32, f32, i64, u64, f64);

    macro_rules! implement_binary_op {
        ($trait: ident: $fn: ident ($self: ident, $rhs: ident) => $value: expr) => {
            impl<T, O> std::ops::$trait<O> for Vec2<T>
            where
                O: Into<Vec2<T>>,
                T: std::ops::$trait<T, Output = T>,
            {
                type Output = Self;

                fn $fn($self, rhs: O) -> Self::Output {
                    let $rhs = rhs.into();
                    $value
                }
            }
        };
    }

    implement_binary_op!(Add: add (self, rhs) => Vec2::new(self.x + rhs.x, self.y + rhs.y));
    implement_binary_op!(Sub: sub (self, rhs) => Vec2::new(self.x - rhs.x, self.y - rhs.y));
    implement_binary_op!(Mul: mul (self, rhs) => Vec2::new(self.x * rhs.x, self.y * rhs.y));
    implement_binary_op!(Div: div (self, rhs) => Vec2::new(self.x / rhs.x, self.y / rhs.y));

    macro_rules! implement_assign_op {
        ($trait: ident, $na_trait: ident: $fn: ident ($self: ident, $rhs: ident) => $value: expr) => {
            impl<T, O> std::ops::$trait<O> for Vec2<T>
            where
                O: Into<Vec2<T>>,
                T: std::ops::$na_trait<Output = T> + Copy,
            {
                fn $fn(&mut $self, rhs: O) {
                    let $rhs = rhs.into();
                    *$self = $value;
                }
            }
        };
    }

    impl<T: std::ops::Neg> std::ops::Neg for Vec2<T> {
        type Output = Vec2<T::Output>;

        fn neg(self) -> Self::Output {
            Self::Output::new(-self.x, -self.y)
        }
    }

    implement_assign_op!(AddAssign, Add: add_assign (self, rhs) => Vec2::new(self.x + rhs.x, self.y + rhs.y));
    implement_assign_op!(SubAssign, Sub: sub_assign (self, rhs) => Vec2::new(self.x - rhs.x, self.y - rhs.y));
    implement_assign_op!(MulAssign, Mul: mul_assign (self, rhs) => Vec2::new(self.x * rhs.x, self.y * rhs.y));
    implement_assign_op!(DivAssign, Div: div_assign (self, rhs) => Vec2::new(self.x / rhs.x, self.y / rhs.y));

    macro_rules! implement_from {
        ($type1: ty => $($type2: ty),+) => {
            $(impl From<Vec2<$type2>> for Vec2<$type1> {
                fn from(other: Vec2<$type2>) -> Self {
                    Self::new(other.x as _, other.y as _)
                }
            })+
        };
    }

    implement_from!(i8 => u8, i16, u16, i32, u32, f32, i64, u64, f64);
    implement_from!(u8 => i8, i16, u16, i32, u32, f32, i64, u64, f64);
    implement_from!(i16 => i8, u8, u16, i32, u32, f32, i64, u64, f64);
    implement_from!(u16 => i8, u8, i16, i32, u32, f32, i64, u64, f64);
    implement_from!(i32 => i8, u8, i16, u16, u32, f32, i64, u64, f64);
    implement_from!(u32 => i8, u8, i16, u16, i32, f32, i64, u64, f64);
    implement_from!(f32 => i8, u8, i16, u16, i32, u32, i64, u64, f64);
    implement_from!(i64 => i8, u8, i16, u16, i32, u32, f32, u64, f64);
    implement_from!(u64 => i8, u8, i16, u16, i32, u32, f32, i64, f64);
    implement_from!(f64 => i8, u8, i16, u16, i32, u32, f32, i64, u64);

    impl<T: Copy> From<T> for Vec2<T> {
        fn from(value: T) -> Self {
            Self::new(value, value)
        }
    }

    impl<T: Copy> From<(T, T)> for Vec2<T> {
        fn from(value: (T, T)) -> Self {
            Self::new(value.0, value.1)
        }
    }

    impl<T: Copy> From<Vec2<T>> for (T, T) {
        fn from(value: Vec2<T>) -> Self {
            (value.x, value.y)
        }
    }

    impl<T: Copy> From<[T; 2]> for Vec2<T> {
        fn from(value: [T; 2]) -> Self {
            Self::new(value[0], value[1])
        }
    }

    impl<T: Copy> From<Vec2<T>> for [T; 2] {
        fn from(value: Vec2<T>) -> Self {
            [value.x, value.y]
        }
    }

    macro_rules! generate_from_into {
        (!for $type: ty, $($self_scalar: ident),+) => {
            $(impl From<$type> for Vec2<$self_scalar> {
                fn from(other: $type) -> Self {
                    Self::new(other.x as _, other.y as _)
                }
            }

            impl From<Vec2<$self_scalar>> for $type {
                fn from(other: Vec2<$self_scalar>) -> Self {
                    Self::new(other.x as _, other.y as _)
                }
            })+
        };
        ($($type: ty),+) => {
            $(generate_from_into!(!for $type, i8, u8, i16, u16, i32, u32, f32, i64, u64, f64);)+
        };
    }
    CUSTOM_VECTORS!();
}

use math::*;

/* --- Traits --- */
pub mod layer {
    use super::math::*;

    /// A layer trait
    pub trait Layer {
        const GRID_SIZE: u32;
        const GUIDE_GRID_SIZE: Vec2<u32>;
        const PX_OFFSET: Vec2<i32>;
        const PARALLAX_FACTOR: Vec2<f32>;
        // TODO: parallaxScaling, requiredTags, excludedTags, tilePivot
        // TODO: vars: px_offset, total_px_offset

        fn size(&self) -> Vec2<u32>;
        fn pixel_size(&self) -> Vec2<u32> {
            self.size() * Self::GRID_SIZE
        }

        fn grid_size(&self) -> Vec2<u32> {
            Vec2::from(Self::GRID_SIZE)
        }
    }

    #[macro_export]
    macro_rules! generate_layer {
        (
            $(!doc $layer_doc: literal)?
            $layer: ident:
                grid_size = $grid_size: expr,
                guide_grid_size = $guide_grid_size: expr,
                px_offset = $px_offset: expr,
                parallax_factor = $parallax_factor: expr,
                $($field: ident: $field_type: ty,)*
        ) => {
            $(#[doc = $layer_doc])?
            #[derive([SERDE]Clone, Debug, PartialEq, PartialOrd)]
            pub struct $layer {
                size: Vec2<u32>,
                $($field: $field_type,)*
            }

            impl layer::Layer for $layer {
                const GRID_SIZE: u32 = $grid_size;
                const GUIDE_GRID_SIZE: Vec2<u32> = $guide_grid_size;
                const PX_OFFSET: Vec2<i32> = $px_offset;
                const PARALLAX_FACTOR: Vec2<f32> = $parallax_factor;
                fn size(&self) -> Vec2<u32> {
                    return self.size;
                }
            }
        };
    }
    pub use generate_layer;

    pub struct RectangularRegion {
        start: Vec2<u32>,
        size: Vec2<u32>,
        position: Vec2<u32>,
    }

    impl RectangularRegion {
        pub fn new(start: Vec2<u32>, size: Vec2<u32>) -> Self {
            Self {
                start,
                size,
                position: start,
            }
        }
    }

    impl Iterator for RectangularRegion {
        type Item = Vec2<u32>;

        fn next(&mut self) -> Option<Self::Item> {
            if self.position.y >= self.start.y + self.size.y {
                return None;
            }
            let tile = self.position;
            self.position.x += 1;
            if self.position.x > self.start.x + self.size.x {
                self.position.x = self.start.x;
                self.position.y += 1;
            }
            Some(tile)
        }
    }

    macro_rules! rectangular_region {
        ($name: ident ($source: ident) -> $type: ty: $self: ident -> $expr: expr) => {
            pub struct $name<'a, S: $source> {
                start: Vec2<u32>,
                size: Vec2<u32>,
                position: Vec2<u32>,
                source: &'a S,
            }

            impl<'a, S: $source> $name<'a, S> {
                pub fn new(source: &'a S, start: Vec2<u32>, size: Vec2<u32>) -> Self {
                    Self {
                        source,
                        start,
                        size,
                        position: start,
                    }
                }
            }

            impl<'a, S: $source> Iterator for $name<'a, S>  {
                type Item = (Vec2<u32>, $type);

                fn next(&mut $self) -> Option<Self::Item> {
                    if $self.position.y >= $self.start.y + $self.size.y {
                        return None;
                    }
                    let tile = ($self.position, $expr);
                    $self.position.x += 1;
                    if $self.position.x > $self.start.x + $self.size.x {
                        $self.position.x = $self.start.x;
                        $self.position.y += 1;
                    }
                    Some(tile)
                }
            }
        };
    }

    rectangular_region!(IntGridRegion(IntGrid) -> Option<&'a S::Tile>: self -> self.source.get(self.position));
    rectangular_region!(TilesRegion(Tiles) -> Option<&'a Tile>: self -> self.source.get(self.position));
    rectangular_region!(AutoLayerRegion(AutoLayer) -> Vec<Tile>: self -> self.source.get_autotile(self.position));

    // * -------------------------------------------------------------------------------- Int Grid -------------------------------------------------------------------------------- * //
    /// An integer grid layer trait
    pub trait IntGrid: Layer {
        type Tile;

        fn get(&self, position: impl Into<Vec2<i32>>) -> Option<&Self::Tile>;
        fn get_mut(&mut self, position: impl Into<Vec2<i32>>) -> Option<&mut Self::Tile>;
        fn rect(
            &self,
            start: impl Into<Vec2<i32>>,
            size: impl Into<Vec2<u32>>,
        ) -> IntGridRegion<'_, Self>
        where
            Self: std::marker::Sized,
        {
            IntGridRegion::new(self, start.into().max(0).into(), size.into())
        }
    }

    #[macro_export]
    macro_rules! generate_int_grid_layer {
        (
            $(!doc $layer_doc: literal)?
            $layer: ident:
                grid_size = $grid_size: expr,
                guide_grid_size = $guide_grid_size: expr,
                px_offset = $px_offset: expr,
                parallax_factor = $parallax_factor: expr,

            $(!auto_layer $auto_layer: ident = $tileset: expr;)?

            $layer_tile: ident:
                $($tile_variant: ident),*
        ) => {
            #[doc = concat!("Possible tiles for '", stringify!($layer_tile), "' int grid layer")]
            #[derive([SERDE]Default, Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
            pub enum $layer_tile {
                #[default]
                Empty,
                $($tile_variant,)*
            }

            layer::generate_layer!(
                $(!doc $layer_doc)?
                $layer:
                    grid_size = $grid_size,
                    guide_grid_size = $guide_grid_size,
                    px_offset = $px_offset,
                    parallax_factor = $parallax_factor,
                    tiles: Vec<$layer_tile>,
                    $(
                        $auto_layer: Vec<Vec<Tile>>,
                    )?
            );

            impl layer::IntGrid for $layer {
                type Tile = $layer_tile;

                fn get(&self, position: impl Into<Vec2<i32>>) -> Option<&Self::Tile> {
                    let position = position.into();
                    if position.x < 0 || position.y < 0 || position.x as u32 >= self.size.x || position.y as u32 >= self.size.y {
                        return None;
                    }
                    return self.tiles.get(position.x as usize + position.y as usize * self.size.x as usize);
                }

                fn get_mut(&mut self, position: impl Into<Vec2<i32>>) -> Option<&mut Self::Tile> {
                    let position = position.into();
                    if position.x < 0 || position.y < 0 || position.x as u32 >= self.size.x || position.y as u32 >= self.size.y {
                        return None;
                    }
                    return self.tiles.get_mut(position.x as usize + position.y as usize * self.size.x as usize);
                }
            }

            impl<T: Into<Vec2<i32>>> std::ops::Index<T> for $layer {
                type Output = <Self as layer::IntGrid>::Tile;

                fn index(&self, position: T) -> &Self::Output {
                    use layer::IntGrid;
                    return self.get(position).unwrap();
                }
            }

            impl<T: Into<Vec2<i32>>> std::ops::IndexMut<T> for $layer {
                fn index_mut(&mut self, position: T) -> &mut Self::Output {
                    use layer::IntGrid;
                    return self.get_mut(position).unwrap();
                }
            }

            $(
                layer::implement_auto_layer!($auto_layer = $tileset, $layer);
            )?
        };
    }
    pub(super) use generate_int_grid_layer;

    // * ---------------------------------------------------------------------------------- Tiles --------------------------------------------------------------------------------- * //
    use super::Tile;
    use super::TilesetID;

    /// A tile layer trait
    pub trait Tiles: Layer {
        const TILESET_ID: TilesetID;

        fn get(&self, position: impl Into<Vec2<i32>>) -> Option<&Tile>;
        fn get_mut(&mut self, position: impl Into<Vec2<i32>>) -> Option<&mut Option<Tile>>;

        fn rect(
            &self,
            start: impl Into<Vec2<i32>>,
            size: impl Into<Vec2<u32>>,
        ) -> TilesRegion<'_, Self>
        where
            Self: std::marker::Sized,
        {
            TilesRegion::new(self, start.into().max(0).into(), size.into())
        }
    }

    #[macro_export]
    macro_rules! generate_tiles_layer {
        (
            $(!doc $layer_doc: literal)?
            $layer: ident:
                grid_size = $grid_size: expr,
                guide_grid_size = $guide_grid_size: expr,
                px_offset = $px_offset: expr,
                parallax_factor = $parallax_factor: expr,
                tileset = $tileset: expr,
        ) => {
            layer::generate_layer!(
                $(!doc $layer_doc)?
                $layer:
                    grid_size = $grid_size,
                    guide_grid_size = $guide_grid_size,
                    px_offset = $px_offset,
                    parallax_factor = $parallax_factor,
                    tiles: Vec<Option<Tile>>,
            );

            impl layer::Tiles for $layer {
                const TILESET_ID: TilesetID = $tileset;

                fn get(&self, position: impl Into<Vec2<i32>>) -> Option<&Tile> {
                    let position = position.into();
                    if position.x < 0 || position.y < 0 || position.x as u32 >= self.size.x || position.y as u32 >= self.size.y {
                        return None;
                    }
                    self.tiles.get(position.x as usize + position.y as usize * self.size.x as usize)?.as_ref()
                }

                fn get_mut(&mut self, position: impl Into<Vec2<i32>>) -> Option<&mut Option<Tile>> {
                    let position = position.into();
                    if position.x < 0 || position.y < 0 || position.x as u32 >= self.size.x || position.y as u32 >= self.size.y {
                        return None;
                    }
                    return self.tiles.get_mut(position.x as usize + position.y as usize * self.size.x as usize);
                }
            }

            impl<T: Into<Vec2<i32>>> std::ops::Index<T> for $layer {
                type Output = Tile;

                fn index(&self, position: T) -> &Self::Output {
                    use layer::Tiles;
                    return self.get(position).unwrap();
                }
            }
        };
    }
    pub(super) use generate_tiles_layer;

    // * -------------------------------------------------------------------------------- AutoLayer ------------------------------------------------------------------------------- * //
    /// An auto layer trait
    pub trait AutoLayer: Layer {
        const TILESET_ID: TilesetID;

        fn get_autotile(&self, position: impl Into<Vec2<i32>>) -> Vec<Tile>;
        fn autotile_rect(
            &self,
            start: impl Into<Vec2<i32>>,
            size: impl Into<Vec2<u32>>,
        ) -> AutoLayerRegion<'_, Self>
        where
            Self: std::marker::Sized,
        {
            AutoLayerRegion::new(self, start.into().max(0).into(), size.into())
        }
    }

    #[macro_export]
    macro_rules! implement_auto_layer {
        ($source: ident = $tileset: expr, $layer: ident) => {
            impl layer::AutoLayer for $layer {
                const TILESET_ID: TilesetID = $tileset;

                fn get_autotile(&self, position: impl Into<Vec2<i32>>) -> Vec<Tile> {
                    let position = position.into();
                    if position.x < 0
                        || position.y < 0
                        || position.x as u32 >= self.size.x
                        || position.y as u32 >= self.size.y
                    {
                        return Vec::new();
                    }
                    return self
                        .auto_tiles
                        .get(position.x as usize + position.y as usize * self.size.x as usize)
                        .cloned()
                        .unwrap_or_default();
                }
            }
        };
    }
    pub(super) use implement_auto_layer;

    // * -------------------------------------------------------------------------------- Entities -------------------------------------------------------------------------------- * //
    use super::EntityObject;

    /// An entities layer trait
    pub trait Entities: Layer {
        fn entities(&self) -> &Vec<EntityObject>;
        fn entities_mut(&mut self) -> &mut Vec<EntityObject>;
    }

    #[macro_export]
    macro_rules! generate_entities_layer {
        (
            $(!doc $layer_doc: literal)?
            $layer: ident:
                grid_size = $grid_size: expr,
                guide_grid_size = $guide_grid_size: expr,
                px_offset = $px_offset: expr,
                parallax_factor = $parallax_factor: expr,
        ) => {
            layer::generate_layer!(
                $(!doc $layer_doc)?
                $layer:
                    grid_size = $grid_size,
                    guide_grid_size = $guide_grid_size,
                    px_offset = $px_offset,
                    parallax_factor = $parallax_factor,
                    entities: Vec<EntityObject>,
            );

            impl layer::Entities for $layer {
                fn entities(&self) -> &Vec<EntityObject> {
                    &self.entities
                }

                fn entities_mut(&mut self) -> &mut Vec<EntityObject> {
                    &mut self.entities
                }
            }

            impl std::ops::Deref for $layer {
                type Target = Vec<EntityObject>;

                fn deref(&self) -> &Self::Target {
                    use layer::Entities;
                    self.entities()
                }
            }

            impl std::ops::DerefMut for $layer {
                fn deref_mut(&mut self) -> &mut Self::Target {
                    use layer::Entities;
                    self.entities_mut()
                }
            }

        };
    }
    pub(super) use generate_entities_layer;
}

#[derive([SERDE]Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct LDTKColor {
    pub r: u8,
    pub g: u8,
    pub b: u8,
    pub a: u8,
}

impl LDTKColor {
    pub const fn new(r: u8, g: u8, b: u8, a: u8) -> Self {
        Self { r, g, b, a }
    }

    pub fn casted<I>(self) -> I
    where
        Self: Into<I>,
    {
        self.into()
    }
}

impl From<u32> for LDTKColor {
    fn from(value: u32) -> Self {
        Self::new(
            (value >> 24 & 0xff) as u8,
            (value >> 16 & 0xff) as u8,
            (value >> 8 & 0xff) as u8,
            (value & 0xff) as u8,
        )
    }
}

impl From<LDTKColor> for u32 {
    fn from(value: LDTKColor) -> Self {
        (value.r as u32) << 24 | (value.g as u32) << 16 | (value.b as u32) << 8 | value.a as u32
    }
}

macro_rules! generate_color_from_into {
    ($($type: ty),+) => {
        $(impl From<$type> for LDTKColor {
            fn from(other: $type) -> Self {
                Self::new(other.r, other.g, other.b, other.a)
            }
        }

        impl From<LDTKColor> for $type {
            fn from(other: LDTKColor) -> Self {
                Self::new(other.r, other.g, other.b, other.a)
            }
        })+
    };
}
CUSTOM_COLORS!();
/* --- Tileset --- */
type TilesetID = u32;

#[derive([SERDE]Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct Tileset {
    id: TilesetID,
    path: std::path::PathBuf,
}

impl Tileset {
    pub fn new(id: TilesetID, path: std::path::PathBuf) -> Self {
        Self { id, path }
    }

    pub fn id(&self) -> TilesetID {
        self.id
    }

    pub fn path(&self) -> &std::path::Path {
        &self.path
    }
}

#[derive([SERDE]Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct Tile {
    position: Vec2<u32>,
    flip: FlipMode,
}

impl Tile {
    pub fn new(position: Vec2<u32>, flip: FlipMode) -> Self {
        Self { position, flip }
    }

    pub fn position(&self) -> Vec2<u32> {
        self.position
    }

    pub fn flip(&self) -> FlipMode {
        self.flip
    }
}

#[derive([SERDE]Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub enum FlipMode {
    None,
    Horizontal,
    Vertical,
    Both,
}

impl FlipMode {
    pub fn horizontal(&self) -> bool {
        matches!(self, Self::Horizontal | Self::Both)
    }

    pub fn vertical(&self) -> bool {
        matches!(self, Self::Vertical | Self::Both)
    }
}

/* --- Entity --- */
#[derive([SERDE]Clone, Debug, PartialEq, PartialOrd)]
pub struct EntityObject {
    pub entity: Entity, // Vec<Component> if LDtk will support ECS
    pub position: Vec2<f32>,
    pub size: Vec2<u32>,
    // TODO: LDTKColor, tileRenderMode (nineSliceBorders, tileRect), tags
}

impl EntityObject {
    pub fn new(entity: Entity, position: Vec2<f32>, size: Vec2<u32>) -> Self {
        Self {
            entity,
            position,
            size,
        }
    }

    pub fn top_left(&self) -> Vec2<f32> {
        self.position - self.size * self.entity.pivot()
    }
}

#[derive([SERDE]Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub enum RenderMode {
    Rectangle,
    Ellipse,
    Cross,
    Tile {
        tileset: TilesetID,
        tile: Vec2<u32>,
        size: Vec2<u32>,
    },
}

#[derive([SERDE]Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct EntityRef {
    level: usize,
    layer: usize,
    entity: usize,
}

impl EntityRef {
    pub fn new(level: usize, layer: usize, entity: usize) -> Self {
        Self {
            level,
            layer,
            entity,
        }
    }

    pub fn find<'a>(&self, world: &'a World) -> Option<&'a EntityObject> {
        let level = world.get(self.level)?;
        match self.layer {
            LAYER_INDEX => GET_LAYER!(),
            _ => None,
        }
    }

    pub fn find_mut<'a>(&self, world: &'a mut World) -> Option<&'a mut EntityObject> {
        let level = world.get_mut(self.level)?;
        match self.layer {
            LAYER_INDEX => GET_LAYER_mut!(),
            _ => None,
        }
    }
}