Esempio n. 1
0
        private void ApplyAction(PointerEventData eventData)
        {
            var gridCoord = GetGridCoordinate(eventData);

            if (IsValidCoordinate(gridCoord))
            {
                if (dragging)
                {
                    if (modifiedPositions.Contains(gridCoord))
                    {
                        return;
                    }

                    modifiedPositions.Add(gridCoord);
                }

                if (eventData.button == PointerEventData.InputButton.Left)
                {
                    manipulator.PerformAction(gridCoord.X, gridCoord.Y);
                }
                else if (eventData.button == PointerEventData.InputButton.Right)
                {
                    var key    = BubbleData.GetKey(gridCoord.X, gridCoord.Y);
                    var models = manipulator.Models;

                    if (models.ContainsKey(key))
                    {
                        contextMenu.Populate(models[key]);
                        contextMenu.Show();
                    }
                }
            }
        }
        public void LoadLevel(string jsonText)
        {
            var levelData = JsonUtility.FromJson <LevelData>(jsonText);
            var placer    = new PlaceBubbleAction();

            randoms = new List <RandomBubbleDefinition>(levelData.Randoms ?? new RandomBubbleDefinition[0]);
            GlobalState.EventService.Dispatch(new RandomBubblesChangedEvent());

            foreach (var bubble in levelData.Bubbles)
            {
                state.BubbleType = bubble.Type;

                placer.Perform(this, bubble.X, bubble.Y);
                bubbleFactory.ApplyEditorModifiers(views[BubbleData.GetKey(bubble.X, bubble.Y)], bubble);
                models[BubbleData.GetKey(bubble.X, bubble.Y)].modifiers = bubble.modifiers;
            }

            LevelProperties.FromLevelData(levelData);
            LevelProperties.StarValues = ScoreUtil.ComputeStarsForLevel(levelData, BubbleFactory);
            LevelProperties.NotifyListeners();

            queue.CopyFrom(levelData.Queue);
            queue.ShotCount = levelData.ShotCount;
            queue.NotifyListeners();

            Background = levelData.Background;

            GlobalState.EventService.Dispatch(new LevelModifiedEvent());
            GlobalState.EventService.Dispatch(new LevelEditorLoadEvent());
        }
 public void PerformAlternate(LevelManipulator manipulator, int x, int y)
 {
     if (manipulator.Models.ContainsKey(BubbleData.GetKey(x, y)) && (manipulator.Modifier != null))
     {
         RemoveModifier(manipulator, x, y, manipulator.Modifier);
         placer.Perform(manipulator, x, y);
     }
 }
