public static void LoadMapObjectsFromTextFile(Map map, string mapTextFilePath, ContentManager content) { Map = map; Content = content; using (var sr = new StreamReader(mapTextFilePath)) { string currentLine = sr.ReadLine(); string command = ""; while (!sr.EndOfStream) { if (Commands.Contains(currentLine)) { command = currentLine; currentLine = sr.ReadLine(); } int positionX = int.Parse(currentLine.Split(' ')[0]); int positionY = int.Parse(currentLine.Split(' ')[1]); Execute(command, positionX, positionY); currentLine = sr.ReadLine(); } } }
public static bool HasCollisionWithObject(Player player, int targetPositionX, int targetPositionY, Map map, Vector2 mapPosition) { Rectangle playerRect = new Rectangle(targetPositionX, targetPositionY, player.Image.Width, player.Image.Height); foreach(var mapElement in map.MapElements) { var mapElementRect = new Rectangle(mapElement.TopLeftX + (int) mapPosition.X, mapElement.TopLeftY + (int) mapPosition.Y, mapElement.Image.Width, mapElement.Image.Height); if (playerRect.Intersects(mapElementRect)) { return true; } } foreach (var mapCreature in map.MapCreatures) { var mapCreatureRect = new Rectangle(mapCreature.TopLeftX + (int)mapPosition.X, mapCreature.TopLeftY + (int)mapPosition.Y, mapCreature.Image.Width, mapCreature.Image.Height); if (playerRect.Intersects(mapCreatureRect)) { return true; } } return false; }
public static Creature GetClosestCreature(Map map, Player player, Vector2 mapPostion) { double closestCreatureDistance = Double.MaxValue; Creature closestCreature = null; foreach (var mapCreature in map.MapCreatures) { double currentDistance = GetDistanceBetweenObjects(player, mapCreature,mapPostion); if (currentDistance < closestCreatureDistance) { closestCreature = mapCreature; closestCreatureDistance = currentDistance; } } return closestCreature; }
public static bool HasCollisionWithEnemy(Player player, Map map, Vector2 mapPosition, out int hashcodeOfCollidedEnemy) { Rectangle playerRect = new Rectangle(player.TopLeftX, player.TopLeftY, player.Image.Width, player.Image.Height); foreach (var creature in map.MapCreatures) { bool intersects = playerRect.Intersects(new Rectangle(creature.TopLeftX + (int)mapPosition.X, creature.TopLeftY + (int)mapPosition.Y, creature.Image.Width, creature.Image.Height)); if (intersects) { hashcodeOfCollidedEnemy = creature.GetHashCode(); return true; } } hashcodeOfCollidedEnemy = 0; return false; }
public static bool HasCollisionWithItem(Player player, Map map, Vector2 mapPosition, out int hashcodeOfCollidedItem) { Rectangle playerRect = new Rectangle(player.TopLeftX, player.TopLeftY, player.Image.Width, player.Image.Height); foreach (var mapItem in map.MapItems) { var mapItemRect = new Rectangle(mapItem.TopLeftX + (int) mapPosition.X, mapItem.TopLeftY + (int) mapPosition.Y, mapItem.Image.Width, mapItem.Image.Height); if (playerRect.Intersects(mapItemRect)) { hashcodeOfCollidedItem = mapItem.GetHashCode(); return true; } } hashcodeOfCollidedItem = 0; return false; }
public static void DrawMap(Map map, Vector2 mapPosition, SpriteBatch spriteBatch, ContentManager content) { spriteBatch.Draw(map.Background, mapPosition); foreach (var mapElement in map.MapElements) { spriteBatch.Draw(mapElement.Image, new Vector2(mapElement.TopLeftX + mapPosition.X, mapElement.TopLeftY + mapPosition.Y)); } foreach (var mapCreature in map.MapCreatures) { if (mapCreature.IsAlive) { spriteBatch.Draw(mapCreature.Image, new Vector2(mapCreature.TopLeftX + mapPosition.X, mapCreature.TopLeftY + mapPosition.Y)); StatBar.DrawEnergyBar(mapCreature, 10, spriteBatch, content, mapPosition); StatBar.DrawFocusBar(mapCreature, 16, spriteBatch, content, mapPosition); } } foreach (var mapItem in map.MapItems) { spriteBatch.Draw(mapItem.Image, new Vector2(mapItem.TopLeftX + mapPosition.X, mapItem.TopLeftY + mapPosition.Y)); } }
protected override void Initialize() { //window settings this.graphics.PreferredBackBufferWidth = MainClass.WindowWidth; this.graphics.PreferredBackBufferHeight = MainClass.WindowHeight; this.Window.Title = MainClass.GameWindowTitle; this.Window.Position = new Point((MainClass.CurrentScreenWidth - MainClass.WindowWidth) / 2, (MainClass.CurrentScreenHeight - MainClass.WindowHeight) / 2); this.IsMouseVisible = true; //this.graphics.ToggleFullScreen(); //this.Window.IsBorderless = true; this.LoadImages(); this.mainMenu = new MainMenu(Content.Load<Texture2D>("GUI/MainMenuTextures/background")); // TODO: Add your initialization logic here this.getItemSound = new Sound(MainClass.GotItem); this.musicTheme = new Sound(MainClass.Music); this.killEnemy = new Sound(MainClass.KillEnemy); this.map = new Map(); this.musicTheme.Play(true); base.Initialize(); }