Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cb309c3

Browse files
committed
Fix TMW WLK exporter to work with multiple layers.
The main functionality of the plugin has been moved to another class to ease use by other programs.
1 parent cd34c4d commit cb309c3

File tree

3 files changed

+79
-32
lines changed

3 files changed

+79
-32
lines changed

plugins/tmw/src/tiled/plugins/tmw/TMWServerMapWriter.java

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -56,34 +56,7 @@ public void writeTileset(TileSet set, String filename) throws Exception {
5656
}
5757

5858
public void writeMap(Map map, OutputStream out) throws Exception {
59-
ByteArrayOutputStream baos = new ByteArrayOutputStream();
60-
61-
MapLayer layer = map.getLayer(3);
62-
if (layer != null && layer instanceof TileLayer) {
63-
int width = layer.getWidth();
64-
int height = layer.getHeight();
65-
66-
// Write width and height
67-
out.write(width & FIRST_BYTE);
68-
out.write(width >> 8 & FIRST_BYTE);
69-
out.write(height & FIRST_BYTE);
70-
out.write(height >> 8 & FIRST_BYTE);
71-
72-
for (int y = 0; y < height; y++) {
73-
for (int x= 0; x < width; x++) {
74-
Tile tile = ((TileLayer) layer).getTileAt(x, y);
75-
if (tile != null && tile.getId() > 0) {
76-
out.write(1);
77-
} else {
78-
out.write(0);
79-
}
80-
}
81-
}
82-
83-
baos.writeTo(out);
84-
} else {
85-
throw new Exception("No collision layer 4 found!");
86-
}
59+
WLKWriter.writeMap(map, out);
8760
}
8861

8962
public void writeTileset(TileSet set, OutputStream out) throws Exception {
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* The Mana World Plugin for Tiled, (c) 2004-2006
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* Adam Turk <[email protected]>
10+
* Bjorn Lindeijer <[email protected]>
11+
*/
12+
13+
package tiled.plugins.tmw;
14+
15+
import java.io.*;
16+
17+
import tiled.core.*;
18+
19+
/**
20+
* The WLK file writer. The format is very simple:
21+
*
22+
* <pre>
23+
* short (width)
24+
* short (height)
25+
* char[] (data)
26+
* </pre>
27+
*
28+
* @version $Id$
29+
*/
30+
public class WLKWriter
31+
{
32+
private static final int FIRST_BYTE = 0x000000FF;
33+
34+
public static void writeMap(Map map, OutputStream out) throws Exception {
35+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
36+
37+
MapLayer layer = null;
38+
39+
// Get the last "collision" layer
40+
for (MapLayer mapLayer : map) {
41+
if (mapLayer.getName().equalsIgnoreCase("collision")) {
42+
layer = mapLayer;
43+
}
44+
}
45+
46+
if (layer != null && layer instanceof TileLayer) {
47+
int width = layer.getWidth();
48+
int height = layer.getHeight();
49+
50+
// Write width and height
51+
out.write(width & FIRST_BYTE);
52+
out.write(width >> 8 & FIRST_BYTE);
53+
out.write(height & FIRST_BYTE);
54+
out.write(height >> 8 & FIRST_BYTE);
55+
56+
for (int y = 0; y < height; y++) {
57+
for (int x= 0; x < width; x++) {
58+
Tile tile = ((TileLayer) layer).getTileAt(x, y);
59+
if (tile != null && tile.getId() > 0) {
60+
out.write(1);
61+
} else {
62+
out.write(0);
63+
}
64+
}
65+
}
66+
67+
baos.writeTo(out);
68+
} else {
69+
throw new Exception("No collision layer found!");
70+
}
71+
}
72+
}

src/tiled/core/MultilayerPlane.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,13 @@
1313
package tiled.core;
1414

1515
import java.awt.Rectangle;
16-
import java.util.Collection;
17-
import java.util.ListIterator;
18-
import java.util.Vector;
16+
import java.util.*;
1917

2018
/**
2119
* MultilayerPlane makes up the core functionality of both Maps and Brushes.
2220
* This class handles the order of layers as a group.
2321
*/
24-
public class MultilayerPlane
22+
public class MultilayerPlane implements Iterable<MapLayer>
2523
{
2624
private Vector<MapLayer> layers;
2725
protected Rectangle bounds; //in tiles
@@ -285,4 +283,8 @@ public void resize(int width, int height, int dx, int dy) {
285283
public boolean inBounds(int x, int y) {
286284
return x >= 0 && y >= 0 && x < bounds.width && y < bounds.height;
287285
}
286+
287+
public Iterator<MapLayer> iterator() {
288+
return layers.iterator();
289+
}
288290
}

0 commit comments

Comments
 (0)