adding/removing actors in game won't go wrong now (perhaps...)

Former-commit-id: c66db4b652e2fae34e66200aca4b101c16bab5d9
Former-commit-id: 5bafa85cabf7ead6db5f48475164d0694f66f15d
This commit is contained in:
Song Minjae
2017-01-26 01:29:17 +09:00
parent 5a76b1cc13
commit 2d34db3022
4 changed files with 68 additions and 41 deletions

View File

@@ -132,11 +132,11 @@ constructor() : BasicGameState() {
// add new player and put it to actorContainer // add new player and put it to actorContainer
playableActorDelegate = PlayableActorDelegate(PlayerBuilderSigrid()) playableActorDelegate = PlayableActorDelegate(PlayerBuilderSigrid())
addActor(player) addNewActor(player)
// test actor // test actor
addActor(PlayerBuilderCynthia()) addNewActor(PlayerBuilderCynthia())
@@ -178,7 +178,11 @@ constructor() : BasicGameState() {
//AudioResourceLibrary.ambientsWoods[0].play() //AudioResourceLibrary.ambientsWoods[0].play()
} }
var particlesActive = 0
private set
override fun update(gc: GameContainer, sbg: StateBasedGame, delta: Int) { override fun update(gc: GameContainer, sbg: StateBasedGame, delta: Int) {
particlesActive = 0
UPDATE_DELTA = delta UPDATE_DELTA = delta
setAppTitle() setAppTitle()
@@ -226,7 +230,7 @@ constructor() : BasicGameState() {
// determine whether the actor should keep being activated or be dormant // determine whether the actor should keep being activated or be dormant
KillOrKnockdownActors() KillOrKnockdownActors()
updateActors(gc, delta) updateActors(gc, delta)
particlesContainer.forEach { it.update(gc, delta) } particlesContainer.forEach { if (!it.flagDespawn) particlesActive++; it.update(gc, delta) }
// TODO thread pool(?) // TODO thread pool(?)
CollisionSolver.process() CollisionSolver.process()
} }
@@ -260,9 +264,9 @@ constructor() : BasicGameState() {
private fun repossessActor() { private fun repossessActor() {
// check if currently pocessed actor is removed from game // check if currently pocessed actor is removed from game
if (!hasActor(player)) { if (!theGameHasActor(player)) {
// re-possess canonical player // re-possess canonical player
if (hasActor(Player.PLAYER_REF_ID)) if (theGameHasActor(Player.PLAYER_REF_ID))
changePossession(Player.PLAYER_REF_ID) changePossession(Player.PLAYER_REF_ID)
else else
changePossession(0x51621D) // FIXME fallback debug mode (FIXME is there for a reminder visible in ya IDE) changePossession(0x51621D) // FIXME fallback debug mode (FIXME is there for a reminder visible in ya IDE)
@@ -270,8 +274,8 @@ constructor() : BasicGameState() {
} }
private fun changePossession(newActor: PlayableActorDelegate) { private fun changePossession(newActor: PlayableActorDelegate) {
if (!hasActor(player)) { if (!theGameHasActor(player)) {
throw IllegalArgumentException("No such actor in actorContainer: $newActor") throw IllegalArgumentException("No such actor in the game: $newActor")
} }
playableActorDelegate = newActor playableActorDelegate = newActor
@@ -281,8 +285,8 @@ constructor() : BasicGameState() {
private fun changePossession(refid: Int) { private fun changePossession(refid: Int) {
// TODO prevent possessing other player on multiplayer // TODO prevent possessing other player on multiplayer
if (!hasActor(refid)) { if (!theGameHasActor(refid)) {
throw IllegalArgumentException("No such actor in actorContainer: $refid") throw IllegalArgumentException("No such actor in the game: $refid (elemsActive: ${actorContainer.size}, elemsInactive: ${actorContainerInactive.size})")
} }
// take care of old delegate // take care of old delegate
@@ -297,7 +301,7 @@ constructor() : BasicGameState() {
Terrarum.appgc.setTitle( Terrarum.appgc.setTitle(
Terrarum.NAME + Terrarum.NAME +
" — F: ${Terrarum.appgc.fps} (${Terrarum.TARGET_INTERNAL_FPS})" + " — F: ${Terrarum.appgc.fps} (${Terrarum.TARGET_INTERNAL_FPS})" +
" — M: ${Terrarum.memInUse}M / ${Terrarum.totalVMMem}M") " — M: ${Terrarum.memInUse}M / ${Terrarum.memTotal}M / ${Terrarum.memXmx}M")
} }
override fun render(gc: GameContainer, sbg: StateBasedGame, gwin: Graphics) { override fun render(gc: GameContainer, sbg: StateBasedGame, gwin: Graphics) {
@@ -399,14 +403,14 @@ constructor() : BasicGameState() {
) )
// velocity // velocity
worldG.color = Color(0x80FFFF) worldG.color = GameFontBase.codeToCol["g"]
worldG.drawString( worldG.drawString(
"vX ${actor.velocity.x}", // doesn't work for NPCs/Player "${0x7F.toChar()}X ${actor.velocity.x}", // doesn't work for NPCs/Player
actor.hitbox.posX.toFloat(), actor.hitbox.posX.toFloat(),
actor.hitbox.pointedY.toFloat() + 4 + 8 actor.hitbox.pointedY.toFloat() + 4 + 8
) )
worldG.drawString( worldG.drawString(
"vY ${actor.velocity.y}", "${0x7F.toChar()}Y ${actor.velocity.y}",
actor.hitbox.posX.toFloat(), actor.hitbox.posX.toFloat(),
actor.hitbox.pointedY.toFloat() + 4 + 8 * 2 actor.hitbox.pointedY.toFloat() + 4 + 8 * 2
) )
@@ -521,10 +525,8 @@ constructor() : BasicGameState() {
var i = 0 var i = 0
while (i < actorContainerSize) { // loop through actorContainerInactive while (i < actorContainerSize) { // loop through actorContainerInactive
val actor = actorContainerInactive[i] val actor = actorContainerInactive[i]
val actorIndex = i
if (actor is ActorVisible && actor.inUpdateRange()) { if (actor is ActorVisible && actor.inUpdateRange()) {
addActor(actor) // duplicates are checked here activateDormantActor(actor) // duplicates are checked here
actorContainerInactive.removeAt(actorIndex)
actorContainerSize -= 1 actorContainerSize -= 1
i-- // array removed 1 elem, so we also decrement counter by 1 i-- // array removed 1 elem, so we also decrement counter by 1
} }
@@ -629,26 +631,22 @@ constructor() : BasicGameState() {
/** /**
* actorContainer extensions * actorContainer extensions
*/ */
fun hasActor(actor: Actor) = hasActor(actor.referenceID) fun theGameHasActor(actor: Actor) = theGameHasActor(actor.referenceID)
fun hasActor(ID: Int): Boolean = fun theGameHasActor(ID: Int): Boolean =
isActive(ID) || isInactive(ID)
fun isActive(ID: Int): Boolean =
if (actorContainer.size == 0) if (actorContainer.size == 0)
false false
else { else
// TODO cherche for inactive actorContainer.binarySearch(ID) >= 0
val binsearch = actorContainer.binarySearch(ID)
if (binsearch < 0) {
if (actorContainerInactive.size == 0) false
else {
val binsearch2 = actorContainerInactive.binarySearch(ID)
binsearch2 >= 0 fun isInactive(ID: Int): Boolean =
} if (actorContainerInactive.size == 0)
} false
else { else
true actorContainerInactive.binarySearch(ID) >= 0
}
}
fun removeActor(ID: Int) = removeActor(getActorByID(ID)) fun removeActor(ID: Int) = removeActor(getActorByID(ID))
/** /**
@@ -693,11 +691,9 @@ constructor() : BasicGameState() {
/** /**
* Check for duplicates, append actor and sort the list * Check for duplicates, append actor and sort the list
*/ */
fun addActor(actor: Actor) { fun addNewActor(actor: Actor) {
if (hasActor(actor.referenceID)) { if (theGameHasActor(actor.referenceID)) {
println("[StateInGame] Replacing actor $actor") throw Error("The actor $actor already exists in the game")
removeActor(actor)
addActor(actor)
} }
else { else {
actorContainer.add(actor) actorContainer.add(actor)
@@ -722,6 +718,37 @@ constructor() : BasicGameState() {
} }
} }
fun activateDormantActor(actor: Actor) {
if (!isInactive(actor.referenceID)) {
if (isActive(actor.referenceID))
throw Error("The actor $actor is already activated")
else
throw Error("The actor $actor already exists in the game")
}
else {
actorContainerInactive.remove(actor)
actorContainer.add(actor)
insertionSortLastElem(actorContainer) // we can do this as we are only adding single actor
if (actor is ActorVisible) {
when (actor.renderOrder) {
ActorOrder.BEHIND -> {
actorsRenderBehind.add(actor); insertionSortLastElemAV(actorsRenderBehind)
}
ActorOrder.MIDDLE -> {
actorsRenderMiddle.add(actor); insertionSortLastElemAV(actorsRenderMiddle)
}
ActorOrder.MIDTOP -> {
actorsRenderMidTop.add(actor); insertionSortLastElemAV(actorsRenderMidTop)
}
ActorOrder.FRONT -> {
actorsRenderFront.add(actor); insertionSortLastElemAV(actorsRenderFront)
}
}
}
}
}
fun addParticle(particle: ParticleBase) { fun addParticle(particle: ParticleBase) {
particlesContainer.add(particle) particlesContainer.add(particle)
} }

View File

@@ -31,7 +31,7 @@ internal object SpawnPhysTestBall : ConsoleCommand {
ball.elasticity = elasticity ball.elasticity = elasticity
ball.applyForce(Vector2(xvel, yvel)) ball.applyForce(Vector2(xvel, yvel))
Terrarum.ingame.addActor(ball) Terrarum.ingame.addNewActor(ball)
} }
else if (args.size == 2) { else if (args.size == 2) {
val elasticity = args[1].toDouble() val elasticity = args[1].toDouble()
@@ -43,7 +43,7 @@ internal object SpawnPhysTestBall : ConsoleCommand {
) )
ball.elasticity = elasticity ball.elasticity = elasticity
Terrarum.ingame.addActor(ball) Terrarum.ingame.addNewActor(ball)
} }
else { else {
printUsage() printUsage()

View File

@@ -16,7 +16,7 @@ object SpawnTapestry : ConsoleCommand {
} }
val tapestry = DecodeTapestry(File(args[1])) val tapestry = DecodeTapestry(File(args[1]))
Terrarum.ingame.addActor(tapestry) Terrarum.ingame.addNewActor(tapestry)
} }
override fun printUsage() { override fun printUsage() {

View File

@@ -13,7 +13,7 @@ object SpawnTikiTorch : ConsoleCommand {
val torch = FixtureTikiTorch() val torch = FixtureTikiTorch()
torch.setPosition(Terrarum.appgc.mouseX, Terrarum.appgc.mouseY) torch.setPosition(Terrarum.appgc.mouseX, Terrarum.appgc.mouseY)
Terrarum.ingame.addActor(torch) Terrarum.ingame.addNewActor(torch)
} }
override fun printUsage() { override fun printUsage() {