From 50f6329578e1f901e098b13714b5fb04fb291830 Mon Sep 17 00:00:00 2001 From: Evan Debenham Date: Fri, 26 Aug 2016 20:08:00 -0400 Subject: [PATCH] v0.4.2: changed sprite movement to use a wait/notify structure --- .../shatteredpixeldungeon/actors/Actor.java | 21 +++++++++++++------ .../sprites/CharSprite.java | 6 +++++- .../sprites/TenguSprite.java | 3 +++ 3 files changed, 23 insertions(+), 7 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 c1a3fdb75..ffcdaf8a0 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/Actor.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/Actor.java @@ -22,6 +22,7 @@ package com.shatteredpixel.shatteredpixeldungeon.actors; import android.util.SparseArray; import com.shatteredpixel.shatteredpixeldungeon.Dungeon; +import com.shatteredpixel.shatteredpixeldungeon.ShatteredPixelDungeon; import com.shatteredpixel.shatteredpixeldungeon.Statistics; import com.shatteredpixel.shatteredpixeldungeon.actors.blobs.Blob; import com.shatteredpixel.shatteredpixeldungeon.actors.buffs.Buff; @@ -96,7 +97,8 @@ public abstract class Actor implements Bundlable { private static HashSet all = new HashSet<>(); private static HashSet chars = new HashSet<>(); - private static Actor current; + private static volatile Actor current; + private static volatile boolean processing; private static SparseArray ids = new SparseArray<>(); @@ -195,15 +197,22 @@ public abstract class Actor implements Bundlable { if (current != null) { - if (current instanceof Char && - ((Char) current).sprite != null && ((Char)current).sprite.isMoving) { + Actor acting = current; + + if (acting instanceof Char && + ((Char) acting).sprite != null && ((Char)acting).sprite.isMoving) { // If it's character's turn to act, but its sprite // is moving, wait till the movement is over - current = null; - break; + try { + synchronized (((Char)acting).sprite) { + ((Char) acting).sprite.wait(); + } + } catch (InterruptedException e) { + ShatteredPixelDungeon.reportException(e); + } } - doNext = current.act(); + doNext = acting.act(); if (doNext && !Dungeon.hero.isAlive()) { doNext = false; current = null; diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/sprites/CharSprite.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/sprites/CharSprite.java index 55d83c3f1..26db8c256 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/sprites/CharSprite.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/sprites/CharSprite.java @@ -98,7 +98,8 @@ public class CharSprite extends MovieClip implements Tweener.Listener, MovieClip public Char ch; - public boolean isMoving = false; + //used to prevent the actor associated with this sprite from acting until movement completes + public volatile boolean isMoving = false; public CharSprite() { super(); @@ -453,6 +454,9 @@ public class CharSprite extends MovieClip implements Tweener.Listener, MovieClip } else if (tweener == motion) { isMoving = false; + synchronized (this){ + notify(); + } motion.killAndErase(); motion = null; diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/sprites/TenguSprite.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/sprites/TenguSprite.java index b016d64a8..eb455a25a 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/sprites/TenguSprite.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/sprites/TenguSprite.java @@ -103,6 +103,9 @@ public class TenguSprite extends MobSprite { public void onComplete( Animation anim ) { if (anim == run) { isMoving = false; + synchronized (this){ + notify(); + } idle(); } else { super.onComplete( anim );