Example project

This commit is contained in:
krnlexception 2024-09-10 00:56:24 +02:00
parent 01099daef1
commit 7407edccb3
No known key found for this signature in database
GPG key ID: DFAB34E39238AEDE
6 changed files with 129 additions and 0 deletions

View file

@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\DotTiled\DotTiled.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="tilemap.tmx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="tileset.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="assets\tileset.tsx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="tileset.tsx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

View file

@ -0,0 +1,78 @@
using DotTiled.Serialization;
namespace DotTiled.Example;
class Program
{
static void Main(string[] args)
{
Quick();
Manual();
}
// QUICK START
// Automatic and easy way to load tilemaps.
static void Quick()
{
var loader = Loader.Default();
var map = loader.LoadMap("tilemap.tmx");
// You can do stuff with it like...
Console.WriteLine($"Tile width and height: {map.TileWidth}x{map.TileHeight}");
TileLayer layer0 = (TileLayer)map.Layers[0]; // Get a layer
Console.WriteLine($"Tile in layer 0 at 0, 0: {layer0.Data.Value.GlobalTileIDs.Value[0]}");
}
// MANUAL
// Manually load a map, if you need to load from a custom source
static void Manual()
{
using var mapFileReader = new StreamReader("tilemap.tmx");
var mapString = mapFileReader.ReadToEnd();
using var mapReader = new MapReader(mapString, ResolveTileset, ResolveTemplate, ResolveCustomType);
var map = mapReader.ReadMap();
// Now do some other stuff with it...
StringProperty hello = (StringProperty)map.Properties.FirstOrDefault(property => property.Name == "hello");
Console.WriteLine($"Layer 1 name: {map.Layers[0].Name}");
Console.WriteLine($"Property 'hello': {hello.Value}");
// Now with tileset
Tileset tileset = map.Tilesets[0];
Console.WriteLine($"Tileset 0 source: {tileset.Source.Value}");
Console.WriteLine($"Tileset 0 image 0 source: {tileset.Image.Value.Source.Value}");
}
/* This function is responsible for loading all tilesets required by a tilemap, if you
want to use a custom source. */
static Tileset ResolveTileset(string source)
{
string tilesetPath = Path.Combine(Directory.GetCurrentDirectory(), source); // Resolve path to a tileset.
using var tilesetFileReader = new StreamReader(tilesetPath); // Read tileset file itself.
var tilesetString = tilesetFileReader.ReadToEnd(); // You can replace this with any custom function
// to load data from any source, eg. .zip file.
using var tilesetReader = new TilesetReader(tilesetString, ResolveTileset, ResolveTemplate, ResolveCustomType); // Parse loaded tileset.
return tilesetReader.ReadTileset(); // Return loaded tileset
}
// This is pretty similar to above, but instead it loads templates, not tilesets.
static Template ResolveTemplate(string source)
{
string templatePath = Path.Combine(Directory.GetCurrentDirectory(), source);
using var templateFileReader = new StreamReader(templatePath);
var templateString = templateFileReader.ReadToEnd();
using var templateReader = new TemplateReader(templateString, ResolveTileset, ResolveTemplate, ResolveCustomType);
return templateReader.ReadTemplate();
}
static ICustomTypeDefinition ResolveCustomType(string name)
{
CustomClassDefinition[] allDefinedTypes =
[
new CustomClassDefinition() { Name = "a" },
];
return allDefinedTypes.FirstOrDefault(type => type.Name == name);
}
}

View file

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.10" tiledversion="1.11.0" orientation="orthogonal" renderorder="right-down" width="10" height="10" tilewidth="16" tileheight="16" infinite="0" nextlayerid="5" nextobjectid="1">
<properties>
<property name="hello" value="Hello from DotTiled!"/>
</properties>
<tileset firstgid="1" source="tileset.tsx"/>
<layer id="1" name="Tile Layer 1" width="10" height="10">
<data encoding="base64">
AQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAA==
</data>
</layer>
</map>

Binary file not shown.

After

Width:  |  Height:  |  Size: 149 B

View file

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<tileset version="1.8" tiledversion="1.10.2" name="tileset" tilewidth="16" tileheight="16" tilecount="1" columns="1">
<image source="tileset.png" width="16" height="16"/>
</tileset>

View file

@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotTiled.Tests", "DotTiled.
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotTiled.Benchmark", "DotTiled.Benchmark\DotTiled.Benchmark.csproj", "{510F3077-8EA4-47D1-8D01-E2D538F1B899}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotTiled.Example", "DotTiled.Example\DotTiled.Example.csproj", "{31D5D3EB-67E1-4C8F-BB59-5BC32817C0FF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -30,5 +32,9 @@ Global
{510F3077-8EA4-47D1-8D01-E2D538F1B899}.Debug|Any CPU.Build.0 = Debug|Any CPU
{510F3077-8EA4-47D1-8D01-E2D538F1B899}.Release|Any CPU.ActiveCfg = Release|Any CPU
{510F3077-8EA4-47D1-8D01-E2D538F1B899}.Release|Any CPU.Build.0 = Release|Any CPU
{31D5D3EB-67E1-4C8F-BB59-5BC32817C0FF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{31D5D3EB-67E1-4C8F-BB59-5BC32817C0FF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{31D5D3EB-67E1-4C8F-BB59-5BC32817C0FF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{31D5D3EB-67E1-4C8F-BB59-5BC32817C0FF}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal