public static Level loadLevelXML(String fileName) { FileStream fileStream = new FileStream(fileName, FileMode.Open); XmlSerializer xml = new XmlSerializer(typeof(LevelXML)); LevelXML currentXML = (LevelXML)xml.Deserialize(fileStream); Level newLevel = new Level(currentXML.sizeX, currentXML.sizeY, currentXML.Name); newLevel.levelGrid = new Grid(currentXML.sizeX, currentXML.sizeY); newLevel.LevelName = currentXML.Name; foreach (RowXML row in currentXML.rows) { foreach (ColumnXML column in row.columns) { newLevel.levelGrid.GridSpaces[row.index, column.index] = new GridSpace(column.type, row.index, column.index, column.elevation, newLevel.levelGrid); newLevel.levelGrid.setMoveCost(row.index, column.index, row.index, column.index - 1, column.costNorth); newLevel.levelGrid.setMoveCost(row.index, column.index, row.index + 1, column.index + (row.index % 2 == 0 ? -1 : 0), column.costNorthEast); newLevel.levelGrid.setMoveCost(row.index, column.index, row.index + 1, column.index + (row.index % 2 == 0 ? 0 : 1), column.costSouthEast); newLevel.levelGrid.setMoveCost(row.index, column.index, row.index, column.index + 1, column.costSouth); newLevel.levelGrid.setMoveCost(row.index, column.index, row.index - 1, column.index + (row.index % 2 == 0 ? 0 : 1), column.costSouthWest); newLevel.levelGrid.setMoveCost(row.index, column.index, row.index - 1, column.index + (row.index % 2 == 0 ? -1 : 0), column.costNorthWest); if (column.hero != null) { HeroXML h = column.hero; newLevel.levelGrid.GridSpaces[row.index, column.index].tryPutActor(new Hero(h.currentHP, h.maxHP, h.currentEnergy, h.maxEnergy, h.moveSpeed, h.level, h.currentXP, h.name, new Vector2(row.index, column.index))); } else if (column.person != null) { PersonXML p = column.person; newLevel.levelGrid.GridSpaces[row.index, column.index].tryPutActor(new Person(p.currentHP, p.maxHP, p.currentEnergy, p.maxEnergy, p.moveSpeed, p.name, new Vector2(row.index, column.index))); } else if (column.actor != null) { ActorXML a = column.actor; newLevel.levelGrid.GridSpaces[row.index, column.index].tryPutActor(new Actor()); } } } for (int i = 0; i < currentXML.sizeX; ++i) { for (int j = 0; j < currentXML.sizeY; ++j) { if (newLevel.levelGrid.GridSpaces[i, j] == null) { newLevel.levelGrid.GridSpaces[i, j] = new GridSpace(0, i, j, 0, newLevel.levelGrid); } } } fileStream.Close(); return(newLevel); }
/// <summary> /// Saves the file in XML format to the given Stream. /// </summary> /// <param name="saveFile">Stream to pass in, usually from a SaveFileDialog, to which to save the file.</param> public void saveLevelXML(Stream saveFile) { FileStream fileStream = (FileStream)saveFile;//new FileStream(filepath, FileMode.OpenOrCreate); XmlSerializer xml = new XmlSerializer(typeof(LevelXML)); LevelXML levelXML = new LevelXML(); levelXML.Name = LevelName; levelXML.sizeX = simpleLevelGrid.SizeX; levelXML.sizeY = simpleLevelGrid.SizeY; for (int i = 0; i < levelXML.sizeX; ++i) { RowXML newRow = new RowXML(); newRow.index = i; for (int j = 0; j < levelXML.sizeY; ++j) { ColumnXML newCol = new ColumnXML(); newCol.index = j; newCol.type = simpleLevelGrid.GridSpaces[i, j].SpaceType; newCol.elevation = simpleLevelGrid.GridSpaces[i,j].Elevation; if (simpleLevelGrid.GridSpaces[i, j].IsOccupied) { Actor temp = simpleLevelGrid.GridSpaces[i,j].CurrentActor; if (temp is Hero) { Hero tempH = (Hero) temp; newCol.hero = new HeroXML(); newCol.hero.name = tempH.Name; newCol.hero.moveSpeed = tempH.MoveSpeed; newCol.hero.currentEnergy = tempH.Energy; newCol.hero.currentHP = tempH.Health; newCol.hero.maxEnergy = tempH.MaxEnergy; newCol.hero.maxHP = tempH.MaximumHealth; newCol.hero.team = tempH.TeamIndex; newCol.hero.classNum = tempH.ClassIndex; newCol.hero.level = tempH.Level; } else if (temp is Person) { Person tempP = (Person)temp; newCol.person = new PersonXML(); newCol.person.name = tempP.Name; newCol.person.moveSpeed = tempP.MoveSpeed; newCol.person.currentEnergy = tempP.Energy; newCol.person.currentHP = tempP.Health; newCol.person.maxEnergy = tempP.MaxEnergy; newCol.person.maxHP = tempP.MaximumHealth; } else { newCol.actor = new ActorXML(); newCol.actor.name = temp.Name; } newCol.costNorth = simpleLevelGrid.withinGrid(i, j - 1) ? simpleLevelGrid.MoveCosts[i, j, i, j - 1] : 0; newCol.costNorthEast = simpleLevelGrid.withinGrid(i + 1, j + (i % 2 == 0 ? -1 : 0)) ? simpleLevelGrid.MoveCosts[i, j, i + 1, j + (i % 2 == 0 ? -1 : 0)] : 0; newCol.costNorthWest = simpleLevelGrid.withinGrid(i - 1, j + (i % 2 == 0 ? -1 : 0)) ? simpleLevelGrid.MoveCosts[i, j, i - 1, j + (i % 2 == 0 ? -1 : 0)] : 0; newCol.costSouth = simpleLevelGrid.withinGrid(i, j - 1) ? simpleLevelGrid.MoveCosts[i, j, i, j - 1] : 0; newCol.costSouthEast = simpleLevelGrid.withinGrid(i + 1, j + (i % 2 == 0 ? 0 : 1)) ? simpleLevelGrid.MoveCosts[i, j, i + 1, j + (i % 2 == 0 ? 0 : 1)] : 0; newCol.costSouthWest = simpleLevelGrid.withinGrid(i - 1, j + (i % 2 == 0 ? 0 : 1)) ? simpleLevelGrid.MoveCosts[i, j, i - 1, j + (i % 2 == 0 ? 0 : 1)] : 0; for (int k = 0; k < 3; ++k) { WallXML newWall = new WallXML(); newWall.direction = k; newWall.type = simpleLevelGrid.GridSpaces[i, j].Walls[k].WallType; newCol.walls.Add(newWall); } } newRow.columns.Add(newCol); } levelXML.rows.Add(newRow); } xml.Serialize(fileStream, levelXML); fileStream.Close(); }
/// <summary> /// Saves the file in XML format to the given Stream. /// </summary> /// <param name="saveFile">Stream to pass in, usually from a SaveFileDialog, to which to save the file.</param> public void saveLevelXML(Stream saveFile) { FileStream fileStream = (FileStream)saveFile;//new FileStream(filepath, FileMode.OpenOrCreate); XmlSerializer xml = new XmlSerializer(typeof(LevelXML)); LevelXML levelXML = new LevelXML(); levelXML.Name = LevelName; levelXML.sizeX = simpleLevelGrid.SizeX; levelXML.sizeY = simpleLevelGrid.SizeY; for (int i = 0; i < levelXML.sizeX; ++i) { RowXML newRow = new RowXML(); newRow.index = i; for (int j = 0; j < levelXML.sizeY; ++j) { ColumnXML newCol = new ColumnXML(); newCol.index = j; newCol.type = simpleLevelGrid.GridSpaces[i, j].SpaceType; newCol.elevation = simpleLevelGrid.GridSpaces[i, j].Elevation; if (simpleLevelGrid.GridSpaces[i, j].IsOccupied) { Actor temp = simpleLevelGrid.GridSpaces[i, j].CurrentActor; if (temp is Hero) { Hero tempH = (Hero)temp; newCol.hero = new HeroXML(); newCol.hero.name = tempH.Name; newCol.hero.moveSpeed = tempH.MoveSpeed; newCol.hero.currentEnergy = tempH.Energy; newCol.hero.currentHP = tempH.Health; newCol.hero.maxEnergy = tempH.MaxEnergy; newCol.hero.maxHP = tempH.MaximumHealth; newCol.hero.team = tempH.TeamIndex; newCol.hero.classNum = tempH.ClassIndex; newCol.hero.level = tempH.Level; } else if (temp is Person) { Person tempP = (Person)temp; newCol.person = new PersonXML(); newCol.person.name = tempP.Name; newCol.person.moveSpeed = tempP.MoveSpeed; newCol.person.currentEnergy = tempP.Energy; newCol.person.currentHP = tempP.Health; newCol.person.maxEnergy = tempP.MaxEnergy; newCol.person.maxHP = tempP.MaximumHealth; } else { newCol.actor = new ActorXML(); newCol.actor.name = temp.Name; } newCol.costNorth = simpleLevelGrid.withinGrid(i, j - 1) ? simpleLevelGrid.MoveCosts[i, j, i, j - 1] : 0; newCol.costNorthEast = simpleLevelGrid.withinGrid(i + 1, j + (i % 2 == 0 ? -1 : 0)) ? simpleLevelGrid.MoveCosts[i, j, i + 1, j + (i % 2 == 0 ? -1 : 0)] : 0; newCol.costNorthWest = simpleLevelGrid.withinGrid(i - 1, j + (i % 2 == 0 ? -1 : 0)) ? simpleLevelGrid.MoveCosts[i, j, i - 1, j + (i % 2 == 0 ? -1 : 0)] : 0; newCol.costSouth = simpleLevelGrid.withinGrid(i, j - 1) ? simpleLevelGrid.MoveCosts[i, j, i, j - 1] : 0; newCol.costSouthEast = simpleLevelGrid.withinGrid(i + 1, j + (i % 2 == 0 ? 0 : 1)) ? simpleLevelGrid.MoveCosts[i, j, i + 1, j + (i % 2 == 0 ? 0 : 1)] : 0; newCol.costSouthWest = simpleLevelGrid.withinGrid(i - 1, j + (i % 2 == 0 ? 0 : 1)) ? simpleLevelGrid.MoveCosts[i, j, i - 1, j + (i % 2 == 0 ? 0 : 1)] : 0; for (int k = 0; k < 3; ++k) { WallXML newWall = new WallXML(); newWall.direction = k; newWall.type = simpleLevelGrid.GridSpaces[i, j].Walls[k].WallType; newCol.walls.Add(newWall); } } newRow.columns.Add(newCol); } levelXML.rows.Add(newRow); } xml.Serialize(fileStream, levelXML); fileStream.Close(); }