示例#1
0
            /// <summary>
            /// Loads a world from path. Returns an error code.
            /// </summary>
            /// <param name="path"></param>
            /// <returns></returns>
            public static LoadStatus TryLoadWorld(string path, out PlannerSettings db)
            {
                using (BinaryReader br = new BinaryReader(File.Open(path, FileMode.Open)))
                {
                    //Header (60 bytes)
                    db = new PlannerSettings(PlannerSettings.DefaultWorldWidth, PlannerSettings.DefaultWorldHeight);
                    if (Encoding.ASCII.GetString(br.ReadBytes(7)) != Encoding.ASCII.GetString(PlannerSettings.SaveMagic))
                    {
                        return(LoadStatus.InvalidMagic);                                                                                                  //Invalid file magic
                    }
                    if (br.ReadInt16() != PlannerSettings.CurrentVersion)
                    {
                        return(LoadStatus.VersionMismatch);                                                  //Different version
                    }
                    db.WorldWidth                   = br.ReadInt16();
                    db.WorldHeight                  = br.ReadInt16();
                    db.hasMainBackground            = br.ReadByte() == 1;
                    db.MainBackground               = (BackgroundData.MainBackgroundType)br.ReadByte();
                    db.hasCustomMainBackgroundColor = br.ReadByte() == 1;
                    db.ARGBBackgroundColor          = br.ReadInt32();
                    br.BaseStream.Seek(40, SeekOrigin.Current); //Skip reserved

                    while (br.BaseStream.Position != br.BaseStream.Length)
                    {
                        int X = br.ReadInt16();
                        int Y = br.ReadInt16();
                        int tileCountAtPos = br.ReadByte();
                        for (int i = 0; i < tileCountAtPos; i++)
                        {
                            byte   index            = br.ReadByte();
                            string tileName         = br.ReadString();
                            bool   existsInDatabase = tileMap.TryGetValue(tileName, out Tile t);
                            if (existsInDatabase)
                            {
                                if (db.TileMap[X, Y] == null)
                                {
                                    db.TileMap[X, Y] = new TileData();
                                }
                                db.TileMap[X, Y].Tiles.Add(t);
                            }
                            else
                            {
                                if (db.InvalidTiles.ContainsKey(tileName))
                                {
                                    db.InvalidTiles[tileName]++;
                                }
                                else
                                {
                                    db.InvalidTiles.Add(tileName, 0);
                                }
                            }
                        }
                    }
                    if (db.InvalidTiles.Count > 0)
                    {
                        return(LoadStatus.TileNotFound);                           //At least one tile is not present in the current version
                    }
                    return(LoadStatus.OK);
                }
            }
示例#2
0
 public bool HasForegroundAt(PlannerSettings s, int X, int Y)
 {
     if (AnyTileAt(s, X, Y))
     {
         return(DB.TileMap[X, Y].Tiles.OfType <Foreground>().Any());
     }
     return(false);
 }
示例#3
0
 public bool HasSpecialAt(PlannerSettings s, int X, int Y)
 {
     if (AnyTileAt(s, X, Y))
     {
         return(DB.TileMap[X, Y].Tiles.OfType <Special>().Any());
     }
     return(false);
 }
示例#4
0
 public Foreground GetForegroundAt(PlannerSettings s, int X, int Y)
 {
     if (HasForegroundAt(s, X, Y))
     {
         return(DB.TileMap[X, Y].Tiles.OfType <Foreground>().ToArray()[0]);
     }
     return(null);
 }
示例#5
0
        /// <summary>
        /// Clone settings from old ones
        /// </summary>
        /// <param name="old"></param>
        public PlannerSettings(PlannerSettings old)
        {
            this.hasMainBackground            = old.hasMainBackground;
            this.MainBackground               = old.MainBackground;
            this.hasCustomMainBackgroundColor = old.hasCustomMainBackgroundColor;
            this.ARGBBackgroundColor          = old.ARGBBackgroundColor;
            this.InvalidTiles = old.InvalidTiles;

            this.WorldWidth  = old.WorldWidth;
            this.WorldHeight = old.WorldHeight;
            this.TileMap     = new TileData[WorldWidth, WorldHeight];
        }
示例#6
0
 public static bool AnyTileAt(PlannerSettings s, int X, int Y)
 {
     if (s.TileMap[X, Y] != null)
     {
         if (s.TileMap[X, Y].Tiles.Count > 0)
         {
             return(true);
         }
         return(false);
     }
     s.TileMap[X, Y] = new TileData();
     return(false);
 }
