void applyForce(BubbleBehaviour bubble) { float parameter = 1.0f; var direction = gameObject.transform.position - bubble.transform.position; bubble.GetComponentInParent <Rigidbody2D>().AddForce(parameter * direction.normalized / direction.magnitude); }
public void MoveAllBubblesDown() { for (int y = 1; y < _bubblesGrid.GetLength(1); y++) { for (int x = 0; x < _bubblesGrid.GetLength(0); x++) { if (_bubblesGrid[x, y - 1] != null) { DestroyBubble(new Vector2Int(x, y - 1), false); } if (_bubblesGrid[x, y] != null) { _bubblesGrid[x, y - 1] = _bubblesGrid[x, y]; _bubblesGrid[x, y - 1].GetComponent <BubbleBehaviour>().MoveToNewPosition(new Vector2Int(x, y - 1)); _bubblesGrid[x, y] = null; } } } for (int x = 0; x < _bubblesGrid.GetLength(0); x++) { BubbleBehaviour bubble = GetBubbleAtPosition(new Vector2Int(x, _bubblesGrid.GetLength(1) - 2)); if (bubble != null) { _lastRowHasOffset = bubble._hasOffset; } } for (int x = 0; x < _bubblesGrid.GetLength(0); x++) { InstantiateBubbleInGrid(new Vector2Int(x, _bubblesGrid.GetLength(1) - 1), !_lastRowHasOffset, GetRandomExponent()); } }
public MovingBubble(BubbleBehaviour bubble, Vector3 origin, Vector3 objective, float time) { this.bubble = bubble; this.origin = origin; this.objective = objective; this.time = time; }
public List <BubbleBehaviour> GetCluster(Vector2Int startingPosition, bool matchExponent) { List <BubbleBehaviour> bubblesCluster = new List <BubbleBehaviour>(); if (_checkedPositions.Contains(startingPosition)) { return(bubblesCluster); } _checkedPositions.Add(startingPosition); bubblesCluster.Add(GetBubbleAtPosition(startingPosition)); for (int currentBubbleID = 0; currentBubbleID < bubblesCluster.Count; currentBubbleID++) { BubbleBehaviour currentBubble = bubblesCluster[currentBubbleID]; if (currentBubble == null) { continue; } foreach (var surroundingPos in currentBubble._hasOffset ? positionsToCheckWithOffset : positionsToCheckWithoutOffset) { Vector2Int searchingPosition = currentBubble._positionOnGrid + surroundingPos; if (_checkedPositions.Contains(searchingPosition)) { continue; } _checkedPositions.Add(searchingPosition); //DebugExtensions.DrawCircle(searchingPosition * new Vector2(1, _yDistanceBetweenCircles) + new Vector2((surroundingPos.y == 0 ? currentBubble._hasOffset : !currentBubble._hasOffset) ? 0.5f : 0, 0), .4f, Color.blue, .1f); if (!IsInsideBoard(searchingPosition)) { continue; } BubbleBehaviour surroundingBubble = GetBubbleAtPosition(searchingPosition); if (surroundingBubble == null) { continue; } if (matchExponent) { if (surroundingBubble._exponent == currentBubble._exponent) { bubblesCluster.Add(surroundingBubble); } } else { bubblesCluster.Add(surroundingBubble); } } } return(bubblesCluster); }
void MoveGhostBubble(RaycastHit2D hit) { Vector2 hitBubblePosition = hit.collider.transform.position; BubbleBehaviour hitBubble = hit.collider.GetComponent <BubbleBehaviour>(); Vector2 direction = (hit.point - (hitBubblePosition)).normalized; int ghostBubbleXPosition; if (direction.y < 0) { ghostBubbleXPosition = (direction.x < 0 ? 0 : 1) + (hitBubble._hasOffset ? 0 : -1); } else { ghostBubbleXPosition = direction.x < 0 ? -1 : 1; } _ghostBubbleGridPosition = hitBubble._positionOnGrid + new Vector2Int(ghostBubbleXPosition, direction.y < 0 ? -1 : 0); _ghostBubbleHasOffset = direction.y < 0 ? !hitBubble._hasOffset : hitBubble._hasOffset; int ghostBubbleAltXPosition; if (direction.y < -(Mathf.Sqrt(2) / 2)) { ghostBubbleAltXPosition = (direction.x < 0 ? 1 : 0) + (hitBubble._hasOffset ? 0 : -1); } else { ghostBubbleAltXPosition = direction.x < 0 ? -1 : 1; } _ghostBubbleAltGridPosition = hitBubble._positionOnGrid + new Vector2Int(ghostBubbleAltXPosition, direction.y < -(Mathf.Sqrt(2) / 2) ? -1 : 0); _ghostBubbleAltHasOffset = direction.y < -(Mathf.Sqrt(2) / 2) ? !hitBubble._hasOffset : hitBubble._hasOffset; //Debug.DrawLine(hitBubblePosition, hit.point, Color.green); //DebugExtensions.DrawCircle(_ghostBubbleGridPosition * new Vector2(1, GameController._yDistanceBetweenCircles) + new Vector2(_ghostBubbleHasOffset ? 0.5f : 0, 0), .5f, Color.blue, 0); //DebugExtensions.DrawCircle(_ghostBubbleAltGridPosition * new Vector2(1, GameController._yDistanceBetweenCircles) + new Vector2(_ghostBubbleAltHasOffset ? 0.5f : 0, 0), .4f, Color.red, 0); if (GameController._gameController.GetBubbleAtPosition(_ghostBubbleGridPosition) == null) { if (GameController._gameController.IsInsideBoard(_ghostBubbleGridPosition)) { _ghostBubble.Show(_ghostBubbleGridPosition * new Vector2(1, GameController._yDistanceBetweenCircles) + new Vector2(_ghostBubbleHasOffset ? 0.5f : 0, 0)); } else { _ghostBubble.Hide(); } } else { if (GameController._gameController.IsInsideBoard(_ghostBubbleAltGridPosition)) { _ghostBubble.Show(_ghostBubbleAltGridPosition * new Vector2(1, GameController._yDistanceBetweenCircles) + new Vector2(_ghostBubbleAltHasOffset ? 0.5f : 0, 0)); } else { _ghostBubble.Hide(); } } }
public void CopyValuesFromBubble(BubbleBehaviour original) { _hasOffset = original._hasOffset; _positionOnGrid = original._positionOnGrid; _exponent = original._exponent; _color = original._color; Setup(); }
void FallBubble(Vector2Int position) { if (IsInsideBoard(position) && _bubblesGrid[position.x, position.y] != null) { score.AddToScore(GetBubbleAtPosition(position)._exponent); BubbleBehaviour fallingBubble = pool.CreateFallingBubble(GetBubbleAtPosition(position).transform.position); fallingBubble.CopyValuesFromBubble(GetBubbleAtPosition(position)); fallingBubble.GetComponent <Rigidbody2D>().AddForce(Random.insideUnitCircle, ForceMode2D.Impulse); DestroyBubble(position, true); } }
public void InstantiateBubbleInGrid(Vector2Int position, bool hasOffset, uint exponent) { if (IsInsideBoard(position)) { BubbleBehaviour bubbleComponent = pool.CreateBubble(position * new Vector2(1, _yDistanceBetweenCircles) + new Vector2(hasOffset ? 0.5f : 0, 0)); bubbleComponent._hasOffset = hasOffset; bubbleComponent._positionOnGrid = position; bubbleComponent._exponent = exponent; bubbleComponent.Refresh(); _bubblesGrid[position.x, position.y] = bubbleComponent; } }
void VisualMergeBubble(Vector2Int original, Vector2Int objective) { if (IsInsideBoard(original) && IsInsideBoard(objective) && _bubblesGrid[original.x, original.y] != null && _bubblesGrid[objective.x, objective.y] != null) { BubbleBehaviour movingBubble = pool.CreateMovingBubble(GetBubbleAtPosition(original).transform.position); movingBubble.CopyValuesFromBubble(GetBubbleAtPosition(original)); _movingBubbles.Add(new MovingBubble( movingBubble, GetBubbleAtPosition(original).transform.position, GetBubbleAtPosition(objective).transform.position, 0 )); } }
public bool CanMoveDown() { bool isSecondLastRowEmpty = true; for (int x = 0; x < _bubblesGrid.GetLength(0); x++) { BubbleBehaviour bubble = GetBubbleAtPosition(new Vector2Int(x, 1)); if (bubble != null) { isSecondLastRowEmpty = false; } } return(isSecondLastRowEmpty); }
public bool IsRowEmpty(int y) { bool isRowEmpty = true; for (int x = 0; x < _bubblesGrid.GetLength(0); x++) { BubbleBehaviour bubble = GetBubbleAtPosition(new Vector2Int(x, y)); if (bubble != null) { isRowEmpty = false; } } return(isRowEmpty); }
private IEnumerator CreateObstacle() { float newWait = 0.0f; for (;;) { int rnd = Random.Range(1, 100); // print(rnd); foreach (Obstacles obs in System.Enum.GetValues(typeof(Obstacles))) { if (rnd < (int)obs) { switch (obs) { case Obstacles.Bubble: BubbleBehaviour.Spawn(baseBubble, spawnBubbleVelocity, this.transform.position); newWait = 0.0f; break; case Obstacles.Floater: FloaterBehaviour.Spawn(baseFloater, spawnFloaterVelocity, this.transform.position); newWait = 0.0f; break; case Obstacles.Bird: Vector2 pos = new Vector2(this.transform.position.x, this.transform.position.y + Random.Range(1.3f, 3f)); BirdBehaviour.Spawn(baseBird, spawnBirdVelocity, pos); break; default: timeNone += newWait; if (timeNone > maxTimeNone) { BubbleBehaviour.Spawn(baseBubble, spawnBubbleVelocity, this.transform.position); } break; } break; } } newWait = Random.Range(randomWait_min, randomWait_max); yield return(new WaitForSeconds(newWait)); } }
void Start() { textBoxReference [0] = GameObject.Find("TextBox_Android"); textBoxReference [1] = GameObject.Find("TextBox"); playerSound = GameObject.FindGameObjectWithTag("Player").GetComponent <PlayerSound>(); if (GameController.instance.isAndroidVersion) { interactButton = GameObject.Find("InteractionButton").GetComponent <BubbleBehaviour> (); textBox = textBoxReference[0].GetComponent <FadeInFadeOut>(); textBoxReference[1].SetActive(false); } else { textBox = textBoxReference[1].GetComponent <FadeInFadeOut>(); textBoxReference[0].SetActive(false); } feedText = textBox.transform.GetChild(0).GetComponent <FeedTextFromObject> (); }
void ExplodeAround(Vector2Int center, int radius) { for (int x = -radius; x < radius; x++) { for (int y = -radius; y < radius; y++) { Vector2Int currentPos = new Vector2Int(x, y) + center; DebugExtensions.DrawCircle(currentPos * new Vector2(1, _yDistanceBetweenCircles), .4f, Color.blue, .1f); if (!IsInsideBoard(currentPos)) { continue; } BubbleBehaviour bubble = GetBubbleAtPosition(currentPos); if (bubble != null) { score.AddToScore(bubble._exponent); DestroyBubble(currentPos, true); } } } }
public List <BubbleBehaviour> GetSurroundingBubbles(Vector2Int position, bool cleanCheckedPositions) { if (cleanCheckedPositions) { _checkedPositions = new List <Vector2Int>(); } List <BubbleBehaviour> surroundingBubbles = new List <BubbleBehaviour>(); BubbleBehaviour currentBubble = GetBubbleAtPosition(position); if (currentBubble == null) { return(surroundingBubbles); } foreach (var surroundingPos in currentBubble._hasOffset ? positionsToCheckWithOffset : positionsToCheckWithoutOffset) { Vector2Int searchingPosition = currentBubble._positionOnGrid + surroundingPos; if (_checkedPositions.Contains(searchingPosition)) { continue; } _checkedPositions.Add(searchingPosition); if (!IsInsideBoard(searchingPosition)) { continue; } BubbleBehaviour surroundingBubble = GetBubbleAtPosition(searchingPosition); if (surroundingBubble == null) { continue; } surroundingBubbles.Add(surroundingBubble); } return(surroundingBubbles); }
void Start() { textBoxReference [0] = GameObject.Find ("TextBox_Android"); textBoxReference [1] = GameObject.Find ("TextBox"); playerSound = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerSound>(); if (GameController.instance.isAndroidVersion) { interactButton = GameObject.Find ("InteractionButton").GetComponent<BubbleBehaviour> (); textBox = textBoxReference[0].GetComponent<FadeInFadeOut>(); textBoxReference[1].SetActive(false); } else { textBox = textBoxReference[1].GetComponent<FadeInFadeOut>(); textBoxReference[0].SetActive(false); } feedText = textBox.transform.GetChild(0).GetComponent<FeedTextFromObject> (); }
// Update is called once per frame void Update() { //increment countdown countdown -= Time.deltaTime; Object[] bubbles = FindObjectsOfType(typeof(BubbleBehaviour)); Object[] negativeBubbles = FindObjectsOfType(typeof(NegativeBubbleBehaviour)); Object[] magneticBubbles = FindObjectsOfType(typeof(MagneticBubbleBehaviour)); //spawn bubbles if not enough bubbles on screen if (bubbles.Length < 5) { Spawn(); } Touch[] myTouches = Input.touches; for (int i = 0; i < Input.touchCount; i++) { Debug.Log("Input position: " + i + " " + Input.GetTouch(i).position); //2D solution Vector3 pos = Camera.main.ScreenToWorldPoint(Input.GetTouch(i).position); RaycastHit2D hit = Physics2D.Raycast(pos, Vector2.zero); if (hit.collider != null) { Debug.Log("Hitting:" + hit.collider.name); BubbleBehaviour bubble = hit.collider.gameObject.GetComponent(typeof(BubbleBehaviour)) as BubbleBehaviour; if (bubble != null) { bubble.onPop(); } NegativeBubbleBehaviour negativeBubble = hit.collider.gameObject.GetComponent(typeof(NegativeBubbleBehaviour)) as NegativeBubbleBehaviour; if (negativeBubble != null) { negativeBubble.onPop(); } MagneticBubbleBehaviour magneticBubble = hit.collider.gameObject.GetComponent(typeof(MagneticBubbleBehaviour)) as MagneticBubbleBehaviour; if (magneticBubble != null) { magneticBubble.onPop(); } } } //fallback for testing on the PC if (Input.GetMouseButtonDown(0)) { Debug.Log("Mouse down"); Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition); RaycastHit2D hit = Physics2D.Raycast(pos, Vector2.zero); if (hit.collider != null) { Debug.Log("Hitting:" + hit.collider.name); BubbleBehaviour bubble = hit.collider.gameObject.GetComponent(typeof(BubbleBehaviour)) as BubbleBehaviour; if (bubble != null) { bubble.onPop(); } NegativeBubbleBehaviour negativeBubble = hit.collider.gameObject.GetComponent(typeof(NegativeBubbleBehaviour)) as NegativeBubbleBehaviour; if (negativeBubble != null) { negativeBubble.onPop(); } MagneticBubbleBehaviour magneticBubble = hit.collider.gameObject.GetComponent(typeof(MagneticBubbleBehaviour)) as MagneticBubbleBehaviour; if (magneticBubble != null) { magneticBubble.onPop(); } } } }
IEnumerator ShootBubbleRoutine(Vector2Int position, bool hasOffset) { _isShooting = true; //Shoot bubble to position Vector2 startingPosition = _currentBubble.transform.position; Vector2 finishingPosition = new Vector2(position.x + (hasOffset ? 0.5f : 0), position.y * GameController._yDistanceBetweenCircles); Vector3 arrowMidPoint = arrow.GetPosition(1); float lengthOfFirstSegment = Vector3.Distance(arrow.GetPosition(0), arrow.GetPosition(1)); float lengthOfSecondSegment = Vector3.Distance(arrow.GetPosition(1), finishingPosition); float timeToCompleteFirstSegment = lengthOfFirstSegment / (lengthOfFirstSegment + lengthOfSecondSegment); float timeToCompleteSecondSegment = 1 - timeToCompleteFirstSegment; float stepSize = Time.fixedDeltaTime * 5; for (float i = 0; i < timeToCompleteFirstSegment; i += stepSize) { _currentBubble.transform.position = EaseMethods.EaseVector2(EaseMethods.GetEase(EaseStyle.LinearEaseInOut), startingPosition, arrowMidPoint, i, timeToCompleteFirstSegment); yield return(new WaitForFixedUpdate()); } for (float i = 0; i < timeToCompleteSecondSegment; i += stepSize) { _currentBubble.transform.position = EaseMethods.EaseVector2(EaseMethods.GetEase(EaseStyle.SineEaseOut), arrowMidPoint, finishingPosition, i, timeToCompleteSecondSegment); yield return(new WaitForFixedUpdate()); } //Place bubble on grid _currentBubble.transform.position = finishingPosition; _currentBubble.GetComponent <CircleCollider2D>().enabled = true; BubbleBehaviour currentBubbleComponent = _currentBubble.GetComponent <BubbleBehaviour>(); _bubblesGrid[position.x, position.y] = currentBubbleComponent; currentBubbleComponent._hasOffset = hasOffset; currentBubbleComponent._positionOnGrid = position; Vibration.VibrateGlobal(75); foreach (var bubble in GetSurroundingBubbles(currentBubbleComponent._positionOnGrid, true)) { bubble.Push(currentBubbleComponent.transform.position); } //Try mergin bubbles; Vector2Int lastMergedBubblePosition; Vector2Int newMergedBubblePosition = position; int comboSize = 0; do { lastMergedBubblePosition = newMergedBubblePosition; newMergedBubblePosition = MergeBubbles(newMergedBubblePosition); Vibration.VibrateGlobal(75); yield return(new WaitWhile(() => _movingBubbles.Count > 0)); comboSize++; } while (!newMergedBubblePosition.Equals(new Vector2Int(-1, -1))); //Explode if more than 2048 if (GetBubbleAtPosition(lastMergedBubblePosition)._exponent > 10) { ExplodeAround(lastMergedBubblePosition, 2 + (int)(GetBubbleAtPosition(lastMergedBubblePosition)._exponent - 10)); } yield return(new WaitForSeconds(0.2f)); bool isAllEmpty = true; for (int y = 0; y < _bubblesGrid.GetLength(1); y++) { if (!IsRowEmpty(y)) { isAllEmpty = false; break; } } if (isAllEmpty) { pool.CreatePerfectText(); Vibration.VibrateGlobal(300); } if (position.y == 0) { MoveAllBubblesUp(); } if (IsRowEmpty(1)) { MoveAllBubblesDown(); } if (IsRowEmpty(2)) { MoveAllBubblesDown(); } if (IsRowEmpty(3)) { MoveAllBubblesDown(); } if (IsRowEmpty(4)) { MoveAllBubblesDown(); } Vector3 currentScale = _nextBubble.transform.localScale; startingPosition = _nextBubble.transform.position; for (float i = 0; i < 1; i += Time.fixedDeltaTime * 5) { _nextBubble.transform.localScale = EaseMethods.EaseVector3(EaseMethods.GetEase(EaseStyle.BackEaseOut), currentScale, new Vector3(1, 1, 1), i, 1); _nextBubble.transform.position = EaseMethods.EaseVector2(EaseMethods.GetEase(EaseStyle.SineEaseOut), startingPosition, new Vector2(2.75f, -3), i, 1); yield return(new WaitForFixedUpdate()); } _nextBubble.transform.localScale = new Vector3(1, 1, 1); _nextBubble.transform.position = new Vector2(2.75f, -3); _currentBubble = _nextBubble; _nextBubble = pool.CreateBubble(new Vector3(1.5f, -3)).gameObject; _nextBubble.transform.localScale = new Vector3(0.75f, 0.75f, 1); _nextBubble.GetComponent <BubbleBehaviour>()._exponent = GetRandomExponent(); _nextBubble.GetComponent <BubbleBehaviour>().Refresh(); _nextBubble.GetComponent <CircleCollider2D>().enabled = false; _isShooting = false; }
public void DestroyMovingBubble(BubbleBehaviour bubble) { CustomDestroyObject(_movingBubblesPool, bubble.gameObject); }