Ejemplo n.º 1
0
        private Dictionary <Entity, int> CalculateCellsOffsets(GameFieldSize gameSettings)
        {
            GravityDirection         gravityDirection = _helper.Gravity;
            CellsMap                 map            = new CellsMap(EntityManager);
            Dictionary <Entity, int> offsettedCells = new Dictionary <Entity, int>();
            var i = new IntIterator(new IteratorSettings(0, gameSettings.Width - 1, 1));

            while (i.MoveNext())
            {
                var j = new IntIterator(new IteratorSettings(0, gameSettings.Height - 1, -(int)gravityDirection));
                while (j.MoveNext())
                {
                    if (map.GetCell(i, j, out var cell))
                    {
                        continue;
                    }

                    var k = new IntIterator(new IteratorSettings(
                                                j - (int)gravityDirection,
                                                IteratorSettings.GetTo(0, gameSettings.Height - 1, -(int)gravityDirection),
                                                -(int)gravityDirection));
                    while (k.MoveNext())
                    {
                        if (!map.GetCell(i, k, out var affectedCell))
                        {
                            continue;
                        }

                        if (!offsettedCells.TryGetValue(affectedCell, out var o))
                        {
                            offsettedCells[affectedCell] = 0;
                        }

                        offsettedCells[affectedCell] += (int)gravityDirection;
                    }
                }
            }

            return(offsettedCells);
        }
        private void GenerateTopLine()
        {
            bool   active       = false;
            Entity refillEntity = Entity.Null;

            Entities.ForEach((Entity entity, ref GenerateLineRequest request) =>
            {
                if (request.Status == RequestStatus.New)
                {
                    refillEntity   = entity;
                    active         = true;
                    request.Status = RequestStatus.Processing;
                }
            });

            if (!active)
            {
                return;
            }

            var           process   = new ProcessGroup <TimerComponent, GenerateCellData>(EntityManager, Entities);
            GameFieldSize fieldSize = _helper.GetSize();

            Color[]  colors = _helper.GetColors();
            CellsMap map    = new CellsMap(EntityManager);
            float    delay  = 0;
            int      y      = _helper.Gravity == GravityDirection.Down ? fieldSize.Height - 1 : 0;

            for (int i = 0; i < fieldSize.Width; i++)
            {
                if (!map.GetCell(i, y, out var cell))
                {
                    delay += GenerateTimeout;
                    process.Add(new TimerComponent(delay), new GenerateCellData {
                        x = i, y = y, Color = colors.GetRandom()
                    });
                }
            }

            process.OnItemCompleted += (Entity entity, ref TimerComponent completable, ref GenerateCellData marker) =>
            {
                AddCellAt(marker.x, marker.y, marker.Color);
            };
            process.OnCompleted += group =>
            {
                EntityManager.SetComponentData(refillEntity, new GenerateLineRequest {
                    Status = RequestStatus.Completed
                });
                DestroyEntityAfterFrame(refillEntity, 2);
            };
            HoldProcess(process);
        }