Skip to content

Commit

Permalink
feat: added KeepInBoundsComponent, fixed KeepOnScreenComponent, closes
Browse files Browse the repository at this point in the history
  • Loading branch information
AlmasB committed Jul 7, 2022
1 parent 27efc09 commit 7c2e659
Showing 1 changed file with 25 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,53 +6,46 @@

package com.almasb.fxgl.dsl.components

import com.almasb.fxgl.app.scene.Viewport
import com.almasb.fxgl.dsl.FXGL
import com.almasb.fxgl.entity.component.Component
import javafx.geometry.Rectangle2D

/**
* A component that keeps an entity within the viewport.
* Entities with physics enabled are not supported.
* Do NOT use this component if viewport is bound to an entity.
*
* @author Almas Baimagambetov ([email protected])
*/
class KeepOnScreenComponent : Component() {

private lateinit var viewport: Viewport
open class KeepInBoundsComponent(var bounds: Rectangle2D) : Component() {

/**
* keep on screen in X axis.
* Keep in bounds in X axis.
*/
var isHorizontal = true

/**
* keep on screen in Y axis.
* Keep in bounds in Y axis.
*/
var isVertical = true

override fun onAdded() {
viewport = FXGL.getGameScene().viewport
}

override fun onUpdate(tpf: Double) {
blockWithBBox()
}

private fun blockWithBBox() {
if (isHorizontal) {
if (getEntity().x < viewport.x) {
getEntity().x = viewport.x
} else if (getEntity().rightX > viewport.x + viewport.width) {
getEntity().x = viewport.x + viewport.width - getEntity().width
if (getEntity().x < bounds.minX) {
getEntity().x = bounds.minX
} else if (getEntity().rightX > bounds.maxX) {
getEntity().x = bounds.maxX - getEntity().width
}
}

if (isVertical) {
if (getEntity().y < viewport.y) {
getEntity().y = viewport.y
} else if (getEntity().bottomY > viewport.y + viewport.height) {
getEntity().y = viewport.y + viewport.height - getEntity().height
if (getEntity().y < bounds.minY) {
getEntity().y = bounds.minY
} else if (getEntity().bottomY > bounds.maxY) {
getEntity().y = bounds.maxY - getEntity().height
}
}
}
Expand All @@ -73,4 +66,17 @@ class KeepOnScreenComponent : Component() {
}

override fun isComponentInjectionRequired(): Boolean = false
}

/**
* A component that keeps an entity within the viewport.
* Entities with physics enabled are not supported.
* Do NOT use this component if viewport is bound to an entity.
*/
class KeepOnScreenComponent : KeepInBoundsComponent(Rectangle2D.EMPTY) {

override fun onUpdate(tpf: Double) {
bounds = FXGL.getGameScene().viewport.visibleArea
super.onUpdate(tpf)
}
}

0 comments on commit 7c2e659

Please sign in to comment.