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> <annotationProcessing>
<profile default="true" name="Default" enabled="true" /> <profile default="true" name="Default" enabled="true" />
</annotationProcessing> </annotationProcessing>
<bytecodeTargetLevel target="11" /> <bytecodeTargetLevel target="17" />
</component> </component>
<component name="JavacSettings"> <component name="JavacSettings">
<option name="PREFER_TARGET_JDK_COMPILER" value="false" /> <option name="PREFER_TARGET_JDK_COMPILER" value="false" />

View File

@@ -1,6 +1,5 @@
package net.torvald.terrarum package net.torvald.terrarum
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.utils.Disposable import com.badlogic.gdx.utils.Disposable
import com.badlogic.gdx.utils.GdxRuntimeException import com.badlogic.gdx.utils.GdxRuntimeException
import net.torvald.terrarum.App.printdbg 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 actorContainerActive = SortedArrayList<Actor>(ACTORCONTAINER_INITIAL_SIZE)
val actorContainerInactive = SortedArrayList<Actor>(ACTORCONTAINER_INITIAL_SIZE) val actorContainerInactive = SortedArrayList<Actor>(ACTORCONTAINER_INITIAL_SIZE)
val actorAdditionQueue = ArrayList<Actor>() val actorAdditionQueue = ArrayList<Pair<Actor, Throwable>>()
val actorRemovalQueue = ArrayList<Actor>() val actorRemovalQueue = ArrayList<Pair<Actor, Throwable>>()
/** /**
* ## BIG NOTE: Calculated actor distance is the **Euclidean distance SQUARED** * ## 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?) { open fun queueActorRemoval(actor: Actor?) {
if (actor == null) return 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 -> arrayOf(actorContainerActive, actorContainerInactive).forEach { actorContainer ->
val indexToDelete = actorContainer.searchFor(actor.referenceID) { it.referenceID } val indexToDelete = actorContainer.searchFor(actor.referenceID) { it.referenceID }
if (indexToDelete != null) { 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 (actor == null) return
if (theGameHasActor(actor.referenceID)) { if (theGameHasActor(actor.referenceID)) {
throw ReferencedActorAlreadyExistsException(actor) throw ReferencedActorAlreadyExistsException(actor, caller)
} }
else { 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?) { open fun queueActorAddition(actor: Actor?) {
if (actor == null) return if (actor == null) return
actorAdditionQueue.add(actor) actorAdditionQueue.add(actor to StackTraceRecorder())
} }
fun isActive(ID: Int): Boolean = 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 NoSuchActorWithIDException(id: ActorID) : Exception("Actor with ID $id does not exist.")
class NoSuchActorWithRefException(actor: Actor) : Exception("No such actor in the game: $actor") 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 ReferencedActorAlreadyExistsException(actor: Actor, caller: Throwable) : Exception("The actor $actor already exists in the game", caller)
class ProtectedActorRemovalException(whatisit: String) : Exception("Attempted to removed protected actor '$whatisit'") class ProtectedActorRemovalException(whatisit: String, caller: Throwable) : Exception("Attempted to removed protected actor '$whatisit'", caller)
val INGAME: IngameInstance val INGAME: IngameInstance
get() = Terrarum.ingame!! get() = Terrarum.ingame!!

View File

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

View File

@@ -26,6 +26,8 @@ open class DroppedItem : ActorWithBody {
var itemID: ItemID = ""; private set var itemID: ItemID = ""; private set
@Transient private var visualItemID = ""
@Transient private var textureRegion: TextureRegion? = null // deserialiser won't call setter of the fields @Transient private var textureRegion: TextureRegion? = null // deserialiser won't call setter of the fields
var itemCount = 1L var itemCount = 1L
@@ -76,8 +78,11 @@ open class DroppedItem : ActorWithBody {
override fun drawBody(batch: SpriteBatch) { override fun drawBody(batch: SpriteBatch) {
// deserialiser won't call setter of the fields // deserialiser won't call setter of the fields
if (visualItemID == "") {
visualItemID = BlockCodex.getOrNull(itemID)?.world ?: itemID
}
if (textureRegion == null) { if (textureRegion == null) {
textureRegion = ItemCodex.getItemImage(itemID)!! textureRegion = ItemCodex.getItemImage(visualItemID)!!
} }
// copy-pasted from ActorWithBody.drawSpriteInGoodPosition() // 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) { 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) item.spawn(Terrarum.mouseTileX, Terrarum.mouseTileY - item.blockBox.height + 1)
// return true when placed, false when cannot be placed // return true when placed, false when cannot be placed