-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor entity Tiled configuration (components)
- Loading branch information
Simon Klausner
committed
Mar 25, 2024
1 parent
86abaf1
commit 4fc4133
Showing
2 changed files
with
125 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
core/src/main/kotlin/com/quillraven/github/quillyjumper/tiled/tiledEntityComponents.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package com.quillraven.github.quillyjumper.tiled | ||
|
||
import com.badlogic.gdx.maps.MapLayer | ||
import com.badlogic.gdx.maps.MapObject | ||
import com.badlogic.gdx.maps.objects.PolygonMapObject | ||
import com.badlogic.gdx.maps.objects.PolylineMapObject | ||
import com.badlogic.gdx.maps.tiled.TiledMapTile | ||
import com.badlogic.gdx.maps.tiled.objects.TiledMapTileMapObject | ||
import com.badlogic.gdx.math.Intersector | ||
import com.badlogic.gdx.math.Vector2 | ||
import com.github.quillraven.fleks.Entity | ||
import com.github.quillraven.fleks.EntityCreateContext | ||
import com.github.quillraven.fleks.World | ||
import com.quillraven.github.quillyjumper.GameObject | ||
import com.quillraven.github.quillyjumper.Quillyjumper.Companion.UNIT_SCALE | ||
import com.quillraven.github.quillyjumper.ai.AiEntity | ||
import com.quillraven.github.quillyjumper.ai.GameObjectStateIdle | ||
import com.quillraven.github.quillyjumper.component.* | ||
import ktx.app.gdxError | ||
import ktx.math.vec2 | ||
import ktx.tiled.* | ||
|
||
fun EntityCreateContext.configureEntityTags(entity: Entity, tile: TiledMapTile) { | ||
val tagsStr = tile.property<String>("entityTags", "") | ||
if (tagsStr.isNotBlank()) { | ||
val tags = tagsStr.split(",").map(EntityTag::valueOf) | ||
entity += tags | ||
} | ||
} | ||
|
||
fun EntityCreateContext.configureAnimation(entity: Entity, tile: TiledMapTile, world: World, gameObject: GameObject) { | ||
val hasAnimation = tile.property<Boolean>("hasAnimation", false) | ||
if (hasAnimation) { | ||
entity += Animation(gdxAnimation(world, gameObject, AnimationType.IDLE)) | ||
} | ||
} | ||
|
||
fun EntityCreateContext.configureState(entity: Entity, tile: TiledMapTile, world: World) { | ||
val hasState = tile.property<Boolean>("hasState", false) | ||
if (hasState) { | ||
entity += State(AiEntity(entity, world), GameObjectStateIdle) | ||
} | ||
} | ||
|
||
fun EntityCreateContext.configureJump(entity: Entity, tile: TiledMapTile) { | ||
val jumpHeight = tile.property<Float>("jumpHeight", 0f) | ||
if (jumpHeight > 0f) { | ||
entity += Jump(maxHeight = jumpHeight) | ||
} | ||
} | ||
|
||
fun EntityCreateContext.configureLife(entity: Entity, tile: TiledMapTile) { | ||
val life = tile.property<Int>("life", 0) | ||
if (life > 0) { | ||
entity += Life(max = life) | ||
} | ||
} | ||
|
||
fun EntityCreateContext.configureMove(entity: Entity, tile: TiledMapTile) { | ||
val speed = tile.property<Float>("speed", 0f) | ||
if (speed > 0f) { | ||
val timeToMaxSpeed = tile.property<Float>("timeToMaxSpeed", 0f) | ||
entity += Move(max = speed, timeToMax = timeToMaxSpeed.coerceAtLeast(0.1f)) | ||
} | ||
} | ||
|
||
fun EntityCreateContext.configureDamage(entity: Entity, tile: TiledMapTile) { | ||
val damage = tile.property<Int>("damage", 0) | ||
if (damage > 0) { | ||
entity += Damage(damage) | ||
} | ||
} | ||
|
||
fun EntityCreateContext.configureTrack(entity: Entity, mapObject: TiledMapTileMapObject, trackLayer: MapLayer) { | ||
val hasTrack = mapObject.propertyOrNull<Boolean>("hasTrack") ?: mapObject.tile.property<Boolean>("hasTrack", false) | ||
if (hasTrack) { | ||
entity += trackLayer.trackCmpOf(mapObject) | ||
} | ||
} | ||
|
||
private fun MapLayer.trackCmpOf(mapObject: MapObject): Track { | ||
objects.forEach { layerObject -> | ||
val lineVertices = when (layerObject) { | ||
is PolylineMapObject -> GdxFloatArray(layerObject.polyline.transformedVertices) | ||
is PolygonMapObject -> GdxFloatArray(layerObject.polygon.transformedVertices) | ||
else -> gdxError("Only Polyline and Polygon map objects are supported for tracks: $layerObject") | ||
} | ||
val rectVertices = GdxFloatArray( | ||
floatArrayOf( | ||
mapObject.x, mapObject.y, | ||
mapObject.x + mapObject.width, mapObject.y, | ||
mapObject.x + mapObject.width, mapObject.y + mapObject.height, | ||
mapObject.x, mapObject.y + mapObject.height | ||
) | ||
) | ||
|
||
if (Intersector.intersectPolygons(lineVertices, rectVertices)) { | ||
// found related track -> convert track vertices to world unit vertices | ||
val trackPoints = mutableListOf<Vector2>() | ||
for (i in 0 until lineVertices.size step 2) { | ||
val vertexX = lineVertices[i] * UNIT_SCALE | ||
val vertexY = lineVertices[i + 1] * UNIT_SCALE | ||
trackPoints += vec2(vertexX, vertexY) | ||
} | ||
return Track(trackPoints, closedTrack = layerObject is PolygonMapObject) | ||
} | ||
} | ||
|
||
gdxError("There is no related track for MapObject ${mapObject.id}") | ||
} |