Example #1
0
    void DragDropStartEndTiles()
    {
        if (Input.GetMouseButtonDown(0))
        {
            int    x = 0, z = 0;
            TDTile draggedTile = GetSelectedTile(ref x, ref z);
            if (draggedTile == null)
            {
                return;
            }
            if (draggedTile.GetTileType() != (int)TILE_TYPE.STARTPOINT && draggedTile.GetTileType() != (int)TILE_TYPE.ENDPOINT)
            {
                return;
            }

            isDragged = true;

            draggedTileType = draggedTile.GetTileType();
            lastTileCoords  = new Vector2(x, z);
        }

        if (Input.GetMouseButtonUp(0))
        {
            if (isDragged)
            {
                RefreshAlgorithm();
            }

            isDragged = false;
        }

        if (isDragged)
        {
            int    x = 0, z = 0;
            TDTile selectedTile = GetSelectedTile(ref x, ref z);
            if (selectedTile == null || selectedTile.GetTileType() == (int)TILE_TYPE.WATER || selectedTile.GetTileType() == (int)TILE_TYPE.WALL ||
                selectedTile.GetTileType() == (int)TILE_TYPE.STARTPOINT || selectedTile.GetTileType() == (int)TILE_TYPE.ENDPOINT)
            {
                return;
            }

            int newTileType = draggedTileType;

            // set old tile
            Vector2 currentTileCoords = new Vector2(x, z);
            if (currentTileCoords != lastTileCoords)
            {
                DragDropRefresh(currentTileCoords, selectedTile, newTileType);
            }
        }
    }
Example #2
0
    private void SetTileTextureSelfMapCreation(TDTile tile, int tileType, int x, int z)
    {
        // start & stop tile can not be overwritten
        if (tile.GetTileType() == (int)TILE_TYPE.STARTPOINT || tile.GetTileType() == (int)TILE_TYPE.ENDPOINT)
        {
            return;
        }

        tile.SetTileType(tileType);

        texture.SetPixels(x * tileResolution, z * tileResolution, tileResolution, tileResolution, spriteArray[tileType]);
        //texture.SetPixels(x * tileResolution, z * tileResolution, tileResolution, tileResolution, tilePixels[tileType]);
        texture.Apply();
    }
Example #3
0
    public override void StartAlgorithm(TDTile start, TDTile end, TGMap map)
    {
        algoSteps.Clear();

        SimplePriorityQueue <TDTile> frontier = new SimplePriorityQueue <TDTile>();

        frontier.Enqueue(start, 0);

        Dictionary <TDTile, TDTile> cameFrom = new Dictionary <TDTile, TDTile>();

        cameFrom.Add(start, null);

        Dictionary <TDTile, float> costSoFar = new Dictionary <TDTile, float>();

        costSoFar.Add(start, 0);

        float priority = 0.0f;

        while (frontier.Count > 0)
        {
            TDTile currentTile = frontier.Dequeue();
            if (currentTile.GetTileType() == (int)TILE_TYPE.ENDPOINT)
            {
                break;
            }

            AlgorithmStep algoStep = new AlgorithmStep(currentTile);
            algoSteps.Add(algoStep);

            foreach (TDTile nextTile in currentTile.neighbours)
            {
                if (nextTile == null || nextTile.GetTileType() == (int)TILE_TYPE.WATER || nextTile.GetTileType() == (int)TILE_TYPE.WALL)
                {
                    continue;
                }

                // diagonal step check: if neighbour is diagonal but diagonal step not allowed --> we skip
                if (IsDiagonalNeighbour(currentTile, nextTile) && !IsDiagonalStepAllowed())
                {
                    continue;
                }

                algoTiles.Add(nextTile);

                float newCost = costSoFar[currentTile] + map.GetCostByTileType(nextTile.GetTileType());

                if (!costSoFar.ContainsKey(nextTile) || newCost < costSoFar[nextTile])
                {
                    costSoFar[nextTile] = newCost;
                    priority            = newCost;
                    frontier.Enqueue(nextTile, priority);
                    cameFrom.Add(nextTile, currentTile);
                    algoStep.NeighbourTiles.Add(nextTile);
                }
            }
        }

        GeneratePath(end, cameFrom);
    }
Example #4
0
    public override void StartAlgorithm(TDTile start, TDTile end, TGMap map)
    {
        algoSteps.Clear();

        SimplePriorityQueue <TDTile> frontier = new SimplePriorityQueue <TDTile>();

        frontier.Enqueue(start, 0);

        Dictionary <TDTile, TDTile> cameFrom = new Dictionary <TDTile, TDTile>();

        cameFrom.Add(start, null);

        float priority = 0.0f;

        while (frontier.Count > 0)
        {
            TDTile currentTile = frontier.Dequeue();
            Debug.Log("Dequeue Tile: " + currentTile.GetHashCode());
            if (currentTile.GetTileType() == (int)TILE_TYPE.ENDPOINT)
            {
                break;
            }

            AlgorithmStep algoStep = new AlgorithmStep(currentTile);
            algoSteps.Add(algoStep);

            foreach (TDTile nextTile in currentTile.neighbours)
            {
                if (nextTile == null || nextTile.GetTileType() == (int)TILE_TYPE.WATER || nextTile.GetTileType() == (int)TILE_TYPE.WALL)
                {
                    continue;
                }

                // diagonal step check: if neighbour is diagonal but diagonal step not allowed --> we skip
                if (IsDiagonalNeighbour(currentTile, nextTile) && !IsDiagonalStepAllowed())
                {
                    continue;
                }

                algoTiles.Add(nextTile);

                if (!cameFrom.ContainsKey(nextTile))
                {
                    priority = Heuristic(end, nextTile) + ComputeVectorCrossProduct(start, end, nextTile);                     // TODO: add priority field to TDTile? to debug and or display priority in game
                    frontier.Enqueue(nextTile, priority);
                    //Debug.Log("Enqueue Tile: " + nextTile.GetHashCode() + " - priority: " + priority);
                    cameFrom.Add(nextTile, currentTile);
                    algoStep.NeighbourTiles.Add(nextTile);                     // WRONG! WE NEED TO LOOK AT THE TILE WITH LOWEST HEURISTIC FIRST
                }
            }
        }

        GeneratePath(end, cameFrom);
    }
