Skip to content

Commit

Permalink
refactor track creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Quillraven committed Mar 24, 2024
1 parent 246d80b commit 1d62642
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 31 deletions.
3 changes: 2 additions & 1 deletion assets/maps/objects.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@
<property name="GameObject" propertytype="GameObject" value="SAW"/>
<property name="bodyType" propertytype="BodyType" value="KinematicBody"/>
<property name="damage" type="int" value="1"/>
<property name="entityTags" propertytype="EntityTag" value="FOLLOW_TRACK"/>
<property name="entityTags" propertytype="EntityTag" value=""/>
<property name="hasAnimation" type="bool" value="true"/>
<property name="hasTrack" type="bool" value="true"/>
<property name="speed" type="float" value="3"/>
</properties>
<image width="38" height="38" source="../graphics/object/saw.png"/>
Expand Down
1 change: 1 addition & 0 deletions assets/maps/test.tmx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<object id="13" gid="244" x="158" y="63" width="38" height="38">
<properties>
<property name="entityTags" propertytype="EntityTag" value=""/>
<property name="hasTrack" type="bool" value="false"/>
</properties>
</object>
</objectgroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import com.github.quillraven.fleks.EntityTags
import com.github.quillraven.fleks.entityTagOf

enum class EntityTag : EntityTags by entityTagOf() {
PLAYER, CAMERA_FOCUS, FOLLOW_TRACK
PLAYER, CAMERA_FOCUS
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package com.quillraven.github.quillyjumper.component

import com.badlogic.gdx.math.Rectangle
import com.github.quillraven.fleks.Component
import com.github.quillraven.fleks.ComponentType
import com.quillraven.github.quillyjumper.GameObject

data class Tiled(
val gameObject: GameObject,
val mapObjectID: Int,
val mapObjectBoundary: Rectangle, // units are in Tiled units (=pixels instead of world units)
) : Component<Tiled> {
override fun type() = Tiled

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell
import com.badlogic.gdx.maps.tiled.objects.TiledMapTileMapObject
import com.badlogic.gdx.math.Intersector
import com.badlogic.gdx.math.MathUtils
import com.badlogic.gdx.math.Rectangle
import com.badlogic.gdx.math.Vector2
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType
import com.badlogic.gdx.physics.box2d.ChainShape
Expand Down Expand Up @@ -83,31 +82,24 @@ class TiledService(
}

// 2) spawn dynamic/kinematic game object bodies
map.layer("objects").objects.forEach { spawnGameObjectEntity(it) }

// 3) link entities that follow a track to its related track in Tiled
world.family { all(EntityTag.FOLLOW_TRACK) }.forEach { entity ->
val (_, mapObjectID, mapObjectBoundary) = entity[Tiled]
val rectVertices = GdxFloatArray(
floatArrayOf(
mapObjectBoundary.x, mapObjectBoundary.y,
mapObjectBoundary.x + mapObjectBoundary.width, mapObjectBoundary.y,
mapObjectBoundary.x + mapObjectBoundary.width, mapObjectBoundary.y + mapObjectBoundary.height,
mapObjectBoundary.x, mapObjectBoundary.y + mapObjectBoundary.height
)
)
val trackCmp = map.layer("tracks").trackCmpOfBoundary(mapObjectID, rectVertices)
entity.configure { it += trackCmp }
}
map.layer("objects").objects.forEach { spawnGameObjectEntity(map, it) }
}

private fun MapLayer.trackCmpOfBoundary(mapObjectId: Int, rectVertices: GdxFloatArray): Track {
private fun MapLayer.trackCmpOfBoundary(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 map objects are supported for tracks: $layerObject")
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
Expand All @@ -121,10 +113,10 @@ class TiledService(
}
}

gdxError("There is no related track for MapObject $mapObjectId")
gdxError("There is no related track for MapObject ${mapObject.id}")
}

private fun spawnGameObjectEntity(mapObject: MapObject) {
private fun spawnGameObjectEntity(map: TiledMap, mapObject: MapObject) {
if (mapObject !is TiledMapTileMapObject) {
gdxError("Unsupported mapObject $mapObject")
}
Expand All @@ -150,8 +142,7 @@ class TiledService(
// spawn entity
world.entity {
body.userData = it
val mapObjectBoundary = Rectangle(mapObject.x, mapObject.y, mapObject.width, mapObject.height)
it += Tiled(gameObject, mapObject.id, mapObjectBoundary)
it += Tiled(gameObject, mapObject.id)
it += Physic(body)
// IMPORTANT: to make a sprite flip it must have a region already. Otherwise,
// the flipX information of the sprite will always be false.
Expand All @@ -160,7 +151,7 @@ class TiledService(
it += Graphic(sprite(gameObject, AnimationType.IDLE.atlasKey, body.position))

// EntityTags
val tagsStr = mapObject.propertyOrNull<String>("entityTags") ?: tile.property<String>("entityTags", "")
val tagsStr = tile.property<String>("entityTags", "")
if (tagsStr.isNotBlank()) {
val tags = tagsStr.split(",").map(EntityTag::valueOf)
it += tags
Expand Down Expand Up @@ -196,6 +187,12 @@ class TiledService(
if (damage > 0) {
it += Damage(damage)
}
// Track
val hasTrack = mapObject.propertyOrNull<Boolean>("hasTrack") ?: tile.property<Boolean>("hasTrack", false)
if (hasTrack) {
val trackCmp = map.layer("tracks").trackCmpOfBoundary(mapObject)
it += trackCmp
}

log.debug {
"""Spawning entity with:
Expand Down
8 changes: 6 additions & 2 deletions tiled-project.tiled-project
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@
"type": "bool",
"value": false
},
{
"name": "hasTrack",
"type": "bool",
"value": false
},
{
"name": "jumpHeight",
"type": "float",
Expand Down Expand Up @@ -96,8 +101,7 @@
"type": "enum",
"values": [
"PLAYER",
"CAMERA_FOCUS",
"FOLLOW_TRACK"
"CAMERA_FOCUS"
],
"valuesAsFlags": true
},
Expand Down
2 changes: 1 addition & 1 deletion tiled-project.tiled-session
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"assets/maps/objects.tsx"
],
"project": "tiled-project.tiled-project",
"property.type": "int",
"property.type": "bool",
"recentFiles": [
"assets/maps/objects.tmx",
"assets/maps/objects.tsx",
Expand Down

0 comments on commit 1d62642

Please sign in to comment.