Esempio n. 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);
                    }
                }
            }
        }
Esempio n. 2
0
 public void OnClick()
 {
     if (IsUnlocked)
     {
         Game.Notify(new Game.GoToLevelMessage(_levelIndex));
     }
     else
     {
         MessagePopup.ShowMessage("Complete earlier levels to unlock this level.");
     }
 }
Esempio n. 3
0
        public void OnClick_EditMode_Save()
        {
            if (!GameConfig.AllMatchRules.Any(rule => rule.ID == _editModeTextMatchRules.text))
            {
                MessagePopup.ShowMessage($"Invalid Match Rules: '{_editModeTextMatchRules.text}'");
                return;
            }

            var allCategories = GameConfig.GetAllGridItemCategories();

            if (!allCategories.Contains(_editModeTextCategory.text))
            {
                MessagePopup.ShowMessage($"Invalid Category: '{_editModeTextCategory.text}'");
                return;
            }

            if (!int.TryParse(_editModeInputMaxItemTypes.text, out var maxItemTypes))
            {
                MessagePopup.ShowMessage($"Invalid Max Item Types: '{_editModeInputMaxItemTypes.text}'");
                return;
            }

            if (!Enum.TryParse(_editModeTextGoalType.text, out LevelGoalType goalType))
            {
                MessagePopup.ShowMessage($"Invalid Goal Type: '{_editModeTextGoalType.text}'");
                return;
            }

            string goalItemId = null;

            if (!string.IsNullOrEmpty(_editModeInputGoalItem.text))
            {
                goalItemId = _editModeInputGoalItem.text;
                if (GameConfig.GetGridItem(goalItemId).ID == null)
                {
                    MessagePopup.ShowMessage($"Invalid Goal Item ID: '{_editModeInputGoalItem.text}'");
                    return;
                }
            }

            if (!int.TryParse(_editModeInputGoal1.text, out var goal1))
            {
                MessagePopup.ShowMessage($"Invalid Goal #1: '{_editModeInputGoal1.text}'");
                return;
            }

            if (!int.TryParse(_editModeInputGoal2.text, out var goal2))
            {
                MessagePopup.ShowMessage($"Invalid Goal #2: '{_editModeInputGoal2.text}'");
                return;
            }

            if (!int.TryParse(_editModeInputGoal3.text, out var goal3))
            {
                MessagePopup.ShowMessage($"Invalid Goal #3: '{_editModeInputGoal3.text}'");
                return;
            }

            if (!Enum.TryParse(_editModeTextChallengeType.text, out LevelChallengeType challengeType))
            {
                MessagePopup.ShowMessage($"Invalid Challenge Type: '{_editModeTextChallengeType.text}'");
                return;
            }

            if (!int.TryParse(_editModeInputChallengeValue.text, out var moves) || moves < 1)
            {
                MessagePopup.ShowMessage($"Invalid Max Moves: '{_editModeInputChallengeValue.text}'");
                return;
            }

            _levelConfig.category       = _editModeTextCategory.text;
            _levelConfig.excludeItemIds = EditMode_ConvertMaxItemTypesToExcludedItemIds(_editModeTextCategory.text, Mathf.Max(0, maxItemTypes));
            _levelConfig.matchRules     = _editModeTextMatchRules.text;
            _levelConfig.goalType       = goalType;
            _levelConfig.goalItemId     = goalItemId;
            _levelConfig.goal1          = Mathf.Max(0, goal1);
            _levelConfig.goal2          = Mathf.Max(0, goal2);
            _levelConfig.goal3          = Mathf.Max(0, goal3);
            _levelConfig.challengeType  = challengeType;
            _levelConfig.challengeValue = moves;

            foreach (var gridNode in _gridNodeInstances)
            {
                var gridItem   = TryGetGridItem(gridNode.Index);
                var nodeLayout = _levelConfig.GetNodeLayout(gridNode.Index);
                nodeLayout.nodeId = gridNode.ID;
                nodeLayout.itemId = (gridItem != null && gridNode.GridNodeConfig.IsOpen) ? gridItem.ID : null;
                _levelConfig.SetNodeLayout(gridNode.Index, nodeLayout);
            }

            LevelConfig.ExportLevelFile(_levelIndex, _levelConfig);
        }