Пример #1
0
        private void LoadLevel(Level.LoadLevelEvent loadLevelEvent)
        {
            _levelIndex  = loadLevelEvent.LevelIndex;
            _levelConfig = loadLevelEvent.LevelConfig;

            Logger.LogEditor($"Load level={_levelIndex}, size={_levelConfig.width}x{_levelConfig.height}");

            _levelScoreView.SetGoals(_levelIndex, _levelConfig);

            // clear the playfield
            ClearGridNodeInstances();
            ClearGridItemInstances();

            // if there is no grid state then auto-open the level editor
            if (loadLevelEvent.InitialGridState == null)
            {
                MessagePopup.ShowMessage("This level is empty. Edit mode is enabled.");
                _levelConfig = LevelConfig.CreateDefaultLevel(5, 5);
                EnableEditMode();
                return;
            }

            // calculate the playfield dimensions
            Vector3 playfieldSize = _playfieldTransform.sizeDelta;

            _tileSize        = CalculateTileSize(playfieldSize, new Vector2(_levelConfig.width, _levelConfig.height));
            _playfieldOrigin = new Vector3(             // [0,0] = lower-left corner
                (_levelConfig.width - 1) * _tileSize * -0.5f,
                (_levelConfig.height - 1) * _tileSize * -0.5f);

            // instantiate the level layout
            for (int y = 0; y < _levelConfig.height; y++)
            {
                for (int x = 0; x < _levelConfig.width; x++)
                {
                    var nodeLayout = _levelConfig.GetNodeLayout(x, y);

                    Assert.IsNotNull(nodeLayout.nodeId, $"Level layout is corrupted at ({x},{y})");

                    var pos = CalculateGridNodePosition(x, y);

                    // load the node from the config
                    CreateGridNodeView(x, y, nodeLayout.nodeId, pos);

                    // if in edit mode then load the item from the config, else load from the state
                    string itemType = _state == State.EditMode ?
                                      nodeLayout.itemId :
                                      loadLevelEvent.InitialGridState.FirstOrDefault(item => item.Index.x == x && item.Index.y == y)?.ItemId;

                    // if (itemType != null)	// HACK: load all items, even if they are invalid. this makes the level editor easier to use
                    {
                        CreateGridItemView(x, y, itemType, pos);
                    }
                }
            }
        }
Пример #2
0
        public static void ResizeLevel(int width, int height, ref LevelConfig config)
        {
            var resizedLayout = new GridNodeLayout[width * height];

            var defaultGridItem = GameConfig.GetDefaultLayoutGridItem(config.category);
            var openNode        = GameConfig.AllGridNodes.First(node => node.IsOpen);

            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    if (config.IsInBounds(x, y))
                    {
                        resizedLayout[GetLinearIndex(x, y, width)] = config.GetNodeLayout(x, y);
                    }
                    else
                    {
                        resizedLayout[GetLinearIndex(x, y, width)] = new GridNodeLayout()
                        {
                            nodeId = openNode.ID, itemId = defaultGridItem.ID
                        }
                    };
                }
            }

            config.width  = width;
            config.height = height;
            config.layout = resizedLayout;
        }
Пример #3
0
        public LevelState(int levelIndex, LevelConfig config)
        {
            LevelIndex = levelIndex;
            Config     = config;
            Grid       = null;

            var includedItems = GameConfig.GetAllGridItemsInCategory(config.category, config.excludeItemIds);

            GridItemDrops = new GridItemDropDistribution(config.category, includedItems);

            if (config.width > 0 && config.height > 0)
            {
                var defaultGridItem = GameConfig.GetDefaultLayoutGridItem(config.category);

                // initialize the grid
                Grid = new GridNodeState[config.width, config.height];
                for (int y = 0; y < config.height; y++)
                {
                    for (int x = 0; x < config.width; x++)
                    {
                        var    layout = config.GetNodeLayout(x, y);
                        string itemId = null;
                        if (!string.IsNullOrEmpty(layout.itemId))
                        {
                            if (layout.itemId != defaultGridItem.ID)
                            {
                                itemId = layout.itemId;
                            }
                            else
                            {
                                itemId = GridItemDrops.Next();
                            }
                        }
                        Grid[x, y] = new GridNodeState(x, y, layout.nodeId, itemId);
                    }
                }
            }
        }