Here are the docs for each class in LDtkMonogame
To open a ldtk project file you should start by creating a new World
object as this is what holds all the data to a ldtk project.
This will load .ldtk
worlds directly from disk.
LDtkWorld world = LDtkWorld.LoadWorld("Assets/World.ldtk");
This will load .ldtk
world from the content pipeline.
LDtkWorld world = LDtkWorld.LoadWorld("World", Content);
You will need to make sure your files are in the same relative folders as the origonal files in the project. If you are unsure check out the example project here.
Now that you have a reference to the world you can use it for handling levels you want to use in your game.
For more information on the world refer to the ldtk documentation ldtk-ProjectJson
Level's hold all the data for each individual level and include the tiles and all the layers including Entities
, Intgrids
and Autotiles
.
If you are using external levels with ldtk. You will need to load the level otherwise ignore this next step.
LDtkLevel level0 = world.LoadLevel("Level_0"); // Identifier
LDtkLevel level1 = world.LoadLevel(Worlds.World.Level_1);// Guid/Iid
Now with myLevel
you can access all the ldtk layers inside of it.
If your level has custom fields you will need to load the data using GetCustomFields()
.
If you are using the Codegen tool the LDtkLevelData
file will be created for you automatically.
public class LDtkLevelData
{
public string biome;
public int difficulty;
}
public class LDtkLevelData
{
public string biome;
public int difficulty;
}
Gun_Pickup level1 = level0.GetEntityRef<Gun_Pickup>();// Guid/Iid
If you have custom fields specified ldtkCodegen should have generated a file named LDtkLevelData
To get the fields from the level
LDtkLevelData levelData = levelName.GetCustomFields<LDtkLevelData>();
For more information on the level refer to the ldtk documentation ldtk-LevelJson
Intgrids can be intgrid rule layers or pure intgrid layers as they both hold integer values.
The intgrid is returned from a level when calling level.GetIntGrid("layer name");
.
In the example game I used the intgrid for collisions.
LDtkIntGrid collisions = level.GetIntGrid("Tiles");
Entities are contained inside the level and are returned from the various GetEntity()
/GetEntities()
methods, check the api docs for more information.
Similar to levels entities can have custom fields you handle that data in the same way by creating a custom inherited class from ILDtkEntity.
If you are using the Codegen tool the Gun_Pickup
file would be created for you automatically.
public class Gun_Pickup : ILDtkEntity
{
public System.Guid Iid { get; set; }
public long Uid { get; set; }
public string Identifier { get; set; }
public Vector2 Size { get; set; }
public Vector2 Position { get; set; }
public Vector2 Pivot { get; set; }
public Rectangle Tile { get; set; }
public Color SmartColor { get; set; }
public EntityRef Test { get; set; }
...
}
To load your Gun_Pickup
you load a level like normal but also pass it your level class
Gun_Pickup[] gunPickups = level.GetEntities<Gun_Pickup>();
If you have one entity Player
you could load it
Player playerSpawn = level.GetEntity<Player>();
EntityRef works be letting you get another entity from a level or world
Gun_Pickup otherGun = level.GetEntityRef<Gun_Pickup>(gun.Test);
Gun_Pickup otherGun = world.GetEntityRef<Gun_Pickup>(gun.Test);