This commit is contained in:
DBotThePony 2022-02-21 17:56:57 +07:00
parent a3f4cf8338
commit a3282098b1
Signed by: DBot
GPG Key ID: DCC23B5715498507
2 changed files with 72 additions and 0 deletions

View File

@ -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 {
val stripes = 0

View File

@ -105,6 +105,40 @@ data class WorldSweepResult(
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) {
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))
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 {
physics.contactFilter = object : IContactFilter {
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)
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)
} catch(err: Throwable) {
throw RuntimeException("Ticking world $this", err)