Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
This repository was archived by the owner on Dec 28, 2021. It is now read-only.
/kt-ldtk-apiPublic archive

A Kotlin LDtk API that can be used for any Kotlin Multiplatform or Java projects.

License

NotificationsYou must be signed in to change notification settings

LeHaine/kt-ldtk-api

Repository files navigation

If you need LDtk support consider checking outLittleKt - A Kotlin Multiplatform game framework which has full LDtk loading and rendering support.

ReleaseVersionLDtk

About

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.

LibGDX Sample ExampleLibGDX Sample Example

LDtk official website

Helpful links:

Features

  • 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

Versioning

API VersionLDtk Version
1.4.00.9.3

Usage

Sample code

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                }            }        }    }}

Sample code when NOT using the annotation to generate code

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.

Download

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"}

Dependencies for any framework

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")}

[8]ページ先頭

©2009-2025 Movatter.jp