Skip to content

Commit

Permalink
chore: clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
闫茂源 committed Apr 17, 2024
1 parent 2de6dcd commit 7974f8f
Show file tree
Hide file tree
Showing 15 changed files with 267 additions and 245 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ private TiledConst() {
public static final String TILE_HEIGHT = "tileheight";
public static final String MARGIN = "margin";
public static final String SPACING = "spacing";
public static final String TILE_COUNT = "tilecount";
public static final String COLUMNS = "columns";
public static final String HEX_SIDE_LENGTH = "hexsidelength";
public static final String STAGGER_AXIS = "staggeraxis";
public static final String STAGGER_INDEX = "staggerindex";
Expand All @@ -48,6 +50,7 @@ private TiledConst() {
public static final String TRANS = "trans";
public static final String FORMAT = "format";
public static final String TILE_OFFSET = "tileoffset";
public static final String GRID = "grid";
// tileset-transformations
public static final String TRANSFORMATIONS = "transformations";
public static final String H_FLIP = "hvlip";
Expand Down Expand Up @@ -77,6 +80,7 @@ private TiledConst() {
// properties
public static final String PROPERTIES = "properties";
public static final String PROPERTY = "property";
public static final String PROPERTY_TYPE = "propertytype";
public static final String LAYER = "layer";
public static final String IMAGELAYER = "imagelayer";
public static final String REPEAT_X = "repeatx";
Expand Down
6 changes: 3 additions & 3 deletions tmx-loader/src/main/java/io/github/jmecn/tiled/TmxLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.jme3.asset.AssetKey;
import com.jme3.asset.AssetLoader;
import com.jme3.asset.AssetManager;
import io.github.jmecn.tiled.loader.TiledMapLoader;
import io.github.jmecn.tiled.loader.MapLoader;
import io.github.jmecn.tiled.loader.TilesetLoader;

import java.io.IOException;
Expand All @@ -25,8 +25,8 @@ public Object load(AssetInfo assetInfo) throws IOException {

switch (extension) {
case TiledConst.TMX_EXTENSION:
TiledMapLoader tiledMapLoader = new TiledMapLoader(assetManager, key);
return tiledMapLoader.load(assetInfo.openStream());
MapLoader mapLoader = new MapLoader(assetManager, key);
return mapLoader.load(assetInfo.openStream());
case TiledConst.TSX_EXTENSION:
TilesetLoader tilesetLoader = new TilesetLoader(assetManager, key);
return tilesetLoader.load(assetInfo.openStream());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ public class MapObject extends Base {
private String name;

/**
* The type of the object. An arbitrary string.
* The class of the object. An arbitrary string.
*/
private String type;
private String clazz;
private ObjectType shape = ObjectType.RECTANGLE;

/**
Expand Down Expand Up @@ -139,12 +139,12 @@ public void setName(String name) {
this.name = name;
}

public String getType() {
return type;
public String getClazz() {
return clazz;
}

public void setType(String type) {
this.type = type;
public void setClazz(String clazz) {
this.clazz = clazz;
}

public ObjectType getShape() {
Expand Down Expand Up @@ -274,7 +274,7 @@ public void setTextData(ObjectText textData) {

@Override
public String toString() {
return "MapObject [id=" + id + ", name=" + name + ", type=" + type
return "MapObject [id=" + id + ", name=" + name + ", clazz=" + clazz
+ ", shape=" + shape + ", x=" + x + ", y=" + y
+ ", width=" + width + ", height=" + height + "]";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public String toString() {

public void copyTo(MapObject obj) {
obj.setName(object.getName());
obj.setType(object.getType());
obj.setClazz(object.getClazz());
obj.setShape(object.getShape());
obj.setWidth(object.getWidth());
obj.setHeight(object.getHeight());
Expand Down
9 changes: 9 additions & 0 deletions tmx-loader/src/main/java/io/github/jmecn/tiled/core/Tile.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class Tile extends Base implements Cloneable {
private Tileset tileset;
private int id = -1;
private int gid = 0;
private String clazz;

/**
* position in the image
Expand Down Expand Up @@ -115,6 +116,14 @@ public int getGidNoMask() {
return gid & ~FLIPPED_MASK;
}

public void setClazz(String clazz) {
this.clazz = clazz;
}

public String getClazz() {
return clazz;
}

public boolean isFlippedHorizontally() {
return (gid & FLIPPED_HORIZONTALLY_FLAG) != 0;
}
Expand Down
117 changes: 52 additions & 65 deletions tmx-loader/src/main/java/io/github/jmecn/tiled/core/Tileset.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io.github.jmecn.tiled.enums.ObjectAlignment;
import io.github.jmecn.tiled.enums.Orientation;
import io.github.jmecn.tiled.enums.TileRenderSize;
import io.github.jmecn.tiled.math2d.Point;

/**
* If there are multiple <tileset> elements, they are in ascending order of
Expand Down Expand Up @@ -65,13 +66,13 @@ public class Tileset extends Base implements Iterable<Tile> {
* The spacing in pixels between the tiles in this tileset (applies to the
* tileset image, defaults to 0). Irrelevant for image collection tilesets.
*/
private int tileSpacing;
private int spacing;

/**
* The margin around the tiles in this tileset (applies to the tileset
* image, defaults to 0). Irrelevant for image collection tilesets.
*/
private int tileMargin;
private int margin;

/**
* The number of tiles in this tileset (since 0.13). Note that there can
Expand Down Expand Up @@ -110,32 +111,12 @@ public class Tileset extends Base implements Iterable<Tile> {
*/
private FillMode fillMode;

private boolean hasGrid = false;
// This element is used to specify an offset in pixels, to be applied when drawing a tile from the related tileset. When not present, no offset is applied.
private final Point tileOffset = new Point(0, 0);

/**
* Orientation of the grid for the tiles in this tileset (orthogonal or isometric, defaults to orthogonal)
*/
private Orientation orientation = Orientation.ORTHOGONAL;

/**
* Width of a grid cell
*/
private int gridWidth;

/**
* Height of a grid cell
*/
private int gridHeight;

/**
* Horizontal offset in pixels.
*/
private int tileOffsetX = 0;
/**
* Vertical offset in pixels (positive is down)
*/
private int tileOffsetY = 0;
private TilesetGrid grid;

// tileset image, in case image-based tileset
private TiledImage image;
private String imageSource;
private Texture texture;
Expand All @@ -160,15 +141,15 @@ public class Tileset extends Base implements Iterable<Tile> {
public Tileset() {
this.tileWidth = 32;
this.tileHeight = 32;
this.tileSpacing = 0;
this.tileMargin = 0;
this.spacing = 0;
this.margin = 0;
}

public Tileset(int width, int height, int space, int margin) {
this.tileWidth = width;
this.tileHeight = height;
this.tileSpacing = space;
this.tileMargin = margin;
this.spacing = space;
this.margin = margin;
}

public int getFirstGid() {
Expand Down Expand Up @@ -211,20 +192,20 @@ public void setTileHeight(int tileHeight) {
this.tileHeight = tileHeight;
}

public int getTileSpacing() {
return tileSpacing;
public int getSpacing() {
return spacing;
}

public void setTileSpacing(int tileSpacing) {
this.tileSpacing = tileSpacing;
public void setSpacing(int spacing) {
this.spacing = spacing;
}

public int getTileMargin() {
return tileMargin;
public int getMargin() {
return margin;
}

public void setTileMargin(int tileMargin) {
this.tileMargin = tileMargin;
public void setMargin(int margin) {
this.margin = margin;
}

public int getTileCount() {
Expand Down Expand Up @@ -368,8 +349,7 @@ public void addNewTile(Tile t) {
* Removal is simply setting the reference at the specified index to
* <b>null</b>.
*
* @param i
* the index to remove
* @param i the index to remove
*/
public void removeTile(int i) {
tiles.set(i, null);
Expand Down Expand Up @@ -442,23 +422,33 @@ public Iterator<Tile> iterator() {
return tiles.iterator();
}

public void setTileOffset(int tileOffsetX, int tileOffsetY) {
this.tileOffsetX = tileOffsetX;
this.tileOffsetY = tileOffsetY;
}

public int getTileOffsetX() {
return tileOffsetX;
/**
* Set the tile offset for the tiles in this tileset.
*
* @param x Horizontal offset in pixels. (defaults to 0)
* @param y Vertical offset in pixels (positive is down, defaults to 0)
*/
public void setTileOffset(int x, int y) {
this.tileOffset.set(x, y);
}

public int getTileOffsetY() {
return tileOffsetY;
/**
* @return Horizontal offset in pixels. (defaults to 0)
*/
public Point getTileOffset() {
return tileOffset;
}

/**
* @return The class of this tileset (since 1.9, defaults to “”).
*/
public String getClazz() {
return clazz;
}

/**
* @param clazz The class of this tileset (since 1.9, defaults to “”).
*/
public void setClazz(String clazz) {
this.clazz = clazz;
}
Expand Down Expand Up @@ -500,26 +490,23 @@ public void setFillMode(String fillMode) {
}

public boolean hasGrid() {
return hasGrid;
return this.grid != null;
}

public Orientation getOrientation() {
return orientation;
}

public int getGridWidth() {
return gridWidth;
}

public int getGridHeight() {
return gridHeight;
/**
* Set the grid for the tiles in this tileset.
* @param grid The grid for the tiles in this tileset.
*/
public void setGrid(TilesetGrid grid) {
this.grid = grid;
}

public void setGrid(Orientation orientation, int width, int height) {
this.orientation = orientation;
this.gridWidth = width;
this.gridHeight = height;
this.hasGrid = true;
/**
* Get the grid for the tiles in this tileset.
* @return The grid for the tiles in this tileset.
*/
public TilesetGrid getGrid() {
return this.grid;
}

public Transformations getTransformations() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package io.github.jmecn.tiled.core;

import io.github.jmecn.tiled.enums.Orientation;

/**
* This element is only used in case of isometric orientation, and determines how tile overlays for terrain and collision information are rendered.
*
* @author yanmaoyuan
*/
public class TilesetGrid {
/**
* Orientation of the grid for the tiles in this tileset (orthogonal or isometric, defaults to orthogonal)
*/
private Orientation orientation;
// Width of a grid cell
private int width;
// Height of a grid cell
private int height;

public TilesetGrid(Orientation orientation, int width, int height) {
this.orientation = orientation;
this.width = width;
this.height = height;
}

public Orientation getOrientation() {
return orientation;
}

public void setOrientation(Orientation orientation) {
this.orientation = orientation;
}

public int getWidth() {
return width;
}

public void setWidth(int width) {
this.width = width;
}

public int getHeight() {
return height;
}

public void setHeight(int height) {
this.height = height;
}
}
Loading

0 comments on commit 7974f8f

Please sign in to comment.