dropped item can be picked up (at a weird distance)

This commit is contained in:
minjaesong
2021-10-02 17:50:27 +09:00
parent 6fda6bafe4
commit f6b0b447a4
5 changed files with 51 additions and 8 deletions

View File

@@ -425,8 +425,8 @@ open class IngameInstance(val batch: SpriteBatch) : Screen {
/** Will use centre point of the actors
* @return List of DistanceResult, list may be empty */
fun findKNearestActors(from: ActorWithBody, maxHits: Int): List<DistanceResult<ActorWithBody>> {
return actorsRTree.nearestNeighbour(actorDistanceCalculator, null, maxHits, object : PointND {
fun findKNearestActors(from: ActorWithBody, maxHits: Int, nodeFilter: (ActorWithBody) -> Boolean): List<DistanceResult<ActorWithBody>> {
return actorsRTree.nearestNeighbour(actorDistanceCalculator, nodeFilter, maxHits, object : PointND {
override fun getDimensions(): Int = 2
override fun getOrd(axis: Int): Double = when(axis) {
0 -> from.hitbox.centeredX
@@ -437,8 +437,8 @@ open class IngameInstance(val batch: SpriteBatch) : Screen {
}
/** Will use centre point of the actors
* @return Pair of: the actor, distance from the actor; null if none found */
fun findNearestActors(from: ActorWithBody): DistanceResult<ActorWithBody>? {
val t = findKNearestActors(from, 1)
fun findNearestActor(from: ActorWithBody, nodeFilter: (ActorWithBody) -> Boolean): DistanceResult<ActorWithBody>? {
val t = findKNearestActors(from, 1, nodeFilter)
return if (t.isNotEmpty())
t[0]
else

View File

@@ -156,6 +156,13 @@ class Hitbox {
infix fun intersects(position: Point2d) =
(position.x >= startX && position.x <= startX + width) &&
(position.y >= startY && position.y <= startY + height)
infix fun intersects(other: Hitbox) =
(this.startX <= other.startX && other.startX <= this.endX) ||
(this.startX <= other.endX && other.endX <= this.endX) &&
(this.startY <= other.startY && other.startY <= this.endY) ||
(this.startY <= other.endY && other.endY <= this.endY)
fun toVector(): Vector2 = Vector2(startX, startY)

View File

@@ -114,6 +114,7 @@ open class GameWorld() : Disposable {
internal var genver = -1
internal var comp = -1
@Deprecated("This value is only used for savegames; DO NOT USE THIS", ReplaceWith("INGAME.actorContainerActive", "net.torvald.terrarum.INGAME"))
internal val actors = ArrayList<ActorID>() // only filled up on save and load; DO NOT USE THIS
/**

View File

@@ -5,11 +5,11 @@ import net.torvald.terrarum.*
import net.torvald.terrarum.TerrarumAppConfiguration.TILE_SIZE
import net.torvald.terrarum.blockproperties.Block
import net.torvald.terrarum.blockproperties.Fluid
import net.torvald.terrarum.gameactors.ActorWithBody
import net.torvald.terrarum.gameactors.Controllable
import net.torvald.terrarum.gameitem.ItemID
import net.torvald.terrarum.modulebasegame.TerrarumIngame.Companion.inUpdateRange
import net.torvald.terrarum.modulebasegame.gameactors.ActorHumanoid
import net.torvald.terrarum.modulebasegame.gameactors.Electric
import net.torvald.terrarum.modulebasegame.gameactors.FixtureBase
import net.torvald.terrarum.modulebasegame.gameactors.*
import org.dyn4j.geometry.Vector2
import kotlin.math.roundToInt
@@ -88,6 +88,9 @@ object WorldSimulator {
App.measureDebugTime("WorldSimulator.wires") {
simulateWires(delta)
}
App.measureDebugTime("WorldSimulator.collisionDroppedItem") {
collideDroppedItems()
}
//printdbg(this, "============================")
}
@@ -140,6 +143,29 @@ object WorldSimulator {
} }
}
fun collideDroppedItems() {
ingame.actorContainerActive.filter { it is DroppedItem }.forEach { droppedItem0 ->
val droppedItem = droppedItem0 as DroppedItem
if (droppedItem.canBePickedUp()) {
val actors = ingame.findKNearestActors(droppedItem0 as ActorWithBody, 64) { it is Controllable && it is Pocketed }
for (result in actors) {
val actor = result.get()
// if hitbox overlaps, pick up
val s = actor.scale
val w = actor.baseHitboxW * s
val h = actor.baseHitboxH * s
val pickupDistance = w*w + h*h// TODO refer to the actorValue
// println("${result.distance}\t$pickupDistance")
if (result.distance < pickupDistance) {
droppedItem.flagDespawn = true
(actor as Pocketed).inventory.add(droppedItem.itemID, droppedItem.itemCount)
break
}
}
}
}
}
/**
* displace fluids. Note that the code assumes the gravity pulls things downward ONLY,
* which means you'll need to modify the code A LOT if you're going to implement zero- or

View File

@@ -17,7 +17,11 @@ import net.torvald.terrarum.worlddrawer.WorldCamera
*/
open class DroppedItem : ActorWithBody {
private var itemID: ItemID = ""
companion object {
const val NO_PICKUP_TIME = 1f
}
var itemID: ItemID = ""; private set
@Transient private var textureRegion: TextureRegion? = null // deserialiser won't call setter of the fields
@@ -25,6 +29,10 @@ open class DroppedItem : ActorWithBody {
protected constructor()
private var timeSinceSpawned = 0f
fun canBePickedUp() = timeSinceSpawned > NO_PICKUP_TIME
constructor(itemID: ItemID, topLeftX: Int, topLeftY: Int) : super(RenderOrder.MIDTOP, PhysProperties.PHYSICS_OBJECT) {
this.itemID = itemID
@@ -97,6 +105,7 @@ open class DroppedItem : ActorWithBody {
override fun update(delta: Float) {
super.update(delta)
timeSinceSpawned += delta
// TODO merge into the already existing droppeditem with isStationary==true if one is detected
}
}