From 36aa02de2b5b32fef0d437939c25a833d872099b Mon Sep 17 00:00:00 2001 From: Evan Debenham Date: Wed, 24 Aug 2016 19:11:28 -0400 Subject: [PATCH] v0.4.2: Game logic now occurs in a separate thread This is going to cause a LOT of bugs, next several commits will likely be fixing them. --- .../shatteredpixeldungeon/actors/Actor.java | 7 ++++++- .../shatteredpixeldungeon/scenes/GameScene.java | 14 +++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/Actor.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/Actor.java index 0f7e136c2..c1a3fdb75 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/Actor.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/Actor.java @@ -164,6 +164,10 @@ public abstract class Actor implements Bundlable { current = null; } } + + public static boolean processing(){ + return current != null; + } public static void process() { @@ -191,7 +195,8 @@ public abstract class Actor implements Bundlable { if (current != null) { - if (current instanceof Char && ((Char)current).sprite.isMoving) { + if (current instanceof Char && + ((Char) current).sprite != null && ((Char)current).sprite.isMoving) { // If it's character's turn to act, but its sprite // is moving, wait till the movement is over current = null; diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/scenes/GameScene.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/scenes/GameScene.java index b203e96d8..b6aed0cc9 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/scenes/GameScene.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/scenes/GameScene.java @@ -400,6 +400,8 @@ public class GameScene extends PixelScene { } } + private Thread t; + @Override public synchronized void update() { if (Dungeon.hero == null || scene == null) { @@ -410,7 +412,17 @@ public class GameScene extends PixelScene { if (!freezeEmitters) water.offset( 0, -5 * Game.elapsed ); - Actor.process(); + if (!Actor.processing() && (t == null || !t.isAlive())) { + t = new Thread() { + @Override + public void run() { + Actor.process(); + } + }; + //if cpu time is limited, game should prefer drawing the current frame + t.setPriority(Thread.MIN_PRIORITY); + t.start(); + } if (Dungeon.hero.ready && Dungeon.hero.paralysed == 0) { log.newLine();