Skip to content

Commit

Permalink
Add horizontal scrolling of parallax layers
Browse files Browse the repository at this point in the history
  • Loading branch information
jobe-m committed Jan 17, 2025
1 parent f4e122a commit cdd5a18
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import com.github.quillraven.fleks.*
import korlibs.korge.fleks.utils.*
import kotlinx.serialization.*

/**
* A component to define movement for an entity.
*
* @param velocityX pixel/second (?)
*/
@Serializable @SerialName("Motion")
data class MotionComponent(
var accelX: Float = 0f,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ data class ParallaxEffectConfig(
y = this@ParallaxEffectConfig.y
) // global position for the whole parallax background
it += MotionComponent(
velocityX = -12f // world units per second
velocityX = -12f // world units (16 pixels) per second (??? TODO: this needs to be ckecked)
)
it += ParallaxComponent(
name = assetName
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package korlibs.korge.fleks.familyHooks

import com.github.quillraven.fleks.*
import korlibs.korge.fleks.components.*
import korlibs.korge.fleks.tags.*

/**
* Not needed because view port size is coming from entity config.
* Kept here for reference.
*/
val mainCameraFamily = World.family { all(SizeComponent, SizeIntComponent, MainCameraTag) }

val onMainCameraAdded: FamilyHook = { entity ->
// Set view port half which is the middle point of the view port
val viewPortSize = entity[SizeIntComponent]
val viewPortHalf = entity[SizeComponent]
viewPortHalf.width = viewPortSize.width * 0.5f
viewPortHalf.height = viewPortSize.height * 0.5f

println("Set view port half: $viewPortHalf")
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ class CameraSystem : IteratingSystem(
cameraPosition.x += xDiff * 0.05f
cameraPosition.y += yDiff * 0.05f

val parallaxFamily = world.family { all(ParallaxComponent, MotionComponent) }
parallaxFamily.forEach { parallaxEntity ->
val motion = parallaxEntity[MotionComponent]

// TODO: Check how we can convert pixel in pixel/second
motion.velocityX = -(xDiff * 0.05f) * 3f // world units per second (?)
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ class PositionSystem : IteratingSystem(
},
interval = EachFrame
) {
private var camera: Entity = Entity.NONE // Needs to be set after the camera entity was created after configuring the fleks world
private val viewPortSize: SizeInt = inject("ViewPortSize")

// Overall world moving (playfield)
val deltaX: Float = -110.0f // TODO this will come from tiledMap scrolling
val deltaY: Float = 0.0f
Expand All @@ -44,8 +41,4 @@ class PositionSystem : IteratingSystem(
positionComponent.y = motion.accelX * 0.5f * deltaTime * deltaTime + motion.velocityY * deltaTime + positionComponent.y
}
}

fun setCamera(cameraName: String) {
this.camera = EntityByName.getOrNull(cameraName) ?: throw Error("ERROR: Camera entity with name $cameraName does not exist and cannot be set in PositionSystem!")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package korlibs.korge.fleks.tags

import com.github.quillraven.fleks.*
import kotlinx.serialization.*

/**
* The [MainCameraTag] component is used to specify which camera entity is used for rendering the game world.
* Make sure to have only one camera entity in the game world with has [MainCameraTag] component attached. Otherwise,
* the first camera entity found by the family search will be used for rendering.
*/
@Serializable @SerialName("MainCamera")
data object MainCameraTag : EntityTag()

0 comments on commit cdd5a18

Please sign in to comment.