My History of Game Programming (Part 2 – XNA/C#)

My game programming journey continues when I left college early and moved to New York City where I begun working on movies in the art department. In Brooklyn I started a business with my friend and we produced sets and props for the entertainment industry. I got back into games and programming a few years later when the business became stable.

During my free time I worked on a metroidvania 2d side scroller, programmed in C# using XNA. XNA gives you the basic functionality to make a game.  During this project I wrote functions to setup a grid based level with using tiles of various types/sizes, animations with sprite sheets, a level editor, saving/loading using xmls, collision detection, simple enemy ai, a gui, an inventory, item pickups, 13 weapons, level switching and an event system.

The below image is of the level editor control window made with Windows Forms which I used to craft the levels.

Below is a gif of the level editor in action. The level is converted to an xml which the game then loads.

GIF

Below is a gif of some game play.

GIF

The below code (the code spacing will not look nice on a small screen) is to the class file that handles the games saving and loading. It is using XNA 3.1. XNA 4.0 release changed the way some of this is done. Also it does not handle the removing of a memory card during the saving and loading process. I later found out that this is one of the requirements of publishing a game onto Xbox Indie Games on the 360.

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using CustomGameData;

namespace Dead_Planet
{
    public class SaveLoad
    {
        Game1 Game;
        public IAsyncResult res;
        Object stateobj;

        //Use for File List Population
        public ContentManager Content
        {
            get { return content; }
        }
        ContentManager content;

        public SaveLoad(Game1 game)
        {
            Game = game;
        }

        //functions to check for both a save and play time
        #region CheckForSave/CheckSaveTime
        //
        //Looks for any existing save directorys returns true if exist 
        //If found set saveDpath to that directory and return true
        //
        public bool CheckForSave(StorageDevice device, string saveSlot)
        {
            if (Game.device != null && Game.device.IsConnected)
            {
                StorageContainer container = device.OpenContainer("Dead_Planet_Save");

                string saveDir = Path.Combine(container.Path, "Save0" + saveSlot);
                if (!Directory.Exists(saveDir))
                {
                    container.Dispose();
                    return false;
                }

                if (Directory.Exists(saveDir))
                {
                    container.Dispose();
                    return true;
                }               

                return false;             
            }
            else
            {
                PromptStorageDevice();
                return false;
            }
        }

        /// <summary>
        /// Enter the path of the save you want to load. string saveslot = "1" through "3"
        /// </summary>
        public void LoadGameTime(StorageDevice device, string saveslot)
        {
            if (Game.device != null && Game.device.IsConnected)
            {
                // Open a storage container.
                StorageContainer container = device.OpenContainer("Dead_Planet_Save");
                // Get the path of the save game.
                string savefile = Path.Combine(container.Path, "Save0" + saveslot + "\\Player.xml");
                // Read the data from the file.
                Game.livePlay.player = new Player();
                content = new ContentManager(Game.Services, "Content");

                if (File.Exists(savefile))
                {
                    using (TextReader reader = new StreamReader(savefile))
                    {
                        XmlSerializer s = new XmlSerializer(typeof(Player));
                        Player tmp = (Player)s.Deserialize(reader);
                        Game.livePlay.player = tmp;
                        reader.Close();
                    }
                }
                container.Dispose();
            }
        }

        #endregion

        //Single function to check both save and time
        #region CheckAllSaveAndTime

        //
        //Check to see if saves exist slots 1-3 and Playtimes. Later need to get progress as well
        //
        public string[] CheckAllSaveAndTime(StorageDevice device)
        {
            string[] saveData = new string[6]; //6 strings. first 3 saves second 3 times.

            if (Game.device != null && Game.device.IsConnected)
            {

                StorageContainer container = device.OpenContainer("Dead_Planet_Save");
              
                for (int saveSlot = 0; saveSlot < 3; saveSlot++)
                {
                    string saveDir = Path.Combine(container.Path, "Save0" + Convert.ToString(saveSlot + 1));
                    string savefile = Path.Combine(container.Path, "Save0" + Convert.ToString(saveSlot + 1) + "\\Player.xml");

                    Game.livePlay.player = new Player();

                    if (!Directory.Exists(saveDir))
                    {
                        saveData[saveSlot] = "0";
                    }

                    if (Directory.Exists(saveDir))
                    {
                        saveData[saveSlot] = "1";
                        
                        //New Game Makes directory but not player file. 
                        if (!File.Exists(savefile))
                        {
                            saveData[saveSlot] = "0";
                        }
                    }

                    if (File.Exists(savefile))
                    {
                        try
                        {
                            XmlSerializer serializer = new XmlSerializer(typeof(Player));
                            using (TextReader reader = new StreamReader(savefile))
                            {
                                Player tmp = (Player)serializer.Deserialize(reader);
                                Game.livePlay.player = tmp;
                                reader.Close();
                            }

                            saveData[saveSlot + 3] = Game.pauseMenu.convertPlayTime();

                        }
                        catch (InvalidOperationException e)
                        {
                            saveData[saveSlot] = "DELETE"; 
                        }
                    }
                    else
                    {

                        saveData[saveSlot + 3] = "New Game"; 
                    }
                }

                container.Dispose();

                return saveData;
            }
            else
            {
                PromptStorageDevice();
                return saveData;
            }
        }

        #endregion

        #region MakeNewGameSave

        //
        //When the player selects to make a new game this will search or if the save directory
        //is full and will prompt user to delete one in order to make new one 
        //
        public void MakeNewGameSave(StorageDevice device, string saveSlot)
        {
            //
            //Might need to add this
            //
            /*if (!device.HasSufficientMemory(gameMemorySize))
                return false;*/
            if (Game.device != null && Game.device.IsConnected)
            {            
                //Create new Player file
                // Open a storage container.
                StorageContainer container = device.OpenContainer("Dead_Planet_Save");
                string filename = Game.Content.RootDirectory + "\\Levels\\level1.xml";

                string dPath = Path.Combine(container.Path, "Save0" + saveSlot);

                if (!Directory.Exists(dPath))
                    Directory.CreateDirectory(dPath);
 
                // Dispose the container, to commit the change.
                container.Dispose();

                LoadGame(Game.device, saveSlot);
            }
            else
                PromptStorageDevice();
        }

        #endregion

        #region LoadGame

        /// <summary>
        /// Enter the path of the save you want to load. string saveslot = "1" through "3"
        /// </summary>
        public void LoadGame(StorageDevice device, string saveslot)
        {
            if (Game.device != null && Game.device.IsConnected)
            {
                // Open a storage container.
                StorageContainer container = device.OpenContainer("Dead_Planet_Save");
                // Get the path of the save game.
                string savefile = Path.Combine(container.Path, "Save0" + saveslot + "\\Player.xml");
                string level1filepath = Game.Content.RootDirectory + "\\Levels\\level1";
                string levelsfilepath = Game.Content.RootDirectory + "\\Levels\\";                
                // Read the data from the file.
                int filecount = 0;
                Game.Levels = new string[0]; //resets room cause mainmenu comes back here and needs to reset
                Game.livePlay.player = new Player();
                content = new ContentManager(Game.Services, "Content");
                string[] filePaths = Directory.GetFiles(Game.Content.RootDirectory + "\\Levels\\");
                Game.Levels = new string[filePaths.Length];
                //load all game rooms
                foreach (string result in filePaths)
                {
                    filecount += 1;
                    // Open the file.         
                    string resultFileName;
                    resultFileName = result;
                    resultFileName =  Path.GetFileName(result);
                    resultFileName = resultFileName.Substring(0, resultFileName.Length - 4);// takes off .fileType
                    Game.Levels[filecount -1] = resultFileName;
                }               
                //loadplayer and room
                //loads level if player save exists
                if (File.Exists(savefile))
                {
                    using (TextReader reader = new StreamReader(savefile))
                    {
                        Type[] extraTypes = new Type[15]; //Must add all guns in this list
                        extraTypes[0] = typeof(Laser);
                        extraTypes[1] = typeof(Gun);
                        extraTypes[2] = typeof(Rifle);
                        extraTypes[3] = typeof(ShotGun);
                        extraTypes[4] = typeof(FlameThrower);
                        extraTypes[5] = typeof(ChainSaw);
                        extraTypes[6] = typeof(AutoShotgun);
                        extraTypes[7] = typeof(BoltRifle);
                        extraTypes[8] = typeof(PewLaser);
                        extraTypes[9] = typeof(MiniGun);
                        extraTypes[10] = typeof(AKRifle);
                        extraTypes[11] = typeof(LightningGun);
                        extraTypes[12] = typeof(MidLaser);
                        extraTypes[13] = typeof(SlugShotGun);
                        extraTypes[14] = typeof(EmptyGun);

                        XmlSerializer s = new XmlSerializer(typeof(Player), extraTypes);
                        Player tmp = (Player)s.Deserialize(reader);
                        Game.livePlay.player = new Player();
                        Game.livePlay.player = tmp;
                        reader.Close();

                        // set player position on newgame
                        if(Game.livePlay.player.saveRoom.CompareTo("levelselect") == 0)
                        {
                            Game.gameState = Game1.GameState.LevelSelect;
                        }
                        else for (int level = 0; level < Game.Levels.Length; level++)
                        {
                            if (Game.Levels[level].CompareTo(Game.livePlay.player.saveRoom) == 0)
                            {
                                //Load game room
                                RoomData roomData = new RoomData();
                                roomData = Game.Content.Load<RoomData>(levelsfilepath + Game.livePlay.player.saveRoom);
                                Game.livePlay.currentRoom = RoomDataToRoom(roomData);
                                Game.livePlay.currentRoom.setUpCamera(Game.viewPortRect);
                                Game.livePlay.currentRoom.changeBG(Game.livePlay.currentRoom.bgName, Game.Content.Load<Texture2D>("BackgroundTexture//" + Game.livePlay.currentRoom.bgName));
                                Game.livePlay.currentRoom.RoomName = Game.Levels[level];
                                Game.livePlay.currentRoom.setTiles(Game);

                                //load player position
                                List<Door> tmpdoors = Game.livePlay.currentRoom.getDoors();
                                for (int i = 0; i < tmpdoors.Count; i++)

                                    if (tmpdoors[i].doorIndex.CompareTo(Game.livePlay.player.saveRoomDoor) == 0)
                                    {
                                        Game.livePlay.player.setPlayerRectX(tmpdoors[i].doorRect.X);
                                        Game.livePlay.player.setPlayerRectY(tmpdoors[i].doorRect.Y);
                                        break;
                                    }

                                break;
                            }
                        }                     
                    }
                }
                else //if file does not exist it is new game and load room based on level1
                {
                    //Load game room
                    RoomData roomData = new RoomData();
                    roomData = Game.Content.Load<RoomData>(level1filepath);
                    Game.livePlay.currentRoom = RoomDataToRoom(roomData);
                    Game.livePlay.currentRoom.setUpCamera(Game.viewPortRect);
                    Game.livePlay.currentRoom.changeBG(Game.livePlay.currentRoom.bgName, Game.Content.Load<Texture2D>("BackgroundTexture//" + Game.livePlay.currentRoom.bgName));
                    Game.livePlay.currentRoom.RoomName = "level1";
                    Game.livePlay.currentRoom.setTiles(Game);
                }
                //end player room load

                // Dispose the container.
                container.Dispose();
                GC.Collect();
            }
            else
                PromptStorageDevice();

        }

        #endregion

        #region RoomDataToRoom/TileDataToTile

        //Cannot implicty convert type roomData to room so this does it
        public Room RoomDataToRoom(RoomData roomdatacopy)
        {
            Room newroom = new Room();

            newroom.Width = Convert.ToInt32(roomdatacopy.Width);
            newroom.Height = Convert.ToInt32(roomdatacopy.Height);
            newroom.bgName = roomdatacopy.bgName;
            newroom.enemies = new Enemies();
            newroom.doors = new List<Door>();
            newroom.items = new List<Item>();
            newroom.events = new List<Event>();
            newroom.tiles = new Tile[newroom.Width, newroom.Height];

            for (int i = 0; i < newroom.Width; i++)
            {
                for (int j = 0; j < newroom.Height; j++)
                {
                    newroom.tiles[i, j] = TileDataToTile(roomdatacopy.tiles[i, j]);
                    if (newroom.tiles[i, j].tileType.CompareTo(TileType.PlayerStart.ToString()) == 0)//player start tile handler
                    {
                        if (!newroom.playerSet && Game.livePlay.player.playTime == 0)
                        {
                            newroom.playerSet = true;
                            newroom.playerX = i * Tile.Width;
                            newroom.playerY = (j * Tile.Height) - 1; //starts low if not there, not sure why
                        }
                        newroom.tiles[i, j] = new Tile();
                        newroom.tiles[i, j].tileType = TileType.Passable.ToString();
                        newroom.tiles[i, j].textureName = "empty";
                    }
                    else if (newroom.tiles[i, j].tileType.CompareTo(TileType.EnemySpawn.ToString()) == 0)
                    {//enemy spawn tile handler
                        if (!newroom.tiles[i, j].isSkipTile())
                        {
                            Enemy tmpEnemy = new Enemy();

                            switch (newroom.tiles[i, j].textureName)
                            {
                                case "enemyTest1_2x2":
                                    tmpEnemy = new ScrapBot(i, j);
                                    break;
                                case "skullture":
                                    tmpEnemy = new Skullture(i, j);
                                    break;
                                case "evanScrapBot":
                                    tmpEnemy = new EvanScrapBot(i, j);
                                    break;
                                case "turrentRobot2x2Ver2":
                                    tmpEnemy = new TurrentBot(i, j);
                                    break;
                                case "zombiefemale32x96":
                                    tmpEnemy = new ZombieFemale(i, j);
                                    break;
                                case "zombiemale32x96":
                                    tmpEnemy = new ZombieMale(i, j);
                                    break;
                                case "squidAlien":
                                    tmpEnemy = new squidAlien(i, j);
                                    break;
                                case "flyingrobot64x64":
                                    tmpEnemy = new FlyingRobot(i, j);
                                    break;
                                case "zombieDoor64x96":
                                    tmpEnemy = new ZombieDoor(i, j);
                                    break;
                                case "scrapBotLarge":
                                    tmpEnemy = new ScrapBotLarge(i, j);
                                    break;
                                case "blockTurrentSketch":
                                    tmpEnemy = new BlockTurrentBot(i, j);
                                    break;
                                case "blockTurrent":
                                    tmpEnemy = new EvanBlockTurrentBot(i, j);
                                    break;
                                case "zombieBazooka":
                                    tmpEnemy = new zombieZooka(i, j);
                                    break;
                                case "wheelyBot":
                                    tmpEnemy = new WheelyBot(i, j);
                                    break;
                                default: tmpEnemy = null; //will throw a null exeption for unknown enemy tile

                                    break;
                            }

                            newroom.enemies.add(tmpEnemy);
                        }
                        newroom.tiles[i, j] = new Tile();
                        newroom.tiles[i, j].tileType = TileType.Passable.ToString();
                        newroom.tiles[i, j].textureName = "empty";
                    }
                    else if (newroom.tiles[i, j].tileType.CompareTo(TileType.DoorWay.ToString()) == 0)//door tile handler
                    {
                        if (!newroom.tiles[i, j].isSkipTile())
                        {
                            Rectangle doorRectangle = new Rectangle(i * Tile.Width, j * Tile.Height, newroom.tiles[i, j].tilesXwidth * Tile.Width, newroom.tiles[i, j].tilesYheight * Tile.Height);

                            Door tmpDoor = new Door(newroom.tiles[i, j].doorIndex, newroom.tiles[i, j].doorLinkIndex, newroom.tiles[i, j].mapLink, doorRectangle, newroom.tiles[i, j].textureName);
                            newroom.doors.Add(tmpDoor);
                            //}
                        }
                        newroom.tiles[i, j] = new Tile();
                        newroom.tiles[i, j].tileType = TileType.Passable.ToString();
                        newroom.tiles[i, j].textureName = "empty";
                    }
                    else if (newroom.tiles[i, j].tileType.CompareTo(TileType.EventTrigger.ToString()) == 0)//eventtrigger tile handler
                    {
                        if (!newroom.tiles[i, j].isSkipTile())
                        {
                            //if !repeat && !player encountered before then add the event
                            if (Convert.ToInt32(newroom.tiles[i, j].doorLinkIndex) > 0 || !Game.livePlay.player.eventCheck(newroom.tiles[i, j].mapLink))
                            {
                                Rectangle eventRectangle = new Rectangle(i * Tile.Width, j * Tile.Height, newroom.tiles[i, j].tilesXwidth * Tile.Width, newroom.tiles[i, j].tilesYheight * Tile.Height);

                                Event tmpEvent = new Event(newroom.tiles[i, j].doorIndex, newroom.tiles[i, j].doorLinkIndex, newroom.tiles[i, j].mapLink, eventRectangle);
                                newroom.events.Add(tmpEvent);
                            }
                        }
                        newroom.tiles[i, j] = new Tile();
                        newroom.tiles[i, j].tileType = TileType.Passable.ToString();
                        newroom.tiles[i, j].textureName = "empty";
                    }
                    else if (newroom.tiles[i, j].tileType.CompareTo(TileType.Foreground.ToString()) == 0)//eventtrigger tile handler
                    {
                        if (!newroom.tiles[i, j].isSkipTile())
                        {
                            Rectangle forgroundRectangle = new Rectangle(i * Tile.Width, j * Tile.Height, newroom.tiles[i, j].tilesXwidth * Tile.Width, newroom.tiles[i, j].tilesYheight * Tile.Height);
                            Foreground tmpForeground = new Foreground(newroom.tiles[i, j].textureName, forgroundRectangle);
                            newroom.foregrounds.Add(tmpForeground);
                        }
                        newroom.tiles[i, j] = new Tile();
                        newroom.tiles[i, j].tileType = TileType.Passable.ToString();
                        newroom.tiles[i, j].textureName = "empty";
                    }
                    else if (newroom.tiles[i, j].tileType.CompareTo(TileType.ItemTile.ToString()) == 0)//item tile handler
                    {
                        if (!newroom.tiles[i, j].isSkipTile())
                        {
                            Item tmpItem;
                            switch (newroom.tiles[i, j].textureName)
                            {
                                case "shieldPill1x1":
                                    tmpItem = new powerPill(i, j, Game);
                                    break;
                                case "piggyBankGold":
                                    tmpItem = new largeMoney(i, j, Game);
                                    break;
                                case "itemShotgunBullets1x1":
                                    tmpItem = new shotGunBullets(i, j, Game);
                                    break;
                                case "itemMachineBullets1x1":
                                    tmpItem = new machineGunBullets(i, j, Game);
                                    break;
                                case "salvage1x1":
                                    tmpItem = new smallSalvage(i, j, Game);
                                    break;
                                case "energyBattery":
                                    tmpItem = new energyBullet(i, j, Game);
                                    break;
                                case "powerTank":
                                    tmpItem = new powerTank(i, j, Game);
                                    break;
                                case "fuelAmmo":
                                    tmpItem = new fuelTank(i, j, Game);
                                    break;
                                case "itemAKRifle":
                                    tmpItem = new weaponItem(new AKRifle(), i, j, Game);
                                    break;
                                case "itemAutoShotgun":
                                    tmpItem = new weaponItem(new AutoShotgun(), i, j, Game);
                                    break;
                                case "itemBoltRifle":
                                    tmpItem = new weaponItem(new BoltRifle(), i, j, Game);
                                    break;
                                case "itemCannon":
                                    tmpItem = new weaponItem(new Laser(), i, j, Game);
                                    break;
                                case "itemChainSaw":
                                    tmpItem = new weaponItem(new ChainSaw(), i, j, Game);
                                    break;
                                case "itemFlamer":
                                    tmpItem = new weaponItem(new FlameThrower(), i, j, Game);
                                    break;
                                case "itemLaserPew":
                                    tmpItem = new weaponItem(new PewLaser(), i, j, Game);
                                    break;
                                case "itemLightningGun":
                                    tmpItem = new weaponItem(new LightningGun(), i, j, Game);
                                    break;
                                case "itemMidLaser":
                                    tmpItem = new weaponItem(new MidLaser(), i, j, Game);
                                    break;
                                case "itemMiniGun":
                                    tmpItem = new weaponItem(new MiniGun(), i, j, Game);
                                    break;
                                case "itemRifle":
                                    tmpItem = new weaponItem(new Rifle(), i, j, Game);
                                    break;
                                case "itemShotgun":
                                    tmpItem = new weaponItem(new ShotGun(), i, j, Game);
                                    break;
                                case "itemSlugShotgun":
                                    tmpItem = new weaponItem(new SlugShotGun(), i, j, Game);
                                    break;
                                case "itemJetPack":
                                    tmpItem = new jetPack(i, j, Game);
                                    break;
                                default:
                                    tmpItem = new Item(i, j, newroom.tiles[i, j].textureName);
                                    break;
                            }
                            newroom.items.Add(tmpItem);
                        }
                        newroom.tiles[i, j] = new Tile();
                        newroom.tiles[i, j].tileType = TileType.Passable.ToString();
                        newroom.tiles[i, j].textureName = "empty";
                    }
                    else if (newroom.tiles[i, j].tileType.CompareTo(TileType.Destructible.ToString()) == 0)//destructible tile handler
                    {
                        if (!newroom.tiles[i, j].isSkipTile())
                        {
                            Destructible newDestructible;
                            Rectangle newDestructibleRect = new Rectangle(i * Tile.Width, j * Tile.Height, Tile.Width * newroom.tiles[i, j].tilesXwidth, Tile.Height * newroom.tiles[i, j].tilesYheight);
                            switch (newroom.tiles[i, j].textureName)
                            {
                                case "metalBarrel":
                                    newDestructible = new DestructibleMetalBarrel(Game, newDestructibleRect);
                                    break;
                                case "newbarrell":
                                    newDestructible = new DestructibleMetalBarrelTwo(Game, newDestructibleRect);
                                    break;
                                case "Door1":
                                    newDestructible = new DestructibleDoor1(Game, newDestructibleRect);
                                    break;
                                default:
                                    newDestructible = new Destructible(Game, newDestructibleRect);
                                    break;
                            }
                            newroom.addDestructible(newDestructible);
                        }
                        newroom.tiles[i, j] = new Tile();
                        newroom.tiles[i, j].tileType = TileType.Passable.ToString();
                        newroom.tiles[i, j].textureName = "empty";
                    }
                }
            }

            return newroom;
        }

        //Cannot implicitly convert type tileData to tile so this does it
        public Tile TileDataToTile(TileData tiledatacopy)
        {
            Tile newtile = new Tile();

            newtile.tilesXwidth = tiledatacopy.tilesXwidth;
            newtile.tilesYheight = tiledatacopy.tilesYheight;
            newtile.textureName = tiledatacopy.textureName;
            newtile.tileType = tiledatacopy.tileType;
            newtile.doorIndex = tiledatacopy.doorIndex;
            newtile.doorLinkIndex = tiledatacopy.doorLinkIndex;
            newtile.mapLink = tiledatacopy.mapLink;

            return newtile;
        }

        #endregion

        /// <summary>
        /// Shows promp only if user has >1 storage device. Else auto assigns the device to Game.device
        /// </summary>
        public void PromptStorageDevice()
        {         
            if (!Guide.IsVisible)
            {
                // Reset the device              
                Game.device = null;
                stateobj = (Object)"GetDevice for Player";
                res = Guide.BeginShowStorageDeviceSelector(Game.controllingPlayer, this.GetDevice, stateobj);
            }

        }
        
        //Used in PrmoptStorageDevice to end storage selection 
        private void GetDevice(IAsyncResult result)
        {
            Game.device = Guide.EndShowStorageDeviceSelector(result);

              
            if (Game.device != null && Game.device.IsConnected)
            {
                Game.deviceSelect = true;
            }
            if (!Game.device.IsConnected || Game.device == null) 
            {
                PromptStorageDevice();
            }
        }

        #region CopySaveGame

        /// <summary>
        /// Add (Game.device, "1" or "2" or "3", "1" or "2" or "3")
        /// first string is the original to be copied and second is the slot to copy to
        /// </summary>
        public bool CopySaveGame(StorageDevice device, string original, string copy)
        {
            if (Game.device != null && Game.device.IsConnected)
            {
                StorageContainer container = device.OpenContainer("Dead_Planet_Save");

                string dirToCopy = Path.Combine(container.Path, "Save0" + original);
                string dirToCreate = Path.Combine(container.Path, "Save0" + copy);
                
                if (!Directory.Exists(dirToCopy))
                {
                    container.Dispose();
                    return false;
                }
                
                if (!Directory.Exists(dirToCreate))
                {
                    Directory.CreateDirectory(dirToCreate);
                }

                content = new ContentManager(Game.Services, "Content");
                string[] filePaths = Directory.GetFiles(container.Path + "Save0" + original);
                
                foreach (string result in filePaths)
                {
                    for (int filenum = 0; filenum < filePaths.Length; filenum++)
                    {
                        string originalFilePath = result;
                        string copyFilePath = Path.Combine(container.Path, ("Save0" + copy + "\\"));
                    }
                }

                container.Dispose();

                return true;
            }
            else
                PromptStorageDevice();
                return false;
        }

        #endregion

        #region DeleteSaveGame

        /// <summary>
        /// Add (Game.device, "1" or "2" or "3")
        /// the string is the GameSlot to be DELETED
        /// </summary>
        public void DeleteSaveGame(StorageDevice device, string savedgame)
        {
            if (Game.device != null && Game.device.IsConnected)
            {
                StorageContainer container = device.OpenContainer("Dead_Planet_Save");

                string dirToDelete = Path.Combine(container.Path, "Save0" + savedgame);

                if (Directory.Exists(dirToDelete))
                {
                    Directory.Delete(dirToDelete, true);
                }

                container.Dispose();
            }
            else
                PromptStorageDevice();
        }

        #endregion

        #region SaveCurrentGame

        /// <summary>
        /// Saves the player 
        /// </summary>
        public void SaveCurrentGame(StorageDevice device, string currentGame)
        {
            if (Game.device != null && Game.device.IsConnected)
            {
                //save in save folder with currrent game and replace
                StorageContainer container = device.OpenContainer("Dead_Planet_Save");
                string path = Path.Combine(container.Path, "Save0" + currentGame + "\\Player.xml");

                Type[] extraTypes = new Type[15]; //Must add all guns in this list
                extraTypes[0] = typeof(Laser);
                extraTypes[1] = typeof(Gun);
                extraTypes[2] = typeof(Rifle);
                extraTypes[3] = typeof(ShotGun);
                extraTypes[4] = typeof(FlameThrower);
                extraTypes[5] = typeof(ChainSaw);
                extraTypes[6] = typeof(AutoShotgun);
                extraTypes[7] = typeof(BoltRifle);
                extraTypes[8] = typeof(PewLaser);
                extraTypes[9] = typeof(MiniGun);
                extraTypes[10] = typeof(AKRifle);
                extraTypes[11] = typeof(LightningGun);
                extraTypes[12] = typeof(MidLaser);
                extraTypes[13] = typeof(SlugShotGun);
                extraTypes[14] = typeof(EmptyGun);

                XmlSerializer s = new XmlSerializer(typeof(Player), extraTypes);

                using (TextWriter w = new StreamWriter(@path))
                {
                    s.Serialize(w, Game.livePlay.player);
                    w.Close();
                }
                container.Dispose();
            }
            else
                PromptStorageDevice();

        }

        #endregion

        #region LoadMap

        public void LoadMap(string levelName, int playerx, int playery)
        {
            string levelsfilepath = Game.Content.RootDirectory + "\\Levels\\";

            for (int level = 0; level < Game.Levels.Length; level++)
            {
                if (Game.Levels[level].CompareTo(levelName) == 0)
                {
                    Game.livePlay.player.currentGun.bulletClear();
                    //Load game room
                    RoomData roomData = new RoomData();
                    roomData = Game.Content.Load<RoomData>(levelsfilepath + levelName);
                    Game.livePlay.currentRoom = RoomDataToRoom(roomData);
                    Game.livePlay.currentRoom.setUpCamera(Game.viewPortRect);
                    Game.livePlay.currentRoom.changeBG(Game.livePlay.currentRoom.bgName, Game.Content.Load<Texture2D>("BackgroundTexture//" + Game.livePlay.currentRoom.bgName));
                    Game.livePlay.currentRoom.RoomName = Game.Levels[level];
                    Game.livePlay.currentRoom.setTiles(Game);

                    //load player position
                    Game.livePlay.player.setPlayerRectX(playerx);
                    Game.livePlay.player.setPlayerRectY(playery);
                }
            }

            GC.Collect();
        }

        #endregion

        #region LoadSaveRoom

        //
        //Loads Player to save door of last save he was at. for use when dies
        //DOES NOT reload player save so current progress is kept
        //
        public void LoadSaveRoom(Game1 Game)
        {
            string levelsfilepath = Game.Content.RootDirectory + "\\Levels\\";

            for (int level = 0; level < Game.Levels.Length; level++)
            {
                if (Game.Levels[level].CompareTo(Game.livePlay.player.saveRoom) == 0)
                {
                    Game.livePlay.player.currentGun.bulletClear();
                    //Load game room
                    RoomData roomData = new RoomData();
                    roomData = Game.Content.Load<RoomData>(levelsfilepath + Game.livePlay.player.saveRoom);
                    Game.livePlay.currentRoom = RoomDataToRoom(roomData);
                    Game.livePlay.currentRoom.setUpCamera(Game.viewPortRect);
                    Game.livePlay.currentRoom.changeBG(Game.livePlay.currentRoom.bgName, Game.Content.Load<Texture2D>("BackgroundTexture//" + Game.livePlay.currentRoom.bgName));
                    Game.livePlay.currentRoom.RoomName = Game.Levels[level];
                    Game.livePlay.currentRoom.setTiles(Game);

                    //load player position
                    List<Door> tmpdoors = Game.livePlay.currentRoom.getDoors();
                    for (int i = 0; i < tmpdoors.Count; i++)

                        if (tmpdoors[i].doorIndex.CompareTo(Game.livePlay.player.saveRoomDoor) == 0)
                        {
                            Game.livePlay.player.setPlayerRectX(tmpdoors[i].doorRect.X);
                            Game.livePlay.player.setPlayerRectY(tmpdoors[i].doorRect.Y);
                            break;
                        }

                    break;
                }
            }

            GC.Collect();
        }

        #endregion

        #region LoadCurrentRoom

        //
        //Loads Player to current room and door he entered from. for use when dies
        //DOES NOT reload player save so current progress is kept
        //reloads enemies
        //
        public void LoadCurrentRoom(Game1 Game)
        {
            string levelsfilepath = Game.Content.RootDirectory + "\\Levels\\";

            for (int level = 0; level < Game.Levels.Length; level++)
            {
                if (Game.Levels[level].CompareTo(Game.livePlay.currentRoom.RoomName) == 0)
                {
                    Game.livePlay.player.currentGun.bulletClear();
                    //Load game room
                    RoomData roomData = new RoomData();
                    roomData = Game.Content.Load<RoomData>(levelsfilepath + Game.livePlay.currentRoom.RoomName);
                    Game.livePlay.currentRoom = RoomDataToRoom(roomData);
                    Game.livePlay.currentRoom.setUpCamera(Game.viewPortRect);
                    Game.livePlay.currentRoom.changeBG(Game.livePlay.currentRoom.bgName, Game.Content.Load<Texture2D>("BackgroundTexture//" + Game.livePlay.currentRoom.bgName));
                    Game.livePlay.currentRoom.RoomName = Game.Levels[level];
                    Game.livePlay.currentRoom.setTiles(Game);

                    //load player position
                    List<Door> tmpdoors = Game.livePlay.currentRoom.getDoors();
                    for (int i = 0; i < tmpdoors.Count; i++)

                        if (tmpdoors[i].doorIndex.CompareTo(Game.livePlay.player.lastRoomDoor) == 0)
                        {
                            Game.livePlay.player.setRoomChangedBools();
                            Game.livePlay.player.setPlayerRectX(tmpdoors[i].doorRect.X);
                            Game.livePlay.player.setPlayerRectY(tmpdoors[i].doorRect.Y);

                            break;
                        }

                    break;
                }
            }

            GC.Collect();
        }

        #endregion

        #region LoadGameToLevelSelect

        public void LoadGameToLevelSelect(StorageDevice device, string saveslot)
        {
            if (Game.device != null && Game.device.IsConnected)
            {
                // Open a storage container.
                StorageContainer container = device.OpenContainer("Dead_Planet_Save");

                //Going to need to remove the level and take in a value for the level needing loaded
                // Get the path of the save game.
                string savefile = Path.Combine(container.Path, "Save0" + saveslot + "\\Player.xml");
                string level1filepath = Game.Content.RootDirectory + "\\Levels\\level1";
                string levelsfilepath = Game.Content.RootDirectory + "\\Levels\\";
                
                // Read the data from the file.
                int filecount = 0;
                Game.Levels = new string[0]; //resets room cause mainmenu comes back here and needs to reset
                Game.livePlay.player = new Player();
                content = new ContentManager(Game.Services, "Content");
                string[] filePaths = Directory.GetFiles(Game.Content.RootDirectory + "\\Levels\\");
                Game.Levels = new string[filePaths.Length];

                //load all game rooms
                foreach (string result in filePaths)
                {
                    filecount += 1;
                    // Open the file.
                 
                    string resultFileName;
                    resultFileName = result;
                    resultFileName =  Path.GetFileName(result);
                    resultFileName = resultFileName.Substring(0, resultFileName.Length - 4);// takes off .fileType
                    Game.Levels[filecount -1] = resultFileName;
                }
                
                //loadplayer and room
                //loads level if player save exists
                if (File.Exists(savefile))
                {
                    using (TextReader reader = new StreamReader(savefile))
                    {
                        Type[] extraTypes = new Type[15]; //Must add all guns in this list
                        extraTypes[0] = typeof(Laser);
                        extraTypes[1] = typeof(Gun);
                        extraTypes[2] = typeof(Rifle);
                        extraTypes[3] = typeof(ShotGun);
                        extraTypes[4] = typeof(FlameThrower);
                        extraTypes[5] = typeof(ChainSaw);
                        extraTypes[6] = typeof(AutoShotgun);
                        extraTypes[7] = typeof(BoltRifle);
                        extraTypes[8] = typeof(PewLaser);
                        extraTypes[9] = typeof(MiniGun);
                        extraTypes[10] = typeof(AKRifle);
                        extraTypes[11] = typeof(LightningGun);
                        extraTypes[12] = typeof(MidLaser);
                        extraTypes[13] = typeof(SlugShotGun);
                        extraTypes[14] = typeof(EmptyGun);

                        XmlSerializer s = new XmlSerializer(typeof(Player), extraTypes);
                        Player tmp = (Player)s.Deserialize(reader);
                        Game.livePlay.player = new Player();
                        Game.livePlay.player = tmp;
                        reader.Close();
                    }

                    Game.gameState = Game1.GameState.LevelSelect;
                }

                // Dispose the container.
                container.Dispose();
                GC.Collect();
            }
            else
                PromptStorageDevice();     
        }

        #endregion
    }
}