public void InitMaze() { Vector2[] ppLocations = new Vector2[] { new Vector2(_TileSize, 64), new Vector2(17 * _TileSize, 64), new Vector2(_TileSize, 15 * _TileSize), new Vector2(17 * _TileSize, 15 * _TileSize) }; if (_PowerPellets.Count < 4) { for (int i = 0; i < 4; i++) { PowerPellet p = new PowerPellet(Convert.ToInt32(ppLocations[i].X), Convert.ToInt32(ppLocations[i].Y)); p.IsActive = true; _PowerPellets.Add(p); } } else { foreach (PowerPellet p in _PowerPellets) { p.IsActive = true; } } _Tiles = new int[, ] { { 23, 17, 17, 17, 17, 17, 17, 17, 17, 28, 17, 17, 17, 17, 17, 17, 17, 17, 20 }, //1 { 16, 00, 00, 00, 00, 00, 00, 00, 00, 01, 00, 00, 00, 00, 00, 00, 00, 00, 18 }, //2 { 16, 33, 14, 15, 00, 14, 19, 15, 00, 01, 00, 14, 19, 15, 00, 14, 15, 33, 18 }, //3 { 16, 00, 13, 12, 00, 13, 17, 12, 00, 08, 00, 13, 17, 12, 00, 13, 12, 00, 18 }, //4 { 16, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 18 }, //5 { 16, 00, 09, 11, 00, 10, 00, 09, 02, 04, 02, 11, 00, 10, 00, 09, 11, 00, 18 }, //6 { 16, 00, 00, 00, 00, 01, 00, 00, 00, 01, 00, 00, 00, 01, 00, 00, 00, 00, 18 }, //7 { 22, 19, 19, 15, 00, 07, 02, 11, 00, 08, 00, 09, 02, 05, 00, 14, 19, 19, 21 }, //8 { 32, 32, 32, 16, 00, 01, 32, 32, 32, 32, 32, 32, 32, 01, 00, 18, 32, 32, 32 }, //9 { 17, 17, 17, 12, 00, 08, 32, 14, 19, 19, 19, 15, 32, 08, 00, 13, 17, 17, 17 }, //10 { 32, 32, 32, 32, 00, 32, 32, 18, 32, 32, 32, 16, 32, 32, 00, 32, 32, 32, 32 }, //11 { 19, 19, 19, 15, 00, 10, 32, 13, 17, 17, 17, 12, 32, 10, 00, 14, 19, 19, 19 }, //12 { 32, 32, 32, 16, 00, 01, 32, 32, 32, 32, 32, 32, 32, 01, 00, 18, 32, 32, 32 }, //13 { 23, 17, 17, 12, 00, 08, 32, 09, 02, 04, 02, 11, 32, 08, 00, 13, 17, 17, 20 }, //14 { 16, 00, 00, 00, 00, 00, 00, 00, 00, 01, 00, 00, 00, 00, 00, 00, 00, 00, 18 }, //15 { 16, 33, 09, 25, 00, 09, 02, 11, 00, 08, 00, 09, 02, 11, 00, 24, 11, 33, 18 }, //16 { 16, 00, 00, 01, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 01, 00, 00, 18 }, //17 { 31, 11, 00, 08, 00, 10, 00, 09, 02, 04, 02, 11, 00, 10, 00, 08, 00, 09, 29 }, //18 { 16, 00, 00, 00, 00, 01, 00, 00, 00, 01, 19, 00, 00, 01, 00, 00, 00, 00, 18 }, //19 { 16, 00, 09, 02, 02, 06, 02, 11, 00, 08, 19, 09, 02, 06, 02, 02, 11, 00, 18 }, //20 { 16, 00, 00, 00, 00, 00, 00, 00, 00, 19, 19, 00, 00, 00, 00, 00, 00, 00, 18 }, //21 { 22, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 21 } }; //22 }
public bool CanMoveThere(float x, float y, Direction prevDirection, Direction direction, float speed, SpriteType spriteType) { float x1 = x; float y1 = y; //Adjust for sprite width and height if (direction == Direction.Right) { x1 += _TileSize; y1 += _TileSize / 2; //if (y % _TileSize == 0) //{ // canMove = true; //} } if (direction == Direction.Down) { y1 += _TileSize; x1 += _TileSize / 2; //if (x % _TileSize == 0) //{ // canMove = true; //} } if (direction == Direction.Left) { y1 += _TileSize / 2; //if (y % _TileSize == 0) //{ // canMove = true; //} } if (direction == Direction.Up) { x1 += _TileSize / 2; //if (x % _TileSize == 0) //{ // canMove = true; //} } int row = Convert.ToInt32(Math.Floor(y1 / _TileSize)); int col = Convert.ToInt32(Math.Floor(x1 / _TileSize)); if ((prevDirection == Direction.Down || prevDirection == Direction.Up) && (direction == Direction.Left || direction == Direction.Right)) { bool isValidMove = false; for (int i = Convert.ToInt32(-speed); i <= Convert.ToInt32(speed); i++) { float testY = y + i; if (testY % _TileSize == 0) { isValidMove = true; break; } } if (!isValidMove) { return(isValidMove); } } if ((prevDirection == Direction.Left || prevDirection == Direction.Right) && (direction == Direction.Up || direction == Direction.Down)) { bool isValidMove = false; for (int i = Convert.ToInt32(-speed); i <= Convert.ToInt32(speed); i++) { float testX = x + i; if (testX % _TileSize == 0) { isValidMove = true; break; } } if (!isValidMove) { return(isValidMove); } } if (row < 0) { row = 0; } if (col < 0) { col = 0; } //If it is a blank space int tileValue; if (row < _Tiles.GetLength(0) && col < _Tiles.GetLength(1)) { tileValue = _Tiles[row, col]; //Blank tile or pellet tile if ((tileValue == PELLET_TILE_VALUE || tileValue == EMPTY_PELLETE_TILE_VALUE || tileValue == POWER_PELLET_TILE_VALUE)) { //Eat the pellet if (spriteType == SpriteType.PacMan && (tileValue == PELLET_TILE_VALUE || tileValue == POWER_PELLET_TILE_VALUE)) { for (int i = -6; i < 6; i++) { for (int j = -6; j < 6; j++) { int testX = (col * _TileSize) + j; int testY = (row * _TileSize) + i; if (x == testX && y == testY) { //Deactivate power pellets if (tileValue == POWER_PELLET_TILE_VALUE) { PowerPellet p = GetPowerPellet(col, row); if (p.IsActive) { p.IsActive = false; if (OnAtePowerPellet != null) { OnAtePowerPellet(this, EventArgs.Empty); } } } else if (tileValue == PELLET_TILE_VALUE) { if (OnAtePellet != null) { OnAtePellet(this, EventArgs.Empty); } } //Change the tile _Tiles[row, col] = 32; break; } } } } return(true); } }//Going through the right exit else if (col >= _Tiles.GetLength(1)) { return(true); } return(false); }