当前位置: 首页>>代码示例>>C#>>正文


C# PriorityQueue.getCount方法代码示例

本文整理汇总了C#中PriorityQueue.getCount方法的典型用法代码示例。如果您正苦于以下问题:C# PriorityQueue.getCount方法的具体用法?C# PriorityQueue.getCount怎么用?C# PriorityQueue.getCount使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在PriorityQueue的用法示例。


在下文中一共展示了PriorityQueue.getCount方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: moveTo

	/**
	 * A coroutine that uses A* pathfinding to find an optimal path between the unit's
	 * 'targetTile' to the given tileToMoveTo
	 * Note: it does not use 'currentTile' since that can often be considered 'previousTile' as well
	 */
	public virtual IEnumerator moveTo(Tile tileToMoveTo, bool activelySetNewTarget = false) {
		// Avoid multiple calls to move the unit while the path is still being calculated
		if ((isCalculatingPath && !activelySetNewTarget) || isInBattle || tileToMoveTo == null) yield break;
		isCalculatingPath = true;
		Debug.Log("Calculating path to new tile");
	
		// Clear the previous path in case the user gave overriding commands
		targetPath.setNewTileQueue(new Queue<Tile>());
		
		// Set up the priority queue for our pathfinding algo: A*
		PriorityQueue priorityQueue = new PriorityQueue();
		priorityQueue.add(
			getNewPath(
				targetTile, 
				Vector2.Distance(targetTile.gameObject.transform.position, tileToMoveTo.gameObject.transform.position), 
				getTileCost(targetTile)
			)
		);
		// Keep track of which tiles (by instance id) we have already 'visited' to cut down on running time
		Dictionary<int, bool> visitedTiles = new Dictionary<int, bool>();
		visitedTiles[targetTile.GetInstanceID()] = true;
		
		// Continue to check and expand the first Path in the queue until we reach our target
		Path path;
		float startingTime = Time.time;
		while (true) {
			// If we have no paths left in the queue, then a solution is impossible
			if (priorityQueue.getCount() == 0) {
				Debug.Log("Could not find a path to target tile");
				isCalculatingPath = false;
				return false;
			}
		
			// Pop our next path and check if we have reached our target yet
			path = priorityQueue.pop();
			if (tileToMoveTo == path.getLastTileInPath()) {
				//Debug.Log("Found optimal path: ");
				//path.printPath();
				break;
			}
			
			// If we havent reached the target, expand on the currently popped path with all adjacent tile options
			foreach (Tile adjacentTile in mapManager.getAdjacentTiles(path.getLastTileInPath())) {
				// Only add it to the path if it is unoccuppied and we haven't already visited it yet
				if (visitedTiles.ContainsKey(adjacentTile.GetInstanceID()) || !canWalkTo(adjacentTile.transform.position)) continue;
				else visitedTiles[adjacentTile.GetInstanceID()] = true;
				
				// Add the tile to a deep copy of our original path
				//Debug.Log("Adding adjacentTile to path: " + adjacentTile.GetInstanceID() + " - " + adjacentTile.gameObject.transform.position.ToString());
				Path copiedPath = getNewPath(path);
				// Our A* heuristic is the straight line distance between our next tile and our target
				float heuristic = Vector2.Distance(adjacentTile.gameObject.transform.position, tileToMoveTo.gameObject.transform.position);
				
				// Add the full path back to our priority queue
				copiedPath.add(adjacentTile, heuristic, getTileCost(adjacentTile));
				priorityQueue.add(copiedPath);
			}
			
			// If 100 milliseconds have passed Yield the coroutine to let the rest of unity work for a bit (until the next frame)
			if (Time.time - startingTime > 0.1f) {
				yield return null;
				startingTime = Time.time;
			}
		}
		//Debug.Log("Found optimal path");
		setPath(path);
		
		// Finish the coroutine
		isCalculatingPath = false;
		return true;
	}
开发者ID:bajuwa,项目名称:IAT410,代码行数:76,代码来源:AntUnit.cs


注:本文中的PriorityQueue.getCount方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。