0
我有一个游戏,这是一个基于2D的地图。看起来像一个巨大的棋盘。玩家可以在瓦片上放置单位。我需要一种有效的方法来确定哪些单位位于给定的瓷砖上。原因是我不想在渲染地图的一部分到屏幕时减慢我的渲染循环。我不想花太多时间找到哪些单位在哪个平铺。2d瓷砖地图 - 如何知道给定瓷砖上的哪些单位?
现在我想哈希映射,像这样:
// Java pseudo-code:
Map<Integer, List<Unit>> units = new HashMap<Integer, List<Unit>>();
// place a unit at tile x,y:
int xy = y * mapWidth + x;
List<Unit> matched = units.get(xy);
if (matched == null) {
matched = new ArrayList<Unit>();
units.put(xy, matched);
}
matched.add(new Airplane());
// render a portion of the map to screen, say tiles 20,5 to 50,17
for (int y = 5; y < 17 y++) {
for (int x = 20; x < 50; x++) {
List<Unit> matched = units.get(y * mapWidth + x);
if (matched != null && matched.size() > 0) {
draw(matched.get(0));
}
}
}
我可以看到这成为一个问题,如果我有巨大的地图,玩家把一个单位每瓦在地图(不太可能即将发生)。在这种情况下,我会在我的散列映射中使用mapWith * mapHeight条目,并且每个值本身就是一个数组。
这是对这个问题我幼稚起飞,将不胜感激任何的替代品,以提高查找速度或者单位占据了地图的每瓦以上的情况下,
感谢
难道你不能使用列表矩阵吗? –
我可以,但是之后我不需要预先分配mapWidth * mapHeight元素吗? (也许我误解 - 假设你的意思是列表 [] map = new ArrayList [mapWidth * mapHeight]?) –
user291701