timer
This commit is contained in:
parent
a3f4cf8338
commit
a3282098b1
@ -137,6 +137,14 @@ fun main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (i in 0 .. 10) {
|
||||||
|
client.world!!.timer(i * 1.0, 1) {
|
||||||
|
val projEnt = Projectile(client.world!!, Starbound.projectilesAccess["pill"]!!)
|
||||||
|
projEnt.position = Vector2d(i * 2.0 - 15.0, 13.0)
|
||||||
|
projEnt.spawn()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
run {
|
run {
|
||||||
val stripes = 0
|
val stripes = 0
|
||||||
|
|
||||||
|
@ -105,6 +105,40 @@ data class WorldSweepResult(
|
|||||||
|
|
||||||
private const val EPSILON = 0.00001
|
private const val EPSILON = 0.00001
|
||||||
|
|
||||||
|
class Timer(val period: Double, val executionTimes: Int, val func: (Timer) -> Unit) {
|
||||||
|
private var counter = 0.0
|
||||||
|
var cycles = 0
|
||||||
|
private set
|
||||||
|
var destroyed: Boolean = false
|
||||||
|
private set
|
||||||
|
|
||||||
|
fun think(delta: Double) {
|
||||||
|
if (destroyed) {
|
||||||
|
throw IllegalStateException("This timer is destroyed")
|
||||||
|
}
|
||||||
|
|
||||||
|
counter += delta
|
||||||
|
|
||||||
|
if (counter >= period) {
|
||||||
|
counter -= period
|
||||||
|
func.invoke(this)
|
||||||
|
cycles++
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!destroyed && executionTimes > 0 && cycles >= executionTimes) {
|
||||||
|
destroy()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun destroy() {
|
||||||
|
if (destroyed) {
|
||||||
|
throw IllegalStateException("Already destroyed")
|
||||||
|
}
|
||||||
|
|
||||||
|
destroyed = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
abstract class World<This : World<This, ChunkType>, ChunkType : Chunk<This, ChunkType>>(val seed: Long = 0L) {
|
abstract class World<This : World<This, ChunkType>, ChunkType : Chunk<This, ChunkType>>(val seed: Long = 0L) {
|
||||||
protected val chunkMap = HashMap<ChunkPos, IMutableWorldChunkTuple<This, ChunkType>>()
|
protected val chunkMap = HashMap<ChunkPos, IMutableWorldChunkTuple<This, ChunkType>>()
|
||||||
|
|
||||||
@ -117,6 +151,14 @@ abstract class World<This : World<This, ChunkType>, ChunkType : Chunk<This, Chun
|
|||||||
|
|
||||||
val physics = B2World(Vector2d(0.0, -EARTH_FREEFALL_ACCELERATION))
|
val physics = B2World(Vector2d(0.0, -EARTH_FREEFALL_ACCELERATION))
|
||||||
|
|
||||||
|
private var timers = ArrayList<Timer>()
|
||||||
|
|
||||||
|
fun timer(period: Double, executionTimes: Int, func: (Timer) -> Unit): Timer {
|
||||||
|
val timer = Timer(period, executionTimes, func)
|
||||||
|
timers.add(timer)
|
||||||
|
return timer
|
||||||
|
}
|
||||||
|
|
||||||
init {
|
init {
|
||||||
physics.contactFilter = object : IContactFilter {
|
physics.contactFilter = object : IContactFilter {
|
||||||
override fun shouldCollide(fixtureA: B2Fixture, fixtureB: B2Fixture): Boolean {
|
override fun shouldCollide(fixtureA: B2Fixture, fixtureB: B2Fixture): Boolean {
|
||||||
@ -220,6 +262,28 @@ abstract class World<This : World<This, ChunkType>, ChunkType : Chunk<This, Chun
|
|||||||
physics.step(delta, 6, 4)
|
physics.step(delta, 6, 4)
|
||||||
|
|
||||||
timer += delta
|
timer += delta
|
||||||
|
|
||||||
|
if (timers.isNotEmpty()) {
|
||||||
|
val current = timers
|
||||||
|
timers = ArrayList()
|
||||||
|
|
||||||
|
val iterator = current.iterator()
|
||||||
|
|
||||||
|
for (timer in iterator) {
|
||||||
|
timer.think(delta)
|
||||||
|
|
||||||
|
if (timer.destroyed) {
|
||||||
|
iterator.remove()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (timers.isNotEmpty()) {
|
||||||
|
current.addAll(timers)
|
||||||
|
}
|
||||||
|
|
||||||
|
timers = current
|
||||||
|
}
|
||||||
|
|
||||||
thinkInner(delta)
|
thinkInner(delta)
|
||||||
} catch(err: Throwable) {
|
} catch(err: Throwable) {
|
||||||
throw RuntimeException("Ticking world $this", err)
|
throw RuntimeException("Ticking world $this", err)
|
||||||
|
Loading…
Reference in New Issue
Block a user