private void MovePlayer() { Player.Move(Player.Position.X - NextMove.X, Player.Position.Y - NextMove.Y); if (Player.Position.X < 0 || Player.Position.X >= Table.Columns || Player.Position.Y < 0 || Player.Position.Y >= Table.Rows || (Table[Player.Position.Y, Player.Position.X] != (double)Tile.EMPTY && Table[Player.Position.Y, Player.Position.X] != (double)Tile.WORM)) { GameOver(); return; } if (Table[Player.Position.Y, Player.Position.X] == (double)Tile.WORM) { Score += 100; Player.Size++; SpawnWorm(); } DrawPlayer(); LastMove = NextMove; }
//true if moved full distance, false if not public static bool MoveWithCollisionCheck(Vector2 offset, Player player, Map map) { var tempPlayer = (Player)player.Clone(); tempPlayer.X += offset.X; tempPlayer.Y += offset.Y; var tempPlayerRect = new Rectangle( Convert.ToInt32(tempPlayer.X), Convert.ToInt32(tempPlayer.Y), Convert.ToInt32(Player.Width), Convert.ToInt32(Player.Height)); for (int i = 0; i < map.MapData.Count; i++) { for (int j = 0; j < map.MapData[i].Count; j++) { var rect = map.MapData[i][j].GetCollisionRectangle(j * Map.TileSize, i * Map.TileSize); if (rect.Width == 0 || rect.Height == 0) { continue; } if (tempPlayerRect.Intersects(rect)) { tempPlayerRect.X -= Convert.ToInt16(offset.X); tempPlayerRect.Y -= Convert.ToInt16(offset.Y); if (offset.Y > 0) { for (int k = 1; k <= Convert.ToInt16(offset.Y); k++) { var tempTempPlayerRect = tempPlayerRect; var tempOffset = Convert.ToInt16(offset.Y) - k; tempTempPlayerRect.Y += tempOffset; if (tempTempPlayerRect.Intersects(rect)) { continue; } break; } if (offset.Y < 0) { offset.Y = 0; } } else if (offset.Y < 0) { for (int k = 1; k >= Convert.ToInt16(Math.Abs(offset.Y)); k++) { var tempTempPlayerRect = tempPlayerRect; var tempOffset = Convert.ToInt16(offset.Y) + k; tempTempPlayerRect.Y += tempOffset; if (tempPlayerRect.Intersects(rect)) { continue; } break; } if (offset.Y > 0) { offset.Y = 0; } } if (offset.X > 0) { for (int k = 1; k <= Convert.ToInt16(offset.X); k++) { var tempOffset = Convert.ToInt16(offset.X) - k; tempPlayerRect.X += tempOffset; if (tempPlayerRect.Intersects(rect)) { tempPlayerRect.X -= tempOffset; continue; } break; } if (offset.X < 0) { offset.X = 0; } } else if (offset.X < 0) { for (int k = -1; k >= Convert.ToInt16(offset.X); k--) { var tempOffset = Convert.ToInt16(offset.X) - k; tempPlayerRect.X += tempOffset; if (tempPlayerRect.Intersects(rect)) { tempPlayerRect.X -= tempOffset; continue; } break; } if (offset.X > 0) { offset.X = 0; } } map.MapData[i][j].OnTouch(player); player.Move(offset); return(false); } } } if (tempPlayer.Y + Player.Height > Map.TileSize * (int)map.MapSize) { return(false); } if (tempPlayer.Y < 0) { return(false); } if (tempPlayer.X + Player.Width > Map.TileSize * (int)map.MapSize) { return(false); } if (tempPlayer.X < 0) { return(false); } player.Move(offset); return(true); }