Skip to content

Commit

Permalink
Add Parallax effect to background
Browse files Browse the repository at this point in the history
  • Loading branch information
srikavin committed Dec 28, 2019
1 parent 5ecaa5b commit 6309a30
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import com.artemis.systems.IteratingSystem
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.graphics.OrthographicCamera
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.maps.tiled.TiledMap
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer
import me.srikavin.fbla.game.ecs.component.Map
import me.srikavin.fbla.game.ecs.component.Transform
Expand All @@ -24,20 +26,55 @@ class BackgroundRenderSystem : IteratingSystem() {

private lateinit var mapRenderer: OrthogonalTiledMapRenderer

private val bgCamera = OrthographicCamera()

override fun initialize() {
super.initialize()
mapRenderer = OrthogonalTiledMapRenderer(null, 1 / 32f, batch)
}

override fun begin() {
bgCamera.position.set(camera.position).scl(0.40f, 1f, 1f).add(30f, 0f, 0f)
bgCamera.direction.set(camera.direction)
bgCamera.zoom = camera.zoom * 1.25f
bgCamera.viewportHeight = camera.viewportHeight
bgCamera.viewportWidth = camera.viewportWidth
bgCamera.update()
camera.update()
}

private val layers: ArrayList<Int> = ArrayList()
private lateinit var layersArr: IntArray
private lateinit var map: TiledMap
private lateinit var backgroundLayer: TiledMapTileLayer

override fun process(entityId: Int) {
val transform = transformMapper[entityId]
mapRenderer.setView(camera.combined, transform.position.x, transform.position.y + 10, Gdx.graphics.width.toFloat(), Gdx.graphics.height.toFloat())
mapRenderer.map = mapMapper[entityId].map
mapRenderer.render()
val map = mapMapper[entityId].map

if (!this::map.isInitialized || this.map != map) {
this.map = map
layers.clear()

map.layers.forEachIndexed { index, mapLayer ->
if (mapLayer.name == "Background") {
backgroundLayer = mapLayer as TiledMapTileLayer
} else {
layers.add(index)
}
}

layersArr = layers.toIntArray()
mapRenderer.map = map
}


batch.begin()
mapRenderer.setView(bgCamera.combined, transform.position.x, transform.position.y, Gdx.graphics.width.toFloat(), Gdx.graphics.height.toFloat())
mapRenderer.renderTileLayer(backgroundLayer)
batch.end()

mapRenderer.setView(camera.combined, transform.position.x, transform.position.y, Gdx.graphics.width.toFloat(), Gdx.graphics.height.toFloat())
mapRenderer.render(layersArr)
}
}
7 changes: 5 additions & 2 deletions core/src/me/srikavin/fbla/game/ecs/system/RenderSystem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import com.badlogic.gdx.Gdx
import com.badlogic.gdx.graphics.GL20
import com.badlogic.gdx.graphics.OrthographicCamera
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import ktx.log.info


class RenderSystem : BaseSystem() {
Expand All @@ -18,7 +17,11 @@ class RenderSystem : BaseSystem() {

override fun processSystem() {
Gdx.gl.glClearColor(0f, 0f, 0f, 1f)
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT)
Gdx.gl.glClear(
GL20.GL_COLOR_BUFFER_BIT
or GL20.GL_DEPTH_BUFFER_BIT
or (if (Gdx.graphics.bufferFormat.coverageSampling) GL20.GL_COVERAGE_BUFFER_BIT_NV else 0)
)
camera.update()
batch.projectionMatrix = camera.combined
}
Expand Down
15 changes: 7 additions & 8 deletions core/src/me/srikavin/fbla/game/map/MapLoader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import com.badlogic.gdx.maps.MapObject
import com.badlogic.gdx.maps.objects.EllipseMapObject
import com.badlogic.gdx.maps.objects.PolygonMapObject
import com.badlogic.gdx.maps.objects.RectangleMapObject
import com.badlogic.gdx.maps.tiled.TiledMap
import com.badlogic.gdx.maps.tiled.TmxMapLoader
import com.badlogic.gdx.math.EarClippingTriangulator
import com.badlogic.gdx.math.Vector2
import com.badlogic.gdx.physics.box2d.*
Expand Down Expand Up @@ -79,13 +79,12 @@ class MapLoader(private val assetManager: AssetManager, private val world: World
}

fun loadMap(path: String): EntityInt {
val map = when {
assetManager.isLoaded(path) -> assetManager.get<TiledMap>(path)
else -> {
assetManager.load(path, TiledMap::class.java)
assetManager.finishLoadingAsset<TiledMap>(path)
}
}
val map = TmxMapLoader().load(path, TmxMapLoader.Parameters().apply {
generateMipMaps = true
textureMagFilter = Texture.TextureFilter.MipMapNearestNearest
textureMinFilter = Texture.TextureFilter.MipMapNearestNearest
})


// val entities = world.aspectSubscriptionManager[Aspect.all()].entities

Expand Down

0 comments on commit 6309a30

Please sign in to comment.