patternjavaMinor
An Update on my TileMap Class
Viewed 0 times
tilemapclassupdate
Problem
I'm making a old-school Final Fantasy type game and I made a tile map class a while back and posted it on here (Is this a good way to implement a tile map from a text file?) But, when I got to collision, it was a nightmare. So I've updated it to make it more collision friendly and I was wondering what the community's thoughts were on this code.
Tile.java
TileMaker.java
TileMap.java
```
import com.stardust.rpgtest2.Game;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class TileMap
{
int p
Tile.java
import java.awt.image.BufferedImage;
public class Tile
{
public int texKey; //number to represent texture
public BufferedImage texture;
public boolean collidable;
public Tile(int texKey, BufferedImage texture, boolean collidable)
{
this.texKey = texKey;
this.texture = texture;
this.collidable = collidable;
}
}TileMaker.java
import com.stardust.rpgtest2.gfx.Assets;
public class TileMaker
{
public Tile[] tileArray= new Tile[28];
public TileMaker()
{
}
public void setTile()
{
for(int i = 0; i <= tileArray.length; i++)
{
switch(i)
{
case 0:
{
tileArray[i] = new Tile(i, Assets.grass, false);
break;
}
case 1:
{
tileArray[i] = new Tile(i, Assets.dirt, false);
break;
}
case 2:
{
tileArray[i] = new Tile(i, Assets.water, false);
break;
}
//this goes up to 27 at the moment
default:
{
break;
}
}
}
}
public Tile[] getTileArray()
{
return tileArray;
}
}TileMap.java
```
import com.stardust.rpgtest2.Game;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class TileMap
{
int p
Solution
- The for-case paradigm is an anti-pattern, you shouldn't use it. One alternative would be to initialize the array in a static block (without for-switch), another would be to use an enum. I would prefer the second approach. Both approaches would save you the call of
TileMaker.setTile().
- Your indent style is not customary in Java, and it's always best to go with what most programmers of a language are used to (opening curly bracket on the same line).
- Your variable names are not very clear. What is the difference between
posXandx? And what issx?
- Get rid of unused fields. If you don't need
mapSize, don't save it.
- Move magic numbers out of your code into static fields.
- I would create a separate class for the parsing of the file.
Context
StackExchange Code Review Q#84847, answer score: 5
Revisions (0)
No revisions yet.