v0.4.3b: game logic thread now uses wait/notify

This commit is contained in:
Evan Debenham 2016-11-02 18:49:04 -04:00
parent 851060251e
commit 44e91ee771
2 changed files with 28 additions and 13 deletions

View File

@ -221,8 +221,18 @@ public abstract class Actor implements Bundlable {
} else { } else {
doNext = false; doNext = false;
} }
} while (doNext); if (!doNext){
synchronized (Thread.currentThread()) {
try {
Thread.currentThread().wait();
} catch (InterruptedException e) {
ShatteredPixelDungeon.reportException(e);
}
}
}
} while (true);
} }
public static void add( Actor actor ) { public static void add( Actor actor ) {

View File

@ -155,7 +155,7 @@ public class GameScene extends PixelScene {
super.create(); super.create();
Camera.main.zoom( GameMath.gate(minZoom, defaultZoom + ShatteredPixelDungeon.zoom(), maxZoom)); Camera.main.zoom( GameMath.gate(minZoom, defaultZoom + ShatteredPixelDungeon.zoom(), maxZoom));
scene = this; scene = this;
terrain = new Group(); terrain = new Group();
@ -402,7 +402,12 @@ public class GameScene extends PixelScene {
} }
} }
private Thread t; private final Thread t = new Thread() {
@Override
public void run() {
Actor.process();
}
};
@Override @Override
public synchronized void update() { public synchronized void update() {
@ -414,16 +419,16 @@ public class GameScene extends PixelScene {
if (!freezeEmitters) water.offset( 0, -5 * Game.elapsed ); if (!freezeEmitters) water.offset( 0, -5 * Game.elapsed );
if (!Actor.processing() && (t == null || !t.isAlive()) && Dungeon.hero.isAlive()) { if (!Actor.processing() && Dungeon.hero.isAlive()) {
t = new Thread() { if (!t.isAlive()) {
@Override //if cpu time is limited, game should prefer drawing the current frame
public void run() { t.setPriority(Thread.NORM_PRIORITY - 1);
Actor.process(); t.start();
} else {
synchronized (t) {
t.notify();
} }
}; }
//if cpu time is limited, game should prefer drawing the current frame
t.setPriority(Thread.NORM_PRIORITY-1);
t.start();
} }
if (Dungeon.hero.ready && Dungeon.hero.paralysed == 0) { if (Dungeon.hero.ready && Dungeon.hero.paralysed == 0) {