1+ using SharpEngine . Core ;
12using SharpEngine . Core . Component ;
3+ using SharpEngine . Core . Math ;
4+ using SharpEngine . Core . Renderer ;
5+ using SharpEngine . Core . Utils ;
6+ using System . Collections . Generic ;
7+ using System . IO ;
8+ using System . Linq ;
9+ using System . Text . Json ;
210
311namespace SharpEngine . Tilemap ;
412
13+ /// <summary>
14+ /// Component which represents a Tilemap
15+ /// </summary>
516public class TilemapComponent : Component
617{
18+ /// <summary>
19+ /// Tilemap of the component
20+ /// </summary>
21+ public Data . Tilemap Tilemap { get ; }
22+
23+ /// <summary>
24+ /// Displayed property of Tilemap
25+ /// </summary>
26+ public bool Displayed { get ; set ; } = true ;
27+
28+ private TransformComponent ? _transform ;
29+
30+ /// <summary>
31+ /// Constructor of TilemapComponent
32+ /// </summary>
33+ /// <param name="file">Tilemap json file</param>
734 public TilemapComponent ( string file )
835 {
9-
36+ Tilemap = JsonSerializer . Deserialize < Data . Tilemap > ( File . ReadAllText ( file ) ) ! ;
37+ }
38+
39+ /// <inheritdoc />
40+ public override void Load ( )
41+ {
42+ base . Load ( ) ;
43+
44+ _transform = Entity ? . GetComponentAs < TransformComponent > ( ) ;
45+
46+ foreach ( var image in Tilemap . Images )
47+ Entity ? . Scene ? . Window ? . TextureManager . AddTexture ( image . Name , image . Path ) ;
48+
49+ }
50+
51+ /// <inheritdoc />
52+ public override void Draw ( )
53+ {
54+ base . Draw ( ) ;
55+
56+ if ( _transform == null || ! Displayed )
57+ return ;
58+
59+ var index = 0 ;
60+
61+ foreach ( var layer in Tilemap . Layers )
62+ {
63+ for ( var i = 0 ; i < layer . Tiles . Count ; i ++ )
64+ {
65+ if ( layer . Tiles [ i ] == null )
66+ continue ;
67+
68+ var tile = Tilemap . Tiles . FirstOrDefault ( x => x . Id == layer . Tiles [ i ] ! . Value ) ;
69+
70+ if ( tile == null )
71+ continue ;
72+
73+ var x = i % Tilemap . Map . Width ;
74+ var y = i / Tilemap . Map . Width ;
75+
76+ var position = new Vec2 (
77+ x * Tilemap . Map . TileWidth * _transform . Scale . X + _transform . Position . X ,
78+ y * Tilemap . Map . TileHeight * _transform . Scale . Y + _transform . Position . Y
79+ ) ;
80+ var texture = Entity ? . Scene ? . Window ? . TextureManager . GetTexture ( tile . Image ) ;
81+
82+ if ( texture == null )
83+ continue ;
84+
85+ var destination = new Rect (
86+ ( int ) position . X ,
87+ ( int ) position . Y ,
88+ Tilemap . Map . TileWidth * _transform . Scale . X ,
89+ Tilemap . Map . TileHeight * _transform . Scale . Y
90+ ) ;
91+
92+ if ( tile . XPos . HasValue && tile . YPos . HasValue && tile . Width . HasValue && tile . Height . HasValue )
93+ {
94+ var source = new Rect ( tile . XPos . Value , tile . YPos . Value , tile . Width . Value , tile . Height . Value ) ;
95+
96+ SERender . DrawTexture (
97+ texture . Value ,
98+ source ,
99+ destination ,
100+ new Vec2 ( Tilemap . Map . TileWidth / 2f , Tilemap . Map . TileHeight / 2f ) ,
101+ 0 ,
102+ Color . White ,
103+ InstructionSource . Entity ,
104+ _transform . ZLayer + index * 0.01f
105+ ) ;
106+ }
107+ else
108+ {
109+ SERender . DrawTexture (
110+ texture . Value ,
111+ new Rect ( 0 , 0 , texture . Value . Width , texture . Value . Height ) ,
112+ destination ,
113+ new Vec2 ( Tilemap . Map . TileWidth / 2f , Tilemap . Map . TileHeight / 2f ) ,
114+ 0 ,
115+ Color . White ,
116+ InstructionSource . Entity ,
117+ _transform . ZLayer + index * 0.01f
118+ ) ;
119+ }
120+
121+ index ++ ;
122+ }
123+ }
10124 }
11125}
0 commit comments