Beispiel #1
0
    public void SaveGrid()
    {
        var gridToSave = new LevelData();

        gridToSave.Grid   = new int[_height * _width];
        gridToSave.Height = _height;
        gridToSave.Width  = _width;

        var startPosition = new Vector2(-_height / 2, -_width / 2);

        for (int row = 0; row < _height; row++)
        {
            for (int column = 0; column < _width; column++)
            {
                var position = startPosition + new Vector2(column, row);
                gridToSave.Grid[column * _width + row] = Grid[position].GetId();
            }
        }

        LevelSaveLoadSystem.SaveLevel(MapName, gridToSave);
    }
Beispiel #2
0
    public void LoadGrid()
    {
        LevelData newLevel = LevelSaveLoadSystem.LoadLevel(MapName);

        if (newLevel != null)
        {
            DestroyGrid();

            _height = newLevel.Height;
            _width  = newLevel.Width;

            var startPosition = new Vector2(-_height / 2, -_width / 2);
            for (int row = 0; row < _height; row++)
            {
                for (int column = 0; column < _width; column++)
                {
                    var position = startPosition + new Vector2(column, row);
                    PlaceBlockByTypeId(position, newLevel.Grid[column * _width + row]);
                }
            }
            OnLevelLoaded?.Invoke();
            _isLoaded = true;
        }
    }