/// <summary> /// Prepares the wall object for tile. /// </summary> /// <returns> /// Prepared wall object as dungeon object. /// </returns> /// <param name='code'> /// Code of tile. /// </param> /// <param name='objectType'> /// Object type. /// </param> /// <param name='prefabReference'> /// Prefab string reference to resource. /// </param> /// <param name='fromI'> /// I index of source tile. /// </param> /// <param name='fromJ'> /// J index of source tile. /// </param> /// <param name='toI'> /// I index of target tile. /// </param> /// <param name='toJ'> /// J index of target tile. /// </param> public static DungeonObject PrepareWallObject(int code, DungeonObjectType objectType, string prefabReference, int fromI, int fromJ, int toI, int toJ) { DungeonObject newDungeonObject = new DungeonObject(code, objectType, prefabReference); newDungeonObject.rotation = DungeonUtils.GetRotationFromToTile(fromI, fromJ, toI, toJ); newDungeonObject.offset = newDungeonObject.rotation * new Vector3(0f, 0f, -DungeonUtils.TileSize * 0.5f); return(newDungeonObject); }
/// <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; }
/// <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); }
// Use this for initialization void Start() { DungeonData newDungeon = DungeonData.RandomizeNewDungeon(); CurrentDungeon = newDungeon; Debug.Log(newDungeon.ToString()); // Init player MyPlayerTr.position = DungeonUtils.GetPositionByIndex(CurrentDungeon.entranceX, CurrentDungeon.entranceY); MyPlayer._currentI = CurrentDungeon.entranceX; MyPlayer._currentJ = CurrentDungeon.entranceY; }
/// <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); }
public override void OnPostDungeonBuild(Dungeon dungeon, DungeonModel model) { var dungeonObjects = DungeonUtils.GetDungeonObjects(dungeon); // Group the dungeon items by cell ids Dictionary <int, List <GameObject> > gameObjectsByCellId = new Dictionary <int, List <GameObject> >(); foreach (var dungeonObject in dungeonObjects) { var data = dungeonObject.GetComponent <DungeonSceneProviderData>(); var cellId = data.userData; if (cellId == -1) { continue; } if (!gameObjectsByCellId.ContainsKey(cellId)) { gameObjectsByCellId.Add(cellId, new List <GameObject>()); } gameObjectsByCellId[cellId].Add(dungeonObject); } // Create new prefabs and group them under it foreach (var cellId in gameObjectsByCellId.Keys) { var cellItems = gameObjectsByCellId[cellId]; var groupName = "Group_Cell_" + cellId; GroupItems(cellItems.ToArray(), groupName, dungeon, cellId); } // Destroy the old group objects DestroyOldGroupObjects(dungeon); var dungeonModel = dungeon.ActiveModel; if (dungeonModel is GridDungeonModel) { var gridModel = dungeonModel as GridDungeonModel; PostInitializeForGridBuilder(dungeon, gridModel); } }
/// <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 } } } }
public void BuildNavMesh() { DestroyNavMesh(); if (enableRuntimeNavigation && Application.isPlaying) { m_NavMesh = new NavMeshData(); m_Instance = NavMesh.AddNavMeshData(m_NavMesh); var dungeon = GetComponent <Dungeon>(); if (!dungeon) { Debug.LogError("DungeonRuntimeNavigation should be attached to a Dungeon prefab. Missing Dungeon Script in the game object"); return; } dungeonBounds = DungeonUtils.GetDungeonBounds(dungeon); dungeonBounds.size = dungeonBounds.size + boundsPadding; UpdateNavMesh(false); } }
/// <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); }
/// <summary> /// Moves to tile according to pressed key realtive to current direction. E.g., if player directed Backwards and pressed key Strafe Right, /// player will move to tile in the left, relative to world coordinates. /// </summary> /// <returns> /// Did moving succeed or not. /// </returns> /// <param name='targetDirectionRelative'> /// Target direction relative to current direction. /// </param> protected bool MoveTo(Direction targetDirectionRelative) { bool ret = false; // Get <see cref="Direction"/> of target tile Direction targetDirectionAbsolute = (Direction)(((byte)_currentDirection + (byte)targetDirectionRelative) % 4); // set target position if (CheckTileMoveability(targetDirectionAbsolute)) { switch (targetDirectionAbsolute) { case Direction.Forward: _targetPosition = DungeonUtils.GetPositionByIndex(_currentI, --_currentJ); ret = true; break; case Direction.Right: _targetPosition = DungeonUtils.GetPositionByIndex(++_currentI, _currentJ); ret = true; break; case Direction.Backward: _targetPosition = DungeonUtils.GetPositionByIndex(_currentI, ++_currentJ); ret = true; break; case Direction.Left: _targetPosition = DungeonUtils.GetPositionByIndex(--_currentI, _currentJ); ret = true; break; default: break; } } return(ret); }
/// <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); } } } } } }