v2.2.0: fixed race condition errors in sortMobSprites

This commit is contained in:
Evan Debenham
2023-09-11 10:17:24 -04:00
parent 061d59c61b
commit 121763f7bb
@@ -881,7 +881,7 @@ public class GameScene extends PixelScene {
} }
} }
private void addMobSprite( Mob mob ) { private synchronized void addMobSprite( Mob mob ) {
CharSprite sprite = mob.sprite(); CharSprite sprite = mob.sprite();
sprite.visible = Dungeon.level.heroFOV[mob.pos]; sprite.visible = Dungeon.level.heroFOV[mob.pos];
mobs.add( sprite ); mobs.add( sprite );
@@ -889,20 +889,23 @@ public class GameScene extends PixelScene {
sortMobSprites(); sortMobSprites();
} }
//ensures that mob sprites are drawn in the correct order, in case of overlap //ensures that mob sprites are drawn from top to bottom, in case of overlap
public static void sortMobSprites(){ public static void sortMobSprites(){
if (scene != null){ if (scene != null){
synchronized (scene) {
scene.mobs.sort(new Comparator() { scene.mobs.sort(new Comparator() {
@Override @Override
public int compare(Object a, Object b) { public int compare(Object a, Object b) {
if (a instanceof CharSprite && b instanceof CharSprite){ if (a instanceof CharSprite && b instanceof CharSprite) {
return ((CharSprite) a).ch.pos - ((CharSprite) b).ch.pos; return (int) Math.signum((((CharSprite) a).y + ((CharSprite) a).height())
- (((CharSprite) b).y + ((CharSprite) b).height()));
} }
return 0; return 0;
} }
}); });
} }
} }
}
private synchronized void prompt( String text ) { private synchronized void prompt( String text ) {