Example #5
0
    private void SetTileTexture(TDTile tile, int tileType, int x, int z)
    {
        // we don't need to set old tile type if current type is PATH_PAST or PATH_CURRENT
        if (tile.GetTileType() != (int)TILE_TYPE.PATH_NEXT && tile.GetTileType() != (int)TILE_TYPE.PATH_CURRENT)
        {
            tile.SetOldTileType(tile.GetTileType());
        }
        tile.SetTileType(tileType);

        if (tileType == (int)TILE_TYPE.STARTPOINT)
        {
            map.SetStartPoint(tile);
        }
        else if (tileType == (int)TILE_TYPE.ENDPOINT)
        {
            map.SetEndPoint(tile);
        }

        texture.SetPixels(x * tileResolution, z * tileResolution, tileResolution, tileResolution, spriteArray[tileType]);
        //texture.SetPixels(x * tileResolution, z * tileResolution, tileResolution, tileResolution, tilePixels[tileType]);
        texture.Apply();
    }
Example #6
0
    void DrawTiles(int tileType)
    {
        if (Input.GetMouseButtonDown(0) || isPressed)
        {
            int    x = 0, z = 0;
            TDTile tile = GetSelectedTile(ref x, ref z);
            if (tile == null)
            {
                return;
            }

            if (!isPressed)
            {
                ClearMap();
            }

            isPressed = true;

            if (tile.GetTileType() == (int)TILE_TYPE.STARTPOINT || tile.GetTileType() == (int)TILE_TYPE.ENDPOINT)
            {
                return;
            }

            SetTileTextureSelfMapCreation(tile, tileType, x, z);
            RefreshMap();
        }

        if (Input.GetMouseButtonUp(0))
        {
            if (isPressed)
            {
                RefreshAlgorithm();
            }

            isPressed = false;
        }
    }
    public override void StartAlgorithm(TDTile start, TDTile end, TGMap map = null)
    {
        algoSteps.Clear();

        Queue <TDTile> frontier = new Queue <TDTile>();

        frontier.Enqueue(start);

        Dictionary <TDTile, TDTile> cameFrom = new Dictionary <TDTile, TDTile>();

        cameFrom.Add(start, null);

        while (frontier.Count > 0)
        {
            TDTile currentTile = frontier.Dequeue();
            if (currentTile.GetTileType() == (int)TILE_TYPE.ENDPOINT)
            {
                break;
            }

            AlgorithmStep algoStep = new AlgorithmStep(currentTile);
            algoSteps.Add(algoStep);

            foreach (TDTile nextTile in currentTile.neighbours)
            {
                if (nextTile == null || nextTile.GetTileType() == (int)TILE_TYPE.WATER || nextTile.GetTileType() == (int)TILE_TYPE.WALL)
                {
                    continue;                                                                                                                                      // || == START TYLE !?
                }
                // diagonal step check: if neighbour is diagonal but diagonal step not allowed --> we skip
                if (IsDiagonalNeighbour(currentTile, nextTile) && !IsDiagonalStepAllowed())
                {
                    continue;
                }

                if (!cameFrom.ContainsKey(nextTile))
                {
                    frontier.Enqueue(nextTile);
                    cameFrom.Add(nextTile, currentTile);
                    algoStep.NeighbourTiles.Add(nextTile);
                }
            }
        }

        GeneratePath(end, cameFrom);
    }
Example #8
0
    protected void GeneratePath(TDTile end, Dictionary <TDTile, TDTile> cameFrom)
    {
        if (cameFrom == null || cameFrom.Count == 0)
        {
            return;
        }

        TDTile currentTile = end;

        path = new List <TDTile>();

        while (currentTile.GetTileType() != (int)TILE_TYPE.STARTPOINT)
        {
            path.Add(currentTile);
            if (!cameFrom.TryGetValue(currentTile, out currentTile))
            {
                return;
            }
        }

        path.Reverse();
    }
Example #9
0
    void BuildTexture()
    {
        map = new TDMap(gridSizeX, gridSizeZ);

        int texWidth  = gridSizeX * tileResolution;
        int texHeight = gridSizeZ * tileResolution;

        texture    = new Texture2D(texWidth, texHeight);
        oldTexture = new Texture2D(texWidth, texHeight);

        for (int y = 0; y < gridSizeZ; y++)
        {
            for (int x = 0; x < gridSizeX; x++)
            {
                TDTile tile = map.GetTile(x, y);
                if (tile == null)
                {
                    continue;
                }

                //texture.SetPixels(x * tileResolution, y * tileResolution, tileResolution, tileResolution, tilePixels[tile.GetTileType()]);
                texture.SetPixels(x * tileResolution, y * tileResolution, tileResolution, tileResolution, spriteArray[tile.GetTileType()]);
            }
        }

        texture.filterMode = FilterMode.Point;
        texture.wrapMode   = TextureWrapMode.Clamp;
        texture.Apply();

        MeshRenderer meshRenderer = GetComponent <MeshRenderer>();

        meshRenderer.sharedMaterials[0].mainTexture = texture;

        Debug.Log("Done Texture!");
    }