Example #1
0
        protected override void OnUpdate()
        {
            int w        = _cache.BoardWidth;
            int h        = _cache.BoardHeight;
            var fallDown = new NativeArray <int>(w * h, Allocator.Temp);

            for (int x = 0; x < w; x++)
            {
                int height = 0;
                for (int y = 0; y < h; y++)
                {
                    fallDown[Index(x, y)] = height;
                    height += (_cache.GetGem(x, y) == GemType.None) ? 1 : 0;
                }
            }

            Entities.ForEach((Entity e, ref BoardPositionComponent gem) => {
                var pos    = gem.BoardPosition;
                int amount = fallDown[Index(pos.x, pos.y)];
                if (amount > 0)
                {
                    pos.y            -= amount;
                    gem.BoardPosition = pos;

                    PostUpdateCommands.AddComponent(e, typeof(GemFallAnimationComponent));
                    PostUpdateCommands.AddComponent(e, typeof(BoardAnimationComponent));
                }
            });

            fallDown.Dispose();

            int Index(int x, int y) => x * _cache.BoardHeight + y;
        }
Example #2
0
        protected override void OnUpdate()
        {
            if (GetSingleton <BoardStateComponent>().State != BoardState.Animating)
            {
                return;
            }

            int w = _boardCache.BoardWidth;
            int h = _boardCache.BoardHeight;

            var board = GetSingleton <BoardComponent>();

            using (var prefabEntities = _gemPrefabs.ToEntityArray(Allocator.TempJob))
            {
                for (int x = 0; x < w; x++)
                {
                    int numToSpawn = 0;
                    for (int y = 0; y < h; y++)
                    {
                        if (_boardCache.GetGem(x, y) == GemType.None)
                        {
                            numToSpawn++;
                        }
                    }
                    if (numToSpawn == 0)
                    {
                        continue;
                    }

                    float maxHeight = math.max(_boardCache.GetMaxHeight(x), board.MaxGridPosition.y + board.CellSize / 2);
                    for (int i = 0; i < numToSpawn; i++)
                    {
                        int y      = h - numToSpawn + i;
                        var entity = EntityManager.Instantiate(prefabEntities[_gemRng.NextInt(prefabEntities.Length)]);
                        EntityManager.AddComponent <BoardAnimationComponent>(entity);
                        EntityManager.AddComponent <GemFallAnimationComponent>(entity);
                        EntityManager.SetComponentData(entity, new Translation
                        {
                            Value = new float3(board.GridCellCenter(x, y).x, maxHeight, 0),
                        });
                        EntityManager.AddComponentData(entity, new BoardPositionComponent
                        {
                            BoardPosition = new int2(x, y),
                        });

                        maxHeight += board.CellSize;
                    }
                }
            }
        }
Example #3
0
        protected override void OnUpdate()
        {
            if (GetSingleton <BoardStateComponent>().State != BoardState.LookingForMatch)
            {
                return;
            }

            var board = GetSingleton <BoardComponent>();
            int w     = board.Size.x;
            int h     = board.Size.y;

            int numMatches = 0;

            using (var matches = new NativeArray <MatchInfo>(w * h, Allocator.Temp))
            {
                MatchFinder.FindMatches(w, _boardCache.Board, matches);

                var eventQueue = _events.CreateEventQueue <MatchEvent>();
                for (int i = 0; i < matches.Length; i++)
                {
                    var  match    = matches[i];
                    bool isHMatch = match.HorizontalLength >= 3 && match.MatchLeft == 0;
                    bool isVMatch = match.VerticalLength >= 3 && match.MatchDown == 0;
                    if (!isHMatch && !isVMatch)
                    {
                        continue;
                    }
                    numMatches += 1;
                    int  x      = i / h;
                    int  y      = i % h;
                    byte length = (byte)(1 + (isHMatch ? match.MatchRight : match.MatchUp));
                    eventQueue.Enqueue(new MatchEvent
                    {
                        Position    = new Unity.Mathematics.int2(x, y),
                        Gem         = _boardCache.GetGem(x, y),
                        MatchLength = length
                    });
                }

                Entities.ForEach((Entity e, ref GemComponent gem, ref BoardPositionComponent boardPos) =>
                {
                    int index = boardPos.BoardPosition.x * h + boardPos.BoardPosition.y;
                    if (matches[index].HorizontalLength >= 3 || matches[index].VerticalLength >= 3)
                    {
                        PostUpdateCommands.AddComponent(e, typeof(MatchedGemComponent));
                        PostUpdateCommands.AddComponent(e, typeof(BoardAnimationComponent));
                        PostUpdateCommands.AddComponent(e, typeof(GemMatchAnimationComponent));
                    }
                });
            }

            if (numMatches > 0)
            {
                _events.PostOneShot(new BoardStateTransitionEvent
                {
                    NewState = BoardState.Animating
                });
                return;
            }

            // check whether there are any moves
            using (var matches = new NativeArray <SwapMatches>(w * h, Allocator.Temp))
            {
                MatchFinder.ScoreSwapMoves(w, _boardCache.Board, matches);
                bool anyMatch = false;
                for (int i = 0; i < matches.Length; i++)
                {
                    if (matches[i].SwapRight1.Match.MaxLength > 2 ||
                        matches[i].SwapRight2.Match.MaxLength > 2 ||
                        matches[i].SwapUp1.Match.MaxLength > 2 ||
                        matches[i].SwapUp2.Match.MaxLength > 2)
                    {
                        anyMatch = true;
                        break;
                    }
                }
                if (anyMatch)
                {
                    _events.PostOneShot(new BoardStateTransitionEvent {
                        NewState = BoardState.Ready
                    });
                    return;
                }
                UnityEngine.Debug.Log("No more matches! Cleaning board");
                EntityManager.DestroyEntity(Entities.WithAll <GemComponent>().ToEntityQuery());
                _events.PostOneShot(new BoardStateTransitionEvent {
                    NewState = BoardState.Initializing
                });
            }
        }