pl/mbogusz3/invaders/controller/InvadersController.java
/**
	 * Start inner timer thread, executing calculations every tick
	 */
Start inner timer thread, executing calculations every tick
Start inner timer thread executing calculations every tick
private void startTimer() {
		System.out.println("Controller: starting timer");
		Runnable tickRateRunnable = () -> {
			long previousTime = System.nanoTime();
			long newTime;
			long nextTime = previousTime + 1000000000 / tickRate;
			long millisDelay = 1000/tickRate;
			InvadersController.this.running = true;
			while (InvadersController.this.running) {
				try {
					Thread.sleep(millisDelay);
				} catch (InterruptedException e) {
					System.out.println("Failed to sleep server timer thread: " + e);
				}
				newTime = System.nanoTime();
				InvadersController.this.frameTime = (double)(newTime - previousTime) / 1000000000.0;
				previousTime = newTime;
				nextTime = previousTime + 1000000000 / tickRate;
				synchronized (InvadersController.this.model) {
					InvadersController.this.model.recalculate(frameTime);
				}
				if(InvadersController.this.model.isGameOver() || InvadersController.this.model.isGameWon()) {
					InvadersController.this.stopTimer();
				}
				long delay = (nextTime - System.nanoTime());
				if (delay < 0) {
					delay = 0;
				}
				delay += 500000;
				millisDelay = delay / 1000000;
			}
		};
		this.tickRateThread = new Thread(tickRateRunnable);
		this.tickRateThread.start();
	}
private void startTimer ( ) { System . out . println ( "Controller: starting timer" ) ; Runnable tickRateRunnable = ( ) -> { long previousTime = System . nanoTime ( ) ; long newTime ; long nextTime = previousTime + 1000000000 / tickRate ; long millisDelay = 1000 / tickRate ; InvadersController . this . running = true ; while ( InvadersController . this . running ) { try { Thread . sleep ( millisDelay ) ; } catch ( InterruptedException e ) { System . out . println ( "Failed to sleep server timer thread: " + e ) ; } newTime = System . nanoTime ( ) ; InvadersController . this . frameTime = ( double ) ( newTime - previousTime ) / 1000000000.0 ; previousTime = newTime ; nextTime = previousTime + 1000000000 / tickRate ; synchronized ( InvadersController . this . model ) { InvadersController . this . model . recalculate ( frameTime ) ; } if ( InvadersController . this . model . isGameOver ( ) || InvadersController . this . model . isGameWon ( ) ) { InvadersController . this . stopTimer ( ) ; } long delay = ( nextTime - System . nanoTime ( ) ) ; if ( delay < 0 ) { delay = 0 ; } delay += 500000 ; millisDelay = delay / 1000000 ; } } ; this . tickRateThread = new Thread ( tickRateRunnable ) ; this . tickRateThread . start ( ) ; }
Start inner timer thread, executing calculations every tick
Start inner timer thread executing calculations every tick
// Check game over or won