private void DebugDrawNavMesh()
    {
        RoomKey roomKey = _gameWorldController.Model.CurrentGame.CurrentRoomKey;

        AsyncRPGSharedLib.Navigation.NavMesh navMesh = PathfindingSystem.GetNavMesh(roomKey);

        if (navMesh != null)
        {
            for (uint navCellIndex = 0; navCellIndex < navMesh.GetNavCellCount(); navCellIndex++)
            {
                int connectivityId = navMesh.GetNavCellConnectivityID(navCellIndex);

                if (connectivityId != AsyncRPGSharedLib.Navigation.NavMesh.EMPTY_NAV_CELL)
                {
                    for (MathConstants.eDirection direction = MathConstants.eDirection.first;
                         direction < MathConstants.eDirection.count;
                         direction++)
                    {
                        if (!navMesh.NavCellHasNeighbor(navCellIndex, direction))
                        {
                            Point3d portalLeft, portalRight;
                            Vector3 portalVertexLeft, portalVertexRight;

                            navMesh.ComputePointsOnNavCellSide(navCellIndex, direction, out portalLeft, out portalRight);
                            portalVertexLeft  = ClientGameConstants.ConvertRoomPositionToVertexPosition(portalLeft);
                            portalVertexRight = ClientGameConstants.ConvertRoomPositionToVertexPosition(portalRight);

                            Debug.DrawLine(
                                portalVertexLeft,
                                portalVertexRight,
                                Color.yellow,
                                0.0f,   // duration
                                false); // depth test
                        }
                    }
                }
            }
        }
    }
    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);
                }
            }
        }
    }
    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 CreateMesh()
    {
        ClearMesh();

        m_gameObject = new GameObject("VisionCone_" + this.Name, typeof(MeshRenderer), typeof(MeshFilter));
        m_gameObject.transform.position = Vector3.zero;
        m_gameObject.transform.rotation = Quaternion.identity;

        m_meshFilter   = m_gameObject.GetComponent <MeshFilter>();
        m_meshRenderer = m_gameObject.GetComponent <MeshRenderer>();

        m_material = Resources.Load <Material>("Gfx/Materials/VisionCone");
        m_meshRenderer.renderer.material = m_material;
        m_mesh = m_meshFilter.mesh;

        {
            List <Vector3> vertices    = new List <Vector3>();
            List <Vector3> normals     = new List <Vector3>();
            List <Vector2> UVs         = new List <Vector2>();
            List <int>     indices     = new List <int>();
            List <Color>   colors      = new List <Color>();
            int            vertexCount = 0;

            float coneHalfRadians = (ConeAngle / 2.0f) * MathConstants.DEGREES_TO_RADIANS;
            float angleStep       = coneHalfRadians / 10;

            Point3d originRoomPoint  = new Point3d(0.0f, 0.0f, this.Depth);
            Point3d currentRoomPoint = new Point3d();
            Point3d nextRoomPoint    = new Point3d();

            Vector3 vertexOffset = ClientGameConstants.ConvertRoomPositionToVertexPosition(originRoomPoint);

            for (float angle = -coneHalfRadians; angle < coneHalfRadians; angle += angleStep)
            {
                float nextAngle    = angle + angleStep;
                float cosAngle     = (float)Math.Cos((float)angle);
                float sinAngle     = (float)Math.Sin((float)angle);
                float cosNextAngle = (float)Math.Cos((float)nextAngle);
                float sinNextAngle = (float)Math.Sin((float)nextAngle);

                currentRoomPoint.Set(Range * cosAngle, Range * sinAngle, Depth);
                nextRoomPoint.Set(Range * cosNextAngle, Range * sinNextAngle, Depth);

                Vector3 currentVertex = ClientGameConstants.ConvertRoomPositionToVertexPosition(currentRoomPoint) - vertexOffset;
                Vector3 nextVertex    = ClientGameConstants.ConvertRoomPositionToVertexPosition(nextRoomPoint) - vertexOffset;

                // Pie wedges
                {
                    // Create a triangle for each pie slice
                    vertices.Add(Vector3.zero);
                    vertices.Add(currentVertex);
                    vertices.Add(nextVertex);

                    // All normals face the camera
                    normals.Add(zVector);
                    normals.Add(zVector);
                    normals.Add(zVector);

                    // Make the visibility arc partially transparent
                    colors.Add(transparentWhite);
                    colors.Add(transparentWhite);
                    colors.Add(transparentWhite);

                    // Create the UVs for each vertex

                    // NOTE: UVs origin is in the lower left of the texture
                    //  _______(1,1)
                    //  |      |
                    //  |      |
                    //  |______|
                    // (0,0)
                    UVs.Add(textureCenter);
                    UVs.Add(new Vector2(0.5f + 0.5f * cosAngle, 0.5f + 0.5f * sinAngle));
                    UVs.Add(new Vector2(0.5f + 0.5f * cosNextAngle, 0.5f + 0.5f * sinNextAngle));


                    // Create the indices for the triangle that makes up the pie slice
                    indices.Add(vertexCount++);
                    indices.Add(vertexCount++);
                    indices.Add(vertexCount++);
                }

                // Edge highlight
                {
                    // Create a triangle for each pie slice
                    vertices.Add(currentVertex * 0.99f);
                    vertices.Add(currentVertex);
                    vertices.Add(nextVertex);
                    vertices.Add(currentVertex * 0.99f);
                    vertices.Add(nextVertex);
                    vertices.Add(nextVertex * 0.99f);

                    // All normals face the camera
                    normals.Add(zVector);
                    normals.Add(zVector);
                    normals.Add(zVector);
                    normals.Add(zVector);
                    normals.Add(zVector);
                    normals.Add(zVector);

                    // Make the visibility arc partially transparent
                    colors.Add(transparentWhite);
                    colors.Add(transparentWhite);
                    colors.Add(transparentWhite);
                    colors.Add(transparentWhite);
                    colors.Add(transparentWhite);
                    colors.Add(transparentWhite);

                    UVs.Add(textureCenter);
                    UVs.Add(textureCenter);
                    UVs.Add(textureCenter);
                    UVs.Add(textureCenter);
                    UVs.Add(textureCenter);
                    UVs.Add(textureCenter);

                    // Create the indices for the triangle that makes up the pie slice
                    indices.Add(vertexCount++);
                    indices.Add(vertexCount++);
                    indices.Add(vertexCount++);
                    indices.Add(vertexCount++);
                    indices.Add(vertexCount++);
                    indices.Add(vertexCount++);
                }
            }

            m_mesh.vertices  = vertices.ToArray();
            m_mesh.uv        = UVs.ToArray();
            m_mesh.triangles = indices.ToArray();
            m_mesh.normals   = normals.ToArray();
            m_mesh.colors    = colors.ToArray();
        }
    }