Пример #1
0
        /// <summary>
        /// saves current game progress
        /// </summary>
        /// <param name="gameProgress"></param>
        public static void Save(GameProgress gameProgress)
        {
            Player           player  = SavePlayer();
            List <Character> enemies = SaveEnemies();

            gameProgress.Player = player;

            gameProgress.Enemies = new List <Character>();
            gameProgress.Enemies.AddRange(enemies);

            BinaryFormatter bf = new BinaryFormatter();

            // write save file
            using (FileStream fs = File.Open(fileName, FileMode.OpenOrCreate))
            {
                bf.Serialize(fs, gameProgress);
            }
        }
        /// <summary>
        /// loads saved game progress
        /// </summary>
        /// <returns></returns>
        public static GameProgress Load()
        {
            GameProgress gameProgress = null;

            if (HasSave())
            {
                // read saved file
                using (FileStream fs = File.Open(fileName, FileMode.Open))
                {
                    BinaryFormatter bf = new BinaryFormatter();
                    gameProgress = bf.Deserialize(fs) as GameProgress;
                }
            }
            else
            {
                // all parameters are set to initial values
                gameProgress = new GameProgress();
            }

            return(gameProgress);
        }
        /// <summary>
        /// saves current game progress
        /// </summary>
        /// <param name="gameProgress"></param>
        public static void Save(GameProgress gameProgress)
        {
            Player player = SavePlayer();

            List <Drone> drones = new List <Drone>();

            drones.Add(SaveDrone(GameObject.FindGameObjectWithTag(Resources.Tags.BattleDrone)));
            drones.Add(SaveDrone(GameObject.FindGameObjectWithTag(Resources.Tags.ServiceDrone)));

            List <Character> enemies = SaveEnemies();

            gameProgress.Player  = player;
            gameProgress.Drones  = drones;
            gameProgress.Enemies = new List <Character>();
            gameProgress.Enemies.AddRange(enemies);

            BinaryFormatter bf = new BinaryFormatter();

            // write save file
            using (FileStream fs = File.Open(fileName, FileMode.OpenOrCreate))
            {
                bf.Serialize(fs, gameProgress);
            }
        }