You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.2KB

  1. package de.hems.trafficsim;
  2. import android.graphics.Canvas;
  3. /**
  4. * Calculation thread, which runs the simulation itself and also updates the view with a set rate.
  5. */
  6. public class Worker extends Thread {
  7. /**
  8. * the track to simulate
  9. */
  10. protected Track track;
  11. /**
  12. * Stop flag, indicating that the worker shall stop
  13. */
  14. protected boolean stop;
  15. /**
  16. * the MainActivity of the gui
  17. */
  18. protected MainActivity gui;
  19. /**
  20. * the Renderer drawing the Track
  21. */
  22. protected Renderer renderer;
  23. /**
  24. * Amount of simulation steps between two frames in the view. Zero means
  25. * that every simulation step is drawn.
  26. */
  27. protected int frameskip;
  28. /**
  29. * Construct a new worker.
  30. *
  31. * @param track the track to simulate
  32. * @param gui the MainActivity of the gui
  33. * @param renderer the Renderer drawing the Track
  34. * @param frameskip Amount of simulation steps between two frames in the view. Zero means
  35. * that every simulation step is drawn
  36. */
  37. public Worker(Track track, MainActivity gui, Renderer renderer, int frameskip) {
  38. super();
  39. this.track = track;
  40. this.stop = false;
  41. this.gui = gui;
  42. this.renderer = renderer;
  43. this.frameskip = frameskip;
  44. }
  45. /**
  46. * Sets a flag that causes the worker to stop.
  47. *
  48. * @param stop flag whether the thread shall stop
  49. */
  50. public void setStop(boolean stop) {
  51. this.stop = stop;
  52. }
  53. /**
  54. * Updates the amount of simulation steps between to view frames.
  55. *
  56. * @param frames amount of steps
  57. */
  58. public void setFrameskip(int frames) {
  59. this.frameskip = frames;
  60. }
  61. /**
  62. * Method which is automatically executed when the thread is started. It alternates between
  63. * updating the simulation and drawing a new frame in the view.
  64. */
  65. @Override
  66. public void run() {
  67. Canvas canvas = null;
  68. int i = 0;
  69. while (!stop) {
  70. this.track.timeElapse();
  71. this.gui.updateStats();
  72. if (i >= this.frameskip) {
  73. this.renderer.draw();
  74. i = 0;
  75. }
  76. i++;
  77. }
  78. }
  79. }