private void btn_findPath_Click(object sender, EventArgs e) { if (points.Count != 2) { MessageBox.Show("Please select start point and end point!"); return; } var start = points[0]; var end = points[1]; Stopwatch stopwatch = new Stopwatch(); PathGrid pathGrid = new PathGrid(readMap.Width, readMap.Height, readMap.Maze); stopwatch.Start(); var tempPoints = pathGrid.FindPath(start, end); stopwatch.Stop(); if (tempPoints == null || tempPoints.Count == 0) { lbl_info.Text = "Can't find the path!"; return; } lbl_info.Text = $"times:{stopwatch.ElapsedMilliseconds}\r\nDistance:{tempPoints.Count}"; var newBitmap = (Bitmap)readMap.clippingZone.Clone(); pic_map.Image = newBitmap; foreach (var item in tempPoints) { newBitmap.SetPixel(item.X, item.Y, Color.Red); } pic_map.Image = newBitmap; }
// Update is called once per frame void Update() { Vector2 currentPos = transform.position; PathNode node = grid.NodeFromWorldPoint(currentPos); if (node != currentTile) { node.entitiesCurrentlyOnTile.Add(gameObject); if (node.entitiesCurrentlyOnTile.Count > 1) { foreach (GameObject obj in node.entitiesCurrentlyOnTile) { if (!obj.GetComponent <Entity>().isPlayer) { node.battling = true; BattleState state = new BattleState(boundEntity, obj.GetComponent <Entity>(), gameManager, node); battleEntryScreen.Open(transform.position, boundEntity.soldierAmount, obj.GetComponent <Entity>().soldierAmount, state); gameManager.currentBattleState = state; gameManager.openMenuLockActions(transform.position); } } } if (currentTile != null) { currentTile.entitiesCurrentlyOnTile.Remove(gameObject); } currentTile = node; } if (gameManager.playerCheckpoint && gameManager.playerCheckpointUpdated) { line.GetComponent <CurveLineRenderer>().ClearVertices(); if (pathNodes != null) { pathNodes.Clear(); } Vector2 sameZ = gameManager.playerCheckpoint.transform.position; Vector2 sameZPlayer = transform.position; if (Vector2.Distance(sameZ, sameZPlayer) > distanceToEnablePathfinding) { pathNodes = grid.FindPath(sameZPlayer, sameZ, boundEntity); pathNodes[pathNodes.Count - 1] = sameZ; currentPathNodeIndex = 0; gameManager.playerCheckpointUpdated = false; } else if (!sameZ.FuzzyEquals(sameZPlayer, 0.01f)) { Vector3 direction = gameManager.playerCheckpoint.transform.position - transform.position; direction.z = 0; direction.Normalize(); GameWorld.Terrain currentTerrain = grid.NodeFromWorldPoint(transform.position).weight; float movementModifier = boundEntity.terrainModifiers[currentTerrain]; Vector3 newPosition = transform.position + direction * movementModifier * gameManager.timeMultiplier * Time.deltaTime; transform.position = newPosition; } else { GameObject.Destroy(gameManager.playerCheckpoint); } } else if (gameManager.playerCheckpoint && !gameManager.playerCheckpointUpdated) { Vector3 checkpointPosition = pathNodes[currentPathNodeIndex]; Vector2 sameZEnd = gameManager.playerCheckpoint.transform.position; Vector2 sameZCheckpoint = checkpointPosition; Vector2 sameZPlayer = transform.position; linePosition.Clear(); linePosition.Add(new Vector3(sameZPlayer.x, sameZPlayer.y, lineZ)); line.GetComponent <CurveLineRenderer>().ClearVertices(); for (int i = currentPathNodeIndex; i < pathNodes.Count; ++i) { linePosition.Add(new Vector3(pathNodes[i].x, pathNodes[i].y, lineZ)); } line.GetComponent <CurveLineRenderer>().SetVertices(linePosition); if (!sameZCheckpoint.FuzzyEquals(sameZPlayer, 0.025f)) { Vector2 direction2D = sameZCheckpoint - sameZPlayer; Vector3 direction = direction2D; direction.Normalize(); GameWorld.Terrain currentTerrain = grid.NodeFromWorldPoint(transform.position).weight; float movementModifier = boundEntity.terrainModifiers[currentTerrain] * boundEntity.pace * boundEntity.speed;; Vector3 newPosition = transform.position + direction * movementModifier * gameManager.timeMultiplier * Time.deltaTime; transform.position = newPosition; } else { transform.position = new Vector3(sameZCheckpoint.x, sameZCheckpoint.y, transform.position.z); if (currentPathNodeIndex + 1 <= pathNodes.Count - 1) { ++currentPathNodeIndex; } else { gameManager.playerCheckpointUpdated = true; } } } }
public PatrolStatus TickMovement(float dt) { Vector2 currentPos = transform.position; PathNode node = grid.NodeFromWorldPoint(currentPos); if (node != currentTile) { node.entitiesCurrentlyOnTile.Add(gameObject); if (node.entitiesCurrentlyOnTile.Count > 1) { foreach (GameObject obj in node.entitiesCurrentlyOnTile) { if (obj.GetComponent <Entity>().isPlayer&& !node.battling) { BattleState state = new BattleState(obj.GetComponent <Entity>(), boundEntity, gameManager, node); battleEntryScreen.Open(transform.position, boundEntity.soldierAmount, obj.GetComponent <Entity>().soldierAmount, state); gameManager.currentBattleState = state; gameManager.openMenuLockActions(transform.position); node.battling = true; } } } if (currentTile != null) { currentTile.entitiesCurrentlyOnTile.Remove(gameObject); } currentTile = node; } if (!usingPathfinding) { Vector2 sameZcheckpoint = currentCheckpoint; Vector2 sameZ = transform.position; if (Vector2.Distance(sameZ, sameZcheckpoint) > distanceToEnablePathfinding) { pathNodes = grid.FindPath(sameZ, sameZcheckpoint, boundEntity); pathNodes[pathNodes.Count - 1] = sameZcheckpoint; currentPathNodeIndex = 0; usingPathfinding = true; return(PatrolStatus.Ongoing); } else if (!sameZ.FuzzyEquals(sameZcheckpoint, 0.01f)) { Vector3 direction = sameZcheckpoint - sameZ; direction.z = 0; direction.Normalize(); GameWorld.Terrain currentTerrain = grid.NodeFromWorldPoint(transform.position).weight; if (currentTerrain == GameWorld.Terrain.Forest) { GetComponent <Entity>().SetHidden(true); } else { GetComponent <Entity>().SetHidden(false); } float movementModifier = boundEntity.terrainModifiers[currentTerrain] * boundEntity.pace * boundEntity.speed; Vector3 newPosition = transform.position + direction * movementModifier * gameManager.timeMultiplier * Time.deltaTime; transform.position = newPosition; return(PatrolStatus.Ongoing); } else { //REACHED THE DESTINATION return(PatrolStatus.Finished); } } else if (usingPathfinding) { Vector3 checkpointPosition = pathNodes[currentPathNodeIndex]; Vector2 sameZEnd = currentCheckpoint; Vector2 sameZCheckpoint = checkpointPosition; Vector2 sameZPlayer = transform.position; if (!sameZCheckpoint.FuzzyEquals(sameZPlayer, 0.025f)) { Vector2 direction2D = sameZCheckpoint - sameZPlayer; Vector3 direction = direction2D; direction.Normalize(); GameWorld.Terrain currentTerrain = grid.NodeFromWorldPoint(transform.position).weight; if (currentTerrain == GameWorld.Terrain.Forest) { GetComponent <Entity>().SetHidden(true); } else { GetComponent <Entity>().SetHidden(false); } float movementModifier = boundEntity.terrainModifiers[currentTerrain] * boundEntity.pace * boundEntity.speed; Vector3 newPosition = transform.position + direction * movementModifier * gameManager.timeMultiplier * Time.deltaTime; transform.position = newPosition; } else { transform.position = new Vector3(sameZCheckpoint.x, sameZCheckpoint.y, transform.position.z); if (currentPathNodeIndex + 1 <= pathNodes.Count - 1) { ++currentPathNodeIndex; } else { usingPathfinding = false; } } } return(PatrolStatus.Ongoing); }
/// <summary> /// 寻路 /// </summary> /// <param name="position"></param> /// <param name="to"></param> /// <returns></returns> protected ActionResult <List <APoint> > FindPath(PositionInfo position, APoint to) { PathGrid pathGrid = new PathGrid(MirContext.ReadMap.Width, MirContext.ReadMap.Height, MirContext.Maze(position.MapInfo)); return(new ActionResult <List <APoint> >(pathGrid.FindPath(position.Point, to))); }