arpg游戏中我们一般都用 1 代表可行走点 0 不可行走点。组成一个二维数组。byte[][] mapConfig。这样就可以使用 坐标系来求解一些问题。比如 这里所说的 如何知道两点之间是否存在障碍物。我的思路从起点开始 x轴 或 Y轴 加 0.5。循环 判断当前点是否是阻挡点。
假如 有此方法判断 改坐标点是否是 阻挡
checkNoBlockPosition(int map,int xPosCell,int yPosCell)
游戏地图中,一般世界坐标 x = 格子 / 2;
假如:PrecisePosition 作为坐标对象:
public class PrecisePosition {
private float x;
private float y;
private int cellX;
private int cellY;
}
/**
* 判断两节点之间是否存在障碍物 已测试
* true :阻挡点
*/
public static boolean hasBarrier(int mapId, PrecisePosition start, PrecisePosition end) {
if (start.getCellX() == end.getCellX() && start.getCellY() == end.getCellY())
return false;
if (!checkNoBlockPosition(selfMLife, mapId, end.getCellX(), end.getCellY()))
return true;
float distX = Math.abs(end.getX() - start.getX());
float distY = Math.abs(end.getY() - start.getY());
//超过100 则直接阻挡
if (distX >= 100 || distY >= 100)
return true;
float i = 0;
float loopStart = 0;
float loopEnd = 0;
if (distX > distY) {
loopStart = Math.min(start.getX(), end.getX());
loopEnd = Math.max(start.getX(), end.getX());
for (i = loopStart; i <= loopEnd; i++) {
if (i == loopStart) {
i += 0.5f;
}
float ypos = lineFunction(start.getX(), start.getY(), end.getX(), end.getY(), 0, i, 0);
//还需要得到该节点下的所有节点来判断周边是否有阻挡点
if (getNodesUnderPointIsBlock(mapId, (int) i * 2, (int) ypos * 2)) {
return true;
}
if (i == loopStart + 0.5f) {
i -= 0.5f;
}
}
} else {
loopStart = Math.min(start.getY(), end.getY());
loopEnd = Math.max(start.getY(), end.getY());
for (i = loopStart; i <= loopEnd; i++) {
if (i == loopStart) {
i += 0.5f;
}
float xpos = lineFunction(start.getX(), start.getY(), end.getX(), end.getY(), 1, 0, i);
if (getNodesUnderPointIsBlock(mapId, (int) xpos * 2, (int) i * 2)) {
return true;
}
if (i == loopStart + 0.5f) {
i -= 0.5f;
}
}
}
return false;
}
为了 行走的直线 是可走的。最好保证 直线周边尽量无 阻挡点,这里做了 误差处理。去找周边的点判断是否是阻挡点。有好的思路可以优化。
/**
* 判断周边点是否也是阻挡 true 阻挡
*
* @param xPos
* @param yPos
* @return
*/
private static boolean getNodesUnderPointIsBlock(int mapId, int xPosCell, int yPosCell) {
if (!checkNoBlockPosition(mapId, xPosCell - 1, yPosCell - 1)) {
return true;
}
if (!checkNoBlockPosition(mapId, xPosCell, yPosCell - 1)) {
return true;
}
if (!checkNoBlockPosition(mapId, xPosCell - 1, yPosCell)) {
return true;
}
if (!checkNoBlockPosition(mapId, xPosCell, yPosCell)) {
return true;
}
return false;
}
大致就是这意思了。感兴趣的可以交流,留言。学习下。。
版权声明:本文为qq_16998379原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。