private void ComputeEndpoints() { m_startNavRef = m_navMesh.ComputeNavRefAtPoint(m_startPosition); m_endNavRef = m_navMesh.ComputeNavRefAtPoint(m_endPosition); if (m_startNavRef.IsValid) { if (m_endNavRef.IsValid) { if (m_navMesh.AreNavRefsConnected(m_startNavRef, m_endNavRef)) { if (m_startNavRef.Equals(m_endNavRef)) { // We can head directly to the destination m_finalPath.Add(new PathStep(m_startNavRef, m_startPosition)); m_finalPath.Add(new PathStep(m_endNavRef, m_endPosition)); m_state = eState.complete; } else { // We have to do the expensive query m_state = eState.setup_raw_path; } } else { m_state = eState.complete; m_resultCode = eResult.failed_start_off_nav_mesh; } } else { m_state = eState.complete; m_resultCode = eResult.failed_end_off_nav_mesh; } } else { m_state = eState.complete; m_resultCode = eResult.failed_start_off_nav_mesh; } }
private void UpdateCurrentNavRef() { SessionData sessionData = SessionData.GetInstance(); GameData gameData = sessionData.CurrentGameData; RoomData roomData = gameData.GetCachedRoomData(gameData.CurrentRoomKey); if (roomData != null) { AsyncRPGSharedLib.Navigation.NavMesh navMesh = roomData.StaticRoomData.NavMesh; Point2d pixelPoint = WidgetEventDispatcher.GetMousePosition(); Point3d roomPoint = GameConstants.ConvertPixelPositionToRoomPosition(pixelPoint); m_currentNavRef = navMesh.ComputeNavRefAtPoint(roomPoint); } }
// Utilities public bool IsWorldPointOnNavMesh(Point3d testPoint) { GameData gameData = CurrentGame; RoomData roomData = gameData.GetCachedRoomData(gameData.CurrentRoomKey); bool pointOnNavMesh = false; if (roomData != null) { AsyncRPGSharedLib.Navigation.NavMesh navMesh = roomData.StaticRoomData.NavMesh; NavRef navRef = navMesh.ComputeNavRefAtPoint(testPoint); if (navRef != null && navRef.IsValid) { pointOnNavMesh = true; } } return(pointOnNavMesh); }
private void DebugDrawTestVisibility() { int characterID = _gameWorldController.Model.CurrentCharacterID; RoomKey roomKey = _gameWorldController.Model.CurrentGame.CurrentRoomKey; CharacterEntity entity = _gameWorldController.Model.GetCharacterEntity(characterID); if (entity != null) { Point3d startWorldPos = entity.Position; Point2d endPixelPos = GetMousePixelPosition(); Point3d endWorldPos = GameConstants.ConvertPixelPositionToRoomPosition(endPixelPos); // Draw the target nav cell AsyncRPGSharedLib.Navigation.NavMesh navMesh = PathfindingSystem.GetNavMesh(roomKey); if (navMesh != null) { NavRef startNavRef = navMesh.ComputeNavRefAtPoint(startWorldPos); NavRef endNavRef = navMesh.ComputeNavRefAtPoint(endWorldPos); if (startNavRef.IsValid && endNavRef.IsValid) { bool canSee = navMesh.NavRefCanSeeOtherNavRef(startNavRef, endNavRef); Color debugColor = canSee ? Color.green : Color.red; // Draw a box around the nav cell { Point3d endCenterWorldPos = navMesh.ComputeNavCellCenter((uint)endNavRef.NavCellIndex); Point2d endCenterPixelPos = GameConstants.ConvertRoomPositionToPixelPosition(endCenterWorldPos); float halfWidth = (float)GameConstants.NAV_MESH_PIXEL_SIZE / 2.0f; Vector3 boxUL = ClientGameConstants.ConvertPixelPositionToVertexPosition( endCenterPixelPos.x - halfWidth, endCenterPixelPos.y - halfWidth, 0.0f); Vector3 boxUR = ClientGameConstants.ConvertPixelPositionToVertexPosition( endCenterPixelPos.x + halfWidth, endCenterPixelPos.y - halfWidth, 0.0f); Vector3 boxLR = ClientGameConstants.ConvertPixelPositionToVertexPosition( endCenterPixelPos.x + halfWidth, endCenterPixelPos.y + halfWidth, 0.0f); Vector3 boxLL = ClientGameConstants.ConvertPixelPositionToVertexPosition( endCenterPixelPos.x - halfWidth, endCenterPixelPos.y + halfWidth, 0.0f); Debug.DrawLine(boxUL, boxUR, debugColor); Debug.DrawLine(boxUR, boxLR, debugColor); Debug.DrawLine(boxLR, boxLL, debugColor); Debug.DrawLine(boxLL, boxUL, debugColor); } // Update the visibility status label _visibilityStatusLabel.Text = canSee ? "VISIBLE" : "INVISIBLE"; _visibilityStatusLabel.SetLocalPosition(endPixelPos.x, endPixelPos.y); _visibilityStatusLabel.Color = debugColor; _visibilityStatusLabel.Visible = true; // Render the ray-cast line { Vector3 startVertex = ClientGameConstants.ConvertRoomPositionToVertexPosition(startWorldPos); Vector3 endVertex = ClientGameConstants.ConvertRoomPositionToVertexPosition(endWorldPos); Debug.DrawLine(startVertex, endVertex, debugColor); } } } } }
private void DebugDrawTestPath() { int characterID = _gameWorldController.Model.CurrentCharacterID; RoomKey roomKey = _gameWorldController.Model.CurrentGame.CurrentRoomKey; CharacterEntity entity = _gameWorldController.Model.GetCharacterEntity(characterID); if (entity != null) { Point2d mousePixelPos = GetMousePixelPosition(); Point3d mouseWorldPos = GameConstants.ConvertPixelPositionToRoomPosition(mousePixelPos); // Draw the target nav cell string targetLabel = ""; AsyncRPGSharedLib.Navigation.NavMesh navMesh = PathfindingSystem.GetNavMesh(roomKey); if (navMesh != null) { NavRef navRef = navMesh.ComputeNavRefAtPoint(mouseWorldPos); if (navRef.IsValid) { Point3d centerWorldPos = navMesh.ComputeNavCellCenter((uint)navRef.NavCellIndex); Point2d centerPixelPos = GameConstants.ConvertRoomPositionToPixelPosition(centerWorldPos); float halfWidth = (float)GameConstants.NAV_MESH_PIXEL_SIZE / 2.0f; Vector3 boxUL = ClientGameConstants.ConvertPixelPositionToVertexPosition( centerPixelPos.x - halfWidth, centerPixelPos.y - halfWidth, 0.0f); Vector3 boxUR = ClientGameConstants.ConvertPixelPositionToVertexPosition( centerPixelPos.x + halfWidth, centerPixelPos.y - halfWidth, 0.0f); Vector3 boxLR = ClientGameConstants.ConvertPixelPositionToVertexPosition( centerPixelPos.x + halfWidth, centerPixelPos.y + halfWidth, 0.0f); Vector3 boxLL = ClientGameConstants.ConvertPixelPositionToVertexPosition( centerPixelPos.x - halfWidth, centerPixelPos.y + halfWidth, 0.0f); Debug.DrawLine(boxUL, boxUR, Color.blue); Debug.DrawLine(boxUR, boxLR, Color.blue); Debug.DrawLine(boxLR, boxLL, Color.blue); Debug.DrawLine(boxLL, boxUL, Color.blue); targetLabel = "\nNavCell=" + navRef.NavCellIndex.ToString(); } // Attempt to compute a path from the active player to the mouse _pathComputer.BlockingPathRequest(navMesh, roomKey, entity.Position, mouseWorldPos); // Update the path status label { if (_pathComputer.ResultCode == PathComputer.eResult.success) { _pathStatusLabel.Text = "VALID" + targetLabel; _pathStatusLabel.Color = Color.green; } else { _pathStatusLabel.Text = _pathComputer.ResultCode + targetLabel; _pathStatusLabel.Color = Color.red; } _pathStatusLabel.SetLocalPosition(mousePixelPos.x, mousePixelPos.y); _pathStatusLabel.Visible = true; } // Render the raw path for (int stepIndex = 1; stepIndex < _pathComputer.FinalPath.Count; stepIndex++) { Point3d previousPoint = _pathComputer.FinalPath[stepIndex - 1].StepPoint; Point3d currentPoint = _pathComputer.FinalPath[stepIndex].StepPoint; Vector3 previousPixelPoint = ClientGameConstants.ConvertRoomPositionToVertexPosition(previousPoint); Vector3 currentPixelPoint = ClientGameConstants.ConvertRoomPositionToVertexPosition(currentPoint); Debug.DrawLine(previousPixelPoint, currentPixelPoint, Color.green); } } } }