Skip to content

Commit eb0b07d

Browse files
committed
Added dummy unit rendering
1 parent 3aa9ef7 commit eb0b07d

22 files changed

+294
-84
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -330,3 +330,4 @@ ASALocalRun/
330330

331331
# Local History for Visual Studio
332332
.localhistory/
333+
Concept/

Common/Common.csproj

+3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
</ItemGroup>
4343
<ItemGroup>
4444
<Compile Include="Colour.cs" />
45+
<Compile Include="Entities\ComponentType.cs" />
46+
<Compile Include="Entities\Entity.cs" />
47+
<Compile Include="Entities\IComponent.cs" />
4548
<Compile Include="IAnimation.cs" />
4649
<Compile Include="IGraphics.cs" />
4750
<Compile Include="IImage.cs" />

Common/Entities/ComponentType.cs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Common.Entities
2+
{
3+
public enum ComponentType
4+
{
5+
None = 0,
6+
Location,
7+
Image
8+
}
9+
}

Common/Entities/Entity.cs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
4+
namespace Common.Entities
5+
{
6+
public class Entity
7+
{
8+
public int Id { get; set; }
9+
public List<IComponent> Components { get; set; }
10+
11+
public Entity()
12+
{
13+
Components = new List<IComponent>();
14+
}
15+
16+
public T GetComponent<T>()
17+
where T : IComponent
18+
{
19+
var component = Components.OfType<T>().LastOrDefault();
20+
return component;
21+
}
22+
23+
public void AddComponent<T>(T component)
24+
where T : IComponent
25+
{
26+
if (!Components.Any(x => x is T))
27+
Components.Add(component);
28+
}
29+
}
30+
}

Common/Entities/IComponent.cs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Common.Entities
2+
{
3+
public interface IComponent
4+
{
5+
ComponentType Type { get; }
6+
}
7+
}

Common/ITexture.cs

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
{
33
public interface ITexture
44
{
5-
int Id { get; }
65
int Width { get; }
76
int Height { get; }
87
IImage Image { get; }

Common/Tile.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ public class Tile
44
{
55
public int Id { get; set; }
66
public Point Location { get; set; }
7-
public int TextureId { get; set; }
7+
public string TextureId { get; set; }
88
public TerrainType Type { get; set; }
99
}
1010
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using Common.Entities;
2+
3+
namespace MapEngine.Components
4+
{
5+
class ImageComponent : IComponent
6+
{
7+
public ComponentType Type => ComponentType.Image;
8+
public string TextureId { get; set; }
9+
}
10+
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using Common;
2+
using Common.Entities;
3+
4+
namespace MapEngine.Components
5+
{
6+
public class LocationComponent : IComponent
7+
{
8+
public ComponentType Type => ComponentType.Location;
9+
public Point Location { get; set; }
10+
}
11+
}

MapEngine/Content/Maps/TestMap1.json

+7-14
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
11
{
22
"Width": 768,
33
"Height": 512,
4-
"Heightmap": "textures/heightmap.png",
4+
"Heightmap": "WaterHeightmap.png",
55
"Description":"Water animation test",
6-
"Textures": [
7-
{
8-
"Id": 0,
9-
"Filename": "animations/water.gif",
10-
"Framerate": 100
11-
}
12-
],
136
"TileWidth":3,
147
"TileHeight":2,
158
"Tiles": [
@@ -19,7 +12,7 @@
1912
"X": 0,
2013
"Y": 0
2114
},
22-
"TextureId": 0,
15+
"TextureId": "Waves",
2316
"Type": 1
2417
},
2518
{
@@ -28,7 +21,7 @@
2821
"X": 253,
2922
"Y": 0
3023
},
31-
"TextureId": 0,
24+
"TextureId": "Waves",
3225
"Type": 1
3326
},
3427
{
@@ -37,7 +30,7 @@
3730
"X": 0,
3831
"Y": 253
3932
},
40-
"TextureId": 0,
33+
"TextureId": "Waves",
4134
"Type": 1
4235
},
4336
{
@@ -46,7 +39,7 @@
4639
"X": 253,
4740
"Y": 253
4841
},
49-
"TextureId": 0,
42+
"TextureId": "Waves",
5043
"Type": 1
5144
},
5245
{
@@ -55,7 +48,7 @@
5548
"X": 506,
5649
"Y": 0
5750
},
58-
"TextureId": 0,
51+
"TextureId": "Waves",
5952
"Type": 1
6053
},
6154
{
@@ -64,7 +57,7 @@
6457
"X": 506,
6558
"Y": 253
6659
},
67-
"TextureId": 0,
60+
"TextureId": "Waves",
6861
"Type": 1
6962
}
7063
]

MapEngine/Content/Textures/Dummy.png

295 Bytes
Loading
File renamed without changes.

MapEngine/Content/Units/Dummy.json

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"Type":"Dummy",
3+
"Location": {
4+
"X": 250,
5+
"Y": 250
6+
},
7+
"Image": {
8+
"Width":24,
9+
"Height":24,
10+
"TextureId": "Dummy",
11+
}
12+
}

MapEngine/EnumerableEx.cs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace MapEngine
2+
{
3+
public static class EnumerableEx
4+
{
5+
public static T[,] Make2DArray<T>(this T[] input, int width, int height)
6+
{
7+
T[,] output = new T[width, height];
8+
for (int x = 0; x < width; x++)
9+
{
10+
for (int y = 0; y < height; y++)
11+
{
12+
output[x, y] = input[x + y * width];
13+
}
14+
}
15+
return output;
16+
}
17+
}
18+
}

