v0.4.2: returned to wait/notify with race condition fixes
thread.sleep has a bigger performance impact than anticipated, plus I figured out how to fix this while staying fairly clean.
This commit is contained in:
parent
8dc0d56eb0
commit
1293607b42
|
@ -199,16 +199,14 @@ public abstract class Actor implements Bundlable {
|
|||
|
||||
Actor acting = current;
|
||||
|
||||
if (acting instanceof Char &&
|
||||
((Char) acting).sprite != null && ((Char)acting).sprite.isMoving) {
|
||||
if (acting instanceof Char && ((Char) acting).sprite != null) {
|
||||
// If it's character's turn to act, but its sprite
|
||||
// is moving, wait till the movement is over
|
||||
try {
|
||||
//yes, we're busy-waiting. This is insignificantly slower than using
|
||||
//a lock/semaphore but results in more readable code.
|
||||
while (((Char)acting).sprite.isMoving) {
|
||||
//tries every ~0.1 milliseconds
|
||||
Thread.sleep(0, 100000);
|
||||
synchronized (((Char)acting).sprite) {
|
||||
if (((Char)acting).sprite.isMoving) {
|
||||
((Char) acting).sprite.wait();
|
||||
}
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
ShatteredPixelDungeon.reportException(e);
|
||||
|
|
|
@ -453,11 +453,16 @@ public class CharSprite extends MovieClip implements Tweener.Listener, MovieClip
|
|||
|
||||
} else if (tweener == motion) {
|
||||
|
||||
motion.killAndErase();
|
||||
motion = null;
|
||||
ch.onMotionComplete();
|
||||
synchronized (this) {
|
||||
isMoving = false;
|
||||
|
||||
motion.killAndErase();
|
||||
motion = null;
|
||||
ch.onMotionComplete();
|
||||
|
||||
notifyAll();
|
||||
}
|
||||
|
||||
isMoving = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -102,8 +102,12 @@ public class TenguSprite extends MobSprite {
|
|||
@Override
|
||||
public void onComplete( Animation anim ) {
|
||||
if (anim == run) {
|
||||
idle();
|
||||
isMoving = false;
|
||||
synchronized (this){
|
||||
isMoving = false;
|
||||
idle();
|
||||
|
||||
notifyAll();
|
||||
}
|
||||
} else {
|
||||
super.onComplete( anim );
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user