📄 xmlmapsaver.java.svn-base
字号:
/*
* XmlMapSaver.java
*
* Created on 3. Februar 2007, 12:14
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package kanjitori.map;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Locale;
import kanjitori.graphics.tile.Tile;
/**
*
* @author Pirx
*/
public class XmlMapSaver {
private static final XmlMapSaver INSTANCE = new XmlMapSaver();
private FileWriter writer;
private Map map;
/** Creates a new instance of XmlMapSaver */
private XmlMapSaver() {
}
public static XmlMapSaver getSaver() {
return INSTANCE;
}
public void save(String fileName, Map map) {
this.map = map;
try {
writer = new FileWriter(fileName);
writeMap();
writer.flush();
writer.close();
writer = null;
map = null;
} catch (IOException ex) {
ex.printStackTrace();
}
}
private void writeMap() throws IOException {
writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
writer.write(String.format("<map name=\"%s\" minBots=\"5\" maxBots=\"30\" startBots=\"10\" x=\"%d\" y=\"%d\">\n",
map.getName(), map.getSize().width, map.getSize().height));
writeTileDefs();
writeLayers();
writeContents();
writeSkyBox();
writer.write("</map>\n");
}
private void writeLayers() throws IOException {
writer.write("<layers>\n");
for (int i = 0; i < map.getLayerCount(); i++) {
Layer l = map.getLayer(i);
writer.write(String.format(Locale.ENGLISH, " <layer name=\"%s\" height=\"%f\">\n",
l.getName(), l.getHeight()));
writeTiles(l);
writer.write(" </layer>");
}
writer.write("</layers>\n");
}
private void writeTileDefs() throws IOException {
java.util.Map<String, Tile> tileMap = new HashMap<String, Tile>();
for(int l = 0; l < map.getLayerCount(); l++) {
Layer layer = map.getLayer(l);
for (int x = 0; x < map.getSize().width; x++) {
for (int y = 0; y < map.getSize().height; y++) {
Tile t = layer.getTile(x, y);
if (t != null) {
tileMap.put(t.getName(), t);
}
}
}
}
writer.write("<tiledefs>\n");
for (java.util.Map.Entry<String, Tile> e : tileMap.entrySet()) {
writer.write(String.format(" <tiledef name=\"%s\" class=\"%s\">\n",
e.getKey(), e.getValue().getClass().getName()));
java.util.Map<String, String> params = e.getValue().getParams();
for (java.util.Map.Entry<String, String> pe : params.entrySet()) {
writer.write(String.format(" <param key=\"%s\" value=\"%s\"/>\n",
pe.getKey(), pe.getValue()));
}
writer.write(" </tiledef>\n");
}
writer.write("</tiledefs>\n");
}
private void writeTiles(Layer layer) throws IOException {
for (int x = 0; x < map.getSize().width; x++) {
for (int y = 0; y < map.getSize().height; y++) {
Tile t = layer.getTile(x, y);
if (t != null) {
writer.write(String.format(" <tile name=\"%s\" x=\"%d\" y=\"%d\"/>\n",
t.getName(), x, y));
}
}
}
}
private void writeContents() throws IOException {
writer.write("<contents>\n");
for (int x = 0; x < map.getSize().width; x++) {
for (int y = 0; y < map.getSize().width; y++) {
if (map.getContent(x,y) != Content.EMPTY) {
writer.write(String.format(" <cont x=\"%d\" y=\"%d\"/>\n",
x, y));
}
}
}
writer.write("</contents>\n");
}
private void writeSkyBox() throws IOException {
writer.write(String.format("<skybox north=\"%s\" south=\"%s\" east=\"%s\" west=\"%s\" up=\"%s\" down=\"%s\"/>\n",
(Object[]) map.getSkyboxTextures()));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -