Skip to content

Commit

Permalink
fix iterator previous non-loop version
Browse files Browse the repository at this point in the history
  • Loading branch information
Quillraven committed Dec 23, 2024
1 parent 5ed4de6 commit 02cc173
Showing 1 changed file with 6 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.quillraven.fleks.collection

import com.github.quillraven.fleks.Entity
import kotlin.math.max

/**
* Creates an [EntityBagIterator] for the bag. If the bag gets updated
Expand Down Expand Up @@ -49,7 +50,11 @@ data class EntityBagIterator(private val bag: EntityBag) {
fun previous(loop: Boolean = false): Entity = when {
hasPrevious() -> {
// if iterator was at the end of the bag then return the second to last element instead
currentIdx = if (currentIdx == bag.size) bag.size - 2 else currentIdx - 1
currentIdx = if (currentIdx == bag.size) {
max(0, bag.size - 2)
} else {
currentIdx - 1
}
bag[currentIdx]
}

Expand Down

0 comments on commit 02cc173

Please sign in to comment.