Skip to content

Commit

Permalink
feat: move runnables to end of functions, abolish TickDuration
Browse files Browse the repository at this point in the history
  • Loading branch information
andantet committed Oct 19, 2024
1 parent ecf2aaa commit 0c7fc7a
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 75 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
kotlin.code.style=official

project_version=0.3
project_version=0.4
maven_group=net.mcbrawls

kotlin_version=1.9.20
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ abstract class AbstractScheduler : Scheduler, Schedulable {
return scheduledTask
}

override fun schedule(runnable: Runnable, delay: Duration, repeatDelay: Duration?, executionType: ExecutionType): ScheduledTask {
override fun schedule(delay: Duration, repeatDelay: Duration?, executionType: ExecutionType, runnable: Runnable): ScheduledTask {
val task = Task(runnable, executionType)
return schedule(task, delay, repeatDelay)
}
Expand Down
12 changes: 2 additions & 10 deletions src/main/kotlin/net/mcbrawls/scheduler/RelativeScheduler.kt
Original file line number Diff line number Diff line change
@@ -1,27 +1,19 @@
package net.mcbrawls.scheduler

import net.mcbrawls.scheduler.duration.TickDuration
import kotlin.time.Duration

/**
* A scheduler which performs in relation to a local tick variable.
*/
class RelativeScheduler(
/**
* The amount of time which is simulated per tick.
*/
durationPerTick: Duration = TickDuration.create(1)
) : AbstractScheduler() {
class RelativeScheduler : AbstractScheduler() {
override val currentTimeNanos: Long get() = tickNanos

private val nanosPerTick = durationPerTick.inWholeNanoseconds

private var tickNanos: Long = 0L

override fun processTick() {
super.processTick()

// increment tick
tickNanos += nanosPerTick
tickNanos += TickDuration.NANOS_PER_TICK
}
}
4 changes: 2 additions & 2 deletions src/main/kotlin/net/mcbrawls/scheduler/Schedulable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ interface Schedulable : Scheduler {
return scheduler.schedule(task, delay, repeatDelay)
}

override fun schedule(runnable: Runnable, delay: Duration, repeatDelay: Duration?, executionType: ExecutionType): ScheduledTask {
return scheduler.schedule(runnable, delay, repeatDelay, executionType)
override fun schedule(delay: Duration, repeatDelay: Duration?, executionType: ExecutionType, runnable: Runnable): ScheduledTask {
return scheduler.schedule(delay, repeatDelay, executionType, runnable)
}

override fun cancel(task: ScheduledTask): Boolean {
Expand Down
11 changes: 6 additions & 5 deletions src/main/kotlin/net/mcbrawls/scheduler/Scheduler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ interface Scheduler {
* @return the scheduled task
*/
fun schedule(
/**
* The function of the task.
*/
runnable: Runnable,

/**
* The delay until the task is performed.
Expand All @@ -43,7 +39,12 @@ interface Scheduler {
/**
* How the task should be executed.
*/
executionType: ExecutionType = ExecutionType.SYNC
executionType: ExecutionType = ExecutionType.SYNC,

/**
* The function of the task.
*/
runnable: Runnable,
): ScheduledTask

/**
Expand Down
53 changes: 7 additions & 46 deletions src/main/kotlin/net/mcbrawls/scheduler/duration/TickDuration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,55 +7,16 @@ import kotlin.time.Duration.Companion.nanoseconds
* Game tick duration utilities.
*/
object TickDuration {
/**
* Creates a duration from ticks and ticks per second.
* @return a duration
*/
fun create(
/**
* The amount of ticks within the duration.
*/
ticks: Long,

/**
* The amount of ticks which comprise a second.
*/
ticksPerSecond: Long = 20
): Duration {
val nanosPerTick = 1_000_000_000L / ticksPerSecond
return (nanosPerTick * ticks).nanoseconds
}
const val NANOS_PER_TICK = 1_000_000_000L / 20

/**
* Calculates the ticks of the given duration.
* @return a quantity of ticks
* A tick duration for a single tick.
*/
fun getTicks(
/**
* The duration to get timings from.
*/
duration: Duration,
val NEXT_TICK = NANOS_PER_TICK.nanoseconds

/**
* The amount of ticks which comprise a second.
*/
ticksPerSecond: Long = 20
): Long {
val nanos = duration.inWholeNanoseconds
val nanosPerTick = 1_000_000_000L / ticksPerSecond
return nanos / nanosPerTick
}
inline val Int.ticks get() = toLong().ticks
inline val Long.ticks get() = (this * NANOS_PER_TICK).nanoseconds
inline val Double.ticks get() = toLong().ticks

/**
* Creates a tick duration for a single tick.
* @return a duration
*/
fun nextTick(
/**
* The amount of ticks which comprise a second.
*/
ticksPerSecond: Long = 20
): Duration {
return create(1, ticksPerSecond)
}
inline val Duration.inWholeTicks get() = inWholeNanoseconds / NANOS_PER_TICK
}
21 changes: 11 additions & 10 deletions src/test/kotlin/net/mcbrawls/scheduler/test/SchedulerTest.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package net.mcbrawls.scheduler.test

import net.mcbrawls.scheduler.AbsoluteScheduler
import net.mcbrawls.scheduler.duration.TickDuration
import net.mcbrawls.scheduler.duration.TickDuration.inWholeTicks
import net.mcbrawls.scheduler.duration.TickDuration.ticks
import kotlin.math.abs
import kotlin.math.min
import kotlin.test.Test
Expand All @@ -22,13 +23,13 @@ object SchedulerTest {
var prescheduleTimeMs = milliTime
println("Scheduling pass")
scheduler.schedule(
{
passes++
println("Pass #$passes after ${(milliTime - prescheduleTimeMs) / 1000.0}s")
prescheduleTimeMs = milliTime
},
TickDuration.create(60), TickDuration.create(20)
)
60.ticks,
20.ticks,
) {
passes++
println("Pass #$passes after ${(milliTime - prescheduleTimeMs) / 1000.0}s")
prescheduleTimeMs = milliTime
}

// wait for schedule and tick
val timeMs = milliTime
Expand Down Expand Up @@ -64,7 +65,7 @@ object SchedulerTest {
@Test
fun tickDurationConversion() {
val expected = 50L
val tickDuration = TickDuration.create(expected)
assertEquals(TickDuration.getTicks(tickDuration), expected)
val tickDuration = expected.ticks
assertEquals(tickDuration.inWholeTicks, expected)
}
}

0 comments on commit 0c7fc7a

Please sign in to comment.