예제 #1
0
        public void to_save()
        {
            Saver saver = new Saver();

            saver.matrix_size = matrix.matrix_size;
            saver.arr_matrix  = matrix.arr_matrix;
            saver.witches     = matrix.witches;
            saver.items       = matrix.items;
            saver.bag_size    = player.bag_size;
            if (bag.Items.Count != 0)
            {
                for (int i = 0; i < bag.Items.Count; i++)
                {
                    saver.bag.Add(bag.Items.GetItemAt(i).ToString());
                }
            }


            saver.map_blocks = new int[matrix.matrix_size, matrix.matrix_size];
            // -1 - не пройден, 1 - пустой, 10 - колдун(не завершенный), 11 - колдун(завершенный), 2 - предмет
            for (int i = 0; i < matrix.matrix_size; i++)
            {
                for (int j = 0; j < matrix.matrix_size; j++)
                {
                    if (map_blocks[i, j].Fill == Brushes.Black)
                    {
                        saver.map_blocks[i, j] = -1;
                    }
                    else if (map_blocks[i, j].Fill == Brushes.Red)
                    {
                        saver.map_blocks[i, j] = 10;
                    }
                    else if (map_blocks[i, j].Fill == Brushes.ForestGreen)
                    {
                        saver.map_blocks[i, j] = 11;
                    }
                    else if (map_blocks[i, j].Fill == Brushes.DarkBlue)
                    {
                        saver.map_blocks[i, j] = 2;
                    }
                    else
                    {
                        saver.map_blocks[i, j] = 1;
                    }
                }
            }

            saver.incomplete_missions = player.incomplete_missions;
            saver.points = player.points;
            saver.moves  = player.moves;
            saver.my_x   = player.show_x();
            saver.my_y   = player.show_y();

            // сохранение для слота-файлика
            string content = JsonConvert.SerializeObject(saver);

            File.WriteAllText(filepath, content);



            // сохранения для меню-файлика

            using (StreamWriter sw = File.AppendText(MainWindow.menu_file))
            {
                Savespot s = new Savespot();
                s.name   = "Слот: " + file_num;
                s.number = file_num;
                s.movies = player.moves;
                s.points = player.points;
                string menu_content = JsonConvert.SerializeObject(s);
                sw.WriteLine(menu_content);
            }

            /*
             * MainWindow.Label_name = FindName("name_" + file_num) as Label;
             * MainWindow.Label_name.Content = file_num;
             *
             * MainWindow.Label_movies = FindName("movies_" + file_num) as Label;
             * MainWindow.Label_movies.Content = player.moves;
             *
             * MainWindow.Label_points = FindName("points_" + file_num) as Label;
             * MainWindow.Label_points.Content = player.points;
             */

            /*
             * string[] changing_slots = new string[MainWindow.spots_count];
             *
             * changing_slots = File.ReadAllText(MainWindow.menu_file).Split('\n');
             *
             * for (int i = 0; i < changing_slots.Length; i++)
             * {
             *  if (i == file_num - 1)
             *  {
             *      changing_slots[i] = "Слот " + file_num + "_" + player.moves + "_" + player.points;
             *  }
             * }
             * File.WriteAllText(MainWindow.menu_file, string.Join("\n", changing_slots));
             *
             */
        }
        private void main_Loaded(object sender, RoutedEventArgs e)
        {
            if (!Directory.Exists(save_path))
            {
                Directory.CreateDirectory(save_path);
            }
            if (!File.Exists(menu_file))
            {
                File.Create(menu_file).Close();

                /*
                 * using (StreamWriter writer = new StreamWriter(menu_file))
                 * {
                 *  writer.WriteLine("Не открыта__");
                 *  writer.WriteLine("Не открыта__");
                 *  writer.WriteLine("Не открыта__");
                 * }*/
            }


            else
            {
                string s = "";
                using (StreamReader sr = File.OpenText(menu_file))
                {
                    while ((s = sr.ReadLine()) != null)
                    {
                        Savespot save = JsonConvert.DeserializeObject <Savespot>(s);
                        savespots.Add(save);
                    }
                }
                for (int i = 1; i <= spots_count; i++)
                {
                    Label_name         = FindName("name_" + i) as Label;
                    Label_name.Content = "Пустой слот";

                    Label_movies         = FindName("movies_" + i) as Label;
                    Label_movies.Content = "";

                    Label_points         = FindName("points_" + i) as Label;
                    Label_points.Content = "";

                    for (int j = 0; j < savespots.Count; j++)
                    {
                        if (i == savespots[j].number)
                        {
                            Label_name         = FindName("name_" + savespots[j].number) as Label;
                            Label_name.Content = savespots[j].name;

                            Label_movies         = FindName("movies_" + savespots[j].number) as Label;
                            Label_movies.Content = savespots[j].movies;

                            Label_points         = FindName("points_" + savespots[j].number) as Label;
                            Label_points.Content = savespots[j].points;
                            break;
                        }
                    }
                }


                /*
                 * string[,] slots = new string[spots_count,3];
                 * var s1 = File.ReadAllText(menu_file).Split('\n');
                 *
                 * for(int i=0;i<spots_count;i++)
                 * {
                 *  var str = s1[i].Split('_');
                 *  for(int j=0;j<spots_count;j++)
                 *  {
                 *      slots[i, j] = str[j];
                 *  }
                 * }
                 *
                 *
                 * for(int i=1;i<=spots_count;i++)
                 * {
                 *  Label_name = FindName("name_" + i) as Label;
                 *  Label_name.Content = slots[i, 0];
                 *
                 *  Label_movies = FindName("movies_" + i) as Label;
                 *  Label_movies.Content = slots[i, 1];
                 *
                 *  Label_points = FindName("points_" + i) as Label;
                 *  Label_points.Content = slots[i, 2];
                 * }
                 */



                /*
                 * name_1.Content = slots[0, 0];
                 * movies_1.Content = slots[0, 1];
                 * points_1.Content = slots[0, 2];
                 *
                 * name_2.Content = slots[1, 0];
                 * movies_2.Content = slots[1, 1];
                 * points_2.Content = slots[1, 2];
                 *
                 *
                 * name_3.Content = slots[2, 0];
                 * movies_3.Content = slots[2, 1];
                 * points_3.Content = slots[2, 2];
                 */
            }


            for (int i = 1; i <= spots_count; i++)
            {
                filepath = save_path + @"\savefile_" + i + ".json";
                if (!File.Exists(filepath))
                {
                    File.Create(filepath).Close();
                }
            }


            room_name.Content = "Хранилище слотов";
        }