4
\$\begingroup\$

This is an update from An Update on my TileMap Class. It's a tileMap class for a top-down old-school final fantasy game. User @Tim gave me suggestions and I updated some of the code accordingly.

Tile.java

import com.stardust.rpgtest2.gfx.Assets;
import java.awt.image.BufferedImage;
public enum Tile
{
 Grass(1, Assets.grass, false),
 Dirt(2, Assets.dirt, false),
 Tree(3, Assets.tree, true),
 Blank(36, Assets.blankTile, false);
 public int texKey;
 public BufferedImage texture;
 public boolean collidable;
 Tile(int texKey, BufferedImage texture, boolean collidable)
 {
 this.texKey = texKey;
 this.texture = texture;
 this.collidable = collidable;
 }
 public static BufferedImage getTileImage(int texKey1)
 {
 BufferedImage image;
 switch(texKey1)
 {
 case 1:
 {
 image = Grass.texture;
 break;
 }
 case 2:
 {
 image = Dirt.texture;
 break;
 }
 case 3:
 {
 image = Tree.texture;
 break;
 }
 case 36:
 {
 image = Blank.texture;
 break;
 }
 default:
 {
 image = Blank.texture;
 break;
 }
 }
 return image;
 }
}

TileMap.java render() method

public void render(Graphics g)
 {
 for(int y = 0; y < tilemap.length; y++)
 { 
 for(int x = 0; x < tilemap.length; x++)
 {
 int textureType = tilemap[x][y];
 BufferedImage texture = Tile.getTileImage(textureType);
 g.drawImage(texture, posX, posY, null);
 posY += 32;
 }
 posX += 32;
 posY = Game.mapY;
 }
 posX = Game.mapX;
 posY = Game.mapY;
 }
asked Mar 24, 2015 at 6:34
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$
 posY += 32;
 }
 posX += 32;

You have your tile sizes hardcoded in the TileMap ... in an obscure place (render code).

Consider placing them as constants in Tile or TileMap. That way, upscaling to 64x64 tiles is a lot easier.

public static BufferedImage getTileImage(int texKey1)
{
 BufferedImage image;
 switch(texKey1)
 {
 case 1:
 {
 image = Grass.texture;
 break;
 }
 case 2:
 {
 image = Dirt.texture;
 break;
 }
 case 3:
 {
 image = Tree.texture;
 break;
 }
 case 36:
 {
 image = Blank.texture;
 break;
 }
 default:
 {
 image = Blank.texture;
 break;
 }
 }
 return image;
}

You could use a Map here. Map<Integer, BufferedImage>, or better, Map<Integer,Tile>. Then you just have to get the Tile from the map, and ask that for the image. And if it's null you can return a blank.

answered Apr 28, 2015 at 10:52
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.