コード例 #1
0
    public void ClearMatchesFromBoard()
    {
        bool removedAtLeastOne = false;

        for (int x = 0; x < gridModel.w; ++x)
        {
            for (int y = gridModel.h - 1; y >= 0; --y)
            {
                if (gridModel.grid[x, y] == null)
                {
                    continue;
                }
                MatchBlock mb = gridModel.grid[x, y].GetComponent <MatchBlock>();
                if (mb.IsMatched)
                {
                    Vector2 v = mb.GridPosition;
                    mb.PlaySettleEffect(settleEffect, true);
                    gridModel.deleteCell((int)v.x, (int)v.y);
                    removedAtLeastOne = true;
                }
            }
        }

        if (removedAtLeastOne)
        {
            SFXSelector.Instance.PlayRandomSound(destructionSounds);
        }
        // Send the matches to the rules object
        TetrisController.Rules.matchesMade.AddRange(matches);
    }
コード例 #2
0
    public IEnumerator PopulateGrid()
    {
        WaitForEndOfFrame wait = new WaitForEndOfFrame();
        int count        = 0;
        int soundCounter = 0;

        for (int y = 0; y < gridModel.h; ++y)
        {
            for (int x = 0; x < gridModel.w; ++x)
            {
                Vector3    pos  = new Vector3(x + gridModel.offset.x, y + gridModel.offset.y, 0);
                GameObject go   = Instantiate <GameObject>(block, pos, Quaternion.identity, cachedTransform);
                int        type = Random.Range(0, blockMaterials.Count);
                MatchBlock mb   = go.GetComponent <MatchBlock>();
                if (mb)
                {
                    mb.gridModel = gridModel;
                    mb.SetBockType(type, blockMaterials[type]);
                    mb.IsMatched = false;
                    mb.settled   = true;
                }
                soundCounter++;
                if (soundCounter > 20)
                {
                    SFXSelector.Instance.PlayNextSound(spawnSounds);
                    soundCounter = 0;
                }
                count++;
                if (count >= spawnCount)
                {
                    count = 0;
                    yield return(wait);
                }
            }
        }
        ready = true;
        TetrisController.Rules.ready = true;
        // Play dust!
        for (int y = 0; y < gridModel.h; ++y)
        {
            for (int x = 0; x < gridModel.w; ++x)
            {
                if (gridModel.grid[x, y] != null)
                {
                    MatchBlock mb = gridModel.grid[x, y].GetComponent <MatchBlock>();
                    mb.PlaySettleEffect(settleEffect);
                }
            }
        }
        SFXSelector.Instance.PlayRandomSound(impactSounds, 1f);
        lastFallTime = lastMatchTime = Time.time;
    }
コード例 #3
0
    public void PieceWasDragged(MatchBlock block)
    {
        if (block == null || block.dragDirection == MatchBlock.DragDirection.None)
        {
            return;
        }

        Vector2 gridPos  = block.GridPosition;
        Vector2 otherPos = gridPos;

        switch (block.dragDirection)
        {
        case MatchBlock.DragDirection.Left:
            otherPos.x -= 1;
            break;

        case MatchBlock.DragDirection.Right:
            otherPos.x += 1;
            break;

        case MatchBlock.DragDirection.Up:
            otherPos.y += 1;
            break;

        case MatchBlock.DragDirection.Down:
            otherPos.y -= 1;
            break;
        }

        if (gridModel.insideBorder(otherPos))
        {
            Transform t = gridModel.grid[(int)otherPos.x, (int)otherPos.y];
            if (t != null)
            {
                Vector3 oldPos = block.transform.position;
                block.transform.position = t.position;
                t.position = oldPos;
                block.updateGrid();
                MatchBlock otherBlock = t.GetComponent <MatchBlock>();
                otherBlock.updateGrid();
            }
            else
            {
                block.transform.position = new Vector3(otherPos.x, otherPos.y) + gridModel.offset;
            }
        }
        block.dragDirection = MatchBlock.DragDirection.None;
        lastMatchTime       = Time.time;
        clearBlocks         = false;
        CheckForMatches();
    }
コード例 #4
0
    private bool CheckMatch(int x, int y, ref int currentType, ref List <MatchBlock> blocks)
    {
        if (gridModel.grid[x, y] == null)
        {
            blocks.Clear();
            return(false);
        }

        MatchBlock mb = gridModel.grid[x, y].GetComponent <MatchBlock>();

        if (mb)
        {
            if (currentType != mb.BlockType || !mb.settled)
            {
                currentType = mb.BlockType;
                blocks.Clear();
            }

            if (mb.settled)
            {
                blocks.Add(mb);
            }

            if (blocks.Count == 3)
            {
                for (int i = blocks.Count - 1; i >= 0; --i)
                {
                    MatchBlock b = blocks[i];
                    b.IsMatched = true;
                }
                blocks.Clear();
                return(true);
            }
        }
        return(false);
    }
