+71
-0
lines changedFilter options
+71
-0
lines changed Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
1
+
/*
2
+
* FXGL - JavaFX Game Library. The MIT License (MIT).
3
+
* Copyright (c) AlmasB (almaslvl@gmail.com).
4
+
* See LICENSE for details.
5
+
*/
6
+
7
+
package com.almasb.fxgl.dsl.components
8
+
9
+
import com.almasb.fxgl.entity.component.Component
10
+
import com.almasb.fxgl.logging.Logger
11
+
import javafx.beans.property.ReadOnlyBooleanProperty
12
+
import javafx.beans.property.ReadOnlyBooleanWrapper
13
+
import javafx.geometry.Point2D
14
+
15
+
/**
16
+
* This component moves the entity using provided Point2D waypoints.
17
+
*
18
+
* @author Almas Baimagambetov (almaslvl@gmail.com)
19
+
*/
20
+
class WaypointMoveComponent(
21
+
var speed: Double,
22
+
waypoints: List<Point2D>) : Component() {
23
+
24
+
private val log = Logger.get<WaypointMoveComponent>()
25
+
26
+
private var points: MutableList<Point2D> = arrayListOf()
27
+
private var nextPoint: Point2D = Point2D.ZERO
28
+
29
+
private val isAtDestinationProp = ReadOnlyBooleanWrapper(true)
30
+
31
+
init {
32
+
move(waypoints)
33
+
}
34
+
35
+
fun atDestinationProperty(): ReadOnlyBooleanProperty = isAtDestinationProp.readOnlyProperty
36
+
37
+
fun move(waypoints: List<Point2D>) {
38
+
points.clear()
39
+
points.addAll(waypoints)
40
+
41
+
if (points.isEmpty()) {
42
+
log.warning("No waypoints given. Using Point2D.ZERO as dummy")
43
+
points.add(Point2D.ZERO)
44
+
}
45
+
46
+
nextPoint = points.removeAt(0)
47
+
48
+
isAtDestinationProp.value = false
49
+
}
50
+
51
+
override fun onUpdate(tpf: Double) {
52
+
if (isAtDestinationProp.value)
53
+
return
54
+
55
+
val dist = tpf * speed
56
+
57
+
if (nextPoint.distance(entity.anchoredPosition) < dist) {
58
+
entity.anchoredPosition = nextPoint
59
+
60
+
if (points.isNotEmpty()) {
61
+
nextPoint = points.removeAt(0)
62
+
} else {
63
+
isAtDestinationProp.value = true
64
+
}
65
+
} else {
66
+
entity.translateTowards(nextPoint, dist)
67
+
}
68
+
}
69
+
70
+
override fun isComponentInjectionRequired(): Boolean = false
71
+
}
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