Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!

We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.

Best method for implementing a tile engine in java

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 Moe091   Members   -  Reputation: 572


Posted 25 May 2012 - 08:57 PM

I need advice for a game I'm working on.

In terms of graphics it'll be basically the same as a square-tiled rpg, I'm thinking about how I wanna write a tile engine and I have a few different ideas in my head, I can't figure which one would be best or if there is an alternative method I'm not thinking of. Here are a few of my ideas:

(one thing to note, the map will be tile based but movement wont be restricted to being on a specific tile, you can move pixel by pixel)

have an array(either 1 or 2 dimensional) of integers, storing a 'tile id' for the tile at that location, then as part of the map file store the different tiles and there id's, with all the information for that tile that isn't going to change(layers, spawn points, collision, triggers, etc) and of course the tile image(s). then you just have to loop through the array and do a calculation based on the array index to find out where it goes and draw the tile stored in the Tile file that correlates to that index number.

this next one sounds easiest but idk if it's efficient/practical. Have the tile editor save the map as one big image, and have a second black and white version(white representing collidable areas) to use for collision detection(by drawing a collision box for the player in a color an checking for overlapping or something) then have a 3rd part saved which holds locations of all spawn points and triggers and interactive stuff like that, gives there location size and points to either a script or an object that defines its behavior. I could then have a Jpanel hold the entire map and move it around as my char moves so that the area around me is what's inside the JFrame and being rendered. I wonder if this will be inefficient because it's trying to render the whole map, not just what's visible. I never looked into collision detection but I have a vague idea of how I would get that system to work.

or I could do the same thing as above, except without a huge JPanel. Just have the map stores as an image and copy the area around my character to the JPanel with drawImage().

can anyone comment on the pros/cons of these methods, give advice for implementing any of them, or suggest an entirely different method?


#2 CaptainKraft   Members   -  Reputation: 266


Posted 05 June 2012 - 02:05 PM


I'm also working on figuring out how to start a tile engine for my next game. Right now I'm leaning towards creating a very simple 2d array that will allow me to place tiles based on 'id'. For collision detection, maybe some sort of hash with the id as the key and a boolean as the value would solve that problem.

Another consideration is whether you plan to have a viewport or not. In my game, the character will be moving to different levels or maps, so I'm also trying to figure out how I would transition between screens.

Thanks for posting this question, because I need some help with it as well.

#3 CryoGenesis   Members   -  Reputation: 496


Posted 05 June 2012 - 05:03 PM

The way I did it when I made a 2D Tile engine was by having an 2D array of Tile objects:
Tile[][] tile;
Then you would have an image where 1 pixel represents a tile and the RGB values represent what image and type of tile it is.
This makes it easy to make maps for instance to load up a map in the engine all you would have to do is:
tile = new Tile[mapImage.getWidth(null][mapImage.getHeight(null)];
tile.setMap(Image mapImage);
The setMap method would do something like:

for(int x etc etc){
for(int y etc etc){
int pixel = (buffered image version of mapimage).getRGB(x,y);
int r, g, b;
r = insert red algorithm here;
g = insert green alg here;
b = insert blue alg here;
int val = r + g + b;
case 1:
tile[x][y] = new ChildOfTileClass();
tile[x][y].setPosition(x * tilesize, y * tilesize);

You could make a map editor if you wanted and it would be very easy.
In the engine I made you could just upload any image and it would convert to a map in the game.
To have tiles that are collide able you could do something like:
if(b == 0){

Probably not very resource efficient but is easy to program with and very flexible.

#4 CryoGenesis   Members   -  Reputation: 496


Posted 05 June 2012 - 05:10 PM

Oh and I forgot to say that doing it with int ID's aren't relatively good because if you wanted to do an effect that effects the player like:
Player.speed - 1
then you would have to do this:
if(Player.getStandingTileID == some id){
instead you could do:
public class SlowTile extends Tile{

public void effect(Player player){
player.speed - 1;

Doing this could mean even more and more complicated effects.

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.