Java / Tiles

Started by
5 comments, last by DreadReillz 20 years ago
Hey i was wondering if I could could get a few suggestions on an issue im trying to figure out. (I''ve already searched the forums and can''t seem to find any solutions specific to the ones im looking for). Anyways on to my question(s) I am working on a game.. for my Java ISU for my programming class. It''s going to be 2D (like secret of mana for the SNES). Anyways i have a question how I would go about implementing "height" in the maps, where stairs are etc..? I was thinking about using a Tile class.. but wouldnt that start taking up alot of memory? I also thought about layers.. and then again with unused map parts on certain layers... it would be an even greater memory hog. (However to fix that i could use vectors.. but im not sure how efficient they are). If anyone has any suggestions how i can go about implementing "height" within a tile engine, it would be greatly appreciated . Thanks in Advance, Reilly
Advertisement
Remember how arrays work in Java - Objects are always held by reference, so arrays contain references.

Of course, when you create an array, it's not mandatory to put a live reference in every place. So you can use sparsely populated arrays fairly efficiently, it won't actually store an instance of your tile object.

Vector is basically just a wrapper around an array, which is resized as necessary. If you always know what dimensions something is going to be in advance, just use arrays they're neater (and strongly typed so less casting is involved).

I would try to calculate approximately how much memory your tilemap is going to require - assume that a reference takes 4 bytes, you might have

Tile [] [] [] tilemap; // tile[x][y][z] : z = layer number.

If not all parts of your map will be the same height (layer-wise), then you could create a kind of jagged 3d array with variable height.

Then when you try to interrogate a tile at some coordinates (x,y,z) you have to first check that tilemap[x][y].length > z

Mark

[edited by - markr on March 29, 2004 3:29:03 PM]
Oh.. so the "Vector" class is just a wrapper for resizing an array? I thought it was like a linked list :/ Anyhow.. alright thanks.. Oh yes.. when I want to deal with "height" altering tiles (like stairs) would i have that information stored in the tile..
so..



class Tile
{

byte type; //what kind of mod does this tile have
int x, y //(in case of a ragged layer thats not full of tiles)

}
And should I include the Image inside the tile class?

[edited by - dreadreillz on March 29, 2004 4:04:49 PM]
quote:Original post by DreadReillz
Oh.. so the "Vector" class is just a wrapper for resizing an array? I thought it was like a linked list :/


If you do happen to need/want a linked list for something (I never do, but I never seem to run into situations where I want to insert into the middle of a list... different programming styles and different problems I guess), the class for that is... LinkedList. :/

quote:Anyhow.. alright thanks.. Oh yes.. when I want to deal with "height" altering tiles (like stairs) would i have that information stored in the tile..


This question is worrisome and suggests that you need to think more about what kind of capabilities your map has. If you just want a variance in height, without ever having one logical Tile underneath another, then you should probably just have ''height'' be an attribute (member variable) of your tile, and use a 2-d array. (You mentioned it being a 2-d game, so this may be what you''re after.) On the other hand, maybe you shouldn''t be working with arrays at all, but just have every Tile keep track of its neighbours (with a Tile[] member; and remember that nice stuff about objects being stored by reference )

quote:
And should I include the Image inside the tile class?


It Depends(TM). The tile should probably contain some data to encode its on-screen representation, yes; and perhaps a drawSelf() method or something like that. But you might for example end up putting all your "tiles" in a single image bitmap; in that case, instead of Image references, you''d want to store some kind of offset value (information that you can use in your drawing routines in order to setClip(), position the image etc.).
Okay, well I know for sure I will have "tiles" on top of other tiles, (so layers yeah...) Should that still be stored in a 3D array, or linked lists like you suggested?

I know *some* layers will not be full of tiles, so the array might not be the best option there.



Thanks in advance,
Reillz
how are you going to actually represent "tiles on top of tiles". that doesn''t seem possible with how i see 2d games. if you mean you''ll have a staircase which, when you enter that tile, you go to another level, then that''s not really tiles on top of tiles. instead you teleport the user to either another part of your 2d tile array or you teleport them to an entirely seperate array. that''s how it was always done in 2D tile-based games. they always cheat the 3rd dimension. when you think you''re going UP you''re really just being teleported. doom and doom2 did this as well. every time you took an elevator you just teleported to another section of the map, you really didn''t go "up" anywhere. you changed heights, but that''s very different than actually going up. you could never have 1 object over another object in 2D tile games or 2.5D games like Doom/Doom2

-me
I know what you mean, but in games like Final fantasy 3 and Secret of mana there are stairs and such where the sprite will move "diagonal" in a sense where they are above the rest of the map.

This topic is closed to new replies.

Advertisement