public class UpdateLoop extends java.lang.Thread implements java.lang.AutoCloseable, ILoop
UpdateLoop is a basic loop implementation that performs operations at the specified tickRate
by continuously processing the registered logic and delaying the loop until the requested rate is met.| Modifier | Constructor and Description |
|---|---|
protected |
UpdateLoop(java.lang.String name,
int tickRate) |
| Modifier and Type | Method and Description |
|---|---|
void |
attach(IUpdateable updatable)
Attaches the update method of the specified IUpdatable instance to be called
every tick.
|
void |
close() |
protected double |
delay()
This method determines how long the current tick should be delayed to match the expected delta time for the specified tick rate.
|
void |
detach(IUpdateable updatable)
Detaches the specified instance from the game loop.
|
long |
getDeltaTime()
Gets the total time in milliseconds that passed since the last tick.
|
protected long |
getExpectedDelta() |
java.util.concurrent.locks.Lock |
getLock()
Returns a lock that can be used for actions that must be performed either within or independently of the loop.
|
double |
getProcessTime()
Gets the actual process time in milliseconds that was required during the last tick.
|
int |
getTickRate()
Gets the rate at which this loop performs its updates.
|
long |
getTicks()
Gets the total amount of ticks performed by this loop since it was started.
|
int |
getUpdatableCount()
Gets the amount of attached
IUpdatable instances of this loop. |
protected java.util.Set<IUpdateable> |
getUpdatables() |
protected void |
process()
Performs the actual workload of a tick.
|
void |
run()
The loop implementation, executing the
process() method which does the actual work. |
void |
setTickRate(int tickRate)
Sets the tickrate at which the loop performs its updates.
|
void |
terminate()
Terminates the operation of this instance.
|
protected void |
update()
Calls the
update() procedure on all registered instances. |
activeCount, checkAccess, clone, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yieldequals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitstartpublic void run()
process() method which does the actual work.
It also tracks the processing time and the total number of performed ticks while making sure that the expected
tick rate is met by delaying the loop accordingly.run in interface java.lang.Runnablerun in class java.lang.Threadprocess(),
delay(),
getDeltaTime(),
getProcessTime()public void terminate()
ILaunchableterminate in interface ILaunchablepublic void close()
close in interface java.lang.AutoCloseablepublic void attach(IUpdateable updatable)
ILooppublic void detach(IUpdateable updatable)
ILooppublic int getUpdatableCount()
ILoopIUpdatable instances of this loop.getUpdatableCount in interface ILooppublic long getTicks()
ILoopgetTicks in interface ILoopILaunchable.start()public int getTickRate()
ILoopgetTickRate in interface ILooppublic long getDeltaTime()
ILoopgetDeltaTime in interface ILoopILoop.getProcessTime()public double getProcessTime()
ILoopgetProcessTime in interface ILoopILoop.getDeltaTime()public void setTickRate(int tickRate)
ILoopsetTickRate in interface ILooptickRate - The tickrate of the loop.protected java.util.Set<IUpdateable> getUpdatables()
protected void process()
protected long getExpectedDelta()
protected void update()
update() procedure on all registered instances.IUpdateable.update()protected double delay()
throws java.lang.InterruptedException
java.lang.InterruptedException - If the thread was interrupted while sleeping