예제 #1
0
    /// <summary>
    /// Updates the visible tiles.
    /// </summary>
    /// <param name='posI'>
    /// Position I index.
    /// </param>
    /// <param name='posJ'>
    /// Position J index.
    /// </param>
    public void UpdateVisibleTiles(int posI, int posJ)
    {
        Dictionary <int, List <GameObject> > newObjects = new Dictionary <int, List <GameObject> > ();

        //for every tile, that should be visible
        for (int j = -visibleTilesInFront; j < visibleTilesInFront; j++)
        {
            for (int i = -visibleTilesInFront; i < visibleTilesInFront; i++)
            {
                int objI = i + posI;
                int objJ = j + posJ;
                int code = DungeonUtils.GetCodeOfTileByIndex(objI, j + posJ);

                // If objects for this tile already instantiated
                if (createdObjects.ContainsKey(code))
                {
                    newObjects.Add(code, createdObjects [code]);
                    // Remove this list from old dictionary, so this objects will not be destroyed
                    createdObjects.Remove(code);
                }
                else                     // else if objects hasn't been instantiated for this tile, create it
                {
                    if (CurrentDungeon.Tiles.ContainsKey(code))
                    {
                        DungeonDataTile tile = CurrentDungeon.Tiles [code];
                        // Create new list
                        List <GameObject> tileObjects = new List <GameObject> ();
                        newObjects.Add(code, tileObjects);

                        // Instantiate every tile object
                        for (int objIndex = 0; objIndex < tile.Objects.Count; objIndex++)
                        {
                            DungeonObject dungeonObject = tile.Objects [objIndex];
                            GameObject    newObject     = Instantiate(
                                ResourceManager.GetResource(dungeonObject.type, dungeonObject.prefabReference) as Object,
                                DungeonUtils.GetPositionByIndex(objI, objJ) + dungeonObject.offset,
                                dungeonObject.rotation
                                ) as GameObject;
                            newObject.name = dungeonObject.prefabReference + "_" + code;
                            tileObjects.Add(newObject);
                        }
                    }
                }
            }
        }

        // Remove objects from dictionaries differences
        foreach (List <GameObject> objectListToRemove in createdObjects.Values)
        {
            for (int i = 0; i < objectListToRemove.Count; i++)
            {
                Destroy(objectListToRemove [i]);
            }
        }

        createdObjects = newObjects;
    }
예제 #2
0
    /// <summary>
    /// Returns a <see cref="System.String"/> that represents the current <see cref="DungeonData"/>.
    /// </summary>
    /// <returns>
    /// A <see cref="System.String"/> that represents the current <see cref="DungeonData"/>.
    /// </returns>
    public override string ToString()
    {
        string ret = "";

        ret += "Dungeon " + width + "x" + height + ". Entrance in " + entranceX + ":" + entranceY + "\n";

        ret += "\nRooms: ";
        for (int i = 0; i < rooms.Count; i++)
        {
            ret += ("Room" + i + " (" + Rooms [i].x + "," + Rooms [i].y + "," + Rooms [i].width + "," + Rooms [i].height + "), ");
        }

        ret += "\n\nRoads: ";
        for (int i = 0; i < rooms.Count; i++)
        {
            ret += ("Road" + i + " (" + Roads [i].x1 + "," + Roads [i].y1 + "," + Roads [i].x2 + "," + Roads [i].y2 + "), ");
        }

        ret += "\n\nMap:\n";


        string[,] tiles = new string[width, height];
        foreach (KeyValuePair <int, DungeonDataTile> kvp in Tiles)
        {
            int             code = kvp.Key;
            DungeonDataTile tile = kvp.Value;

            int i, j;
            DungeonUtils.GetIndexesOfTileByCode(code, out i, out j);
            if (tile.room != null)
            {
                tiles [i, j] = "0 ";
            }
            else if (tile.road != null)
            {
                tiles [i, j] = "X ";
            }
            else
            {
                tiles [i, j] = "- ";
            }
        }

        for (int j = 0; j < height; j++)
        {
            for (int i = 0; i < width; i++)
            {
                ret += tiles [i, j];
            }
            ret += "\n";
        }

        return(ret);
    }
