refactor: make TileSpec do its own reference resolving

git-svn-id: https://micropolis.googlecode.com/svn/trunk/micropolis-java@905 d9718cc8-9f43-0410-858b-315f434eb58c
This commit is contained in:
jason@long.name 2013-12-14 22:10:21 +00:00
parent 447a78b4d7
commit ae13414b34
2 changed files with 63 additions and 30 deletions

View file

@ -284,4 +284,65 @@ public class TileSpec
{
return "{tile#"+tileNumber+"}";
}
void resolveReferences(Map<String,TileSpec> tileMap)
{
String tmp = this.getAttribute("becomes");
if (tmp != null) {
this.animNext = tileMap.get(tmp);
}
tmp = this.getAttribute("onpower");
if (tmp != null) {
this.onPower = tileMap.get(tmp);
}
tmp = this.getAttribute("onshutdown");
if (tmp != null) {
this.onShutdown = tileMap.get(tmp);
}
tmp = this.getAttribute("building-part");
if (tmp != null) {
this.handleBuildingPart(tmp, tileMap);
}
}
private void handleBuildingPart(String text, Map<String,TileSpec> tileMap)
{
String [] parts = text.split(",");
if (parts.length != 3) {
throw new Error("Invalid building-part specification");
}
this.owner = tileMap.get(parts[0]);
this.ownerOffsetX = Integer.parseInt(parts[1]);
this.ownerOffsetY = Integer.parseInt(parts[2]);
assert this.owner != null;
assert this.ownerOffsetX != 0 || this.ownerOffsetY != 0;
}
public static String [] generateTileNames(Properties recipe)
{
int ntiles = recipe.size();
String [] tileNames = new String[ntiles];
ntiles = 0;
for (int i = 0; recipe.containsKey(Integer.toString(i)); i++) {
tileNames[ntiles++] = Integer.toString(i);
}
int naturalNumberTiles = ntiles;
for (Object n_obj : recipe.keySet()) {
String n = (String)n_obj;
if (n.matches("^\\d+$")) {
int x = Integer.parseInt(n);
if (x >= 0 && x < naturalNumberTiles) {
assert tileNames[x].equals(n);
continue;
}
}
assert ntiles < tileNames.length;
tileNames[ntiles++] = n;
}
assert ntiles == tileNames.length;
return tileNames;
}
}

View file

@ -54,22 +54,8 @@ public class Tiles
tiles = tilesList.toArray(new TileSpec[0]);
for (int i = 0; i < tiles.length; i++) {
String tmp = tiles[i].getAttribute("becomes");
if (tmp != null) {
tiles[i].animNext = get(Integer.parseInt(tmp));
}
tmp = tiles[i].getAttribute("onpower");
if (tmp != null) {
tiles[i].onPower = get(Integer.parseInt(tmp));
}
tmp = tiles[i].getAttribute("onshutdown");
if (tmp != null) {
tiles[i].onShutdown = get(Integer.parseInt(tmp));
}
tmp = tiles[i].getAttribute("building-part");
if (tmp != null) {
handleBuildingPart(tiles[i], tmp);
}
tiles[i].resolveReferences(tilesByName);
TileSpec.BuildingInfo bi = tiles[i].getBuildingInfo();
if (bi != null) {
for (int j = 0; j < bi.members.length; j++) {
@ -90,20 +76,6 @@ public class Tiles
}
}
private static void handleBuildingPart(TileSpec partTile, String tmp)
{
String [] parts = tmp.split(",");
if (parts.length != 3) {
throw new Error("Invalid building-part specification");
}
partTile.owner = get(Integer.parseInt(parts[0]));
partTile.ownerOffsetX = Integer.parseInt(parts[1]);
partTile.ownerOffsetY = Integer.parseInt(parts[2]);
assert partTile.ownerOffsetX != 0 || partTile.ownerOffsetY != 0;
}
public static TileSpec get(int tileNumber)
{
if (tileNumber >= 0 && tileNumber < tiles.length) {