internal bool intersects(Tile t, Game2048 game) { if (position == GridPosition) { if (moveX != 0) { return(t.GridPosition == new Vector2(GridPosition.X - moveX, GridPosition.Y)); } if (moveY != 0) { return(t.GridPosition == new Vector2(GridPosition.X, GridPosition.Y - moveY)); } } else { if ((moveX != 0 && t.GridPosition.Y == GridPosition.Y)) { return(Math.Abs(GetCenter(game).X - t.GetCenter(game).X) <= game.drawTileSize + game.drawMarginSize * 2); } if ((moveY != 0 && t.GridPosition.X == GridPosition.X)) { return(Math.Abs(GetCenter(game).Y - t.GetCenter(game).Y) <= game.drawTileSize + game.drawMarginSize * 2); } } return(false); }
internal bool Move(Game2048 game) { isMoving = false; if (checkMerger(game)) { return(false); } if (moveX == 0 && moveY == 0) { return(false); } Vector2 nextPosition = new Vector2(position.X + moveX, position.Y + moveY); Rectangle nextBox = game.getTileSquare(new Vector2(nextPosition.X, nextPosition.Y)); if (moveX == 0 && moveY == 0) { return(false); } bool inBounds = nextBox.X - game.drawMarginSize >= game.drawPosition.X && nextBox.Y - game.drawMarginSize >= game.drawPosition.Y && nextBox.X + nextBox.Width + game.drawMarginSize <= game.drawBoardSize + game.drawPosition.X && nextBox.Y + nextBox.Height + game.drawMarginSize <= game.drawBoardSize + game.drawPosition.Y; if (!inBounds) { snapToGrid(); return(false); } Vector2 next = new Vector2(GridPosition.X + (moveX < 0 ? -1 : moveX > 0 ? 1 : 0), GridPosition.Y + (moveY < 0 ? -1 : moveY > 0 ? 1 : 0)); Tile collision = game.Tiles.Find(t => t != this && t.value > 0 && (t.value != value || hasmerged || t.hasmerged) && t.position == t.GridPosition && t.GridPosition == next && getDistance(t.GetCenter(game), GetCenter(game)) <= (t.value == value ? getDistance(t.GetGridCenter(game), GetGridCenter(game)) : getDistance(t.GetGridCenter(game), GetGridCenter(game)))); if (collision is Tile) { snapToGrid(); return(false); } isMoving = true; position = nextPosition; return(true); }
internal bool checkMerger(Game2048 game) { if (hasmerged) { return(false); } Tile merge = game.Tiles.Find(t => t != this && t.value == value && getDistance(t.GetCenter(game), GetCenter(game)) < game.drawMarginSize); if (merge is Tile tile && !tile.hasmerged) { value = 0; game.score += tile.nextValue(); tile.hasmerged = true; return(false); } return(false); }
internal Point GetGridCenter(Game2048 game) { return(game.getTileSquare(GridPosition).Center); }