- Notifications
You must be signed in to change notification settings - Fork3
A Kotlin LDtk API that can be used for any Kotlin Multiplatform or Java projects.
License
LeHaine/kt-ldtk-api
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
If you need LDtk support consider checking outLittleKt - A Kotlin Multiplatform game framework which has full LDtk loading and rendering support.
This is a Kotlin and Java API to parse and loadLDtk project files.Theldtk-api
module is built for Kotlin Multiplatform which means it works for JVM and JS targets. Other targets have not been tested yet.
This library can be used for any Kotlin Multiplatform or JVM game engine/framework/project. It features a sample LibGDX project for both annotation processing and rendering as well as a KorGE example.
Helpful links:
- KorGE Kotlin Multiplatform Example -May be outdated but should give a good idea on how to use it
- KorGE LDtk View - A view implemented for rendering LDtk levels in KorGE used in my personal library for KorGE calledKiwi.
- Annotation processing: Anoptional annotation that will allow fully generated typesafe code to be used within your game.
- Compile time code gen: When using the annotation processor, the project code will be generated at compile time and available right away.
- No runtime reflection: Reflection is used at compile time which is used to generate code.
- Extremely simple: Parsing and loading a file is extremely easy in just a few lines of code
API Version | LDtk Version |
---|---|
1.4.0 | 0.9.3 |
Create any class and add an@LDtkProject
annotation to it with the location to your LDtk file on your projects classpath.
Build your project, and it is ready to be used.
Kotlin example
// designate class for loading and attaching LDtk file to@LDtkProject(ldtkFileLocation="sample.ldtk", name="World")class_Worldfunmain(args:Array<String>) {// create new LDtk worldval world=World() world.load()// loads the file and parses it// get a levelval level:World.WorldLevel= world.allLevels[0] level.load()// force load a level// levels are loaded automatically when accessing any layer of that level// iterate over a layers tiles level.layerBackground.autoTiles.forEach {// logic for handling the tile } level.layerEntities.allMob.forEach { mob->// access entity fieldsval type:World.MobType= mob.type// generated enum classval patrolPoint:Point?= mob.patrol// pointsval health:Int= mob.health } level.layerEntities.allCart.forEach { cart->// field arrays cart.items.forEach { item->if (item==World.Items.Pickaxe) {// spawn pickaxe } } }}
Java Example
// designate class for loading and attaching LDtk file to@LDtkProject(ldtkFileLocation ="sample.ldtk",name ="JavaWorld")publicclassSampleJava {publicstaticvoidmain(String[]args) {// create new LDtk worldJavaWorldworld =newJavaWorld();world.load();// loads the file and parses it// get a levelJavaWorld.JavaWorldLevellevel =world.getAllLevels().get(0);level.load();// force load a level// levels are loaded automatically when accessing any layer of that level// iterate over a layers tilesfor (LayerAutoLayer.AutoTiletile :level.getLayerBackground().getAutoTiles()) {// logic for handling the tileintx =tile.getRenderX(); }// iterate over entitiesfor (JavaWorld.EntityMobmob :level.getLayerEntities().getAllMob()) {JavaWorld.MobTypetype =mob.type;PointpatrolPoint =mob.getPatrol();inthealth =mob.getHealth(); }for (JavaWorld.EntityCartcart :level.getLayerEntities().getAllCart()) {// field arrays / listsList<JavaWorld.Items>items =cart.getItems();for (JavaWorld.Itemsitem :items) {if (item ==JavaWorld.Items.Pickaxe) {// spawn pickaxe } } } }}
val proj=Project("sample.ldtk").apply { load() }val level= proj.allUntypedLevels[0]level.load()// this is only needed if levels are saved in separate files!val gridSize=16level.allUntypedEntities?.forEach { entity->val x= entity.cx* gridSizeval y= entity.cy* gridSize entity.json.fieldInstances.forEach {if (it.identifier=="Color") {val color= it.value!!.content } }}level.allUntypedLayers.forEach { layer->if (layer.type==LayerType.IntGrid) {val intGridLayer= layerasLayerIntGrid intGridLayer.getInt(0,5) }}
You can check out a few samples in thesamples module.
In your gradle build script ensure you have thekotlin
andkapt
gradle plugins enabled.
If you are usingKotlin then they may already be enabled.
If you are usingJava then they need to be enabled.Warning: This will most likely force you to change anyannontationPrcessing
dependencies tokapt
.
plugins { kotlin("jvm") version"1.4.31" kotlin("kapt") version"1.4.31"}
Note: Check for latest version at the top.
build.gradle.kts
allprojects { repositories { maven(url="https://jitpack.io") }}
configurations.all {// kapt has an issue with determining the correct KMM library, so we need to help itif (name.contains("kapt")) { attributes.attribute(KotlinPlatformType.attribute,KotlinPlatformType.jvm// pass in the JVM ) }}dependencies { implementation("com.lehaine.kt-ldtk-api:ldtk-api:$version") kapt("com.lehaine.kt-ldtk-api:ldtk-processor:$version")}
About
A Kotlin LDtk API that can be used for any Kotlin Multiplatform or Java projects.