package mapCompiler.main;
import java.util.ArrayList;
import org.newdawn.slick.*;
import org.newdawn.slick.geom.Vector2f;
import org.newdawn.slick.util.xml.*;
public class Tileset {
protected TileshiftSet[] tileshifts;
protected String spritesheet;
protected String miniSpritesheet;
protected String pixelSpritesheet;
protected XMLElementList links;
public Tileset (String filepath) throws SlickException {
XMLParser parser = new XMLParser();
XMLElement root = parser.parse(filepath);
tileshifts = new TileshiftSet[root.getChildrenByName("tileshifts").get(0).getChildrenByName("tile").size()];
for (int i=0; i<tileshifts.length; i++) {
tileshifts[i] = new TileshiftSet(root.getChildrenByName("tileshifts").get(0).getChildrenByName("tile").get(i));
}
links = root.getChildrenByName("sprites").get(0).getChildren();
spritesheet = root.getAttribute("spritesheet");
miniSpritesheet = root.getAttribute("miniSpritesheet");
pixelSpritesheet = root.getAttribute("pixelSpritesheet");
}
public String updateTileImage (String tile, String[] directions) throws SlickException {
for (int i=0; i<tileshifts.length; i++) {
if (tileshifts[i].getName().equals(tile)) {
tile = tileshifts[i].tryShift(tile, directions);
}
}
return tile;
}
public String getSpriteSheet() {
return spritesheet;
}
public String getMiniSpriteSheet() {
return miniSpritesheet;
}
public String getPixelSpriteSheet() {
return pixelSpritesheet;
}
public String getNumbers (String s) throws Exception {
for (int i=0; i<links.size(); i++) {
XMLElement link = links.get(i);
if (link.getName() == "animLink") {
if (s.equals(link.getAttribute("tile"))) {
String temp = "";
for (int n=0; n < link.getChildrenByName("frame").size(); n++) {
temp += format(link.getChildrenByName("frame").get(n).getIntAttribute("u"), 2) + format(link.getChildrenByName("frame").get(n).getIntAttribute("v"), 2);
}
return "!" + (link.getChildrenByName("frame").size()) + format(link.getIntAttribute("speed"), 4) + temp;
}
} else {
if (s.equals(link.getAttribute("tile"))) {
return "" + format(link.getIntAttribute("u"), 2) + format(link.getIntAttribute("v"), 2);
}
}
}
throw new SlickException("Unknown: " + s);
}
public Vector2f[] getAllTileNumbers() throws SlickXMLException {
ArrayList<Vector2f> result = new ArrayList<Vector2f>();
for (int i=0; i < links.size(); i++) {
XMLElement link = links.get(i);
if (link.getName().equals("animLink")) {
for (int j=0; j < link.getChildrenByName("frame").size(); j++) {
result.add(new Vector2f(link.getChildrenByName("frame").get(j).getIntAttribute("u"), link.getChildrenByName("frame").get(j).getIntAttribute("v")));
}
} else {
result.add(new Vector2f(link.getIntAttribute("u"), link.getIntAttribute("v")));
}
}
//Convert to array.
Vector2f[] array = new Vector2f[result.size()];
for (int i=0; i < array.length; i++) {
array[i] = result.get(i);
}
return array;
}
public String format (int n, int chars) throws Exception {
String result = new Integer(n).toString();
if (result.length() > chars || n < 0) {
throw new Exception("Bad data. Not within the expected bounds.");
}
for (int i=0; i < chars-result.length(); i++) {
result = "0" + result;
}
return result;
}
}