I was making a game with XNA game studio and now I want to rewrite it in Java. It's something like a 2D Minecraft clone. For collision detection, I have to loop through all blocks in the game to check on whether the player is colliding with a block. With a huge number of blocks, it is impossible to do so, so I made a grid system. I divided the world into grids which contain blocks, and put them into a dictionary.
Dictionary<string, List<Block>> gameBlocks;
Now I have only to loop through the blocks in the current grid.
This is the method to register a block:
public void RegisterBlock(Block block)
{
idX = (int)(block.blockPosition.X / width);
idY = (int)(block.blockPosition.Y / height);
string id = idX.ToString() + "_" + idY.ToString();
if (gameBlocks.ContainsKey(id))
{
gameBlocks[id].Add(block);
}
else
{
gameBlocks.Add(id, new List<Block>());
gameBlocks[id].Add(block);
}
}
Now I am trying to rewrite it in Java but I don't know how to put something into a Dictionary.