fixed a stupid bug where spawning a same kind of fixture twice would crash the game because the spawner would not renew the fixture instance after a first spawn

This commit is contained in:
minjaesong
2022-03-23 10:03:03 +09:00
parent de45ad1c25
commit 76435dbbdf
5 changed files with 24 additions and 19 deletions

2
.idea/compiler.xml generated
View File

@@ -4,7 +4,7 @@
<annotationProcessing>
<profile default="true" name="Default" enabled="true" />
</annotationProcessing>
<bytecodeTargetLevel target="11" />
<bytecodeTargetLevel target="17" />
</component>
<component name="JavacSettings">
<option name="PREFER_TARGET_JDK_COMPILER" value="false" />

View File

@@ -1,6 +1,5 @@
package net.torvald.terrarum
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.utils.Disposable
import com.badlogic.gdx.utils.GdxRuntimeException
import net.torvald.terrarum.App.printdbg
@@ -126,8 +125,8 @@ open class IngameInstance(val batch: FlippingSpriteBatch, val isMultiplayer: Boo
val actorContainerActive = SortedArrayList<Actor>(ACTORCONTAINER_INITIAL_SIZE)
val actorContainerInactive = SortedArrayList<Actor>(ACTORCONTAINER_INITIAL_SIZE)
val actorAdditionQueue = ArrayList<Actor>()
val actorRemovalQueue = ArrayList<Actor>()
val actorAdditionQueue = ArrayList<Pair<Actor, Throwable>>()
val actorRemovalQueue = ArrayList<Pair<Actor, Throwable>>()
/**
* ## BIG NOTE: Calculated actor distance is the **Euclidean distance SQUARED**
@@ -316,10 +315,10 @@ open class IngameInstance(val batch: FlippingSpriteBatch, val isMultiplayer: Boo
*/
open fun queueActorRemoval(actor: Actor?) {
if (actor == null) return
actorRemovalQueue.add(actor)
actorRemovalQueue.add(actor to StackTraceRecorder())
}
protected open fun forceRemoveActor(actor: Actor) {
protected open fun forceRemoveActor(actor: Actor, caller: Throwable = StackTraceRecorder()) {
arrayOf(actorContainerActive, actorContainerInactive).forEach { actorContainer ->
val indexToDelete = actorContainer.searchFor(actor.referenceID) { it.referenceID }
if (indexToDelete != null) {
@@ -329,14 +328,14 @@ open class IngameInstance(val batch: FlippingSpriteBatch, val isMultiplayer: Boo
}
}
protected open fun forceAddActor(actor: Actor?) {
protected open fun forceAddActor(actor: Actor?, caller: Throwable = StackTraceRecorder()) {
if (actor == null) return
if (theGameHasActor(actor.referenceID)) {
throw ReferencedActorAlreadyExistsException(actor)
throw ReferencedActorAlreadyExistsException(actor, caller)
}
else {
actorAdditionQueue.add(actor)
actorAdditionQueue.add(actor to StackTraceRecorder())
}
}
@@ -345,7 +344,7 @@ open class IngameInstance(val batch: FlippingSpriteBatch, val isMultiplayer: Boo
*/
open fun queueActorAddition(actor: Actor?) {
if (actor == null) return
actorAdditionQueue.add(actor)
actorAdditionQueue.add(actor to StackTraceRecorder())
}
fun isActive(ID: Int): Boolean =
@@ -498,10 +497,11 @@ inline fun Lock.lock(body: () -> Unit) {
}
}
class StackTraceRecorder() : Exception("(I'm here to just record the stack trace, move along)")
class NoSuchActorWithIDException(id: ActorID) : Exception("Actor with ID $id does not exist.")
class NoSuchActorWithRefException(actor: Actor) : Exception("No such actor in the game: $actor")
class ReferencedActorAlreadyExistsException(actor: Actor) : Exception("The actor $actor already exists in the game")
class ProtectedActorRemovalException(whatisit: String) : Exception("Attempted to removed protected actor '$whatisit'")
class ReferencedActorAlreadyExistsException(actor: Actor, caller: Throwable) : Exception("The actor $actor already exists in the game", caller)
class ProtectedActorRemovalException(whatisit: String, caller: Throwable) : Exception("Attempted to removed protected actor '$whatisit'", caller)
val INGAME: IngameInstance
get() = Terrarum.ingame!!

View File

@@ -775,7 +775,7 @@ open class TerrarumIngame(batch: FlippingSpriteBatch) : IngameInstance(batch) {
repossessActor()
// process actor addition requests
actorAdditionQueue.forEach { forceAddActor(it) }
actorAdditionQueue.forEach { forceAddActor(it.first, it.second) }
actorAdditionQueue.clear()
// determine whether the inactive actor should be activated
wakeDormantActors()
@@ -784,7 +784,7 @@ open class TerrarumIngame(batch: FlippingSpriteBatch) : IngameInstance(batch) {
// determine whether the actor should keep being activated or be dormant
killOrKnockdownActors()
// process actor removal requests
actorRemovalQueue.forEach { forceRemoveActor(it) }
actorRemovalQueue.forEach { forceRemoveActor(it.first, it.second) }
actorRemovalQueue.clear()
// update particles
particlesContainer.forEach { if (!it.flagDespawn) particlesActive++; it.update(delta) }
@@ -1141,7 +1141,7 @@ open class TerrarumIngame(batch: FlippingSpriteBatch) : IngameInstance(batch) {
}
override fun forceRemoveActor(actor: Actor) {
override fun forceRemoveActor(actor: Actor, caller: Throwable) {
arrayOf(actorContainerActive, actorContainerInactive).forEach { actorContainer ->
val indexToDelete = actorContainer.searchForIndex(actor.referenceID) { it.referenceID }
if (indexToDelete != null) {
@@ -1185,11 +1185,11 @@ open class TerrarumIngame(batch: FlippingSpriteBatch) : IngameInstance(batch) {
/**
* Check for duplicates, append actor and sort the list
*/
override fun forceAddActor(actor: Actor?) {
override fun forceAddActor(actor: Actor?, caller: Throwable) {
if (actor == null) return
if (theGameHasActor(actor.referenceID)) {
throw ReferencedActorAlreadyExistsException(actor)
throw ReferencedActorAlreadyExistsException(actor, caller)
}
else {
actorContainerActive.add(actor)

View File

@@ -26,6 +26,8 @@ open class DroppedItem : ActorWithBody {
var itemID: ItemID = ""; private set
@Transient private var visualItemID = ""
@Transient private var textureRegion: TextureRegion? = null // deserialiser won't call setter of the fields
var itemCount = 1L
@@ -76,8 +78,11 @@ open class DroppedItem : ActorWithBody {
override fun drawBody(batch: SpriteBatch) {
// deserialiser won't call setter of the fields
if (visualItemID == "") {
visualItemID = BlockCodex.getOrNull(itemID)?.world ?: itemID
}
if (textureRegion == null) {
textureRegion = ItemCodex.getItemImage(itemID)!!
textureRegion = ItemCodex.getItemImage(visualItemID)!!
}
// copy-pasted from ActorWithBody.drawSpriteInGoodPosition()

View File

@@ -87,7 +87,7 @@ open class FixtureItemBase(originalID: ItemID, val fixtureClassName: String) : G
}
override fun startPrimaryUse(actor: ActorWithBody, delta: Float) = mouseInInteractableRange(actor) {
val item = ghostItem.get()//makeFixture()
val item = ghostItem.getAndSet(makeFixture()) // renew the "ghost" otherwise you'll be spawning exactly the same fixture again; old ghost will be returned
item.spawn(Terrarum.mouseTileX, Terrarum.mouseTileY - item.blockBox.height + 1)
// return true when placed, false when cannot be placed