示例#7
0
 /// <summary>
 /// Creates a new world.
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void NewWorld_Click(object sender, RoutedEventArgs e)
 {
     if (MessageBox.Show("Are you sure to create a new world? You may lose all your unsaved progress!", "Question", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.Yes)
     {
         MainCanvas.Children.Clear();
         DB = new PlannerSettings(80, 60);
         DrawGrid(DB.WorldWidth, DB.WorldHeight);
         DrawBedrock();
         MainCanvas.Background = BackgroundData.GetBackground(DB.MainBackground);
         defaultMatrix         = MainCanvas.LayoutTransform.Value;
         //_selectedTile.TileName
         ComboTypes.SelectedIndex = 1;
         firstPlaced          = false;
         FirstSelected        = false;
         SaveButton.IsEnabled = false;
         PreviousTiles.Items.Clear();
         PreviousTiles.SelectedItem = null;
         SavedPath = String.Empty;
         _selectedTile.Reset();
     }
 }
示例#8
0
        //World Load
        private void LoadWorld_Click(object sender, RoutedEventArgs e)
        {
            if (firstPlaced && MessageBox.Show("Are you sure to load a new world? You may lose all your unsaved progress!", "Question", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.No)
            {
                return;
            }

            isLoading = true;

            OpenFileDialog dialog = new OpenFileDialog()
            {
                Filter           = "Pixel Worlds World (*.pww)|*.pww",
                DefaultExt       = "pww",
                RestoreDirectory = true
            };
            Nullable <bool> Selected = dialog.ShowDialog();
            string          path     = dialog.FileName;

            if (Selected == true)
            {
                PlannerSettings        newWorldDB;
                DataHandler.LoadStatus status = DataHandler.TryLoadWorld(path, out newWorldDB);

                if (status == DataHandler.LoadStatus.VersionMismatch)
                {
                    MessageBox.Show("This world is not compatible with this planner version.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                else if (status == DataHandler.LoadStatus.InvalidMagic)
                {
                    MessageBox.Show("This is not a world file.", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
                else //Load-able
                {
                    MainCanvas.Children.Clear();
                    DB = new PlannerSettings(newWorldDB);
                    DrawGrid(DB.WorldWidth, DB.WorldHeight);

                    if (status == DataHandler.LoadStatus.TileNotFound)
                    {
                        if (newWorldDB.InvalidTiles.Count > 0)
                        {
                            StringBuilder sb = new StringBuilder();
                            sb.AppendLine($"Could not load {newWorldDB.InvalidTiles.Count} tiles (Using an older version?)");
                            foreach (KeyValuePair <string, int> entry in newWorldDB.InvalidTiles)
                            {
                                sb.AppendLine($"-{entry.Key} [x{entry.Value}]");
                            }
                            MessageBox.Show(sb.ToString(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                    }

                    for (int y = 0; y < DB.WorldHeight; y++)
                    {
                        for (int x = 0; x < DB.WorldWidth; x++)
                        {
                            if (!AnyTileAt(newWorldDB, x, y))
                            {
                                continue;
                            }

                            foreach (Tile t in newWorldDB.TileMap[x, y].Tiles.ToList())
                            {
                                Image image = new Image()
                                {
                                    Source = t.Image.Source
                                };
                                Canvas.SetTop(image, y * 32);
                                Canvas.SetLeft(image, x * 32);
                                image.SetValue(Canvas.ZIndexProperty, t.ZIndex);
                                MainCanvas.Children.Add(image);
                                if (DB.TileMap[x, y] == null)
                                {
                                    DB.TileMap[x, y] = new TileData();
                                }


                                if (t is Foreground)
                                {
                                    DB.TileMap[x, y].Tiles.Add(new Foreground(image)
                                    {
                                        TileName = t.TileName
                                    });
                                }
                                else if (t is Background)
                                {
                                    DB.TileMap[x, y].Tiles.Add(new Background(image)
                                    {
                                        TileName = t.TileName
                                    });
                                }
                                else
                                {
                                    DB.TileMap[x, y].Tiles.Add(new Special(image)
                                    {
                                        TileName = t.TileName
                                    });
                                }
                            }
                        }
                    }

                    if (!DB.hasMainBackground)
                    {
                        MainCanvas.Background = new SolidColorBrush(Utils.IntToARGBColor(DB.ARGBBackgroundColor));
                    }
                    else
                    {
                        MainCanvas.Background = BackgroundData.GetBackground(DB.MainBackground);
                    }

                    ColorSelector.SelectedColor = Utils.IntToARGBColor(DB.ARGBBackgroundColor);
                    firstPlaced          = false;
                    FirstSelected        = false;
                    SaveButton.IsEnabled = false;
                    SavedPath            = String.Empty;
                    PreviousTiles.Items.Clear();
                    PreviousTiles.SelectedItem = null;
                    _selectedTile.Reset();
                }
            }
            isLoading = false;
        }
示例#9
0
 private void GenerateTileMap(short Width, short Height)
 {
     DB = new PlannerSettings(Width, Height);
 }