Skip to content

Commit

Permalink
Minor updates to Targeting to support multiple targets
Browse files Browse the repository at this point in the history
  • Loading branch information
darkfrog26 committed Aug 24, 2023
1 parent 7a46f5b commit f5e0141
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
8 changes: 6 additions & 2 deletions core/src/main/scala/org/sgine/component/Targeting.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ import reactify.Val
* Targeting mix-in provides convenience functionality to target nearby
*/
trait Targeting[Target <: Component] extends Component {
lazy val currentTarget: Val[Option[Target]] = Val {
targets.find(c => shouldTarget(c, calculateDistance(c)))
lazy val currentTargets: Val[Seq[Target]] = Val {
targets
.filter(c => shouldTarget(c, calculateDistance(c)))
.take(maxTargets)
}

protected def maxDistance: Double

protected def maxTargets: Int

protected def shouldTarget(c: Target, distance: Double): Boolean = distance <= maxDistance

protected def calculateDistance(c: Target): Double = MathUtils.distanceFromCenter(this, c)
Expand Down
4 changes: 3 additions & 1 deletion core/src/test/scala/examples/TargetingExample.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ object TargetingExample extends Example {

override protected def maxDistance: Double = 500.0

override protected def maxTargets: Int = 1

override protected def targets: List[Enemy] = nearest()

private lazy val image = new Image("basketball.png")
Expand All @@ -40,7 +42,7 @@ object TargetingExample extends Example {
override def draw(drawer: Drawer): Unit = {
drawer.color = Color.White
drawer.circle(110.0, 110.0, 500.0, 6.0)
currentTarget().foreach { target =>
currentTargets().foreach { target =>
val x = global.localizeX(target.global.center)
val y = global.localizeY(target.global.middle)
drawer.color = Color.Green
Expand Down

0 comments on commit f5e0141

Please sign in to comment.