Skip to content

Commit

Permalink
support background/foreground game objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Quillraven committed Mar 27, 2024
1 parent 25abce6 commit 2ed2155
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 40 deletions.
20 changes: 12 additions & 8 deletions assets/maps/test.tmx
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.10" tiledversion="1.10.2" orientation="orthogonal" renderorder="right-down" width="30" height="10" tilewidth="16" tileheight="16" infinite="0" nextlayerid="6" nextobjectid="14">
<map version="1.10" tiledversion="1.10.2" orientation="orthogonal" renderorder="right-down" width="30" height="10" tilewidth="16" tileheight="16" infinite="0" nextlayerid="8" nextobjectid="15">
<properties>
<property name="musicAsset" value="TUTORIAL"/>
<property name="name" value="Tutorial"/>
</properties>
<tileset firstgid="1" source="terrain.tsx"/>
<tileset firstgid="243" source="objects.tsx"/>
<objectgroup id="6" name="bgdObjects">
<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>
<object id="4" gid="244" x="333" y="75" width="38" height="38"/>
</objectgroup>
<layer id="1" name="ground" width="30" height="10">
<data encoding="base64" compression="zlib">
eJxjYBgFgwWwAzEHFHMOsFtwgcHuRmT34cKcJKonxjxZIJYjgOWR7CVGPTHmDZS9xkBsQgCbItlLjHpizAMArX8NYQ==
Expand All @@ -18,14 +27,9 @@
</layer>
<objectgroup id="3" name="objects">
<object id="3" gid="243" x="103" y="97" width="32" height="32"/>
<object id="4" gid="244" x="333" y="75" width="38" height="38"/>
</objectgroup>
<objectgroup id="7" name="fgdObjects">
<object id="11" gid="244" x="269" y="105" width="38" height="38"/>
<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>
<objectgroup id="5" name="tracks">
<object id="10" x="320" y="57">
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
PLAYER, CAMERA_FOCUS, BACKGROUND, FOREGROUND
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ import com.badlogic.gdx.graphics.g2d.Sprite
import com.github.quillraven.fleks.Component
import com.github.quillraven.fleks.ComponentType

