private void CheckSwipe(GemView fromGem) { var touchPosWorld = cameraMain.ScreenToWorldPoint(Input.mousePosition); Vector2 touchPosWorld2D = new Vector2(touchPosWorld.x, touchPosWorld.y); Vector2 swipe = new Vector2(touchPosWorld2D.x - fromGem.transform.position.x, touchPosWorld2D.y - fromGem.transform.position.y); if (swipe.magnitude < 0.05f) // Too short swipe { return; } var fromPosition = entityManager.GetComponentData <BoardPositionComponent>(fromGem.Entity); int2 nextGemCoord = fromPosition.Position; if (Mathf.Abs(swipe.x) > Mathf.Abs(swipe.y)) { if (swipe.x > 0) { nextGemCoord.x += 1; } else { nextGemCoord.x -= 1; } } else { if (swipe.y > 0) { nextGemCoord.y += 1; } else { nextGemCoord.y -= 1; } } if (boardModel.IsPositionInBorders(nextGemCoord) && boardModel.HasEntityAt(nextGemCoord)) { StartSwap(fromGem.Entity, boardModel.GetEntityAt(nextGemCoord)); } }
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();