/// <summary> /// Changes the given tiles to the specified tile and updates the UI bitmap to show the updated tiles. /// </summary> public void SetTile(IEnumerable <TileStatus> tiles, UITile newTile) { var dirtyRects = new ConcurrentBag <Int32Rect>(); _vm.TileBitmap.Lock(); var ptr = _vm.TileBitmap.BackBuffer; var stride = _vm.TileBitmap.BackBufferStride; Parallel.ForEach(tiles, tile => { tile.Tile = newTile; var rect = BitmapHelper.OverwriteTile(ptr, stride, tile.Tile, tile.X, tile.Y, TileHelper.UI_MAP_TILE_PIXELSIZE); dirtyRects.Add(rect); if (tile.Selected) { Select(tile, ptr, stride); } }); foreach (var rect in dirtyRects) { _vm.TileBitmap.AddDirtyRect(rect); } _vm.TileBitmap.Unlock(); }
void SendInput() { List <List <Tuple <String, int, int> > > newDataToBeSent = new List <List <Tuple <String, int, int> > >(); for (int phase = 1; phase <= 3; phase++) { List <Tuple <String, int, int> > phaseT = new List <Tuple <String, int, int> >(); for (int i = 0; i < x; i++) { for (int j = 0; j < z; j++) { if (moveArray[i, j] != null && moveArray[i, j].Count > 0) { String curPuid = pieceArray[i, j].GetComponent <UIPiece>().puid; UITile t = moveArray[i, j].Dequeue().GetComponentInChildren <UITile>(); t.KillGreen(i, j); phaseT.Add(new Tuple <string, int, int>(curPuid, t.coordx, t.coordz)); pieceArray[i, j].GetComponent <UIPiece>().HideHPBar(); } } } newDataToBeSent.Add(phaseT); } moveArray = new Queue <GameObject> [x, z]; //test NewTestProcessInput(newDataToBeSent); }
public UITile[,] GetMapTiles() { var map = new UITile[_tiles.GetLength(0), _tiles.GetLength(1)]; foreach (var tileStatus in _tiles) { map[tileStatus.X, tileStatus.Y] = tileStatus.Tile; } return(map); }
public static string GetTileName(UITile tile) { switch (tile) { case UITile.Floor: return("Floor"); case UITile.Wall: return("Wall"); case UITile.Unknown: return("Unknown Tile"); case UITile.StairSingle_North: case UITile.StairSingle_South: case UITile.StairSingle_East: case UITile.StairSingle_West: return("Stair"); case UITile.StairPartOne_North: case UITile.StairPartOne_South: case UITile.StairPartOne_East: case UITile.StairPartOne_West: return("Stair, Part 1"); case UITile.StairPartTwo_North: case UITile.StairPartTwo_South: case UITile.StairPartTwo_East: case UITile.StairPartTwo_West: return("Stair, Part 2"); case UITile.DoorClosed_NorthSouth: case UITile.DoorClosed_EastWest: return("Door, Closed"); case UITile.DoorOpen_NorthSouth: case UITile.DoorOpen_EastWest: return("Door, Open"); case UITile.DoorSecret: return("Door, Secret"); default: Debug.WriteLine($"Tile doesn't have a specified name. Enum value was {tile}"); return("Missing name"); } }
/// <summary> /// Converts from a map of Tile-enums to a map of UITile-enums, ensuring that the orientation of each tile is correct based on adjacent tiles. /// </summary> /// <param name="map">The map to convert</param> /// <returns>A map of UITiles with correct orientation of all orientation-sensitive tiles</returns> public static UITile[,] Convert(Tile[,] map) { var UIMap = new UITile[map.GetLength(0), map.GetLength(1)]; for (int x = 0; x < map.GetLength(0); x++) { for (int y = 0; y < map.GetLength(1); y++) { switch (map[x, y]) { case Tile.Unassigned: case Tile.Wall: UIMap[x, y] = UITile.Wall; break; case Tile.Room: case Tile.Hallway: UIMap[x, y] = UITile.Floor; break; case Tile.StairUp: if (IsInRange(x - 1, y) && IsFloorTile(map[x - 1, y])) { UIMap[x, y] = UITile.StairSingle_East; } else if (IsInRange(x + 1, y) && IsFloorTile(map[x + 1, y])) { UIMap[x, y] = UITile.StairSingle_West; } else if (IsInRange(x, y + 1) && IsFloorTile(map[x, y + 1])) { UIMap[x, y] = UITile.StairSingle_North; } else if (IsInRange(x, y - 1) && IsFloorTile(map[x, y - 1])) { UIMap[x, y] = UITile.StairSingle_South; } else { UIMap[x, y] = UITile.StairSingle_North; } break; case Tile.StairDown: if (IsInRange(x - 1, y) && IsFloorTile(map[x - 1, y])) { UIMap[x, y] = UITile.StairSingle_West; } else if (IsInRange(x + 1, y) && IsFloorTile(map[x + 1, y])) { UIMap[x, y] = UITile.StairSingle_East; } else if (IsInRange(x, y + 1) && IsFloorTile(map[x, y + 1])) { UIMap[x, y] = UITile.StairSingle_South; } else if (IsInRange(x, y - 1) && IsFloorTile(map[x, y - 1])) { UIMap[x, y] = UITile.StairSingle_North; } else { UIMap[x, y] = UITile.StairSingle_North; } break; case Tile.StairPartOne: if (IsInRange(x - 1, y) && map[x - 1, y] == Tile.StairPartTwo) { UIMap[x, y] = UITile.StairPartOne_East; } else if (IsInRange(x + 1, y) && map[x + 1, y] == Tile.StairPartTwo) { UIMap[x, y] = UITile.StairPartOne_West; } else if (IsInRange(x, y + 1) && map[x, y + 1] == Tile.StairPartTwo) { UIMap[x, y] = UITile.StairPartOne_North; } else if (IsInRange(x, y - 1) && map[x, y - 1] == Tile.StairPartTwo) { UIMap[x, y] = UITile.StairPartOne_South; } else { UIMap[x, y] = UITile.StairPartOne_North; } break; case Tile.StairPartTwo: if (IsInRange(x - 1, y) && map[x - 1, y] == Tile.StairPartOne) { UIMap[x, y] = UITile.StairPartTwo_West; } else if (IsInRange(x + 1, y) && map[x + 1, y] == Tile.StairPartOne) { UIMap[x, y] = UITile.StairPartTwo_East; } else if (IsInRange(x, y + 1) && map[x, y + 1] == Tile.StairPartOne) { UIMap[x, y] = UITile.StairPartTwo_South; } else if (IsInRange(x, y - 1) && map[x, y - 1] == Tile.StairPartOne) { UIMap[x, y] = UITile.StairPartTwo_North; } else { UIMap[x, y] = UITile.StairPartTwo_North; } break; case Tile.DoorClosed: if ((IsInRange(x - 1, y) && IsFloorTile(map[x - 1, y])) || (IsInRange(x + 1, y) && IsFloorTile(map[x + 1, y]))) { UIMap[x, y] = UITile.DoorClosed_EastWest; } else if ((IsInRange(x, y + 1) && IsFloorTile(map[x, y + 1])) || (IsInRange(x, y - 1) && IsFloorTile(map[x, y - 1]))) { UIMap[x, y] = UITile.DoorClosed_NorthSouth; } else { UIMap[x, y] = UITile.DoorClosed_NorthSouth; } break; case Tile.DoorOpen: if ((IsInRange(x - 1, y) && IsFloorTile(map[x - 1, y])) || (IsInRange(x + 1, y) && IsFloorTile(map[x + 1, y]))) { UIMap[x, y] = UITile.DoorOpen_EastWest; } else if ((IsInRange(x, y + 1) && IsFloorTile(map[x, y + 1])) || (IsInRange(x, y - 1) && IsFloorTile(map[x, y - 1]))) { UIMap[x, y] = UITile.DoorOpen_NorthSouth; } else { UIMap[x, y] = UITile.DoorOpen_NorthSouth; } break; case Tile.DoorSecret: UIMap[x, y] = UITile.DoorSecret; break; case Tile.Unknown: UIMap[x, y] = UITile.Unknown; break; default: Debug.WriteLine($"Tile-enum missing from Tile to UITile converter. Tile was: {map[x, y]}"); UIMap[x, y] = UITile.Unknown; break; } } } bool IsInRange(int x, int y) { return(x >= 0 && y >= 0 && x < map.GetLength(0) && y < map.GetLength(1)); } bool IsFloorTile(Tile tile) { return(tile == Tile.Room || tile == Tile.Hallway); } return(UIMap); }
public static BitmapSource GetTileImage(UITile tile) { switch (tile) { case UITile.Unknown: return(TileUnknown); case UITile.Wall: return(TileWall); case UITile.Floor: return(TileFloor); case UITile.StairSingle_North: return(TileStairSingle_North); case UITile.StairSingle_South: return(TileStairSingle_South); case UITile.StairSingle_East: return(TileStairSingle_East); case UITile.StairSingle_West: return(TileStairSingle_West); case UITile.StairPartOne_North: return(TileStairPartOne_North); case UITile.StairPartOne_South: return(TileStairPartOne_South); case UITile.StairPartOne_East: return(TileStairPartOne_East); case UITile.StairPartOne_West: return(TileStairPartOne_West); case UITile.StairPartTwo_North: return(TileStairPartTwo_North); case UITile.StairPartTwo_South: return(TileStairPartTwo_South); case UITile.StairPartTwo_East: return(TileStairPartTwo_East); case UITile.StairPartTwo_West: return(TileStairPartTwo_East); case UITile.DoorClosed_NorthSouth: return(TileDoorClosed_NorthSouth); case UITile.DoorClosed_EastWest: return(TileDoorClosed_EastWest); case UITile.DoorSecret: return(TileDoorSecret); case UITile.DoorOpen_NorthSouth: return(TileDoorOpen_NorthSouth); case UITile.DoorOpen_EastWest: return(TileDoorOpen_EastWest); default: Debug.WriteLine($"Tile doesn't have a specified image. Enum value was {tile}"); return(TileUnknown); } }
/// <summary> /// Get the byte array that makes up the given tile, at the given tilesize. /// The returned byte-array may NOT be modified due to cached return values. /// /// If you want a modifiable byte-array use <see cref="GetTileImage"/>, scale the image and then call <see cref="CopyPixels"/> /// </summary> /// <param name="tile">The tile to get pixels for</param> /// <param name="tileSize">The desired width and height of the tile</param> /// <returns> /// A tuple with the following values. /// Item 1: A byte-array containing the pixels of the image representing the tile. Do not modify. /// Item 2: The pixelwidth of the image. /// Item 3: The pixelheight of the image. /// </returns> public static ImageData <byte[]> GetTilePixels(UITile tile, int tileSize) { //If we need to scale to the desired tile size, we have to scale the tile and grab the pixels. if (tileSize != UI_MAP_TILE_PIXELSIZE) { BitmapSource tileImage = GetTileImage(tile); tileImage = new TransformedBitmap(tileImage, new ScaleTransform(tileSize / (double)tileImage.PixelWidth, tileSize / (double)tileImage.PixelHeight)); return(new ImageData <byte[]> { Data = CopyPixels(tileImage), Width = tileImage.PixelWidth, Height = tileImage.PixelHeight }); } //Otherwise use the cached, precomputed values. switch (tile) { case UITile.Wall: return(TileWallPixels); case UITile.Floor: return(TileFloorPixels); case UITile.StairSingle_North: return(TileStairSingle_NorthPixels); case UITile.StairSingle_South: return(TileStairSingle_SouthPixels); case UITile.StairSingle_East: return(TileStairSingle_EastPixels); case UITile.StairSingle_West: return(TileStairSingle_WestPixels); case UITile.StairPartOne_North: return(TileStairPartOne_NorthPixels); case UITile.StairPartOne_South: return(TileStairPartOne_SouthPixels); case UITile.StairPartOne_East: return(TileStairPartOne_EastPixels); case UITile.StairPartOne_West: return(TileStairPartOne_WestPixels); case UITile.StairPartTwo_North: return(TileStairPartTwo_NorthPixels); case UITile.StairPartTwo_South: return(TileStairPartTwo_SouthPixels); case UITile.StairPartTwo_East: return(TileStairPartTwo_EastPixels); case UITile.StairPartTwo_West: return(TileStairPartTwo_WestPixels); case UITile.DoorClosed_NorthSouth: return(TileDoorClosed_NorthSouthPixels); case UITile.DoorClosed_EastWest: return(TileDoorClosed_EastWestPixels); case UITile.DoorSecret: return(TileDoorSecretPixels); case UITile.Unknown: return(TileUnknownPixels); case UITile.DoorOpen_NorthSouth: return(TileDoorOpen_NorthSouthPixels); case UITile.DoorOpen_EastWest: return(TileDoorOpen_EastWestPixels); default: return(TileUnknownPixels); } }
/// <summary> /// Overlay the tile-image on the given image, at position x, y. /// </summary> /// <param name="backBufferPtr">A pointer to the backbuffer of the WriteableBitmap. See <see cref="WriteableBitmap.BackBuffer"/></param> /// <param name="backBufferStride">The stride of the backbuffer of the WriteableBitmap. See <see cref="WriteableBitmap.BackBufferStride"/></param> /// <param name="tile">The tile to place on the image</param> /// <param name="x">The x-coordinate of the tile. Zero-indexed</param> /// <param name="y">The y-coordinate of the tile. Zero-indexed</param> /// <param name="tileSize">The number of pixels that the side of one tile is. MUST be identical to the tilesize used to create the image.</param> /// <returns>A rectangle specifing the dirty rectangle that has to be updated.</returns> public static unsafe Int32Rect OverwriteTile(IntPtr backBufferPtr, int backBufferStride, UITile tile, int x, int y, int tileSize) { var imageData = TileHelper.GetTileColours(tile, tileSize); var tileColours = imageData.Data; var pixelWidth = imageData.Width; var pixelHeight = imageData.Height; for (int j = 0; j < pixelHeight; j++) { // The location of a pixel in the backbuffer is: bmp.BackBuffer + (bmp.BackBufferStride * Y) + X * (bmp.Format.BitsPerPixel / 8) var ptr = (TileHelper.Colour *)(backBufferPtr + (y * tileSize + j) * backBufferStride + x * tileSize * 4); var offset = j * pixelWidth; for (int i = 0; i < pixelWidth; i++, ptr++, offset++) { *ptr = tileColours[offset]; } } return(new Int32Rect(x * tileSize, y * tileSize, tileSize, tileSize)); }