コード例 #5
0
ファイル: TestHelper.cs プロジェクト: wastaz/Zander
 public static IEnumerable<Tuple<string, string>> ToValueTuples(MatchBlock m)
 {
     return m.Rows.SelectMany(r => r.Cells
                         .Where(c => c.CellType == CellType.Value)
                         .Select(c => Tuple.Create(c.Name, c.Value)));
 }
コード例 #6
0
ファイル: TestHelper.cs プロジェクト: wastaz/Zander
 public static IEnumerable<IDictionary<string, string>> ToDictionaries(MatchBlock m)
 {
     return m.Rows.Select(r => r.ToDictionary());
 }
コード例 #7
0
        /// <summary>
        /// Perform the block operation
        /// </summary>
        private IEnumerable <T> DoBlock <T>(T input, MatchBlock block, IEnumerable <Guid> ignoreKeys, IRecordMatchingDiagnosticSession collector = null) where T : IdentifiedData
        {
            // Load the persistence service
            int tr = 1;

            try
            {
                collector?.LogStartAction(block);
                // Perpare filter
                var filter = block.Filter;
                NameValueCollection qfilter = new NameValueCollection();
                foreach (var b in filter)
                {
                    bool shouldIncludeExpression = true;
                    if (b.When?.Any() == true) // Only include the filter when the conditions are met
                    {
                        var guardExpression = b.GuardExpression;

                        if (guardExpression == null) // not built
                        {
                            var        parameter = Expression.Parameter(typeof(T));
                            Expression guardBody = null;
                            foreach (var whenClause in b.When)
                            {
                                var selectorExpression = QueryExpressionParser.BuildPropertySelector <T>(whenClause, true);
                                if (guardBody == null)
                                {
                                    guardBody = Expression.MakeBinary(ExpressionType.NotEqual, Expression.Invoke(selectorExpression, parameter), Expression.Constant(null));
                                }
                                else
                                {
                                    guardBody = Expression.MakeBinary(ExpressionType.AndAlso, Expression.Invoke(guardBody, parameter), Expression.MakeBinary(ExpressionType.NotEqual, Expression.Invoke(selectorExpression, parameter), Expression.Constant(null)));
                                }
                            }
                            b.GuardExpression = guardExpression = Expression.Lambda(guardBody, parameter).Compile();
                        }

                        shouldIncludeExpression = (bool)guardExpression.DynamicInvoke(input);
                    }

                    if (shouldIncludeExpression)
                    {
                        var nvc = NameValueCollection.ParseQueryString(b.Expression);
                        // Build the expression
                        foreach (var nv in nvc)
                        {
                            foreach (var val in nv.Value)
                            {
                                qfilter.Add(nv.Key, val);
                            }
                        }
                    }
                }

                // Do we skip when no conditions?
                if (!qfilter.Any())
                {
                    yield break;
                }

                // Add ignore clauses
                if (ignoreKeys?.Any() == true)
                {
                    qfilter.Add("id", ignoreKeys.Select(o => $"!{o}"));
                }
                if (input.Key.HasValue)
                {
                    qfilter.Add("id", $"!{input.Key}");
                }

                // Make LINQ query
                // NOTE: We can't build and store this since input is a closure
                // TODO: Figure out a way to compile this expression once
                var linq = QueryExpressionParser.BuildLinqExpression <T>(qfilter, new Dictionary <string, Func <Object> >()
                {
                    { "input", ((Func <T>)(() => input)) }
                }, safeNullable: true, lazyExpandVariables: false);

                // Add status keys
                if (typeof(IHasState).IsAssignableFrom(typeof(T)))
                {
                    var        stateAccess = Expression.MakeMemberAccess(linq.Parameters[0], typeof(T).GetProperty(nameof(IHasState.StatusConceptKey)));
                    Expression statePart   = null;
                    foreach (var stateKey in StatusKeys.ActiveStates)
                    {
                        if (statePart == null)
                        {
                            statePart = Expression.MakeBinary(ExpressionType.Equal, stateAccess, Expression.Convert(Expression.Constant(stateKey), typeof(Guid?)));
                        }
                        else
                        {
                            statePart = Expression.MakeBinary(ExpressionType.OrElse, Expression.MakeBinary(ExpressionType.Equal, stateAccess, Expression.Convert(Expression.Constant(stateKey), typeof(Guid?))), statePart);
                        }
                    }
                    linq = Expression.Lambda <Func <T, bool> >(Expression.MakeBinary(ExpressionType.AndAlso,
                                                                                     statePart, linq.Body), linq.Parameters[0]);
                }
                this.m_tracer.TraceVerbose("Will execute block query : {0}", linq);

                // Query control variables for iterating result sets
                var batch = block.BatchSize;
                if (batch == 0)
                {
                    batch = 100;
                }

                // Set the authentication context
                using (AuthenticationContext.EnterSystemContext())
                {
                    int ofs = 0;
                    while (ofs < tr)
                    {
                        if (block.UseRawPersistenceLayer)
                        {
                            var persistenceService = ApplicationServiceContext.Current.GetService <IDataPersistenceService <T> >();
                            if (persistenceService == null)
                            {
                                throw new InvalidOperationException($"Cannot find persistence service for {typeof(T).FullName}");
                            }

                            var records = persistenceService.Query(linq, ofs, batch, out tr, AuthenticationContext.SystemPrincipal);
                            collector?.LogSample(linq.ToString(), tr);
                            foreach (var itm in records)
                            {
                                yield return(itm);
                            }
                            ofs += batch;
                        }
                        else
                        {
                            var persistenceService = ApplicationServiceContext.Current.GetService <IRepositoryService <T> >();
                            if (persistenceService == null)
                            {
                                throw new InvalidOperationException($"Cannot find persistence service for {typeof(T).FullName}");
                            }

                            var records = persistenceService.Find(linq, ofs, batch, out tr);
                            collector?.LogSample(linq.ToString(), tr);
                            foreach (var itm in records)
                            {
                                if (itm.Key.HasValue &&
                                    itm.Key != input.Key &&
                                    ignoreKeys?.Contains(itm.Key.Value) != true)
                                {
                                    yield return(itm);
                                }
                            }
                            ofs += batch;
                        }

                        if (ofs > 100)
                        {
                            collector?.LogSample("too-many-results", tr);
                            yield break;
                        }
                    }
                }
            }
            finally
            {
                collector?.LogEndAction();
            }
        }
