2013-09-28 20:23:22 +00:00
|
|
|
// This file is part of MicropolisJ.
|
|
|
|
// Copyright (C) 2013 Jason Long
|
|
|
|
// Portions Copyright (C) 1989-2007 Electronic Arts Inc.
|
|
|
|
//
|
|
|
|
// MicropolisJ is free software; you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU GPLv3, with additional terms.
|
|
|
|
// See the README file, included in this distribution, for details.
|
|
|
|
|
2013-07-17 20:11:54 +00:00
|
|
|
package micropolisj.engine;
|
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
import java.nio.charset.Charset;
|
|
|
|
import java.util.*;
|
|
|
|
|
2014-03-05 01:58:31 +00:00
|
|
|
/**
|
|
|
|
* Provides global methods for loading tile specifications.
|
|
|
|
*/
|
2013-07-17 20:11:54 +00:00
|
|
|
public class Tiles
|
|
|
|
{
|
|
|
|
static final Charset UTF8 = Charset.forName("UTF-8");
|
2013-12-14 22:10:06 +00:00
|
|
|
static TileSpec [] tiles;
|
2013-12-14 22:10:14 +00:00
|
|
|
static Map<String,TileSpec> tilesByName = new HashMap<String,TileSpec>();
|
2013-07-17 20:11:54 +00:00
|
|
|
static {
|
|
|
|
try {
|
|
|
|
readTiles();
|
2013-09-01 00:26:29 +00:00
|
|
|
checkTiles();
|
2013-07-17 20:11:54 +00:00
|
|
|
}
|
|
|
|
catch (IOException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void readTiles()
|
|
|
|
throws IOException
|
|
|
|
{
|
2013-12-14 22:10:06 +00:00
|
|
|
ArrayList<TileSpec> tilesList = new ArrayList<TileSpec>();
|
2013-07-17 20:11:54 +00:00
|
|
|
|
|
|
|
Properties tilesRc = new Properties();
|
|
|
|
tilesRc.load(
|
|
|
|
new InputStreamReader(
|
|
|
|
Tiles.class.getResourceAsStream("/tiles.rc"),
|
|
|
|
UTF8
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2013-12-14 17:06:33 -05:00
|
|
|
String [] tileNames = TileSpec.generateTileNames(tilesRc);
|
|
|
|
tiles = new TileSpec[tileNames.length];
|
|
|
|
|
|
|
|
for (int i = 0; i < tileNames.length; i++) {
|
|
|
|
String tileName = tileNames[i];
|
2013-07-17 20:11:54 +00:00
|
|
|
String rawSpec = tilesRc.getProperty(tileName);
|
|
|
|
if (rawSpec == null) {
|
2013-12-14 22:10:06 +00:00
|
|
|
break;
|
2013-07-17 20:11:54 +00:00
|
|
|
}
|
|
|
|
|
2013-12-14 17:06:33 -05:00
|
|
|
TileSpec ts = TileSpec.parse(i, tileName, rawSpec, tilesRc);
|
2013-12-14 22:10:14 +00:00
|
|
|
tilesByName.put(tileName, ts);
|
2013-12-14 17:06:33 -05:00
|
|
|
tiles[i] = ts;
|
2013-07-17 20:11:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < tiles.length; i++) {
|
2013-12-14 22:10:21 +00:00
|
|
|
tiles[i].resolveReferences(tilesByName);
|
|
|
|
|
2013-09-01 00:25:39 +00:00
|
|
|
TileSpec.BuildingInfo bi = tiles[i].getBuildingInfo();
|
|
|
|
if (bi != null) {
|
|
|
|
for (int j = 0; j < bi.members.length; j++) {
|
2014-06-21 21:04:07 -04:00
|
|
|
TileSpec memberTile = bi.members[j];
|
2013-09-01 00:26:16 +00:00
|
|
|
int offx = (bi.width >= 3 ? -1 : 0) + j % bi.width;
|
|
|
|
int offy = (bi.height >= 3 ? -1 : 0) + j / bi.width;
|
|
|
|
|
2014-06-21 21:04:07 -04:00
|
|
|
if (memberTile.owner == null &&
|
2013-09-01 00:26:16 +00:00
|
|
|
(offx != 0 || offy != 0)
|
|
|
|
)
|
|
|
|
{
|
2014-06-21 21:04:07 -04:00
|
|
|
memberTile.owner = tiles[i];
|
|
|
|
memberTile.ownerOffsetX = offx;
|
|
|
|
memberTile.ownerOffsetY = offy;
|
2013-09-01 00:26:16 +00:00
|
|
|
}
|
2013-09-01 00:25:39 +00:00
|
|
|
}
|
|
|
|
}
|
2013-07-17 20:11:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-14 17:06:33 -05:00
|
|
|
public static TileSpec load(String tileName)
|
|
|
|
{
|
|
|
|
return tilesByName.get(tileName);
|
|
|
|
}
|
|
|
|
|
2014-06-21 19:14:31 -04:00
|
|
|
public static TileSpec loadByOrdinal(int tileNumber)
|
|
|
|
{
|
|
|
|
return load(Integer.toString(tileNumber));
|
|
|
|
}
|
|
|
|
|
2014-03-05 01:58:31 +00:00
|
|
|
/**
|
|
|
|
* Access a tile specification by index number.
|
|
|
|
*
|
|
|
|
* @return a tile specification, or null if there is no tile
|
|
|
|
* with the given number
|
|
|
|
*/
|
2013-07-17 20:11:54 +00:00
|
|
|
public static TileSpec get(int tileNumber)
|
|
|
|
{
|
2013-07-18 13:31:50 +00:00
|
|
|
if (tileNumber >= 0 && tileNumber < tiles.length) {
|
|
|
|
return tiles[tileNumber];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return null;
|
|
|
|
}
|
2013-07-17 20:11:54 +00:00
|
|
|
}
|
2013-09-01 00:26:29 +00:00
|
|
|
|
2014-06-16 21:08:04 -04:00
|
|
|
public static int getTileCount()
|
|
|
|
{
|
|
|
|
return tiles.length;
|
|
|
|
}
|
|
|
|
|
2013-09-01 00:26:29 +00:00
|
|
|
static void checkTiles()
|
|
|
|
{
|
|
|
|
for (int i = 0; i < tiles.length; i++) {
|
2013-09-01 00:26:44 +00:00
|
|
|
// do something here
|
2013-09-01 00:26:29 +00:00
|
|
|
}
|
|
|
|
}
|
2013-07-17 20:11:54 +00:00
|
|
|
}
|