Exemplo n.º 1
0
    // Fog of war
    void VisitTile(int index)
    {
        // Expanded vision around player
        int column, newX, newY, row = 0;

        PositionUtility.CalculatePosition(index, map.columns, out tmpX, out tmpY);
        // Find halfway point in fov
        var half = Mathf.FloorToInt(distance / 2f);

        // Shift over area to look at tiles around player
        tmpX -= half;
        tmpY -= half;

        var total = distance * distance;
        // Max columns of tiles to visit for reveal tiles
        var maxColumns = distance - 1;

        for (int i = 0; i < total; i++)
        {
            // Current column we're on
            column = i % distance;

            newX = column + tmpX;
            newY = row + tmpY;

            // Recalculate index starting loop on
            PositionUtility.CalculateIndex(newX, newY, map.columns, out index);

            // Make sure index in range
            if (index > -1 && index < map.tiles.Length)
            {
                var tile = map.tiles [index];
                tile.visited = true;
                DecorateTile(index);

                // Update neighbouring tiles
                foreach (var neighbour in tile.neighbours)
                {
                    if (neighbour != null)
                    {
                        if (!neighbour.visited)
                        {
                            neighbour.CalculateFoWAutotileID();
                            DecorateTile(neighbour.id);
                        }
                    }
                }
            }

            if (column == maxColumns)
            {
                row++;
            }
        }
    }
Exemplo n.º 2
0
    void CenterMap(int index)
    {
        var camPos = Camera.main.transform.position;
        var width  = map.columns;

        PositionUtility.CalculatePosition(index, width, out tmpX, out tmpY);

        camPos.x = tmpX * tileSize.x;
        camPos.y = -tmpY * tileSize.y;
        Camera.main.transform.position = camPos;
    }
Exemplo n.º 3
0
    // Figure out the next tile to move to, based on direction from key press
    public void MoveInDirection(Vector2 dir)
    {
        PositionUtility.CalculatePosition(currentTile, map.columns, out tmpX, out tmpY);

        // Add direction values to temp x and y
        tmpX += (int)dir.x;
        tmpY += (int)dir.y;

        // Create index of tile to move to
        PositionUtility.CalculateIndex(tmpX, tmpY, map.columns, out tmpIndex);

        // Move player to next tile, with animation
        MoveTo(tmpIndex, true);
    }
Exemplo n.º 4
0
    public void MoveTo(int index, bool animate = false)
    {
        // Only move if movement valid
        if (!CanMove(index))
        {
            return;
        }

        // Player can move, trigger beginning movement event
        if (moveActionCallback != null)
        {
            moveActionCallback();
        }

        currentTile = index;

        // Calculate new position
        PositionUtility.CalculatePosition(index, map.columns, out tmpX, out tmpY);

        // Calculate actual position in scene
        tmpX *= (int)tileSize.x;
        tmpY *= -(int)tileSize.y;

        var newPos = new Vector3(tmpX, tmpY, 0);

        // Move GameObject to new position
        if (!animate)
        {
            transform.position = newPos;
            // Trigger event when player lands on this tile
            if (tileActionCallback != null)
            {
                tileActionCallback(map.tiles[currentTile].autotileID);
            }
        }
        else
        {
            // Animate movement
            startPos = transform.position;
            endPos   = newPos;
            moveTime = 0;
            moving   = true;
        }
    }