private static void ProcessHorizontal(Grid grid, TetraminoMono tetraminoMono, Vector2Int offset) { if (offset.sqrMagnitude == 0) { return; } Tetramino tetraminoData = tetraminoMono.tetramino; //bool collision = // Grid.Collision(grid, tetraminoData, offset, Tetramino.RotationType.None); //if (!collision) //{ // tetraminoMono.TranslateCenterPosition(offset); //} Vector2Int projectionDir = VectorUtil.V2_V2Int(((Vector2)offset).normalized); Vector2Int stopPosition = Grid.StopPosition(Grid.Ins, tetraminoData, projectionDir); Vector2Int stopDeltaPosition = stopPosition - tetraminoData.centerPos; int stopDeltaPositionSqrManitude = stopDeltaPosition.sqrMagnitude; int offsetPositionSqrManitude = offset.sqrMagnitude; if ((stopDeltaPositionSqrManitude < offsetPositionSqrManitude)) { tetraminoMono.SetCenterPosition(stopPosition); Debug.Log("stopDeltaPositionSqrManitude < offsetPositionSqrManitude"); } else { tetraminoMono.TranslateCenterPosition(offset); } }
public void DeveDesenharFormaZ() { var forma = Tetramino.Draw(PieceType.Z, new Point(), 0); Assert.AreEqual(4, forma.Blocks.Count()); Assert.AreEqual("1+0,1+1,0+1,0+2", forma.ToString()); }
// for each colummn in each row check if null public void UpdateGrid(Tetramino tetramino) { for (int y = 0; y < gridHeight; ++y) { for (int x = 0; x < gridWidth; ++x) { if (grid[x, y] != null) { if (grid[x, y].parent == tetramino.transform) { grid[x, y] = null; } } } } foreach (Transform mino in tetramino.transform) { Vector2 pos = Round(mino.position); if (pos.y < gridHeight) { grid[(int)pos.x, (int)pos.y] = mino; } } }
private static int[] RowsToCheckForSweep(Tetramino tetramino) { Vector2Int[] absPoses = tetramino.AbsPoses; int[] rowsToCheckForSweepNotUnique = LoopUtil.LoopFunc <int>((i) => absPoses[i].y, 4); int[] rowsToCheckForSweepUnique = rowsToCheckForSweepNotUnique.Distinct().OrderBy(i => i).ToArray(); return(rowsToCheckForSweepUnique); }
// coordinates where tetramino stop after moving in projection direction // tetramino stops because of the grid's stopping blocks(saved in grid.blocks 2D array) public static Vector2Int StopPosition(Grid grid, Tetramino tetramino, Vector2Int projectionDir) { int offsetCount = 0; while (!Grid.Collision (grid, tetramino, projectionDir * offsetCount, Tetramino.RotationType.None)) { offsetCount++; if (offsetCount > 100) { Debug.LogError("Collision doesn't work properly apparentlly."); break; } } if (offsetCount == 0) { Debug.Log("Tetramino already is in position where it can't be."); Debug.LogError("?"); Debug.Log(projectionDir); Debug.Break(); } offsetCount--; Vector2Int stopPosition = tetramino.centerPos + projectionDir * offsetCount; return(stopPosition); }
private static void ProcessHorizontal(Grid grid, TetraminoMono tetraminoMono, Vector2Int offset) { if (offset.sqrMagnitude == 0) { return; } Tetramino tetraminoData = tetraminoMono.tetramino; //Vector2Int projectionDir = VectorUtil.V2_V2Int(((Vector2)offset).normalized); Vector2Int projectionDir = offset; Vector2Int stopPosition = Grid.StopPosition(grid, tetraminoData, projectionDir); Vector2Int stopDeltaPosition = stopPosition - tetraminoData.centerPos; int stopDeltaPositionSqrManitude = stopDeltaPosition.sqrMagnitude; int offsetPositionSqrManitude = offset.sqrMagnitude; if (stopDeltaPositionSqrManitude < offsetPositionSqrManitude) { tetraminoMono.SetCenterPosition(stopPosition); Debug.Log("stopDeltaPositionSqrManitude < offsetPositionSqrManitude"); // tetramino can't move because of grid blocks being occupied } else { tetraminoMono.TranslateCenterPosition(offset); } }
public static Tetramino SetUp(Tetramino tetramino, Tetramino.TetraminoType type) { tetramino.type = type; switch (type) { case TetraminoType.I: tetramino.Poses = GetPoses(new int[4, 2] { { 0, 0 }, { 1, 0 }, { 2, 0 }, { 3, 0 } }); tetramino.rotationPoint = new Vector2(3f / 2f, -1f / 2f); break; case TetraminoType.J: tetramino.Poses = GetPoses(new int[4, 2] { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 1, 2 } }); tetramino.rotationPoint = new Vector2(1f, 1f); break; case TetraminoType.L: tetramino.Poses = GetPoses(new int[4, 2] { { 0, 0 }, { 1, 0 }, { 0, 1 }, { 0, 2 } }); tetramino.rotationPoint = new Vector2(0f, 1f); break; case TetraminoType.O: tetramino.Poses = GetPoses(new int[4, 2] { { 0, 0 }, { 1, 0 }, { 0, 1 }, { 1, 1 } }); tetramino.rotationPoint = new Vector2(1f / 2f, 1f / 2f); break; case TetraminoType.S: tetramino.Poses = GetPoses(new int[4, 2] { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 2, 1 } }); tetramino.rotationPoint = new Vector2(1f, 0f); break; case TetraminoType.T: tetramino.Poses = GetPoses(new int[4, 2] { { 1, 0 }, { 0, 1 }, { 1, 1 }, { 2, 1 } }); tetramino.rotationPoint = new Vector2(1f, 1f); break; case TetraminoType.Z: tetramino.Poses = GetPoses(new int[4, 2] { { 0, 1 }, { 1, 1 }, { 1, 0 }, { 2, 0 } }); tetramino.rotationPoint = new Vector2(1f, 0f); break; default: Debug.LogError("Unsupported tetramino type: " + type); break; } return(tetramino); }
void Start() { print("Started"); takenMatrixPositions = new bool[rows, cols]; initialTetraminoYAddition = (cols / 2) - 3; tetraminos = new Tetramino[predefinedStates.Length]; for (int r = 0; r < rows; r++) { for (int c = 0; c < cols; c++) { Transform instance = Instantiate(squarePrefab, new Vector3(r, c, 0), Quaternion.identity) as Transform; instance.transform.SetParent(transform.Find("Manager")); } } for (int i = 0; i < predefinedStates.Length; i++) { Tetramino currTetramino = new Tetramino(); currTetramino.States = predefinedStates [i]; tetraminos [i] = currTetramino; } GenerateNewTetramino(); if (isWithStupidAI) { controlsEnabled = false; } lastRoutine = StartCoroutine(MoveTetraminos()); }
void GenerateNewTetramino() { currentCubesArr = new List <GameObject> (); currTetramino = tetraminos [Random.Range(0, tetraminos.Length)]; currTetraminoState = currTetramino.getRandomState(); currTetraminoCoords = new int[8]; horizontalAddition = initialTetraminoYAddition; isFirstRotation = true; verticalAddition = 0; for (int i = 0; i < currTetramino.States [currTetraminoState].Length; i += 2) { currTetraminoCoords [i] = currTetramino.States [currTetraminoState] [i]; currTetraminoCoords [i + 1] = currTetramino.States [currTetraminoState] [i + 1] + horizontalAddition; if (takenMatrixPositions [currTetraminoCoords [i], currTetraminoCoords [i + 1]]) { isGameOver = true; controlsEnabled = false; print("GAME OVER"); break; } } if (!isGameOver) { for (int i = 0; i < 4; i++) { GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube); cube.GetComponent <Renderer> ().material.color = currTetramino.color; currentCubesArr.Add(cube); } UpateCurrentCubesPosition(); } }
public void UpdateGrid(Tetramino tetramino) { for(int y = 0; y < gridHeight; y++) { for(int x = 0; x < gridWidth; ++x) { if(grid[x, y] != null) { if(grid[x, y].parent == tetramino.transform) { grid[x, y] = null; } } } } foreach(Transform mino in tetramino.transform) { Vector2 pos = Round(mino.position); if(pos.y < gridHeight) { grid[(int)pos.x, (int)pos.y] = mino; } } }
private void Build(Tetramino.TetraminoType tetrominoType) { Tetramino tetromino = new Tetramino(tetrominoType); Vector3 rotationPoint = tetromino.rotationPoint; Vector2Int[] poses = tetromino.Poses; Vector3 w_tetrominoPosition = transform.position; Vector3 l_rotationTransformPosition = rotationPoint; Vector3[] l_cellTransformPositions = CellPosesRelative2RotationPoint(poses, rotationPoint); GameObject rotation_gameObject = new GameObject("Rotation transform"); rotationTransform = AssignParentAndLocalPosition(rotation_gameObject, transform, l_rotationTransformPosition); List <Transform> cellTransformsList = new List <Transform>(); int cellIndex = 0; foreach (Vector3 pos in l_cellTransformPositions) { GameObject cell_go = Instantiate(tetrominoCellPrefab).gameObject; cell_go.name = $"Cell index: {cellIndex++}"; cellTransformsList.Add ( AssignParentAndLocalPosition(cell_go, rotationTransform, pos) ); } cellTransforms = cellTransformsList.ToArray(); }
public Block(Vector2Int _spawnPosition, Tetramino _tetramino, Game.SquareDefinition[,] _grid) { position = _spawnPosition; tetramino = _tetramino; grid = _grid; needToUpdate = true; }
private static List <int> RowsToSweepExtended(Tetramino tetramino, out bool sweep) { List <int> rowsToSweep = RowsToSweep(tetramino, out sweep); List <int> rowsToSweepExtended = rowsToSweep.ToList();// copy list int rowCount = Grid.gridSize.y; rowsToSweepExtended.Add(rowCount + 1); return(rowsToSweepExtended); }
public GameBoard(Board board) { rows = board.GetMainGridRow; cols = board.GetMainGridCol; ROWS = board.GetExtraGridRow; COLS = board.GetExtraGridCol; tetraminoRandom = new Random(); curNumber = tetraminoRandom.Next(0, 7); nextNumber = tetraminoRandom.Next(0, 7); score = 0; linesFilled = 0; BlockControls = new PictureBox[cols, rows]; // block trong main grid BlockShape = new PictureBox[COLS, ROWS]; // block tiếp theo for (int h = 0; h < COLS; h++) { for (int k = 0; k < ROWS; k++) { BlockShape[h, k] = new PictureBox { BackgroundImage = NoImage, BackgroundImageLayout = ImageLayout.Stretch, BackColor = Color.Transparent, Width = 27, Height = 27, }; board.GetExtraGrid.Add(BlockShape[h, k], h, k); } } for (int i = 0; i < cols; i++) { for (int j = 0; j < rows; j++) { BlockControls[i, j] = new PictureBox { BackgroundImage = NoImage, BackgroundImageLayout = ImageLayout.Stretch, Width = 27, Height = 27, BackColor = Color.Transparent, }; board.GetMainGrid.Add(BlockControls[i, j], i, j); } } curTetramino = new Tetramino(this.curNumber); nextTetramino = new Tetramino(nextNumber); CurTetraminoDraw(); NextTetraminoDraw(); }
public bool CheckIfAboveGrid(Tetramino tetramino) { foreach (Transform mino in tetramino.transform) { Vector2 pos = mino.position; if (pos.y > gridHeight - 1) { return(true); } } return(false); }
public static Dictionary <TetraminoName, Tetramino> DefineTetraminos() { Dictionary <TetraminoName, Tetramino> tetraminoMap = new Dictionary <TetraminoName, Tetramino>(); bool[,] longyGrid = new bool[1, 4] { { true, true, true, true } }; Tetramino longy = new Tetramino(TetraminoName.LongBoi, longyGrid, Color.red, new Vector2Int(0, 2)); tetraminoMap.Add(longy.name, longy); return(tetraminoMap); }
private static string VectorPosesString(Tetramino.TetraminoType tetraminoType, RotationDirection rotationDirection) { Vector2Int[] tetraminoPoses = TetraminoTransformUtil.GetPoses(tetraminoType, RotationDirection.None); Vector2Int[] rotationVectorPoses = new Vector2Int[4]; Vector2 rotationPoint = new Tetramino(tetraminoType).rotationPoint; for (int i = 0; i < 4; i++) { Vector2 rotated = RotateVector.Rotate(tetraminoPoses[i], rotationPoint, rotationDirection); rotationVectorPoses[i] = VectorUtil.V2_V2Int(rotated); } return(string.Join("\t", rotationVectorPoses)); }
// makes cells occupied by tetramino impassable public void FreezeTetraminoArea(TetraminoMono tetraminoMono) { Tetramino tetramino = tetraminoMono.tetramino; Vector2Int[] absPoses = tetramino.AbsPoses; LoopUtil.LoopAction((i) => { this[absPoses[i]] = new WallBlock(BlockType.Unspecified, tetraminoMono.GetChildGameObject(i)); } , absPoses.Length); DisplayBlocks.UpdateBlocks(); }
public void CurTetraminoMovDown() { Point Position = curTetramino.GetPosition; Point[] Shape = curTetramino.GetShape; bool move = true; CurTetraminoErase(); foreach (Point S in Shape) { if (((int)(S.Y + Position.Y) + 1 + 1) >= rows) { move = false; } else if (BlockControls[((int)(S.X + Position.X) + ((cols / 2) - 1)), (int)(S.Y + Position.Y) + 1 + 1].BackgroundImage != NoImage) { move = false; } } if (move) { curTetramino.MoveDown(); CurTetraminoDraw(); } else { CurTetraminoDraw(); CheckRows(); if ((int)Position.Y <= 1) { gameOver = true; } else { curNumber = nextNumber; nextNumber = tetraminoRandom.Next(0, 7); curTetramino = new Tetramino(curNumber); NextTetraminoErase(); nextTetramino = new Tetramino(nextNumber); NextTetraminoDraw(); } } }
private void TetraminoViewSet(Tetramino tetramino, TileView[,] tetraminoView) { for (int i = 0; i < _size; i++) { for (int j = 0; j < _size; j++) { if (tetramino[j, i, _game.Rotation].State) { int colorIndex = tetramino[j, i, _game.Rotation].Color; tetraminoView[j, i] = GetTile(_colorsArray[colorIndex]); } } } }
private static List <int> RowsToSweepExtended(Tetramino tetramino, out bool sweep) { List <int> rowsToSweep = RowsToSweep(tetramino, out sweep); List <int> rowsToSweepExtended = new List <int>(); foreach (int rowToSweep in rowsToSweep) { rowsToSweepExtended.Add(rowToSweep); } int rowCount = Grid.gridSize.y; rowsToSweepExtended.Add(rowCount + 1); return(rowsToSweepExtended); }
//Checks if blocks are above grid for game over public bool CheckIsAboveGrid(Tetramino tetromino) { for (int x = 0; x < gridWidth; x++) { foreach (Transform mino in tetromino.transform) { Vector2 pos = Round(mino.position); if (pos.y > gridHeight - 1) { return(true); } } } return(false); }
private static List <int> RowsToSweep(Tetramino tetramino, out bool sweep) { List <int> rowsToSweep = new List <int>(); int[] rowsToCheckForSweep = RowsToCheckForSweep(tetramino); foreach (int row in rowsToCheckForSweep) { if (Grid.FullRow(row)) { rowsToSweep.Add(row); } } sweep = rowsToSweep.Count != 0; return(rowsToSweep); }
public bool CheckIsAboveGrid(Tetramino tetramino) { for(int x = 0; x < gridWidth; ++x) { foreach(Transform mino in tetramino.transform) { Vector2 pos = Round(mino.position); if(pos.y > gridHeight -1) { return true; } } } return false; }
private static void ProcessRotation(TetraminoMono tetraminoMono, TetraminoMono projection) { Tetramino tetraminoData = tetraminoMono.tetramino; if (Input.GetKeyDown(KeyCode.W)) { bool collision = Grid.Collision(Grid.Ins, tetraminoData, Vector2Int.zero, Tetramino.RotationType.Clockwise); if (!collision) { tetraminoMono.RotateClockwise(); projection.RotateClockwise(); } } }
// makes cells occupied by tetramino impassable public void FreezeTetraminoArea(TetraminoMono tetraminoMono) { //foreach (Vector2Int pos in ) //{ // blocks[pos.x, pos.y] = new WallBlock(BlockType.Unspecified); //} Tetramino tetramino = tetraminoMono.tetramino; Vector2Int[] absPoses = tetramino.AbsPoses; LoopUtil.LoopAction((i) => { blocks[absPoses[i].x, absPoses[i].y] = new WallBlock(BlockType.Unspecified, tetraminoMono.GetChildGameObject(i)); } , absPoses.Length); DisplayBlocks.UpdateBlocks(); }
//Game loop private async Task CreateNewBlock() { await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { actualTetramino = TetraminoesFactory.CreateRandomTetramino(); //actualTetramino = TetraminoesFactory.CreateTetramino(TetraminoShape.I); CheckForBottomCollision(); if (bottomCollision) { gameOver = true; } else { view.ActualTetramino = actualTetramino; view.DrawTetramino(); } }); }
private void NewTetramino() { tetramino = nextTetramino; nextTetramino = new Tetramino(); for (int i = 0; i < Height; i++) { for (int j = 0; j < BORDERS_COUNT; j++) { float H, S, V; Color.RGBToHSV(tetramino.Color, out H, out S, out V); S = (float)(i + 1) / Height; SpriteRenderer renderer = borders[i, j].GetComponent <SpriteRenderer>(); renderer.color = Color.HSVToRGB(H, S, V); } } scoreTitle.color = tetramino.Color; scoreNumber.color = tetramino.Color; tetraminoTitle.color = nextTetramino.Color; tetraminoImage.color = nextTetramino.Color; tetraminoImage.sprite = Resources.Load <Sprite>("TetraminoImages/tetramino_" + nextTetramino.Id.ToString()); }
public MainWindowController(Board BOARD, MainWindow mainWindow, int level) { this.board = BOARD; mainWindowControl = mainWindow; tetramino = new Tetramino(); te_moving = new TetraminoMoving(tetramino); shapeRandom = new Random(); currentShapeNumber = shapeRandom.Next(1, 8); nextShapeNumber = shapeRandom.Next(1, 8); this.gameSpeed = GAMESPEED; setLevel(level); timer = new DispatcherTimer(); timer.Interval = new TimeSpan(0, 0, 0, 0, gameSpeed); timer.Tick += Timer_Tick; mainWindow.txtfinish.Visibility = Visibility.Collapsed; mainWindow.scoreTxt.Visibility = mainWindow.nextTxt.Visibility = mainWindow.restartStopBtn.Visibility = mainWindow.levelTxt.Visibility = Visibility.Hidden; startGame(mainWindow); }
public static void Sweep(Tetramino tetramino) { bool sweep; List <int> rowsToSweepExtended = RowsToSweepExtended(tetramino, out sweep); if (!sweep) { return; } List <List <int> > splitList = SplitList(rowsToSweepExtended); for (int dropCount = 1; dropCount <= splitList.Count; dropCount++) { int sweepRowIndex = rowsToSweepExtended[dropCount - 1]; Grid.EmptyAndDestroyBricksInRow(sweepRowIndex); foreach (int rowIndex in splitList[dropCount - 1]) { Grid.DropBricksInRow(rowIndex, dropCount); } } }
// Update is called once per frame void Update() { bool update = UpdateTimer(Time.deltaTime); bool instantMove = Input.GetKeyDown(KeyCode.Space); if (update || instantMove) { Grid grid = Grid.Ins; Tetramino tetramino = tetraminoMono.tetramino; Vector2Int offset = Vector2Int.down; bool spawnNewTetramino = false; if (instantMove) { //Vector2Int projectionPos = projection.projection.GetComponent<TetraminoMono>().tetramino.centerPos; //Vector2Int tetraminoPos = tetramino.centerPos; //offset = projectionPos - tetraminoPos; spawnNewTetramino = true; } else { bool collision = Grid.Collision(grid, tetramino, Vector2Int.down, Tetramino.RotationType.None); if (collision) { //Debug.Log("collision"); spawnNewTetramino = true; } else { tetraminoMono.TranslateCenterPosition(offset); } } if (spawnNewTetramino) { Tetramino tetraminoProjectionCopy = projection.DropProjection(); //TODO Check exact spawn time SpawnNewTetramino(); SweepLine.Sweep(tetraminoProjectionCopy); } } }
public static Vector2Int[] GetPoses(Tetramino.TetraminoType tetraminoType, RotationDirection rotationDirection) { Tetramino tetramino = new Tetramino(tetraminoType); switch (rotationDirection) { case RotationDirection.None: break; case RotationDirection.Clockwise: tetramino.RotateClockwise(); break; case RotationDirection.CounterClockwise: tetramino.RotateAntiClockwise(); break; default: throw new UnityException($"RotationDirection type<{rotationDirection}> not supported!"); } return(tetramino.Poses); }
//Game loop private async Task CreateNewBlock() { await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { actualTetramino = TetraminoesFactory.CreateRandomTetramino(); //actualTetramino = TetraminoesFactory.CreateTetramino(TetraminoShape.I); CheckForBottomCollision(); if (bottomCollision) gameOver = true; else { view.ActualTetramino = actualTetramino; view.DrawTetramino(); } }); }