private void DebugDrawHoverWidget()
    {
        IWidget widget = _gameWorldController.View.WidgetEventDispatcher.MouseOverWidget;

        if (widget != null)
        {
            Vector3 boxUL =
                ClientGameConstants.ConvertPixelPositionToVertexPosition(
                    widget.WorldX, widget.WorldY, 0.0f);
            Vector3 boxUR =
                ClientGameConstants.ConvertPixelPositionToVertexPosition(
                    widget.WorldX + widget.Width, widget.WorldY, 0.0f);
            Vector3 boxLR =
                ClientGameConstants.ConvertPixelPositionToVertexPosition(
                    widget.WorldX + widget.Width, widget.WorldY + widget.Height, 0.0f);
            Vector3 boxLL =
                ClientGameConstants.ConvertPixelPositionToVertexPosition(
                    widget.WorldX, widget.WorldY + widget.Height, 0.0f);

            Debug.DrawLine(boxUL, boxUR, Color.red);
            Debug.DrawLine(boxUR, boxLR, Color.red);
            Debug.DrawLine(boxLR, boxLL, Color.red);
            Debug.DrawLine(boxLL, boxUL, Color.red);

            _widgetLabel.SetLocalPosition(widget.WorldX + (widget.Width / 2.0f), widget.WorldY + (widget.Height / 2.0f));
            _widgetLabel.Text    = widget.GetType().Name;
            _widgetLabel.Visible = true;
        }
    }
    public override void UpdateWorldPosition()
    {
        base.UpdateWorldPosition();

        if (m_gameObject != null)
        {
            m_gameObject.transform.position =
                ClientGameConstants.ConvertPixelPositionToVertexPosition(this.WorldX, this.WorldY, this.Depth);
        }
    }
Esempio n. 3
0
    public override void UpdateWorldPosition()
    {
        base.UpdateWorldPosition();

        if (m_spriteObject != null)
        {
            float   spriteZ        = 0.0f;
            Vector3 vertexPosition =
                ClientGameConstants.ConvertPixelPositionToVertexPosition(
                    this.WorldX - (m_style.Width / 2.0f),
                    this.WorldY - m_style.BoundsHeight,
                    spriteZ);

            m_spriteObject.transform.position = vertexPosition;
        }
    }
    private void DebugCursorPosition()
    {
        Point2d pixelPosition  = GetMousePixelPosition();
        Point2d screenPosition = ClientGameConstants.ConvertPixelPositionToScreenPosition(pixelPosition);
        Point3d roomPosition   = GameConstants.ConvertPixelPositionToRoomPosition(pixelPosition);
        Vector3 vertexPosition = ClientGameConstants.ConvertPixelPositionToVertexPosition(pixelPosition.x, pixelPosition.y, 0.0f);
        NavRef  navRef         = _gameWorldController.OverlayController.View.CurrentNavRef;

        _positionLabel.Text =
            string.Format(
                "Pixel: {0},{1}\nScreen: {2}, {3}\nRoom: {4}, {5}, {6}\nVertex: {7}, {8}, {9}\nNavRef: {10}",
                pixelPosition.x, pixelPosition.y,
                screenPosition.x, screenPosition.y,
                roomPosition.x, roomPosition.y, roomPosition.z,
                vertexPosition.x, vertexPosition.y, vertexPosition.z,
                navRef.NavCellIndex);
        _positionLabel.Visible = true;
    }
    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);
                }
            }
        }
    }
    public void LoadMap(
        int[] tileIndices,
        uint tileRows,
        uint tileColomns,
        string tileSetMaterialName,
        float tilePixelWidth,
        float tilePixelHeight)
    {
        ClearMap();

        TileRows        = tileRows;
        TileColomns     = tileColomns;
        TilePixelWidth  = tilePixelWidth;
        TilePixelHeight = tilePixelHeight;
        TileSet         = tileSetMaterialName;

        m_gameObject =
            new GameObject(
                "TileGrid_" + this.Name + "_" + tileSetMaterialName,
                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/TileSets/" + tileSetMaterialName + "_material");
        m_meshRenderer.renderer.material = m_material;
        m_mesh = m_meshFilter.mesh;

        // Generate a regular grid of vertices
        // Top to Bottom, Left to Right
        {
            List <Vector3> vertices      = new List <Vector3>();
            List <Vector2> UVs           = new List <Vector2>();
            List <int>     indices       = new List <int>();
            uint           tileListIndex = 0;

            for (int rowIndex = 0; rowIndex < tileRows; ++rowIndex)
            {
                for (int colIndex = 0; colIndex < tileColomns; ++colIndex)
                {
                    int tileIndex = tileIndices[tileListIndex];

                    if (tileListIndex >= 0)
                    {
                        int startVertexIndex = vertices.Count;

                        // Create 4 vertices in a square, starting from the upper left, going clockwise
                        vertices.Add(
                            ClientGameConstants.ConvertPixelPositionToVertexPosition(
                                this.WorldX + (float)(TilePixelWidth * colIndex),
                                this.WorldY + (float)(TilePixelHeight * rowIndex),
                                this.Depth));
                        vertices.Add(
                            ClientGameConstants.ConvertPixelPositionToVertexPosition(
                                this.WorldX + (float)(TilePixelWidth * (colIndex + 1)),
                                this.WorldY + (float)(TilePixelHeight * rowIndex),
                                this.Depth));
                        vertices.Add(
                            ClientGameConstants.ConvertPixelPositionToVertexPosition(
                                this.WorldX + (float)(TilePixelWidth * (colIndex + 1)),
                                this.WorldY + (float)(TilePixelHeight * (rowIndex + 1)),
                                this.Depth));
                        vertices.Add(
                            ClientGameConstants.ConvertPixelPositionToVertexPosition(
                                this.WorldX + (float)(TilePixelWidth * colIndex),
                                this.WorldY + (float)(TilePixelHeight * (rowIndex + 1)),
                                this.Depth));

                        // Create the UVs for each vertex
                        {
                            float   tileTexelWidth  = 0;
                            float   tileTexelHeight = 0;
                            Vector2 tileTexelOrigin =
                                ComputeTexelInfoForTileIndex(
                                    tileIndex, out tileTexelWidth, out tileTexelHeight);

                            // NOTE: UVs origin is in the lower left of the texture
                            //  _______(1,1)
                            //  |      |
                            //  |      |
                            //  |______|
                            // (0,0)
                            UVs.Add(tileTexelOrigin);
                            UVs.Add(new Vector2(tileTexelOrigin.x + tileTexelWidth, tileTexelOrigin.y));
                            UVs.Add(new Vector2(tileTexelOrigin.x + tileTexelWidth, tileTexelOrigin.y - tileTexelHeight));
                            UVs.Add(new Vector2(tileTexelOrigin.x, tileTexelOrigin.y - tileTexelHeight));
                        }


                        // Create the indices for the two triangles that make up the square
                        indices.Add(startVertexIndex + 0);
                        indices.Add(startVertexIndex + 1);
                        indices.Add(startVertexIndex + 2);
                        indices.Add(startVertexIndex + 0);
                        indices.Add(startVertexIndex + 2);
                        indices.Add(startVertexIndex + 3);
                    }

                    tileListIndex++;
                }
            }

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