I'm using printwriter to generate a LUA script for a tile map maker. It seems hackish to me, but it works.
Purposes are for allowing users to make their own maps for my game.
- Is there a better practice to generate scripts?
- Secondary - Why is it bad to do it my way?
I'm only including the function to write this Lua code, but if you want to run it on your own machine the full source is here.
public void buildLua() {
block25 : {
boolean foundGoal = false;
boolean foundPlayer = false;
boolean foundWall = false;
int m = 0;
while (!(foundGoal && foundPlayer && foundWall)) {
if (this.mapGridList.get(m).getIcon().equals(this.goalIcon)) {
foundGoal = true;
} else if (this.mapGridList.get(m).getIcon().equals(this.playerIcon)) {
foundPlayer = true;
} else if (this.mapGridList.get(m).getIcon().equals(this.wallIcon)) {
foundWall = true;
}
if (++m > (this.mapSize - 2) * (this.mapSize - 2)-1) break;
}
if (foundGoal && foundPlayer && foundWall) {
try {
JFileChooser chooser = new JFileChooser(String.valueOf(System.getProperty("user.home")) + "/Desktop");
chooser.setFileSelectionMode(2);
int result = chooser.showSaveDialog(chooser);
if (result == 0) {
this.mapName = chooser.getSelectedFile().getName();
if (this.mapName.contains(".lua")) {
this.mapName = this.mapName.substring(0, this.mapName.length() - 4);
}
JRadioButton temp = new JRadioButton();
System.out.println(temp.getText());
this.setMaxWalls();
String path = chooser.getSelectedFile().getAbsolutePath();
PrintWriter writer = path.endsWith(".lua") ? new PrintWriter(path, "UTF-8") : new PrintWriter(String.valueOf(path) + ".lua", "UTF-8");
writer.println("local Map = IceRunner.Map");
writer.println("local MapKit = IceRunner.MapKit");
writer.println("local Up = IceRunner.MapTools.UpExtent");
writer.println("local Down = IceRunner.MapTools.DownExtent");
writer.println("local Left = IceRunner.MapTools.LeftExtent");
writer.println("local Right = IceRunner.MapTools.RightExtent");
writer.println("local Wall = IceRunner.Map.Wall");
writer.println("local MapKit = IceRunner.MapTools.MapKit");
writer.println("local Player = Map.Player");
writer.println("local Goal = Map.Goal");
writer.println("");
writer.println("local map = Map({");
writer.println("name = \"" + this.mapName.toUpperCase() + "\",");
writer.println("level = " + this.difficulty + ",");
writer.println("kit = MapKit({size = " + this.mapSize + ", walls = " + this.maxWalls + " })");
writer.println("})");
writer.println("");
if (this.mapSize == 15) {
writer.println("map:add_walls(Wall(0, 0), Right(14))");
writer.println("map:add_walls(Wall(1, 0), Down(13))");
writer.println("map:add_walls(Wall(1, 14), Down(13))");
writer.println("map:add_walls(Wall(14, 1), Right(13))");
} else if (this.mapSize == 20) {
writer.println("map:add_walls(Wall(0, 0), Right(19))");
writer.println("map:add_walls(Wall(1, 0), Down(18))");
writer.println("map:add_walls(Wall(1, 19), Down(18))");
writer.println("map:add_walls(Wall(19, 1), Right(18))");
} else if (this.mapSize == 25) {
writer.println("map:add_walls(Wall(0, 0), Right(24))");
writer.println("map:add_walls(Wall(1, 0), Down(23))");
writer.println("map:add_walls(Wall(1, 24), Down(23))");
writer.println("map:add_walls(Wall(24, 1), Right(23))");
}
int z = this.mapSize - 2;
int i = 1;
while (i < z * z + 1) {
int x;
int y;
if ((i - 1) % z > 0) {
x = (i - 1) / z;
y = (i - 1) % z;
} else {
x = (i - 1) / z;
y = 0;
}
if (this.mapGridList.get(i - 1).getIcon().equals(this.wallIcon)) {
writer.println("map:add_walls(Wall(" + (x + 1) + "," + (y + 1) + "), Up(0))");
} else if (this.mapGridList.get(i - 1).getIcon().equals(this.playerIcon)) {
writer.println("map:set_player(Player(" + (x + 1) + "," + (y + 1) + "))");
} else if (this.mapGridList.get(i - 1).getIcon().equals(this.goalIcon)) {
writer.println("map:set_goal(Goal(" + (x + 1) + "," + (y + 1) + "))");
}
++i;
}
writer.println("");
writer.println("IceRunner.register_map(map);");
writer.close();
break block25;
}
JOptionPane.showMessageDialog(this.frmIceRunnerMap, "Map Not Saved!");
}
catch (IOException e) {
JOptionPane.showMessageDialog(this.frmIceRunnerMap, "Error Occured While Saving!");
}
} else {
JOptionPane.showMessageDialog(this.frmIceRunnerMap, "Please place at least one wall, start tile, and finish tile...");
}
}
}