Skip to content

Commit

Permalink
remove FOREGROUND/BACKGROUND tag and use partitionTo function instead
Browse files Browse the repository at this point in the history
  • Loading branch information
Quillraven committed May 6, 2024
1 parent d03404d commit 5b03270
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ data class AiEntity(
}

fun notInRange(location: Vector2, tolerance: Float): Boolean = with(world) {
val (_, center) = entity[Graphic]
val center = entity[Graphic].center
val diffX = location.x - center.x
val diffY = location.y - center.y

return (diffX * diffX) + (diffY * diffY) > tolerance * tolerance
}

fun inRange(location: Vector2, tolerance: Float): Boolean = with(world) {
val (_, center) = entity[Graphic]
val center = entity[Graphic].center
val diffX = location.x - center.x
val diffY = location.y - center.y

Expand All @@ -63,8 +63,8 @@ data class AiEntity(
}

fun angleTo(target: Entity): Float = with(world) {
val (_, center) = entity[Graphic]
val (_, targetCenter) = target[Graphic]
val center = entity[Graphic].center
val targetCenter = target[Graphic].center

return atan2(targetCenter.y - center.y, targetCenter.x - center.x)
}
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, BACKGROUND, FOREGROUND, COLLECTABLE
PLAYER, CAMERA_FOCUS, COLLECTABLE
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.github.quillraven.fleks.Component
import com.github.quillraven.fleks.ComponentType
import ktx.math.vec2

data class Graphic(val sprite: Sprite) : Component<Graphic>, Comparable<Graphic> {
data class Graphic(val sprite: Sprite, val z: Int) : Component<Graphic>, Comparable<Graphic> {
val center = vec2()
get() {
field.x = sprite.x + sprite.width * 0.5f
Expand All @@ -16,14 +16,14 @@ 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
sprite.x > other.sprite.x -> 1
else -> 0
}

operator fun component2() = center

companion object : ComponentType<Graphic>()
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ 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.Family
import com.github.quillraven.fleks.IntervalSystem
import com.github.quillraven.fleks.Entity
import com.github.quillraven.fleks.IteratingSystem
import com.github.quillraven.fleks.World.Companion.family
import com.github.quillraven.fleks.World.Companion.inject
import com.github.quillraven.fleks.collection.MutableEntityBag
import com.github.quillraven.fleks.collection.compareEntityBy
import com.quillraven.github.quillyjumper.Assets
import com.quillraven.github.quillyjumper.Quillyjumper
import com.quillraven.github.quillyjumper.ShaderAsset
import com.quillraven.github.quillyjumper.component.EntityTag.BACKGROUND
import com.quillraven.github.quillyjumper.component.EntityTag.FOREGROUND
import com.quillraven.github.quillyjumper.component.Flash
import com.quillraven.github.quillyjumper.component.Graphic
import com.quillraven.github.quillyjumper.event.GameEvent
Expand All @@ -34,35 +33,43 @@ class RenderSystem(
private val stage: Stage = inject(),
private val gameCamera: OrthographicCamera = inject(),
assets: Assets = inject()
) : IntervalSystem(), GameEventListener {
) : IteratingSystem(family = family { all(Graphic) }, comparator = compareEntityBy(Graphic)), 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) }
private val bgdEntities = MutableEntityBag()
private val entities = MutableEntityBag()
private val fgdEntities = MutableEntityBag()
private val tmpEntities = MutableEntityBag()

private val flashShader = assets[ShaderAsset.FLASH]
private val uLocFlashColor = flashShader.getUniformLocation("u_FlashColor")
private val uLocFlashWeight = flashShader.getUniformLocation("u_FlashWeight")

override fun onTick() {
// split render entities into three parts (background, normal and foreground entities)
onSort()
family.entities.partitionTo(bgdEntities, tmpEntities) { it[Graphic].z < 0 }
tmpEntities.partitionTo(fgdEntities, entities) { it[Graphic].z > 0 }

// game rendering
gameViewport.apply()
mapRenderer.use(gameCamera) {
// background rendering
bgdEntities.renderEntities()
bgdEntities.forEach { onTickEntity(it) }
batch.resetShader()
bgdLayers.forEach { mapRenderer.renderTileLayer(it) }

// middle layer rendering
entities.renderEntities()
entities.forEach { onTickEntity(it) }
batch.resetShader()

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

// ui rendering
Expand All @@ -71,26 +78,21 @@ class RenderSystem(
stage.draw()
}

private fun Family.renderEntities() {
sort(entityComparator)
forEach { entity ->
val flashCmp = entity.getOrNull(Flash)
if (flashCmp != null && flashCmp.doFlash) {
if (batch.shader != flashShader) {
batch.shader = flashShader
}
flashShader.use {
flashShader.setUniformf(uLocFlashColor, flashCmp.color)
flashShader.setUniformf(uLocFlashWeight, flashCmp.weight)
}
} else {
batch.resetShader()
override fun onTickEntity(entity: Entity) {
val flashCmp = entity.getOrNull(Flash)
if (flashCmp != null && flashCmp.doFlash) {
if (batch.shader != flashShader) {
batch.shader = flashShader
}

entity[Graphic].sprite.draw(batch)
flashShader.use {
flashShader.setUniformf(uLocFlashColor, flashCmp.color)
flashShader.setUniformf(uLocFlashWeight, flashCmp.weight)
}
} else {
batch.resetShader()
}

batch.resetShader()
entity[Graphic].sprite.draw(batch)
}

private fun Batch.resetShader() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,7 @@ class TiledService(
// the flipX information of the sprite will always be false.
// 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
}
it += Graphic(sprite(gameObject, AnimationType.IDLE.atlasKey, body.position), zIndex)
configureEntityTags(it, tile)
// check for player tag and remember player start location
if (it has EntityTag.PLAYER) {
Expand Down
8 changes: 4 additions & 4 deletions core/src/test/kotlin/RenderTestScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ class RenderTestScreen : KtxScreen {
setPosition(3f, 1f)
setSize(1f, 1f)
}
it += Graphic(entitySprite)
it += Graphic(entitySprite, 0)
}
world.entity {
val entitySprite = Sprite(textureRegion("frog/idle")).apply {
setPosition(3f, 2f)
setSize(1f, 1f)
setFlip(true, false)
}
it += Graphic(entitySprite)
it += Graphic(entitySprite, 0)
}

// animation entities
Expand All @@ -80,7 +80,7 @@ class RenderTestScreen : KtxScreen {
setSize(1f, 1f)
}
it += Tiled(GameObject.FROG, 0)
it += Graphic(entitySprite)
it += Graphic(entitySprite, 0)
it += Animation(animationService.gdxAnimation(GameObject.FROG, AnimationType.IDLE), type = NORMAL_ANIMATION)
}
world.entity {
Expand All @@ -90,7 +90,7 @@ class RenderTestScreen : KtxScreen {
setFlip(true, false)
}
it += Tiled(GameObject.FROG, 0)
it += Graphic(entitySprite)
it += Graphic(entitySprite, 0)
it += Animation(animationService.gdxAnimation(GameObject.FROG, AnimationType.IDLE), type = NORMAL_ANIMATION)
}
}
Expand Down

0 comments on commit 5b03270

Please sign in to comment.