我结束了使用这两种方法的东西。下面的代码片段应有助于说明我承担了方法:
class TerrainType {
public String displayName;
public String regionName;
public int movementCost;
/* additional properties omitted */
/* constructors omitted */
}
此结构包含一个地形类型,包括运行成本和其他游戏相关的统计(我为了简化省略其余部分)的相关信息,显示检查时显示的地形类型的显示名称,以及从TextureAtlas
拉取的TextureRegion
的名称,我的渲染器对我非常友善。
class GameMapSystem extends EntityProcessingSystem {
@Mapper private ComponentMapper<MapPosition> pm;
@Mapper private ComponentMapper<SolidObject> som;
private ListMultimap<MapPosition, Entity> entityByLocation;
private int[][] map;
private int width, height;
private Array<TerrainType> terrainTypes;
/**
* Accepts an Array of TerrainType objects and an 2d integer array with
* values corresponding to indices into the array for the correct type.
*
* In my case, these values are gleaned by reading a level description
* file, but any source should be fine.
*/
public GameMapSystem(Array<TerrainType> terrainTypes, int[][] map) {
super(Aspect.getForAll(MapPosition.class));
this.terrainTypes = terrainTypes;
this.map = map;
this.width = map.length;
this.height = map[0].length;
this.entityByLocation = ArrayListMultimap.create();
}
public boolean isOccupied(int x, int y) {
List<Entity> entities = entityByLocation(new MapPosition(x, y));
for(Entity e : entities) {
if(som.has(e)) {
return true;
}
}
return false;
}
@Override
protected void inserted(Entity e) {
this.entityByLocation.put(pm.get(e), e);
}
@Override
protected void removed(Entity e) {
this.entityByLocation.remove(pm.get(e), e);
}
/* additional EntityProcessingSystem overrides omitted */
}
这个EntityProcessingSystem
然后以被动模式连接到我的世界。应该没有任何真正的理由在这个系统中为我的世界实际做任何处理,我真正想要的是能够听取inserted
和removed
事件将实体放入地图。一个经理在这种情况下可能会过于矫枉过正,因为它会告诉我每个插入或删除的实体,而我只关心与地图有关的地图(或者更具体地说与地图相关的位置)。然后,我有一些单独的路径查找逻辑,它会消耗额外的(这里未见到的)方法来简单地通过向世界对象请求这个被动系统来引导AI。
为了完整,MapPosition
类也如下所示。重要的是包含equals()
和hashcode()
以帮助使用MapPosition
作为集合中的关键。
public class MapPosition extends Component
{
public int x, y;
public MapPosition(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object other) {
if(!(other instanceof MapPosition)) {
return false;
}
MapPosition pos = (MapPosition)other;
return (pos.x == this.x && pos.y == this.y);
}
@Override
public int hashCode() {
int hash = 7;
hash = 59 * hash + this.x;
hash = 59 * hash + this.y;
return hash;
}
}
我很可能会试图找到比使用番石榴Multimap
最终更方便的数据结构,但它工作,现在和我感觉很舒服移动到充实公共API的论文类,其余。如果此答案确实对其他人有帮助,请记住,此实现的ArrayListMultimap
的性能未经过严格测试!
欢迎的StackOverflow!请务必参考[Tour](http://stackoverflow.com/tour)并仔细阅读帮助中心以了解[如何撰写正确答案](http://stackoverflow.com/help/how-回答)。在这种情况下,您或许可以通过至少总结链接中讨论的问题来改进您的答案,而不是简单地粘贴链接本身。 – dg99
啊,好的谢谢,将尽力改进和编辑总结。 –