Skip to content

Commit

Permalink
Better Targeting flexibility
Browse files Browse the repository at this point in the history
  • Loading branch information
darkfrog26 committed Aug 14, 2023
1 parent e141b30 commit 7ce79a1
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 11 deletions.
12 changes: 3 additions & 9 deletions core/src/main/scala/org/sgine/component/Targeting.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,15 @@ import reactify.Val
* Targeting mix-in provides convenience functionality to target nearby
*/
trait Targeting[Target <: Component] extends Component {
lazy val nearestTargets: Val[List[Target]] = Val {
targets.sortBy(calculateDistance)
}
lazy val currentTarget: Val[Option[Target]] = Val {
nearestTargets()
.iterator
.takeWhile(c => calculateDistance(c) <= maxDistance && shouldTarget(c))
.nextOption()
targets.find(c => shouldTarget(c, calculateDistance(c)))
}

protected def maxDistance: Double

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

protected def calculateDistance(c: Target): Double = MathUtils.distanceFromCenter(this, c)

protected def targets: List[Target]
protected def targets: Seq[Target]
}
2 changes: 1 addition & 1 deletion core/src/main/scala/org/sgine/util/MathUtils.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.sgine.util

import com.badlogic.gdx.math.{Intersector, Vector2}
import org.sgine.component.{Component, DimensionedSupport}
import org.sgine.component.Component

object MathUtils {
private lazy val t1: Vector2 = new Vector2()
Expand Down
2 changes: 1 addition & 1 deletion core/src/test/scala/examples/TargetingExample.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ object TargetingExample extends Example {

override protected def maxDistance: Double = 500.0

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

private lazy val image = new Image("basketball.png")
private lazy val drawing = new Image {
Expand Down

0 comments on commit 7ce79a1

Please sign in to comment.