TrafficSim/app/src/main/java/de/hems/trafficsim/Worker.java
2020-11-15 16:30:41 +01:00

93 lines
2.2 KiB
Java

package de.hems.trafficsim;
import android.graphics.Canvas;
/**
* Calculation thread, which runs the simulation itself and also updates the view with a set rate.
*/
public class Worker extends Thread {
/**
* the track to simulate
*/
protected Track track;
/**
* Stop flag, indicating that the worker shall stop
*/
protected boolean stop;
/**
* the MainActivity of the gui
*/
protected MainActivity gui;
/**
* the Renderer drawing the Track
*/
protected Renderer renderer;
/**
* Amount of simulation steps between two frames in the view. Zero means
* that every simulation step is drawn.
*/
protected int frameskip;
/**
* Construct a new worker.
*
* @param track the track to simulate
* @param gui the MainActivity of the gui
* @param renderer the Renderer drawing the Track
* @param frameskip Amount of simulation steps between two frames in the view. Zero means
* that every simulation step is drawn
*/
public Worker(Track track, MainActivity gui, Renderer renderer, int frameskip) {
super();
this.track = track;
this.stop = false;
this.gui = gui;
this.renderer = renderer;
this.frameskip = frameskip;
}
/**
* Sets a flag that causes the worker to stop.
*
* @param stop flag whether the thread shall stop
*/
public void setStop(boolean stop) {
this.stop = stop;
}
/**
* Updates the amount of simulation steps between to view frames.
*
* @param frames amount of steps
*/
public void setFrameskip(int frames) {
this.frameskip = frames;
}
/**
* Method which is automatically executed when the thread is started. It alternates between
* updating the simulation and drawing a new frame in the view.
*/
@Override
public void run() {
Canvas canvas = null;
int i = 0;
while (!stop) {
this.track.timeElapse();
this.gui.updateStats();
if (i >= this.frameskip) {
this.renderer.draw();
i = 0;
}
i++;
}
}
}