Esempio n. 4
0
    private void GetNeighbors(int x, int y, int[] neighbors)
    {
        var offset = (y & 1) * 2 - 1;

        neighbors[0] = BubbleData.GetKey(x + offset, y - 1);
        neighbors[1] = BubbleData.GetKey(x, y - 1);
        neighbors[2] = BubbleData.GetKey(x - 1, y);
        neighbors[3] = BubbleData.GetKey(x + 1, y);
        neighbors[4] = BubbleData.GetKey(x + offset, y + 1);
        neighbors[5] = BubbleData.GetKey(x, y + 1);
    }
        private void ApplyModifier(LevelManipulator manipulator, int x, int y, BubbleModifierInfo modifier)
        {
            var model     = manipulator.Models[BubbleData.GetKey(x, y)];
            var modifiers = new List <BubbleData.ModifierData>(model.modifiers ?? new BubbleData.ModifierData[0]);

            manipulator.SetBubbleType(model.Type);

            modifiers.Add(new BubbleData.ModifierData
            {
                type = modifier.Type,
                data = modifier.Data,
            });

            model.modifiers = modifiers.ToArray();
        }
        public void Perform(LevelManipulator manipulator, int x, int y)
        {
            var key = BubbleData.GetKey(x, y);

            manipulator.Models.Remove(key);

            if (manipulator.Views.ContainsKey(key))
            {
                GameObject.Destroy(manipulator.Views[key]);
                manipulator.Views.Remove(key);
            }

            GlobalState.EventService.Dispatch(new LevelModifiedEvent());
            manipulator.RecomputeScores();
        }
        private void RemoveModifier(LevelManipulator manipulator, int x, int y, BubbleModifierInfo modifier)
        {
            var model = manipulator.Models[BubbleData.GetKey(x, y)];

            manipulator.SetBubbleType(model.Type);

            if (model.modifiers != null)
            {
                model.modifiers = model.modifiers.Where(m => m.type != modifier.Type).ToArray();

                if (model.modifiers.Length == 0)
                {
                    model.modifiers = null;
                }
            }
        }
        public void Perform(LevelManipulator manipulator, int x, int y)
        {
            var key      = BubbleData.GetKey(x, y);
            var modifier = manipulator.Modifier;

            if (manipulator.Models.ContainsKey(key) && (modifier != null))
            {
                var bubble      = manipulator.Models[key];
                var hasModifier = (bubble.modifiers != null) && bubble.modifiers.Any(m => m.type == modifier.Type);

                RemoveModifier(manipulator, x, y, modifier);

                if (!hasModifier)
                {
                    ApplyModifier(manipulator, x, y, modifier);
                }

                placer.Perform(manipulator, x, y);
            }
        }
        private void PlaceBubble(LevelManipulator manipulator, int x, int y, BubbleType type)
        {
            var key = BubbleData.GetKey(x, y);

            BubbleData.ModifierData[] modifiers = null;

            if (manipulator.Models.ContainsKey(key))
            {
                modifiers = manipulator.Models[key].modifiers;
            }

            deleter.Perform(manipulator, x, y);

            var definition     = manipulator.BubbleFactory.GetDefinitionByType(type);
            var prefabRenderer = definition.Prefab.GetComponentInChildren <SpriteRenderer>();

            if (prefabRenderer != null)
            {
                var instance = GameObject.Instantiate(manipulator.BubblePrefab);

                var image = instance.GetComponent <Image>();
                image.sprite = prefabRenderer.sprite;

                instance.name = string.Format("{0} ({1}, {2})", type, x, y);
                instance.transform.SetParent(manipulator.BubbleContainer, false);
                instance.transform.localPosition = GetBubbleLocation(x, y);

                var model = new BubbleData(x, y, type)
                {
                    modifiers = modifiers
                };
                manipulator.Views.Add(model.Key, instance);
                manipulator.Models.Add(model.Key, model);

                manipulator.BubbleFactory.ApplyEditorModifiers(instance, model);

                GlobalState.EventService.Dispatch(new LevelModifiedEvent());
            }
        }
Esempio n. 10
0
        static private float ExtractCluster(Dictionary <int, float> bubbleMap, int key, float current)
        {
            float value = 0.0f;

            if (bubbleMap.ContainsKey(key) && (bubbleMap[key] <= current))
            {
                value = bubbleMap[key];
                bubbleMap.Remove(key);

                var x      = key & ((1 << 4) - 1);
                var y      = key >> 4;
                var offset = (y & 1) * 2 - 1;
                var next   = Mathf.Min(current, value);

                value += ExtractCluster(bubbleMap, BubbleData.GetKey(x + offset, y - 1), next);
                value += ExtractCluster(bubbleMap, BubbleData.GetKey(x, y - 1), next);
                value += ExtractCluster(bubbleMap, BubbleData.GetKey(x - 1, y), next);
                value += ExtractCluster(bubbleMap, BubbleData.GetKey(x + 1, y), next);
                value += ExtractCluster(bubbleMap, BubbleData.GetKey(x + offset, y + 1), next);
                value += ExtractCluster(bubbleMap, BubbleData.GetKey(x, y + 1), next);
            }

            return(value);
        }