public void CheckWallCollisions(Map map, Character c) { Rectangle charRect = new Rectangle((int)c.WorldPosition.X, (int)c.WorldPosition.Y, 32, 32); for (int y = 0; y < map.Height; y++) { for (int x = 0; x < map.Width; x++) { Tile tile = map.getTile(x,y); if (tile.type == 2 && c.Collision.Intersects(tile.GetTileBounds())) { if (c.WorldPosition.X < tile.worldPosition.X + 32) { c.worldPosition.X -= c.velocity.X; c.velocity.X = 0; } if (c.WorldPosition.X + 32 > tile.worldPosition.X) { c.worldPosition.X += c.velocity.X; c.velocity.X = 0; } if (c.WorldPosition.Y < tile.worldPosition.Y + 32) { c.worldPosition.Y -= c.velocity.Y; c.velocity.Y = 0; } if (c.WorldPosition.Y + 32 > tile.worldPosition.Y) { c.worldPosition.Y += c.velocity.Y; c.velocity.Y = 0; } } } } }
public static Map LoadMap(string mapName, int width, int height, ContentManager Content) { // test out load file dialog Tile[,] tiles = new Tile[width,height]; Map tempMap = new Map("", 0, 0); int xCount = 0; int yCount = 0; Tile[,] tempTiles = new Tile[0, 0]; using (XmlReader reader = XmlReader.Create(mapName + ".xml")) { while (reader.Read()) { // only detect start elements if (reader.IsStartElement()) { // get the element name and switch on it switch (reader.Name) { case "Name": reader.Read(); tempMap.name = reader.Value; break; case "height": reader.Read(); tempMap.Height = Int32.Parse(reader.Value); break; case "width": reader.Read(); tempMap.Width = Int32.Parse(reader.Value); tempTiles = new Tile[tempMap.Width, tempMap.Height]; break; case "tile": int type = 0; reader.Read(); reader.Read(); reader.Read(); type = Int32.Parse(reader.Value); Texture2D text = Content.Load<Texture2D>("water"); if (type == 0) text = Content.Load<Texture2D>("water"); if (type == 1) text = Content.Load<Texture2D>("grass"); if (type == 2) text = Content.Load<Texture2D>("rock"); tempTiles[xCount, yCount] = new Tile(type, new Vector2(xCount * 32, yCount * 32), text); xCount++; if (xCount > tempMap.Width - 1) { xCount = 0; yCount++; } break; } } } } //Map map = new Map("My Map", tiles, width, height); tempMap.setTiles(tempTiles); return tempMap; }
public static void SaveMap(string mapName, Map map) { // test out save file dialog // 0 is grass // 1 is rock // 2 is water List<Tile> tiles = new List<Tile>(); for (int y = 0; y < map.Height; y++) { for (int x = 0; x < map.Width; x++) { tiles.Add(map.getTile(x, y)); } } // save map with xml using (StringWriter str = new StringWriter()) using (XmlTextWriter xml = new XmlTextWriter(mapName + ".xml", Encoding.UTF8)) { xml.Formatting = Formatting.Indented; xml.WriteStartDocument(); xml.WriteStartElement("map"); xml.WriteElementString("Name", map.name); xml.WriteStartElement("dimensions"); xml.WriteElementString("height", map.Height.ToString()); xml.WriteElementString("width", map.Width.ToString()); xml.WriteEndElement(); xml.WriteStartElement("tiles"); foreach (Tile tile in tiles) { xml.WriteStartElement("tile"); xml.WriteElementString("type", tile.type.ToString()); xml.WriteEndElement(); } xml.WriteEndElement(); xml.WriteEndElement(); xml.WriteEndDocument(); } }
public static void SaveMap(Map map) { SaveFileDialog dialog = new SaveFileDialog(); dialog.InitialDirectory = "./"; dialog.Filter = "xml files (*.xml)|*.xml|AllFiles (*.*)|*.*"; dialog.FilterIndex = 1; dialog.RestoreDirectory = true; dialog.ShowDialog(); SaveMap(dialog.FileName.Substring(0, dialog.FileName.IndexOf(".")), map); }
/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { currKeyState = Keyboard.GetState(); currMouseState = Mouse.GetState(); // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || (currKeyState.IsKeyUp(Keys.Escape) && prevKeyState.IsKeyDown(Keys.Escape) )) this.Exit(); c.Update(); foreach (NPC npc in npcs) { npc.Update(rand.Next(16, 600), rand.Next(16, 600)); } cam.Update(c.worldPosition); if (currKeyState.IsKeyUp(Keys.F5) && prevKeyState.IsKeyDown(Keys.F5)) mapMode = !mapMode; MagicManager.Update(); colMan.CheckMagicCollisions(npcs); colMan.CheckWallCollisions(map, c); Rectangle cRect = new Rectangle((int)c.WorldPosition.X, (int)c.WorldPosition.Y, 32, 32); foreach (Event evnt in map.Events) { Rectangle eventRect = new Rectangle((int)evnt.Position.X, (int)evnt.Position.Y, 32, 32); if (cRect.Intersects(eventRect)) { map = FileManager.LoadMap(evnt.Value, mapWidth, mapHeight, Content); events = new List<Event>(); events.Add(new Event(new Vector2(352, 256), "building1")); events.Add(new Event(new Vector2(384, 256), "building1")); events.Add(new Event(new Vector2(416, 256), "building1")); map.Events = events; c.WorldPosition = new Vector2(160, 160); } } if (mapMode) { if (currKeyState.IsKeyUp(Keys.D1) && prevKeyState.IsKeyDown(Keys.D1)) selectedTile = 0; if (currKeyState.IsKeyUp(Keys.D2) && prevKeyState.IsKeyDown(Keys.D2)) selectedTile = 1; if (currKeyState.IsKeyUp(Keys.D3) && prevKeyState.IsKeyDown(Keys.D3)) selectedTile = 2; if (currKeyState.IsKeyDown(Keys.LeftControl) && currKeyState.IsKeyUp(Keys.S) && prevKeyState.IsKeyDown(Keys.S)) { FileManager.SaveMap(map); } if (currKeyState.IsKeyDown(Keys.LeftControl) && currKeyState.IsKeyUp(Keys.L) && prevKeyState.IsKeyDown(Keys.L)) { map = FileManager.LoadMap(Content); events = new List<Event>(); events.Add(new Event(new Vector2(352, 256), "building1")); events.Add(new Event(new Vector2(384, 256), "building1")); events.Add(new Event(new Vector2(416, 256), "building1")); map.Events = events; } Rectangle mouseRect = new Rectangle(currMouseState.X + (int)cam.position.X, currMouseState.Y + (int)cam.position.Y, 8, 8); for (int x = 0; x < map.Width; x++) { for (int y = 0; y < map.Height; y++) { if (mouseRect.Intersects(map.getTile(x, y).GetTileBounds())) { if (currMouseState.RightButton == ButtonState.Pressed) { Tile tile = new Tile(); tile.sprite = tilePalette[selectedTile]; tile.worldPosition = new Vector2(x * 32, y * 32); tile.type = selectedTile; map.SetTile(tile, x, y); } } } } } if (currKeyState.IsKeyUp(Keys.F1) && prevKeyState.IsKeyDown(Keys.F1)) { graphics.ToggleFullScreen(); if (graphics.IsFullScreen) { graphics.PreferredBackBufferWidth = 1920; graphics.PreferredBackBufferHeight = 1080; cam.height = 1080; cam.width = 1920; } else { graphics.PreferredBackBufferWidth = 800; graphics.PreferredBackBufferHeight = 480; cam.width = 800; cam.height = 480; } c.position = new Vector2((graphics.PreferredBackBufferWidth / 2) - 32, (graphics.PreferredBackBufferHeight / 2) - 32); cam.camOffset = c.position; graphics.ApplyChanges(); } prevKeyState = currKeyState; prevMouseState = currMouseState; base.Update(gameTime); }
/// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); colMan = new CollisionManager(); currKeyState = Keyboard.GetState(); prevKeyState = currKeyState; currMouseState = Mouse.GetState(); prevMouseState = currMouseState; font = Content.Load<SpriteFont>("font"); graphics.PreferredBackBufferWidth = 800; graphics.PreferredBackBufferHeight = 480; graphics.ApplyChanges(); c = new Character(new Vector2(0, 0), new Vector2(520,520), Content.Load<Texture2D>(@"hero"), Content.Load<Texture2D>("testProjectile")); c.position = new Vector2((graphics.PreferredBackBufferWidth / 2), (graphics.PreferredBackBufferHeight / 2)); cam = new Camera(c.worldPosition); cam.camOffset = c.position; cam.width = 800; cam.height = 480; map = FileManager.LoadMap("overworld", mapWidth, mapHeight, Content); /* string mapName = "temp"; int width = 10; int height = 10; Tile[,] tiles = new Tile[width, height]; Random rand = new Random(); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { int num = rand.Next(0, 3); Tile t = new Tile(); t.worldPosition = new Vector2(x * 32, y * 32); if (num == 0) { t.type = 0; t.sprite = Content.Load<Texture2D>("water"); } if (num == 1) { t.type = 1; t.sprite = Content.Load<Texture2D>("grass"); } if (num == 2) { t.type = 2; t.sprite = Content.Load<Texture2D>("rock"); } tiles[x, y] = t; } } map = new Map(mapName, tiles, width, height); */ map.Events = events; npcs = new List<NPC>(); cam = new Camera(c.worldPosition); List<Tile> paletteTiles = new List<Tile>(); Tile grassTile = new Tile(0, new Vector2(0, 0), Content.Load<Texture2D>("grass")); Tile waterTile = new Tile(0, new Vector2(0, 32), Content.Load<Texture2D>("water")); Tile rockTile = new Tile(0, new Vector2(0, 64), Content.Load<Texture2D>("rock")); tilePalette = new List<Texture2D>(); tilePalette.Add(Content.Load<Texture2D>("water")); tilePalette.Add(Content.Load<Texture2D>("grass")); tilePalette.Add(Content.Load<Texture2D>("rock")); tile = Content.Load<Texture2D>("selectedTile"); mapMode = false; selectedTile = 0; }