Well, I currently making a game, kinda like a Sim city game. However I am not going to say too much about this game just yet.
However, I thought I try something new in when developing this game. This game involves displaying objects on a 2d plain of land. The simplest way to store positions of objects is to create a 2d array where you set the variable at array[x][y]. Well thats ok, until you want to store lots of objects on a very large map.
When I say large, I only mean a map about 2000×2000 squares. However each square is 32×32. This give me 64000×64000 of space to work on. For my game, that would be in pixels. I didnt really want a 64000×64000 array so I took a different approach.
Creating a 2D HashMap
So, I though, hashmaps are great for storing data in, all you need to know is the key, and you can replace the data, grab data out of it and also it is only as big as required saving memory.
So, I created a class where it stores a hashmap. Now, the key for this hashmap is a Integer object, this will become the y coordinate of my plain. The object is another HashMap where its key is the x coordinate. So now, i can put in a object with X and Y coordinates.
To get a object out, you just ask for y in the first hashmap and x in the second that is returned by hashmap y.
Why This Method is Great
This is really handy way of storing my map, for this game at least. I can insert images at pixel accuracy and the size of my plain is not limited by a absolute integer (when my heap overflows). However, I maybe limited by the amount of objects I can put in it, so far I managed 2000×2000 without a problem and using default java heap size. An array has a finite size and also would waste space on unused blocks.
I could also add another dimension to be layers, so I can place objects on top of each other at the same location but I haven’t got round to needing that yet.
It is also to move objects, just remove from one list and insert it back into the other at the new location.
I have also coded my game to display a tunnel vision of my map. This will only display 30×30 blocks of the map. I can drag the map around but only the data visible is taken out. I don’t iterate through all my data.
Collision detection would also be quite simple to do, as I can query an area in the hashmap. I havent made it possible to find objects within a whole area though, so at the moment each possible location needs to be checked one by one. I will figure this problem out later, I don’t need collision detection accurate to the pixel yet.
The Code
Here is the code I am currently using! Do what you want with it.
import java.util.*;
public class PlainMap {
HashMap map;
public PlainMap() {
map = new HashMap();
}
public void put(Object object, int x, int y) {
Integer ix, iy;
ix = new Integer(x);
iy = new Integer(y);
HashMap xmap = (HashMap) map.get(ix);
if (xmap == null) {
xmap = new HashMap();
map.put(ix, xmap);
}
xmap.put(iy, object);
}
public Object get(int x, int y) {
Integer ix, iy;
ix = new Integer(x);
iy = new Integer(y);
HashMap xmap = (HashMap) map.get(ix);
if (xmap == null) return null;
return xmap.get(iy);
}
}
More to come later!