public void CreateBackgroundGrid(Canvas canvas) { if (canvas.Height == 0) { return; } // calculate size of each tile var tileSize = (canvas.Width - (margin * (size + 1))) / size; // render childrens: for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { double x = i * tileSize + (i + 1) * margin; double y = j * tileSize + (j + 1) * margin; int value = tiles[j, i]; var gameTile = new GameTile(0, x, y, tileSize, j, i); canvas.Children.Add(gameTile.GetElement()); } } }
public void RenderWithAnimation(Game game) { if (game != null) { this.tiles = game.GameBoard; } foreach (var transform in game.LastTransforms) { if (transform.Type == GameLib.GameBoard.TransformType.Tranlate) { var Tile = GetChildren(transform.LastY, transform.LastX); Canvas.SetZIndex(Tile.GetElement(), 1); int offsetX = transform.X - transform.LastX; int offsetY = transform.Y - transform.LastY; Tile.ApplyTransform(offsetX, offsetY); } if (transform.Type == GameLib.GameBoard.TransformType.Merge) { var Tile = GetChildren(transform.LastY, transform.LastX); Canvas.SetZIndex(Tile.GetElement(), 2); int transformX = (transform.X - transform.LastX); int transformY = (transform.Y - transform.LastY); Tile.ApplyTransform(transformX, transformY); Tile.ApplyMerge(game.GameBoard[transform.Y, transform.X]); } else if (transform.Type == GameLib.GameBoard.TransformType.New) { double x = transform.X * tileSize + (transform.X + 1) * margin; double y = transform.Y * tileSize + (transform.Y + 1) * margin; var Tile = new GameTile(game.GameBoard[transform.Y, transform.X], x, y, tileSize, transform.Y, transform.X); ch.Add(Tile); canvas.Children.Add(Tile.GetElement()); Canvas.SetZIndex(Tile.GetElement(), 3); Tile.ApplyScale(); } } }
public void Render(Game game = null) { if (game != null) { this.tiles = game.GameBoard; } // if tiles is empty or canvas is invisible, do not rerender if (tiles == null || canvas.Width == 0) { return; } // get number of tiles in row/col int size = tiles.GetLength(0); // calculate size of each tile tileSize = (canvas.Width - (margin * (size + 1))) / size; // clear canvas ClearChildrens(); // render childrens: for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { double x = i * tileSize + (i + 1) * margin; double y = j * tileSize + (j + 1) * margin; int value = tiles[j, i]; var gameTile = new GameTile(value, x, y, tileSize, j, i); if (value != 0) { ch.Add(gameTile); } canvas.Children.Add(gameTile.GetElement()); } } }