Exemplo n.º 1
0
 public override List <Match> FindMatches(RuntimeGridData grid, SwapEventData swapData, List <Match> previousMatches)
 {
     foreach (var acc in accumulators)
     {
         previousMatches = acc.FindMatches(grid, swapData, previousMatches);
     }
     return(previousMatches);
 }
Exemplo n.º 2
0
        public override void OnCreate(SwapEventData swapData, Tile cause)
        {
            if (cause == null)
            {
                // stay default
                return;
            }

            GetComponent <SpriteRenderer>().CopyFrom(cause.GetComponent <SpriteRenderer>());

            tag = cause.tag;
        }
Exemplo n.º 3
0
        public override void DestroyMatches(List <Match> matches, SwapEventData swapData)
        {
            var distinctElements = new HashSet <MatchElement>();

            // for each element run any on destruction stuff and add to end of matches list
            for (int i = 0; i < matches.Count; i++)
            {
                foreach (var elem in matches[i].elements)
                {
                    if (!distinctElements.Contains(elem))
                    {
                        matches.AddRange(elem.tile.GetComponent <Tile>().OnDestroyed());
                        distinctElements.Add(elem);
                    }
                }
            }

            var spawned = new List <MatchElement>();

            // for each match see if they for the shapes for a powerup
            // add any tiles caught in startup effects to the elements to be destroyed
            // add any powerups spawned to a list to be added to the grid once the other tiles have been destroyed
            for (int i = 0; i < matches.Count; i++)
            {
                Effect effect = SpawnEffects(matches[i], swapData);
                if (effect.match.HasValue)
                {
                    foreach (var elem in effect.match.Value.elements)
                    {
                        distinctElements.Add(elem);
                    }
                }

                if (effect.spawned.HasValue)
                {
                    spawned.Add(effect.spawned.Value);
                }
            }

            // actually destroy the tiles
            foreach (var elem in distinctElements)
            {
                Destroy(elem.tile);
                grid.SetTileAt(null, elem.i, elem.j);
            }

            // add spawned powerups to the grid
            foreach (var newTile in spawned)
            {
                grid.SetTileAt(newTile.tile.GetComponent <Tile>(), newTile.i, newTile.j);
            }
        }
Exemplo n.º 4
0
        public override List <Match> FindMatches(RuntimeGridData grid, SwapEventData swapData, List <Match> previousMatches)
        {
            for (int i = 0; i < previousMatches.Count; i++)
            {
                var match = previousMatches[i];
                match.causeElem = System.Array.FindIndex(match.elements, item =>
                {
                    var pos = new Vector2Int(item.i, item.j);
                    return(pos == swapData.swappedPos1 || pos == swapData.swappedPos2);
                });

                previousMatches[i] = match;
            }
            return(previousMatches);
        }
Exemplo n.º 5
0
        // move to a seperate object that triggers in a match destruction
        private Effect SpawnEffects(Match match, SwapEventData swapData)
        {
            if (match.canCreateShape == false)
            {
                return(Effect.None);
            }

            var shape = MatchShapes.FindShape(match);

            if (shape == null)
            {
                return(Effect.None);
            }
            else
            {
                int causePos;
                // right now if we couldnt work out the element that caused the match (the one you swapped)
                // then we just pick the last one in the list
                // TODO(chris) - a match is always cause by _some_ element moving, that should be the cause
                if (match.causeElem > -1)
                {
                    causePos = match.causeElem;
                }
                else
                {
                    causePos = match.elements.Length - 1;
                }
                var cause = match.elements[causePos];

                var powerup = powerups.SpawnPowerup(shape.Value, new Vector3(cause.i, cause.j, cause.tile.transform.position.z));

                if (powerup != null)
                {
                    powerup.OnCreate(swapData, cause.tile.GetComponent <Tile>());
                    return(new Effect
                    {
                        match = null,
                        spawned = new MatchElement {
                            i = cause.i, j = cause.j, tile = powerup.gameObject
                        }
                    });
                }
                else
                {
                    return(Effect.None);
                }
            }
        }
