/// <summary> /// Grid Generator and attach required components, which help to get info of each object of grid /// </summary> private void GridGenertorCall() { gridInfo.enemyInfos.Clear(); if (_enemyPrefabs.Length == 0 || _gridTransformParent == null) { return; } for (int i = 0; i < _gridSize.x; i++) { for (int j = 0; j < _gridSize.y; j++) { int number = Random.Range(0, _enemyPrefabs.Length); GameObject enemy = Instantiate(_enemyPrefabs[number]); enemy.transform.position = new Vector2(_gridTransformParent.position.x + (j * _offSet), _gridTransformParent.position.y - (i * _offSet)); enemy.transform.SetParent(_gridTransformParent); // EnemyInfo enemyInfo = enemy.GetComponent <EnemyInfo>(); enemyInfo.EnemyPosition = new Vector2(i, j); enemyInfo.IsEnemyAlive = true; enemyInfo.IsEnemyFire = false; if (i == _gridSize.x - 1) { enemyInfo.IsEnemyFire = true; } gridInfo.enemyInfos.Add(enemyInfo); } } }
/// <summary> /// Check Enemy type are same or not. /// </summary> /// <param name="enemyInfo"></param> /// <returns></returns> private bool MatchAdjacentObject(EnemyInfo enemyInfo, Vector2 vector2) { var enemy = GridGenerator.Instance.gridInfo.enemyInfos.Find((obj) => obj.EnemyPosition == vector2); if (enemy != null) { if (enemyInfo.EnemyTypeValue == enemy.EnemyTypeValue) { GridGenerator.Instance.gridInfo.enemyInfos.Remove(enemy); Destroy(enemy.gameObject); return(true); } } return(false); }
/// <summary> /// Find Adjacent enemy and update score and delete /// </summary> /// <param name="enemyInfo"></param> private IEnumerator DeleteAdjacentEnemy(EnemyInfo enemyInfo) { yield return(new WaitForEndOfFrame()); // find Adjacent Enemy , Find in above column first int aboveRow = (int)enemyInfo.EnemyPosition.x - 1; if (aboveRow >= 0) { FindAdjcentPos(enemyInfo, aboveRow); } // find Adjacent Enemy , Find in below column first int belowRow = (int)enemyInfo.EnemyPosition.x + 1; if (belowRow < GridGenerator.Instance.gridInfo.gridSize.x) { FindAdjcentPos(enemyInfo, belowRow); } // Score Update updateScore?.Invoke(_destroyedEnemyByBullet); }
/// <summary> /// Find the Adjacent object position /// </summary> /// <param name="enemyInfo"></param> /// <param name="rowIndex"></param> private void FindAdjcentPos(EnemyInfo enemyInfo, int rowIndex) { // First Adjacent int coloumPosFirst = (int)enemyInfo.EnemyPosition.y - 1; if (coloumPosFirst >= 0) { if (MatchAdjacentObject(enemyInfo, new Vector2(rowIndex, coloumPosFirst))) { _destroyedEnemyByBullet++; } } // Second Adjacent int coloumPosSecond = (int)enemyInfo.EnemyPosition.y + 1; if (coloumPosSecond < GridGenerator.Instance.gridInfo.gridSize.y) { if (MatchAdjacentObject(enemyInfo, new Vector2(rowIndex, coloumPosSecond))) { _destroyedEnemyByBullet++; } } }