Ejemplo n.º 1
0
        void LoadLevel()
        {
            currentLevel     = DataService.Instance.GetCurrentLevel();
            currentLevelData = DataService.Instance.GetLevels()[currentLevel];

            player.transform.position = currentLevelData.playerStartPosition;
            baby.transform.position   = currentLevelData.babyStartPosition;

            terrainTileMap.ClearAllTiles();
            terrainTileMap.SetTiles(currentLevelData.terrainPositions, currentLevelData.terrainTiles);
            obstructionsTileMap.ClearAllTiles();
            obstructionsTileMap.SetTiles(currentLevelData.obstructionPositions, currentLevelData.obstructionTiles);

            // Set level object tiles
            // Then instantiate gameobjects after destroying all children

            levelObjectsTileMap.ClearAllTiles();
            levelObjectsTileMap.SetTiles(currentLevelData.levelObjectPositions, currentLevelData.levelObjectTiles);

            navigationGrid = new NavGrid(terrainTileMap, levelObjectsTileMap, obstructionsTileMap);
            navigationGrid.CalculateGridNavigation();

            var children = levelObjectsTileMap.transform.Cast <Transform>().ToList();

            foreach (var child in children)
            {
                DestroyImmediate(child.gameObject);
            }

            for (var i = 0; i < currentLevelData.levelObjectPositions.Length; i++)
            {
                Vector3Int levelObjectPosition = currentLevelData.levelObjectPositions[i];
                var        pos = levelObjectPosition;
                InstantiateLevelObjectPrefabInCell(levelObjectsTileMap.layoutGrid, levelObjectsTileMap.gameObject,
                                                   levelObjectPosition,
                                                   DataService.Instance.GetLevelObjectMap()
                                                   .GetPrefab(currentLevelData.levelObjectTiles[i].levelObjectType),
                                                   () =>
                {
                    levelObjectsTileMap.SetTile(levelObjectPosition, null);
                    navigationGrid.CalculateCellNavigation(levelObjectPosition);
                });
            }

            LoadHints();
        }