Skip to content

Commit 16d2953

Browse files
committed
feat(version): 1.0.0
1 parent 59c3ea9 commit 16d2953

File tree

14 files changed

+239
-14
lines changed

14 files changed

+239
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
[#] Bug Fixes<br>
88
[.] Others
99

10-
### V 1.0.0 - XX/XX/2023
10+
### V 1.0.0 - 26/12/2024
1111
[.] First version

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ Tilemap Package of SharpEngine - C# 2D Game Engine
88

99
## Dependencies
1010

11-
- Net7
11+
- Net8
1212
- SharpEngine.Core

SharpEngine.Tilemap/Data/Image.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace SharpEngine.Tilemap.Data;
8+
9+
public class Image
10+
{
11+
public required string Name { get; set; }
12+
public required string Path { get; set; }
13+
}

SharpEngine.Tilemap/Data/Layer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ public class Layer
1010
/// <summary>
1111
/// List of Tiles ID in the layer
1212
/// </summary>
13-
public required List<int> Tiles { get; set; }
13+
public required List<int?> Tiles { get; set; }
1414
}

SharpEngine.Tilemap/Data/Map.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ namespace SharpEngine.Tilemap.Data;
66
public class Map
77
{
88
/// <summary>
9-
/// Format of Map (currently : 1)
9+
/// Name of Map
1010
/// </summary>
11-
public required int Format { get; set; }
12-
11+
public required string Name { get; set; }
12+
1313
/// <summary>
1414
/// Width of Map in Tiles
1515
/// </summary>

SharpEngine.Tilemap/Data/Tile.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ public class Tile
1313
/// <summary>
1414
/// Path of Texture
1515
/// </summary>
16-
public required string Path { get; set; }
17-
16+
public required string Image { get; set; }
17+
1818
/// <summary>
1919
/// X Position of Tile (for tilesheet)
2020
/// </summary>

SharpEngine.Tilemap/Data/Tilemap.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,16 @@ public class Tilemap
1111
/// Map Informations of TileMap
1212
/// </summary>
1313
public required Map Map { get; set; }
14-
14+
15+
/// <summary>
16+
/// Images of TileMap
17+
/// </summary>
18+
public required List<Image> Images { get; set; }
19+
1520
/// <summary>
16-
/// Tileset of TileMap
21+
/// Tiles of TileMap
1722
/// </summary>
18-
public required List<Tile> Tileset { get; set; }
23+
public required List<Tile> Tiles { get; set; }
1924

2025
/// <summary>
2126
/// Layers of TileMap
Lines changed: 115 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,125 @@
1+
using SharpEngine.Core;
12
using 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

311
namespace SharpEngine.Tilemap;
412

13+
/// <summary>
14+
/// Component which represents a Tilemap
15+
/// </summary>
516
public 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
}

Testing/MyScene.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
using SharpEngine.Core;
2+
using SharpEngine.Core.Component;
3+
using SharpEngine.Core.Entity;
4+
using SharpEngine.Tilemap;
25

36
namespace Testing;
47

58
public class MyScene: Scene
69
{
7-
10+
public MyScene()
11+
{
12+
var entity = new Entity();
13+
entity.AddComponent(new TransformComponent(new SharpEngine.Core.Math.Vec2(200), new SharpEngine.Core.Math.Vec2(4)));
14+
entity.AddComponent(new TilemapComponent("Resource/tilemap.json"));
15+
AddEntity(entity);
16+
}
817
}

Testing/Resource/tile1.png

121 Bytes
Loading

0 commit comments

Comments
 (0)