List <BasicEntity> GetEnemyAround(BasicEntity entity) { VoxelBlocks map = GameObject.Find("Voxel Map").transform.GetComponent <VoxelBlocks> (); List <BlockInfo> list = map.GetNeibor(entity.GetComponent <BlockInfoComponent> ().m_logicPosition); List <BasicEntity> lb = new List <BasicEntity> (); foreach (var b in list) { if (b.entity == null) { continue; } BlockType blockType = b.entity.GetComponent <BlockInfoComponent>().m_blockType; if (blockType != BlockType.Enemy && blockType != BlockType.Player) { continue; } if (b.entity != null && blockType != entity.GetComponent <BlockInfoComponent> ().m_blockType) { lb.Add(b.entity); } } if (lb.Count == 0) { return(null); } return(lb); }
static public List <Vector3> FindPathInStep(Vector3 s, int n) { VoxelBlocks voxelBlocks = GameObject.Find("Voxel Map").GetComponent <VoxelBlocks>(); List <Vector3> openSet = new List <Vector3>(); List <Vector3> nextSet = new List <Vector3>(); List <Vector3> closeSet = new List <Vector3>(); openSet.Add(s); closeSet.Add(openSet[0]); //步数限制 for (int i = 0; i < n; i++) { //遍历第i步的所有格子 for (int j = 0; j < openSet.Count; j++) { foreach (var blocks in voxelBlocks.GetNeibor(openSet[j])) { if (blocks.entity != null || closeSet.Contains(blocks.logPos)) { continue; } //将下一步能达到的格子加入到总表 closeSet.Add(blocks.logPos); //将下一步能到达的格子单独保存 nextSet.Add(blocks.logPos); } } openSet.Clear(); //取出下一步能达到的格子 if (nextSet.Count > 0) { openSet.AddRange(nextSet); } nextSet.Clear(); } if (closeSet.Count != 0) { closeSet.Remove(s); return(closeSet); } return(null); }
private Vector3 FindCover(Vector3 pos) { VoxelBlocks map = GameObject.Find("Voxel Map").GetComponent <VoxelBlocks>(); List <BlockInfo> list = map.GetNeibor(pos); if (list == null) { return(Vector3.down); } foreach (var e in list) { if (e.entity != null && e.entity.GetComponent <BlockInfoComponent>().m_blockType == BlockType.Wall) { return(e.logPos); } } return(Vector3.down); }
static public List <Vector3> GetPath(Vector3 s, Vector3 e) { List <NodeItem> openSet = new List <NodeItem>(); List <NodeItem> closeSet = new List <NodeItem>(); VoxelBlocks voxelBlocks = GameObject.Find("Voxel Map").GetComponentInChildren <VoxelBlocks>(); NodeItem startNode = BItoNI(s); NodeItem endNode = BItoNI(e); openSet.Add(startNode); while (openSet.Count > 0) { NodeItem curNode = openSet[0]; //获取openSet内权重最低的点。 for (int i = 0, max = openSet.Count; i < max; i++) { if (openSet[i].fCost <= curNode.fCost && openSet[i].hCost < curNode.hCost) { curNode = openSet[i]; } } openSet.Remove(curNode); closeSet.Add(curNode); // 找到的目标节点 if (curNode.m_logPos == e) { return(generatePath(curNode)); } // 判断周围节点,选择一个最优的节点 foreach (var block in voxelBlocks.GetNeibor(curNode.m_logPos)) { NodeItem item = BItoNI(block.logPos); // 如果是墙或者已经在关闭列表中 if (item.isWall || ContainsNode(closeSet, item)) { continue; } // 计算当前相领节点现开始节点距离 int newCost = curNode.gCost + getDistanceNodes(curNode, item); if (block.logPos.x != curNode.m_logPos.x && block.logPos.y != curNode.m_logPos.y) { newCost += (int)(5 * voxelBlocks.m_blockSize.x); } // 如果距离更小,或者原来不在开始列表中 if (newCost < item.gCost || !ContainsNode(openSet, item)) { if (!ContainsNode(openSet, item)) { // 更新与开始节点的距离 item.gCost = newCost; // 更新与终点的距离 item.hCost = getDistanceNodes(item, endNode); // 更新父节点为当前选定的节点 item.parent = curNode; openSet.Add(item); } else { foreach (var ni in openSet) { if (ni.m_logPos == item.m_logPos) { ni.gCost = newCost; ni.hCost = getDistanceNodes(item, endNode); ni.parent = curNode; } } } } } } return(null); }