コード例 #8
0
    void Update()
    {
        if (Input.GetButtonDown("Fire1") && viewCamera.pixelRect.Contains(Input.mousePosition))
        {
            Debug.Log("Itsa mee, " + transform.name);
        }

        if (!ready)
        {
            return;
        }

        float tickInterval = 1f / matchSpeed;

        if (Time.time - lastMatchTime > tickInterval)
        {
            if (!clearBlocks)
            {
                CheckForMatches();
            }
            else
            {
                ClearMatchesFromBoard();
            }
            clearBlocks   = !clearBlocks;
            lastMatchTime = Time.time;
        }

        tickInterval = 1f / fallSpeed;
        if (Time.time - lastFallTime > tickInterval)
        {
            // Compact the grid, bottom up
            bool settledAtLeastOne = false;
            for (int y = 0; y < gridModel.h; ++y)
            {
                for (int x = 0; x < gridModel.w; ++x)
                {
                    if (gridModel.grid[x, y] != null)
                    {
                        MatchBlock mb = gridModel.grid[x, y].GetComponent <MatchBlock>();
                        if (mb)
                        {
                            if (mb.AttemptMove(Vector3.down, Vector3.up))
                            {
                                mb.settled = false;
                            }
                            else if (!mb.settled)
                            {
                                mb.settled        = true;
                                settledAtLeastOne = true;
                            }
                        }
                    }
                }
                if (settledAtLeastOne)
                {
                    //SFXSelector.Instance.PlayRandomSound(impactSounds);
                }
            }

            lastFallTime = Time.time;
        }
    }
コード例 #9
0
 /// <summary>
 /// Create match action info from block
 /// </summary>
 /// <param name="block">The block to create the action information for</param>
 public MatchActionDiagnosticInfo(MatchBlock block) : this()
 {
     this.ActionType    = "blocking-instruction";
     this.ActionData    = block.Filter.Select(o => o.Expression).ToArray();
     this.StartOfAction = DateTimeOffset.Now;
 }
コード例 #10
0
 public static IEnumerable <IDictionary <string, string> > ToDictionaries(MatchBlock m)
 {
     return(m.Rows.Select(r => r.ToDictionary()));
 }
コード例 #11
0
 public static IEnumerable <Tuple <string, string> > ToValueTuples(MatchBlock m)
 {
     return(m.Rows.SelectMany(r => r.Cells
                              .Where(c => c.CellType == CellType.Value)
                              .Select(c => Tuple.Create(c.Name, c.Value))));
 }