MapEngine/Handlers/MapHandler.cs

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using Common;
2+
3+
namespace MapEngine.Handlers
4+
{
5+
public class MapHandler
6+
{
7+
private Map _map;
8+
private TextureHandler _textures = new TextureHandler();
9+
10+
public MapHandler(TextureHandler textures)
11+
{
12+
_textures = textures;
13+
}
14+
15+
public void Init(string mapFile)
16+
{
17+
// Infuture this would load only relevant map textures
18+
//_textures.LoadTextures(@"C:\Source\MapEditor\MapEngine\Content\Textures\");
19+
20+
_map = ResourceLoader.LoadMap(mapFile);
21+
}
22+
23+
public void Render(IGraphics graphics)
24+
{
25+
DrawTiles(graphics, _map.Tiles);
26+
}
27+
28+
private void DrawTiles(IGraphics graphics, Tile[,] tiles)
29+
{
30+
foreach (var tile in tiles)
31+
{
32+
if (tile == null)
33+
continue;
34+
35+
if (_textures.TryGetTexture(tile.TextureId, out var texture))
36+
{
37+
var area = new Rectangle(tile.Location.X, tile.Location.Y, texture.Width, texture.Height);
38+
graphics.DrawImage(texture.Image, area);
39+
}
40+
}
41+
}
42+
}
43+
}

MapEngine/Handlers/TextureHandler.cs

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
5+
namespace MapEngine.Handlers
6+
{
7+
public class TextureHandler
8+
{
9+
public Dictionary<string, Texture> _textures = new Dictionary<string, Texture>(StringComparer.OrdinalIgnoreCase);
10+
11+
public void LoadTextures(string filepath)
12+
{
13+
foreach (var file in Directory.GetFiles(filepath, "*.gif"))
14+
{
15+
var name = Path.GetFileNameWithoutExtension(file).ToUpper();
16+
var animation = ResourceLoader.LoadAnimation(file);
17+
var texture = new Texture(animation);
18+
_textures.Add(name, texture);
19+
}
20+
21+
foreach (var file in Directory.GetFiles(filepath, "*.png"))
22+
{
23+
var name = Path.GetFileNameWithoutExtension(file);
24+
var image = ResourceLoader.LoadImage(file);
25+
var texture = new Texture(image);
26+
_textures.Add(name, texture);
27+
}
28+
}
29+
30+
public bool TryGetTexture(string textureId, out Texture texture)
31+
{
32+
return _textures.TryGetValue(textureId, out texture);
33+
}
34+
}
35+
}

MapEngine/Handlers/UnitHandler.cs

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using Common;
2+
using Common.Entities;
3+
using MapEngine.Components;
4+
using System.Collections.Generic;
5+
6+
namespace MapEngine.Handlers
7+
{
8+
public class UnitHandler
9+
{
10+
private int _unitIndex;
11+
private readonly TextureHandler _textures;
12+
private Dictionary<int, Entity> _units = new Dictionary<int, Entity>();
13+
14+
public UnitHandler(TextureHandler textures)
15+
{
16+
_textures = textures;
17+
}
18+
19+
public void Init(string filename)
20+
{
21+
_textures.LoadTextures(@"C:\Source\MapEditor\MapEngine\Content\Textures");
22+
23+
var unit = ResourceLoader.LoadUnit(filename);
24+
_units.Add(_unitIndex++, unit);
25+
}
26+
27+
public void Render(IGraphics graphics)
28+
{
29+
foreach (var unit in _units.Values)
30+
{
31+
var location = unit.GetComponent<LocationComponent>().Location;
32+
var textureId = unit.GetComponent<ImageComponent>().TextureId;
33+
if (_textures.TryGetTexture(textureId, out var texture))
34+
{
35+
var area = texture.Area(location);
36+
graphics.DrawImage(texture.Image, area);
37+
}
38+
}
39+
}
40+
}
41+
}

MapEngine/Map.cs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using Common;
2-
using System.Collections.Generic;
32

43
namespace MapEngine
54
{
@@ -8,6 +7,5 @@ public class Map
87
public int Width { get; set; }
98
public int Height { get; set; }
109
public Tile[,] Tiles { get; set; }
11-
public Dictionary<int, Texture> Textures { get; set; }
1210
}
1311
}

MapEngine/MapEngine.csproj

+6-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,13 @@
5858
<Generator>MSBuild:Compile</Generator>
5959
<SubType>Designer</SubType>
6060
</ApplicationDefinition>
61+
<Compile Include="Components\ImageComponent.cs" />
62+
<Compile Include="EnumerableEx.cs" />
63+
<Compile Include="Handlers\MapHandler.cs" />
64+
<Compile Include="Handlers\TextureHandler.cs" />
65+
<Compile Include="Handlers\UnitHandler.cs" />
6166
<Compile Include="ImageEx.cs" />
67+
<Compile Include="Components\LocationComponent.cs" />
6268
<Compile Include="Map.cs" />
6369
<Compile Include="ResourceLoader.cs" />
6470
<Compile Include="Scene.cs" />
@@ -114,7 +120,6 @@
114120
</ProjectReference>
115121
</ItemGroup>
116122
<ItemGroup>
117-
<Resource Include="Content\Animations\water.gif" />
118123
<Resource Include="Content\Textures\water.png" />
119124
</ItemGroup>
120125
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

0 commit comments

Comments
 (0)