예제 #3
0
    /// <summary>
    /// Retrieves the tiles of this dungeon.
    /// </summary>
    /// <returns>
    /// The tiles of this dungeon.
    /// </returns>
    public Dictionary <int, DungeonDataTile> GetTiles()
    {
        DungeonDataTile[,] tilesGrid = new DungeonDataTile[width, height];

        for (int j = 0; j < height; j++)
        {
            for (int i = 0; i < width; i++)
            {
                tilesGrid [i, j] = new DungeonDataTile();
            }
        }

        for (int k = 0; k < roads.Count; k++)
        {
            for (int j = 0; j <= roads[k].y2 - roads[k].y1; j++)
            {
                for (int i = 0; i <= roads[k].x2 - roads[k].x1; i++)
                {
                    tilesGrid [i + roads [k].x1, j + roads [k].y1].road = roads [k];
                }
            }
        }

        for (int k = 0; k < rooms.Count; k++)
        {
            for (int j = 0; j < rooms[k].height; j++)
            {
                for (int i = 0; i < rooms[k].width; i++)
                {
                    tilesGrid [i + rooms [k].x, j + rooms [k].y].room = rooms [k];
                }
            }
        }

        Dictionary <int, DungeonDataTile> ret = new Dictionary <int, DungeonDataTile> (width * height);

        for (int j = 0; j < height; j++)
        {
            for (int i = 0; i < width; i++)
            {
                ret.Add(DungeonUtils.GetCodeOfTileByIndex(i, j), tilesGrid [i, j]);
            }
        }

        return(ret);
    }
예제 #4
0
    /// <summary>
    /// Prepares the coridors decoration.
    /// </summary>
    /// <param name='dungeon'>
    /// Dungeon data.
    /// </param>
    public static void PrepareCoridorsDecoration(DungeonData dungeon)
    {
        List <DungeonRoad> roads = dungeon.Roads;

        for (int c = 0; c < roads.Count; c++)
        {
            DungeonRoad corridor = roads [c];

            // for every tile in corridor
            for (int j = corridor.y1; j <= corridor.y2; j++)
            {
                for (int i = corridor.x1; i <= corridor.x2; i++)
                {
                    int code = DungeonUtils.GetCodeOfTileByIndex(i, j);
                    #region Lights
                    // 50% to create a torchlight
                    if (Random.value > 0.5f)
                    {
                        DungeonDataTile tile = dungeon.Tiles [code];

                        //if it is a corridor
                        if (tile.room == null)
                        {
                            for (int o = 0; o < tile.Objects.Count; o++)
                            {
                                if (tile.Objects [o].type == DungeonObjectType.Wall)
                                {
                                    DungeonObject newTorch = new DungeonObject(code, DungeonObjectType.LightSource, "Torch");
                                    newTorch.offset   = tile.Objects [o].offset;
                                    newTorch.rotation = tile.Objects [o].rotation;
                                    tile.Objects.Add(newTorch);
                                    break;
                                }
                            }
                        }
                    }
                    #endregion
                }
            }
        }
    }
예제 #5
0
    /// <summary>
    /// Checks the tile moveability.
    /// </summary>
    /// <returns>
    /// Can creature make move to tile in specified direction from current tile or can not.
    /// </returns>
    /// <param name='moveDirection'>
    /// Absolute direction of target tile relatively to current
    /// </param>
    protected bool CheckTileMoveability(Direction moveDirection)
    {
        DungeonDataTile tile = Dm.CurrentDungeon.Tiles [DungeonUtils.GetCodeOfTileByIndex(_currentI, _currentJ)];
        Quaternion      rot;

        // Get rotation that should have wall to stop moving
        switch (moveDirection)
        {
        case Direction.Forward:
            rot = DungeonUtils.GetRotationFromToTile(0, 0, 0, 1);
            break;

        case Direction.Right:
            rot = DungeonUtils.GetRotationFromToTile(1, 0, 0, 0);
            break;

        case Direction.Backward:
            rot = DungeonUtils.GetRotationFromToTile(0, 1, 0, 0);
            break;

        case Direction.Left:
            rot = DungeonUtils.GetRotationFromToTile(0, 0, 1, 0);
            break;

        default:
            return(false);
        }

        // Check if there is any wall nearly rotated against to movementDirection
        for (int i = 0; i < tile.Objects.Count; i++)
        {
            if (tile.Objects [i].type == DungeonObjectType.Wall && Quaternion.Angle(rot, tile.Objects [i].rotation) < 10)
            {
                return(false);
            }
        }

        return(true);
    }
