/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package puppyeyes.engine.LowerLevel;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
import puppyeyes.engine.GameWindow;
import puppyeyes.engine.Level;
import puppyeyes.engine.Settings.GameSettings;
/**
*
* @author majora
*/
public class StepProcessor implements Runnable {
ExecutorService stepPool;
ScheduledExecutorService timer;
public StepProcessor() {
stepPool = Executors.newCachedThreadPool();
timer = Executors.newSingleThreadScheduledExecutor();
}
public void start(){
timer.scheduleAtFixedRate(this, 0, 1000/GameSettings.stepsPerSecond, TimeUnit.MILLISECONDS);
}
@Override
public void run(){
// Get current level
Level onScreen = GameWindow.getLevel();
// Invoke call() on each actor
try {
stepPool.invokeAll(onScreen.getActorList());
} catch (InterruptedException e1) {
// Interrupt is the shutdown command in java, shouldn't circumvent it, the exception is here to allow cleanup
Thread.currentThread().interrupt();
}
}
/**
* Called to shutdown the threads this StepProcessor started.
*/
public void shutdown(){
stepPool.shutdown();
timer.shutdown();
}
}