v0.3.0c: refactored finding a character, no longer need to maintain chars[] array

This commit is contained in:
Evan Debenham 2015-06-06 00:17:07 -04:00
parent 3cf7979a4c
commit dbeff25d2c
12 changed files with 9 additions and 46 deletions

View File

@ -102,13 +102,10 @@ public abstract class Actor implements Bundlable {
private static float now = 0;
private static Char[] chars = new Char[Level.LENGTH];
public static void clear() {
now = 0;
Arrays.fill( chars, null );
all.clear();
ids.clear();
@ -160,14 +157,6 @@ public abstract class Actor implements Bundlable {
public static void resetNextID(){
nextID = 1;
}
public static void occupyCell( Char ch ) {
chars[ch.pos] = ch;
}
public static void freeCell( int pos ) {
chars[pos] = null;
}
/*protected*/public void next() {
if (current == this) {
@ -186,8 +175,7 @@ public abstract class Actor implements Bundlable {
do {
now = Float.MAX_VALUE;
current = null;
Arrays.fill( chars, null );
for (Actor actor : all) {
@ -197,11 +185,7 @@ public abstract class Actor implements Bundlable {
now = actor.time;
current = actor;
}
if (actor instanceof Char) {
Char ch = (Char)actor;
chars[ch.pos] = ch;
}
}
if (current != null) {
@ -247,7 +231,6 @@ public abstract class Actor implements Bundlable {
if (actor instanceof Char) {
Char ch = (Char)actor;
chars[ch.pos] = ch;
for (Buff buff : ch.buffs()) {
all.add( buff );
buff.onAdd();
@ -268,7 +251,11 @@ public abstract class Actor implements Bundlable {
}
public static Char findChar( int pos ) {
return chars[pos];
for (Actor actor : all){
if (actor instanceof Char && ((Char)actor).pos == pos)
return (Char)actor;
}
return null;
}
public static Actor findById( int id ) {

View File

@ -279,7 +279,6 @@ public abstract class Char extends Actor {
public void destroy() {
HP = 0;
Actor.remove( this );
Actor.freeCell( pos );
}
public void die( Object src ) {
@ -404,10 +403,7 @@ public abstract class Char extends Actor {
Door.leave( pos );
}
Actor.freeCell(pos);
pos = step;
Actor.occupyCell( this );
if (flying && Dungeon.level.map[pos] == Terrain.DOOR) {
Door.enter( pos );

View File

@ -181,8 +181,6 @@ public class Ghost extends NPC {
}
if (newPos != -1) {
Actor.freeCell(pos);
CellEmitter.get(pos).start(Speck.factory(Speck.LIGHT), 0.2f, 3);
pos = newPos;
sprite.place(pos);
@ -317,7 +315,6 @@ public class Ghost extends NPC {
ghost.pos = level.randomRespawnCell();
} while (ghost.pos == -1);
level.mobs.add( ghost );
Actor.occupyCell( ghost );
spawned = true;
//dungeon depth determines type of quest.

View File

@ -217,9 +217,8 @@ public class Imp extends NPC {
npc.pos = level.randomRespawnCell();
} while (npc.pos == -1 || level.heaps.get( npc.pos ) != null);
level.mobs.add( npc );
Actor.occupyCell( npc );
spawned = true;
spawned = true;
alternative = Random.Int( 2 ) == 0;
given = false;

View File

@ -214,7 +214,6 @@ public class Wandmaker extends NPC {
npc.pos = room.random();
} while (level.map[npc.pos] == Terrain.ENTRANCE || level.map[npc.pos] == Terrain.SIGN);
level.mobs.add( npc );
Actor.occupyCell( npc );
spawned = true;
alternative = Random.Int( 2 ) == 0;

View File

@ -271,7 +271,6 @@ public class CursedWand {
sheep.pos = ch.pos;
ch.sprite.killAndErase();
Actor.remove(ch);
Actor.freeCell(ch.pos);
Dungeon.level.mobs.remove(ch);
HealthIndicator.instance.target(null);
GameScene.add(sheep);

View File

@ -591,7 +591,6 @@ public abstract class RegularLevel extends Level {
if (Actor.findChar(mob.pos) == null && Level.passable[mob.pos]) {
mobsToSpawn--;
mobs.add(mob);
Actor.occupyCell(mob);
//TODO: perhaps externalize this logic into a method. Do I want to make mobs more likely to clump deeper down?
if (mobsToSpawn > 0 && Random.Int(4) == 0){
@ -601,7 +600,6 @@ public abstract class RegularLevel extends Level {
if (Actor.findChar(mob.pos) == null && Level.passable[mob.pos]) {
mobsToSpawn--;
mobs.add(mob);
Actor.occupyCell(mob);
}
}
}

View File

@ -63,6 +63,5 @@ public class BlacksmithPainter extends Painter {
npc.pos = room.random( 1 );
} while (level.heaps.get( npc.pos ) != null);
level.mobs.add( npc );
Actor.occupyCell( npc );
}
}

View File

@ -77,7 +77,6 @@ public class PoolPainter extends Painter {
piranha.pos = room.random();
} while (level.map[piranha.pos] != Terrain.WATER|| Actor.findChar( piranha.pos ) != null);
level.mobs.add( piranha );
Actor.occupyCell( piranha );
}
}

View File

@ -67,6 +67,5 @@ public class StatuePainter extends Painter {
Statue statue = new Statue();
statue.pos = cx + cy * Level.WIDTH;
level.mobs.add( statue );
Actor.occupyCell( statue );
}
}

View File

@ -48,12 +48,6 @@ public class SummoningTrap extends Trap {
return;
}
Char c = Actor.findChar( pos );
if (c != null) {
Actor.occupyCell( c );
}
int nMobs = 1;
if (Random.Int( 2 ) == 0) {
nMobs++;
@ -79,7 +73,6 @@ public class SummoningTrap extends Trap {
int index = Random.index( candidates );
DUMMY.pos = candidates.get( index );
Actor.occupyCell( DUMMY );
respawnPoints.add( candidates.remove( index ) );
nMobs--;

View File

@ -546,14 +546,12 @@ public class GameScene extends PixelScene {
public static void add( Mob mob ) {
Dungeon.level.mobs.add( mob );
Actor.add( mob );
Actor.occupyCell( mob );
scene.addMobSprite( mob );
}
public static void add( Mob mob, float delay ) {
Dungeon.level.mobs.add( mob );
Actor.addDelayed( mob, delay );
Actor.occupyCell( mob );
scene.addMobSprite( mob );
}