+25
-19
lines changedFilter options
+25
-19
lines changed Original file line number Diff line number Diff line change
@@ -6,53 +6,46 @@
6
6
7
7
package com.almasb.fxgl.dsl.components
8
8
9
-
import com.almasb.fxgl.app.scene.Viewport
10
9
import com.almasb.fxgl.dsl.FXGL
11
10
import com.almasb.fxgl.entity.component.Component
11
+
import javafx.geometry.Rectangle2D
12
12
13
13
/**
14
14
* A component that keeps an entity within the viewport.
15
15
* Entities with physics enabled are not supported.
16
-
* Do NOT use this component if viewport is bound to an entity.
17
16
*
18
17
* @author Almas Baimagambetov (almaslvl@gmail.com)
19
18
*/
20
-
class KeepOnScreenComponent : Component() {
21
-
22
-
private lateinit var viewport: Viewport
19
+
open class KeepInBoundsComponent(var bounds: Rectangle2D) : Component() {
23
20
24
21
/**
25
-
* keep on screen in X axis.
22
+
* Keep in bounds in X axis.
26
23
*/
27
24
var isHorizontal = true
28
25
29
26
/**
30
-
* keep on screen in Y axis.
27
+
* Keep in bounds in Y axis.
31
28
*/
32
29
var isVertical = true
33
30
34
-
override fun onAdded() {
35
-
viewport = FXGL.getGameScene().viewport
36
-
}
37
-
38
31
override fun onUpdate(tpf: Double) {
39
32
blockWithBBox()
40
33
}
41
34
42
35
private fun blockWithBBox() {
43
36
if (isHorizontal) {
44
-
if (getEntity().x < viewport.x) {
45
-
getEntity().x = viewport.x
46
-
} else if (getEntity().rightX > viewport.x + viewport.width) {
47
-
getEntity().x = viewport.x + viewport.width - getEntity().width
37
+
if (getEntity().x < bounds.minX) {
38
+
getEntity().x = bounds.minX
39
+
} else if (getEntity().rightX > bounds.maxX) {
40
+
getEntity().x = bounds.maxX - getEntity().width
48
41
}
49
42
}
50
43
51
44
if (isVertical) {
52
-
if (getEntity().y < viewport.y) {
53
-
getEntity().y = viewport.y
54
-
} else if (getEntity().bottomY > viewport.y + viewport.height) {
55
-
getEntity().y = viewport.y + viewport.height - getEntity().height
45
+
if (getEntity().y < bounds.minY) {
46
+
getEntity().y = bounds.minY
47
+
} else if (getEntity().bottomY > bounds.maxY) {
48
+
getEntity().y = bounds.maxY - getEntity().height
56
49
}
57
50
}
58
51
}
@@ -73,4 +66,17 @@ class KeepOnScreenComponent : Component() {
73
66
}
74
67
75
68
override fun isComponentInjectionRequired(): Boolean = false
69
+
}
70
+
71
+
/**
72
+
* A component that keeps an entity within the viewport.
73
+
* Entities with physics enabled are not supported.
74
+
* Do NOT use this component if viewport is bound to an entity.
75
+
*/
76
+
class KeepOnScreenComponent : KeepInBoundsComponent(Rectangle2D.EMPTY) {
77
+
78
+
override fun onUpdate(tpf: Double) {
79
+
bounds = FXGL.getGameScene().viewport.visibleArea
80
+
super.onUpdate(tpf)
81
+
}
76
82
}
You can’t perform that action at this time.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4