Exemplo n.º 6
0
        public override List <Match> FindMatches(RuntimeGridData grid, SwapEventData swapData, List <Match> previousMatches)
        {
            var potentialMatch = new List <MatchElement>();
            var lastTileTag    = "";

            // check for vertical matches
            for (int x = 0; x < grid.Width; x++)
            {
                for (int y = 0; y < grid.Height; y++)
                {
                    lastTileTag = ProcessTile(previousMatches, potentialMatch, grid, lastTileTag, x, y);
                }

                if (potentialMatch.Count > 2)
                {
                    previousMatches.Add(new Match(potentialMatch.ToArray()));
                }
                potentialMatch.Clear();
                lastTileTag = "";
            }


            // check for horizontal matches
            for (int y = 0; y < grid.Height; y++)
            {
                for (int x = 0; x < grid.Width; x++)
                {
                    lastTileTag = ProcessTile(previousMatches, potentialMatch, grid, lastTileTag, x, y);
                }

                if (potentialMatch.Count > 2)
                {
                    previousMatches.Add(new Match(potentialMatch.ToArray()));
                }
                potentialMatch.Clear();
                lastTileTag = "";
            }

            return(previousMatches);
        }
Exemplo n.º 7
0
        public override List <Match> FindMatches(RuntimeGridData grid, SwapEventData swapData, List <Match> previousMatches)
        {
            List <Match> merged = new List <Match>();

            bool mergeFound = false;

            for (int i = 0; i < previousMatches.Count; i++)
            {
                Match m = previousMatches[i];
                for (int j = i + 1; j < previousMatches.Count; j++)
                {
                    if (m.OverlapsWith(previousMatches[j]))
                    {
                        // create the merged Match

                        var l = new List <MatchElement>(m.elements.Length + previousMatches[j].elements.Length);
                        l.AddRange(m.elements);
                        l.AddRange(previousMatches[j].elements);
                        var mergedElements = l.Distinct(new MatchComparator()).ToArray();

                        // add it to the merged list
                        merged.Add(new Match(mergedElements));
                        // make sure we dont add this match to the merged list twice
                        previousMatches.RemoveAt(j);
                        // make sure we dont add the 3 at the end of this
                        mergeFound = true;
                    }
                }

                if (!mergeFound)
                {
                    merged.Add(m);
                }
            }


            return(merged);
        }
Exemplo n.º 8
0
 public virtual void OnCreate(SwapEventData swapData, Tile cause)
 {
 }
Exemplo n.º 9
0
        private System.Collections.IEnumerator SwapTile(Vector2Int a, Vector2Int b)
        {
            try
            {
                yield return(StartCoroutine(TweenTiles(a, b)));

                var swapData = new SwapEventData
                {
                    swappedPos2 = a,
                    swappedPos1 = b
                };

                // trigger any effects that happen on tile swap (eg bombs exploding)
                var matches = grid.GetTileAt(a.x, a.y).OnSwap(grid.GetTileAt(b.x, b.y));
                matches.AddRange(grid.GetTileAt(b.x, b.y).OnSwap(grid.GetTileAt(a.x, a.y)));

                var matchCount = 0;
                var noMatches  = true;
                do
                {
                    yield return(null);


                    matches.AddRange(matchFinder.FindMatches(grid, swapData));

                    matchCount = matches.Count;

                    if (matchCount > 0)
                    {
                        noMatches = false;
                        // notify of matches found
                        MatchesFound.Invoke(new Matches
                        {
                            matches = matches
                        });

                        yield return(null);

                        destroyer.DestroyMatches(matches, swapData);

                        yield return(StartCoroutine(fixer.FixGrid()));

                        matches.Clear();
                    }
                } while (matchCount > 0);

                // if there were never any matches then want to swap the tiles back
                if (noMatches)
                {
                    yield return(new WaitForSeconds(0.3f));

                    yield return(StartCoroutine(TweenTiles(a, b)));
                }

                yield return(null);
            }
            finally
            {
                MatchesResolved.Invoke();
            }
        }
Exemplo n.º 10
0
 public override void OnCreate(SwapEventData swapData, Tile cause)
 {
     GenerateNewTile();
 }
Exemplo n.º 11
0
 public abstract void DestroyMatches(List <Match> matches, SwapEventData swapData);
Exemplo n.º 12
0
 public abstract List <Match> FindMatches(RuntimeGridData grid, SwapEventData swapData, List <Match> previousMatches);
Exemplo n.º 13
0
 public List <Match> FindMatches(RuntimeGridData grid, SwapEventData swapData)
 {
     return(FindMatches(grid, swapData, new List <Match>()));
 }