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 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(); } }