data class Graphic(val sprite: Sprite, val z: Int = 0) : Component<Graphic>, Comparable<Graphic> {
data class Graphic(val sprite: Sprite) : Component<Graphic>, Comparable<Graphic> {
override fun type() = Graphic

override fun compareTo(other: Graphic): Int = when {
z < other.z -> -1
z > other.z -> 1
sprite.y < other.sprite.y -> -1
sprite.y > other.sprite.y -> 1
sprite.x < other.sprite.x -> -1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@ package com.quillraven.github.quillyjumper.system

import com.badlogic.gdx.graphics.OrthographicCamera
import com.badlogic.gdx.graphics.g2d.Batch
import com.badlogic.gdx.maps.MapLayer
import com.badlogic.gdx.maps.tiled.TiledMap
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer
import com.badlogic.gdx.scenes.scene2d.Stage
import com.badlogic.gdx.utils.viewport.Viewport
import com.github.quillraven.fleks.Entity
import com.github.quillraven.fleks.IteratingSystem
import com.github.quillraven.fleks.Family
import com.github.quillraven.fleks.IntervalSystem
import com.github.quillraven.fleks.World.Companion.family
import com.github.quillraven.fleks.World.Companion.inject
import com.github.quillraven.fleks.collection.compareEntityBy
import com.quillraven.github.quillyjumper.Quillyjumper
import com.quillraven.github.quillyjumper.component.EntityTag.BACKGROUND
import com.quillraven.github.quillyjumper.component.EntityTag.FOREGROUND
import com.quillraven.github.quillyjumper.component.Graphic
import com.quillraven.github.quillyjumper.event.GameEvent
import com.quillraven.github.quillyjumper.event.GameEventListener
import com.quillraven.github.quillyjumper.event.MapChangeEvent
import com.quillraven.github.quillyjumper.tiled.TiledService.Companion.isObjectsLayer
import ktx.assets.disposeSafely
import ktx.tiled.use

Expand All @@ -27,25 +29,31 @@ class RenderSystem(
private val uiViewport: Viewport = inject("uiViewport"),
private val stage: Stage = inject(),
private val gameCamera: OrthographicCamera = inject(),
) : IteratingSystem(
family = family { all(Graphic) },
comparator = compareEntityBy(Graphic),
), GameEventListener {
) : IntervalSystem(), GameEventListener {

private val mapRenderer = OrthogonalTiledMapRenderer(null, Quillyjumper.UNIT_SCALE, batch)
private val bgdLayers = mutableListOf<TiledMapTileLayer>()
private val fgdLayers = mutableListOf<TiledMapTileLayer>()

private val entityComparator = compareEntityBy(Graphic)
private val bgdEntities = family { all(Graphic, BACKGROUND) }
private val entities = family { all(Graphic).none(BACKGROUND, FOREGROUND) }
private val fgdEntities = family { all(Graphic, FOREGROUND) }

override fun onTick() {
// game rendering
gameViewport.apply()
mapRenderer.use(gameCamera) {
// background rendering
bgdEntities.renderEntities()
bgdLayers.forEach { mapRenderer.renderTileLayer(it) }

// render entities
super.onTick()
// middle layer rendering
entities.renderEntities()

// foreground rendering
fgdLayers.forEach { mapRenderer.renderTileLayer(it) }
fgdEntities.renderEntities()
}

// ui rendering
Expand All @@ -54,9 +62,9 @@ class RenderSystem(
stage.draw()
}

override fun onTickEntity(entity: Entity) {
val (sprite) = entity[Graphic]
sprite.draw(batch)
private fun Family.renderEntities() {
sort(entityComparator)
forEach { it[Graphic].sprite.draw(batch) }
}

override fun onEvent(event: GameEvent) {
Expand All @@ -75,10 +83,10 @@ class RenderSystem(
fgdLayers.clear()
var currentLayers = bgdLayers
tiledMap.layers.forEach { layer ->
when (layer) {
is TiledMapTileLayer -> currentLayers += layer
when {
layer is TiledMapTileLayer -> currentLayers += layer

is MapLayer -> {
layer.isObjectsLayer() -> {
// we have an ObjectLayer -> switch from background to foreground layer parsing
currentLayers = fgdLayers
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ import com.quillraven.github.quillyjumper.PhysicWorld
import com.quillraven.github.quillyjumper.Quillyjumper.Companion.OBJECT_FIXTURES
import com.quillraven.github.quillyjumper.Quillyjumper.Companion.UNIT_SCALE
import com.quillraven.github.quillyjumper.TextureAtlasAsset
import com.quillraven.github.quillyjumper.component.AnimationType
import com.quillraven.github.quillyjumper.component.Graphic
import com.quillraven.github.quillyjumper.component.Physic
import com.quillraven.github.quillyjumper.component.Tiled
import com.quillraven.github.quillyjumper.component.*
import com.quillraven.github.quillyjumper.event.GameEvent
import com.quillraven.github.quillyjumper.event.GameEventListener
import com.quillraven.github.quillyjumper.event.MapChangeEvent
Expand Down Expand Up @@ -79,10 +76,17 @@ class TiledService(

// 2) spawn dynamic/kinematic game object bodies
val trackLayer = map.layer("tracks")
map.layer("objects").objects.forEach { spawnGameObjectEntity(it, trackLayer) }
val objectLayers = mapOf(
-1 to "bgdObjects",
0 to "objects",
1 to "fgdObjects",
)
objectLayers.forEach { (zIndex, layerName) ->
map.layer(layerName).objects.forEach { spawnGameObjectEntity(it, zIndex, trackLayer) }
}
}

private fun spawnGameObjectEntity(mapObject: MapObject, trackLayer: MapLayer) {
private fun spawnGameObjectEntity(mapObject: MapObject, zIndex: Int, trackLayer: MapLayer) {
if (mapObject !is TiledMapTileMapObject) {
gdxError("Unsupported mapObject $mapObject")
}
Expand Down Expand Up @@ -115,6 +119,10 @@ class TiledService(
// Since the AnimationSystem is updating the region of the sprite and is also
// restoring the flip information, we should set the Sprite region already at this point.
it += Graphic(sprite(gameObject, AnimationType.IDLE.atlasKey, body.position))
when {
zIndex < 0 -> it += EntityTag.BACKGROUND
zIndex > 0 -> it += EntityTag.FOREGROUND
}
configureEntityTags(it, tile)
configureAnimation(it, tile, world, gameObject)
configureState(it, tile, world)
Expand Down Expand Up @@ -165,6 +173,8 @@ class TiledService(
companion object {
private val log = logger<TiledService>()

fun MapLayer.isObjectsLayer(): Boolean = this.name == "objects"

fun fixtureDefOf(mapObject: MapObject): FixtureDefUserData {
val fixtureDef = when (mapObject) {
is RectangleMapObject -> rectangleFixtureDef(mapObject)
Expand Down
14 changes: 7 additions & 7 deletions tiled-project.tiled-session
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
"height": 4300,
"width": 2
},
"activeFile": "assets/maps/objects.tsx",
"activeFile": "assets/maps/test.tmx",
"expandedProjectPaths": [
"assets",
".",
"assets",
"lwjgl3/build",
"assets/maps"
],
Expand Down Expand Up @@ -46,11 +46,11 @@
"expandedObjectLayers": [
3
],
"scale": 3,
"selectedLayer": 2,
"scale": 4,
"selectedLayer": 1,
"viewCenter": {
"x": 230.33333333333331,
"y": 72.16666666666666
"x": 157.75,
"y": 61.875
}
}
},
Expand All @@ -71,8 +71,8 @@
"property.type": "bool",
"recentFiles": [
"assets/maps/objects.tmx",
"assets/maps/test.tmx",
"assets/maps/objects.tsx",
"assets/maps/test.tmx",
"assets/maps/terrain.tsx",
"assets/maps/terrain_extruded.tsx"
],
Expand Down

0 comments on commit 2ed2155

Please sign in to comment.