public void LoadNextLevel() { Levels.Instance.current++; if (_next != null) { Destroy(_next.gameObject); _next = null; } if (_shooter != null) { Destroy(_shooter.gameObject); _shooter = null; } _newRoot = 0; float realWidth = G.cols * G.radius * 2.0f; float realHeight = Mathf.Sqrt(3.0f) * (G.rows - 1) * G.radius + 2 * G.radius; _boundRect = new LBRect(-realWidth / 2.0f, -realHeight / 2.0f, realWidth, realHeight); iTween.Stop(compressor); compressor.transform.position = new Vector3(0, _boundRect.top, -1); _compressorLevel = 0; // load first Levels.Instance.Load(); int number = Random.Range(0, Levels.Instance.Count - 1); LevelData ld = Levels.Instance.GetLevel(number); _grid.Reset(); if (ld != null) { for (int i = 0; i < G.rows; i++) { for (int j = 0; j < G.cols; j++) { char ch = ld.data[i, j]; if (ch != '-') { Box one = GetOneBoxAtPosition(Misc.IndexToPosition(_boundRect, new Index(i, j)), ch); _grid.Set(i, j, one); } } } } // initialize the shooter LoadShooterBubble(); }
public void Recalculate(LBRect rect) { for (int i = 0; i < _rows; i++) { for (int j = 0; j < _cols; j++) { var one = _grids[i, j]; if (one != null) { one.transform.position = Misc.IndexToPosition(rect, new Index(i, j)); } } } }
/// <summary> /// Get the index from an approximate position. /// </summary> /// <returns> /// The index. /// </returns> /// <param name='position'> /// Approximate position. /// </param> public static Index PositionToIndex(LBRect bounds, Vector3 position) { float x = position.x - bounds.left; float y = position.y - bounds.bottom; int row; int col; row = Mathf.FloorToInt((bounds.height - G.radius - y + Mathf.Sqrt(3) / 2 * G.radius) / (Mathf.Sqrt(3) * G.radius)); if (row % 2 == 0) { if (x < 0) { x = 0; } col = Mathf.FloorToInt(x / (2 * G.radius)); } else { if (x < G.radius) { x = G.radius; } col = Mathf.FloorToInt((x - G.radius) / (2 * G.radius)); // NOTE: we need to check whether col is _numberColumns - 1, // because in this case, half of the actual grid if out of the screen. // The actual reason behind this is that when our shooter's position is the // same as right bound, the above calculation would give us _numberColumns-1, // since we round things up a little bit, which means: // x position x index // 0 0 // 2R 1 // 19R cols-1 if (col >= G.cols - 1) { col = G.cols - 2; } } return(new Index(row, col)); }
/// <summary> /// Get bubble's position from index. /// </summary> /// <returns> /// The bubble position. /// </returns> /// <param name='idx'> /// The index. /// </param> /// <param name='z'> /// Z. /// </param> public static Vector3 IndexToPosition(LBRect bounds, Index idx, float z = -1) { float x; float y; if (idx.row % 2 == 0) { x = G.radius + 2 * G.radius * idx.col; } else { x = 2 * G.radius + 2 * G.radius * idx.col; } y = bounds.height - G.radius - Mathf.Sqrt(3) * G.radius * idx.row; return(new Vector3(x + bounds.left, y + bounds.bottom, z)); }