Example #1
0
        protected override void OnUpdate()
        {
            if (boardModel == null || gameStateModel.State != Match3State.MatchesFall)
            {
                return;
            }

            for (int x = 0; x < boardModel.BoardWidth; x++)
            {
                if (boardModel.HasEntityAt(new int2(x, boardModel.BoardHeight - 1)) == false)
                {
                    var levelConfig   = gameStateModel.GetCurrentLevelConfig();
                    var boardPosition = new int2(x, boardModel.BoardHeight);
                    var spawnPosition = BoardCalculator.ConvertBoardPositionToTransformPosition(boardPosition);
                    if (HasGemInProximityInColumn(x, spawnPosition))
                    {
                        continue;
                    }

                    var entity = EntitiesHelper.CreateGem(EntitiesHelper.GetRandomColor(levelConfig.availableColors),
                                                          BoardCalculator.ConvertBoardPositionToTransformPosition(boardPosition), boardPosition);

                    signalBus.Fire(new Match3Signals.GemCreatedSignal(entity, BoardCalculator.ConvertBoardPositionToTransformPosition(boardPosition)));
                    EntityManager.AddComponent <IsFallingComponent>(entity);
                }
            }
        }
Example #2
0
 private void CreateGems(LevelConfig levelConfig)
 {
     for (int x = 0; x < levelConfig.Width; x++)
     {
         for (int y = 0; y < levelConfig.Height; y++)
         {
             var gemColor      = EntitiesHelper.GetRandomColor(levelConfig.availableColors);
             var boardPosition = new int2(x, y);
             var position      = BoardCalculator.ConvertBoardPositionToTransformPosition(boardPosition);
             var gemEntity     = EntitiesHelper.CreateGem(gemColor,
                                                          position,
                                                          boardPosition);
             signalBus.Fire(new Match3Signals.GemCreatedSignal(gemEntity, position));
             boardModel.SetEntityAt(x, y, gemEntity);
         }
     }
 }
        protected override void OnUpdate()
        {
            if (gameStateModel == null)
            {
                return;
            }

            var time = Time.DeltaTime;

            Entities.WithStructuralChanges().ForEach(
                (Entity entity,
                 int entityInQueryIndex,
                 ref Translation translation,
                 ref BoardPositionComponent boardPositionComponent,
                 in SwappingComponent swapComponent) =>
            {
                var startPosition  = BoardCalculator.ConvertBoardPositionToTransformPosition(swapComponent.OriginBoardPosition);
                var targetPosition = BoardCalculator.ConvertBoardPositionToTransformPosition(swapComponent.TargetBoardPosition);

                var direction = targetPosition - translation.Value;

                if (math.distancesq(translation.Value, targetPosition) < 0.01f)
                {
                    boardPositionComponent = new BoardPositionComponent {
                        Position = swapComponent.TargetBoardPosition
                    };
                    boardModel.SetEntityAt(swapComponent.TargetBoardPosition.x, swapComponent.TargetBoardPosition.y, entity);
                    translation = new Translation {
                        Value = targetPosition
                    };
                    EntityManager.RemoveComponent <SwappingComponent>(entity);
                }
                else
                {
                    var velocity       = math.normalize(direction) * 7 * time;
                    translation.Value += velocity;
                }
            }).Run();
        }
Example #4
0
        protected override void OnUpdate()
        {
            if (gameStateModel == null || gameStateModel.State != Match3State.MatchesFall)
            {
                return;
            }

            var time = Time.DeltaTime;

            Entities.WithAll <IsFallingComponent>().WithNone <DestroyedComponent>().WithStructuralChanges().ForEach(
                (Entity entity, int entityInQueryIndex, ref BoardPositionComponent boardPositionComponent) =>
            {
                var positionUnder = new int2(boardPositionComponent.Position.x, boardPositionComponent.Position.y - 1);
                if (boardModel.IsPositionInBorders(positionUnder) && !boardModel.HasEntityAt(positionUnder))
                {
                    boardModel.ClearAt(boardPositionComponent.Position);

                    if (EntityManager.HasComponent <MoveToBoardPositionComponent>(entity) == false)
                    {
                        EntityManager.AddComponentData(entity, new MoveToBoardPositionComponent {
                            TargetPosition = positionUnder
                        });
                    }
                    else
                    {
                        EntityManager.SetComponentData(entity, new MoveToBoardPositionComponent {
                            TargetPosition = positionUnder
                        });
                    }

                    boardPositionComponent = new BoardPositionComponent {
                        Position = positionUnder
                    };
                    boardModel.SetEntityAt(positionUnder.x, positionUnder.y, entity);
                }
                else
                {
                    if (boardModel.IsPositionInBorders(positionUnder) == false)
                    {
                        EntityManager.RemoveComponent <IsFallingComponent>(entity);
                    }
                    else
                    {
                        var entityUnder = boardModel.GetEntityAt(positionUnder.x, positionUnder.y);
                        if (EntityManager.HasComponent <IsFallingComponent>(entityUnder) == false)
                        {
                            EntityManager.RemoveComponent <IsFallingComponent>(entity);
                        }
                    }
                }
            }).Run();


            Entities.WithNone <DestroyedComponent>().WithStructuralChanges().ForEach(
                (Entity entity,
                 int entityInQueryIndex,
                 ref Translation translation,
                 in BoardPositionComponent boardPositionComponent,
                 in MoveToBoardPositionComponent moveComponent) =>
            {
                var targetPosition = BoardCalculator.ConvertBoardPositionToTransformPosition(boardPositionComponent.Position);

                var direction = targetPosition - translation.Value;
                if (math.distancesq(translation.Value, targetPosition) < 0.01f || translation.Value.y < targetPosition.y)
                {
                    translation = new Translation {
                        Value = targetPosition
                    };
                    EntityManager.RemoveComponent <MoveToBoardPositionComponent>(entity);
                }
                else
                {
                    var velocity       = math.normalize(direction) * 10 * time;
                    translation.Value += velocity;
                }
            }).Run();