示例#1
0
 /* Unload the currently loaded level */
 public void Unload()
 {
     metadata.SetLoadState(false);
     metadata = new LevelMetadata();
     theGrid.ClearGrid();
     LevelUnloadCompleted?.Invoke();
 }
示例#2
0
    public IEnumerator Load(int level_guid)
    {
        loadedInDebug = false;

        AsyncPropAndTileSetup.Instance.LoadConfigs();
        while (!TileProperties.Loaded || !PropProperties.Loaded)
        {
            yield return(new WaitForEndOfFrame());
        }

        if (!didTriggerMetaUpdate)
        {
            StartCoroutine(RefreshLevelData());
        }
        while (!hasUpdatedMetas)
        {
            yield return(new WaitForEndOfFrame());
        }

        Unload();

        //Find our metadata
        LevelMetadata ourMeta = null;

        foreach (LevelMetadata meta in LevelMetadata)
        {
            if (meta.levelGUID == level_guid)
            {
                ourMeta = meta;
                break;
            }
        }
        if (ourMeta == null)
        {
            Debug.LogError("Cannot find requested level GUID!");
            yield break;
        }
        metadata = ourMeta;
        metadata.SetLoadState(false);
        Debug.Log("Loading level: " + metadata.levelName + " (GUID " + metadata.levelGUID + ")");
        int invisibleProps = 0;
        int invisibleTiles = 0;

        string configFile = Application.streamingAssetsPath + "/LEVELS/" + metadata.levelName + ".dwb";

#if !UNITY_WEBGL
        if (File.Exists(configFile)) //It's ok not to exist if we're in editor
        {
#endif
        //Load our level config
        levelData = null;
        StartCoroutine(FileLoader.Instance.LoadAsBytes(configFile, LoadCallback));
        while (levelData == null || !levelData.CanRead)
        {
            yield return(new WaitForEndOfFrame());
        }

        //Read our level config
        BinaryReader reader = new BinaryReader(levelData);
        int versionNum      = reader.ReadInt32();
        if (!(versionNum == LevelFileVersion.VERSION_NUM))     //This can allow through back-supported config versions
        {
            Debug.LogError("Tried to load an outdated level file!");
            reader.Close();
            yield break;
        }
        if (versionNum != LevelFileVersion.VERSION_NUM)
        {
            Debug.LogWarning("This level uses file version " + versionNum + " (latest is " + LevelFileVersion.VERSION_NUM + "). While this version is supported, please re-save the level in editor ASAP to keep updated with the latest features.");
        }
        int tileCount    = reader.ReadInt32();
        Vector2 gridDims = new Vector2(reader.ReadInt16(), reader.ReadInt16());
        if (gridDims != theGrid.GridTileDims)
        {
            Debug.LogError("Grid resizing is unsupported! This level is invalid.");
            reader.Close();
            yield break;
        }

        //Load occupiers
        int tileOccupierCount = reader.ReadInt32();
        for (int i = 0; i < tileOccupierCount; i++)
        {
            Tile parentTile = theGrid.AllTiles[reader.ReadInt32()];
            int  enumIndex  = reader.ReadInt16();

            if (!Enum.IsDefined(typeof(TileTypes), enumIndex))
            {
                Debug.LogError("ERROR! When loading, a tile's occupier was of a type that no longer exists.");
                continue;
            }

            //Populate the tile's occupier
            GameObject parentTileObject = null;
            parentTileObject = Instantiate(worldTileObject, parentTile.Position, Quaternion.identity) as GameObject;
            TileTypes tileType = (TileTypes)enumIndex;
#if UNITY_EDITOR
            if (!TileProperties.IsVisibleInEditor(tileType))
            {
                invisibleTiles++;
            }
#endif
            parentTileObject.GetComponent <LevelTileEntity>().Initialise(tileType, parentTile, false);
        }

        //Load props
        int tilePropCount = reader.ReadInt32();
        for (int i = 0; i < tilePropCount; i++)
        {
            if (versionNum == 13)
            {
                reader.BaseStream.Position += 4;
            }
            int          enumIndex = reader.ReadInt16();
            PropRotation rotation  = (PropRotation)reader.ReadInt16();

            int         propTileCount = reader.ReadInt32();
            List <Tile> propTiles     = new List <Tile>();
            for (int x = 0; x < propTileCount; x++)
            {
                propTiles.Add(theGrid.AllTiles[reader.ReadInt32()]);
            }

            if (!Enum.IsDefined(typeof(PropTypes), enumIndex))
            {
                Debug.LogError("ERROR! When loading, a tile's prop was of a type that no longer exists.");
                continue;
            }

            PropTypes  propType = (PropTypes)enumIndex;
            GameObject parentTileDecoratorObject = Instantiate(worldPropObject, propTiles[0].Position, Quaternion.identity) as GameObject;
#if UNITY_EDITOR
            if (!PropProperties.IsVisibleInEditor(propType))
            {
                invisibleProps++;
            }
#endif
            parentTileDecoratorObject.GetComponent <LevelPropEntity>().Initialise(propType, propTiles, rotation, false);
        }

        //Load some metadata
        metadata.SetGoonCount(reader.ReadInt32());
        metadata.SetHazardCount(reader.ReadInt32());

        reader.Close();

        //Refresh all sprites to get the right contexts
        for (int i = 0; i < Grid.AllTiles.Count; i++)
        {
            if (Grid.AllTiles[i].IsOccupied)
            {
                Grid.AllTiles[i].Occupier.RefreshSprite();
            }
        }

        metadata.SetLoadState(true);
#if !UNITY_WEBGL
    }

    else
    {
        //User is just starting to create this level
        metadata.SetLoadState(true);
        LevelEditor.Instance.SaveLevel();
    }
#endif

        //Show/hide grid depending on editor state
        GetComponent <SpriteRenderer>().enabled = IsInEditor;

        if (invisibleProps == 0 && invisibleTiles == 0)
        {
            Debug.Log("Level loaded without warnings!");
        }
        else
        {
            Debug.LogWarning("Level loaded successfully, but contains " + invisibleProps + " deprecated props and " + invisibleTiles + " deprecated tiles. Consider updating this content.");
        }
        LevelLoadCompleted?.Invoke();

        validateNextTick = true;
    }