예제 #6
0
    /// <summary>
    /// Prepares tile objects such as a walls, passages, floors and ceilings.
    /// </summary>
    /// <param name='dungeon'>
    /// Dungeon.
    /// </param>
    public static void PrepareBaseGeometry(DungeonData dungeon)
    {
        Dictionary <int, DungeonDataTile> tiles = dungeon.Tiles;

        // For every tile in map
        foreach (KeyValuePair <int, DungeonDataTile> kvp in tiles)
        {
            int             code = kvp.Key;
            DungeonDataTile tile = kvp.Value;

            int i, j;
            DungeonUtils.GetIndexesOfTileByCode(code, out i, out j);

            // If current tile is a room..
            if (tiles [code].room != null)
            {
                // Set floor
                DungeonObject floor = new DungeonObject(code, DungeonObjectType.Floor, "dirt_floor");
                tile.Objects.Add(floor);

                // Set ceiling
                DungeonObject ceiling = new DungeonObject(code, DungeonObjectType.Ceiling, "dirt_ceiling");
                tile.Objects.Add(ceiling);

                // LEFT wall
                // If it is a left border set left wall
                if (i == 0)
                {
                    DungeonObject newWall = PrepareWallObject(code, DungeonObjectType.Wall, "wall_rock", i - 1, j, i, j);
                    tile.Objects.Add(newWall);
                }
                else
                {
                    int leftCode = DungeonUtils.GetCodeOfTileByIndex(i - 1, j);
                    if (tiles.ContainsKey(leftCode))
                    {
                        if (tiles [leftCode].room == null && tiles [leftCode].road != null)
                        {
                            DungeonObject newPassage = PrepareWallObject(code, DungeonObjectType.Passage, "passage_rock_arc", i - 1, j, i, j);
                            tile.Objects.Add(newPassage);
                        }
                        else if (tiles [leftCode].room == null && tiles [leftCode].road == null)
                        {
                            DungeonObject newWall = PrepareWallObject(code, DungeonObjectType.Wall, "wall_rock", i - 1, j, i, j);
                            tile.Objects.Add(newWall);
                        }
                    }
                }

                // TOP wall
                // If it is a top border set top wall
                if (j == 0)
                {
                    DungeonObject newWall = PrepareWallObject(code, DungeonObjectType.Wall, "wall_rock", i, j - 1, i, j);
                    tile.Objects.Add(newWall);
                }
                else
                {
                    int topCode = DungeonUtils.GetCodeOfTileByIndex(i, j - 1);
                    if (tiles.ContainsKey(topCode))
                    {
                        if (tiles [topCode].room == null && tiles [topCode].road != null)
                        {
                            DungeonObject newPassage = PrepareWallObject(code, DungeonObjectType.Passage, "passage_rock_arc", i, j - 1, i, j);
                            tile.Objects.Add(newPassage);
                        }
                        else if (tiles [topCode].room == null && tiles [topCode].road == null)
                        {
                            DungeonObject newWall = PrepareWallObject(code, DungeonObjectType.Wall, "wall_rock", i, j - 1, i, j);
                            tile.Objects.Add(newWall);
                        }
                    }
                }

                // RIGHT wall
                // If it is a right border set right wall
                if (i == dungeon.width - 1)
                {
                    DungeonObject newWall = PrepareWallObject(code, DungeonObjectType.Wall, "wall_rock", i + 1, j, i, j);
                    tile.Objects.Add(newWall);
                }
                else
                {
                    int rightCode = DungeonUtils.GetCodeOfTileByIndex(i + 1, j);
                    if (tiles.ContainsKey(rightCode))
                    {
                        if (tiles [rightCode].room == null && tiles [rightCode].road != null)
                        {
                            DungeonObject newPassage = PrepareWallObject(code, DungeonObjectType.Passage, "passage_rock_arc", i + 1, j, i, j);
                            tile.Objects.Add(newPassage);
                        }
                        else if (tiles [rightCode].room == null && tiles [rightCode].road == null)
                        {
                            DungeonObject newWall = PrepareWallObject(code, DungeonObjectType.Wall, "wall_rock", i + 1, j, i, j);
                            tile.Objects.Add(newWall);
                        }
                    }
                }

                // BOTTOM wall
                // If it is a bottom border set bottom wall
                if (j == dungeon.height - 1)
                {
                    DungeonObject newWall = PrepareWallObject(code, DungeonObjectType.Wall, "wall_rock", i, j + 1, i, j);
                    tile.Objects.Add(newWall);
                }
                else
                {
                    int bottomCode = DungeonUtils.GetCodeOfTileByIndex(i, j + 1);
                    if (tiles.ContainsKey(bottomCode))
                    {
                        if (tiles [bottomCode].room == null && tiles [bottomCode].road != null)
                        {
                            DungeonObject newPassage = PrepareWallObject(code, DungeonObjectType.Passage, "passage_rock_arc", i, j + 1, i, j);
                            tile.Objects.Add(newPassage);
                        }
                        else if (tiles [bottomCode].room == null && tiles [bottomCode].road == null)
                        {
                            DungeonObject newWall = PrepareWallObject(code, DungeonObjectType.Wall, "wall_rock", i, j + 1, i, j);
                            tile.Objects.Add(newWall);
                        }
                    }
                }
            }
            else if (tiles [code].road != null)
            {
                // Set floor
                DungeonObject floor = new DungeonObject(code, DungeonObjectType.Floor, "dirt_floor");
                tile.Objects.Add(floor);

                // Set ceiling
                DungeonObject ceiling = new DungeonObject(code, DungeonObjectType.Ceiling, "dirt_ceiling");
                tile.Objects.Add(ceiling);

                // LEFT wall
                // If it is a left border set left wall
                if (i == 0)
                {
                    DungeonObject newWall = PrepareWallObject(code, DungeonObjectType.Wall, "wall_rock", i - 1, j, i, j);
                    tile.Objects.Add(newWall);
                }
                else
                {
                    int leftCode = DungeonUtils.GetCodeOfTileByIndex(i - 1, j);
                    if (tiles.ContainsKey(leftCode))
                    {
                        if (tiles [leftCode].room != null)
                        {
                            DungeonObject newPassage = PrepareWallObject(code, DungeonObjectType.Passage, "passage_rock_arc", i - 1, j, i, j);
                            tile.Objects.Add(newPassage);
                        }
                        else if (tiles [leftCode].room == null && tiles [leftCode].road == null)
                        {
                            DungeonObject newWall = PrepareWallObject(code, DungeonObjectType.Wall, "wall_rock", i - 1, j, i, j);
                            tile.Objects.Add(newWall);
                        }
                    }
                }

                // TOP wall
                // If it is a top border set top wall
                if (j == 0)
                {
                    DungeonObject newWall = PrepareWallObject(code, DungeonObjectType.Wall, "wall_rock", i, j - 1, i, j);
                    tile.Objects.Add(newWall);
                }
                else
                {
                    int topCode = DungeonUtils.GetCodeOfTileByIndex(i, j - 1);
                    if (tiles.ContainsKey(topCode))
                    {
                        if (tiles [topCode].room != null)
                        {
                            DungeonObject newPassage = PrepareWallObject(code, DungeonObjectType.Passage, "passage_rock_arc", i, j - 1, i, j);
                            tile.Objects.Add(newPassage);
                        }
                        else if (tiles [topCode].room == null && tiles [topCode].road == null)
                        {
                            DungeonObject newWall = PrepareWallObject(code, DungeonObjectType.Wall, "wall_rock", i, j - 1, i, j);
                            tile.Objects.Add(newWall);
                        }
                    }
                }

                // RIGHT wall
                // If it is a right border set right wall
                if (i == dungeon.width - 1)
                {
                    DungeonObject newWall = PrepareWallObject(code, DungeonObjectType.Wall, "wall_rock", i + 1, j, i, j);
                    tile.Objects.Add(newWall);
                }
                else
                {
                    int rightCode = DungeonUtils.GetCodeOfTileByIndex(i + 1, j);
                    if (tiles.ContainsKey(rightCode))
                    {
                        if (tiles [rightCode].room != null)
                        {
                            DungeonObject newPassage = PrepareWallObject(code, DungeonObjectType.Passage, "passage_rock_arc", i + 1, j, i, j);
                            tile.Objects.Add(newPassage);
                        }
                        else if (tiles [rightCode].room == null && tiles [rightCode].road == null)
                        {
                            DungeonObject newWall = PrepareWallObject(code, DungeonObjectType.Wall, "wall_rock", i + 1, j, i, j);
                            tile.Objects.Add(newWall);
                        }
                    }
                }

                // BOTTOM wall
                // If it is a bottom border set bottom wall
                if (j == dungeon.height - 1)
                {
                    DungeonObject newWall = PrepareWallObject(code, DungeonObjectType.Wall, "wall_rock", i, j + 1, i, j);
                    tile.Objects.Add(newWall);
                }
                else
                {
                    int bottomCode = DungeonUtils.GetCodeOfTileByIndex(i, j + 1);
                    if (tiles.ContainsKey(bottomCode))
                    {
                        if (tiles [bottomCode].room != null)
                        {
                            DungeonObject newPassage = PrepareWallObject(code, DungeonObjectType.Passage, "passage_rock_arc", i, j + 1, i, j);
                            tile.Objects.Add(newPassage);
                        }
                        else if (tiles [bottomCode].room == null && tiles [bottomCode].road == null)
                        {
                            DungeonObject newWall = PrepareWallObject(code, DungeonObjectType.Wall, "wall_rock", i, j + 1, i, j);
                            tile.Objects.Add(newWall);
                        }
                    }
                }
            }
        }
    }