Beispiel #1
0
        internal bool Load(string filename)
        {
            try
            {
                Entities.Clear();
                Tiles.Clear();
                TileHistory.Clear();
                HistoryProgress = -1;
                IdManager.EntityId.Reset();
                IdManager.QueueId.Reset();

                Dictionary <Point3D, Rectangle> dictionary = new Dictionary <Point3D, Rectangle>();
                Dictionary <Point3D, Tuple <Entity, Rectangle> > entities = new Dictionary <Point3D, Tuple <Entity, Rectangle> >();

                string data      = File.ReadAllText(filename);
                Map    loadedMap = JsonConvert.DeserializeObject <Map>(data);

                Width  = (int)loadedMap.tiles.Max(x => x.x) + 1;
                Height = (int)loadedMap.tiles.Max(x => x.y) + 1;

                // Setup the tiles in the dictionary
                foreach (Tile tile in loadedMap.tiles)
                {
                    if (tile.x < 0 || tile.y < 0 || tile.z < 0 || InvertHeight(tile.y) < 0)
                    {
                        continue;
                    }
                    Rectangle rectangle = new Rectangle
                    {
                        Fill   = Brushes[tile.sprite].Clone(),
                        Width  = TileSize,
                        Height = TileSize
                    };
                    if (tile.deco == true)
                    {
                        rectangle.Tag = "deco";
                    }
                    else if (tile.walkOn == false)
                    {
                        rectangle.Tag = "noWalkOn";
                    }

                    if (tile.z > 3.7 && tile.z < 4.0)
                    {
                        Console.Write(tile.z);
                    }
                    rectangle.SetCurrentValue(Canvas.ZIndexProperty, GameToCanvasZ(tile.z));
                    rectangle.RenderTransform = new TranslateTransform((tile.x) * TileSize, InvertHeight(tile.y) * TileSize);
                    if (!(tile.x >= Width || /*InvertHeight(tile.y)*/ tile.y >= Height))
                    {
                        // tile.z needs to be converted this way otherwise the cast makes 4.9 become 4.90000000023 xd
                        Point3D point3D = new Point3D(tile.x, tile.y, System.Convert.ToDouble(tile.z.ToString()));
                        if (dictionary.ContainsKey(point3D))
                        {
                            dictionary.Remove(point3D);
                        }
                        dictionary.Add(point3D, rectangle);
                    }
                }

                Tiles = dictionary;

                // Setup the entities in the dictionary
                foreach (Entity e in loadedMap.entities)
                {
                    IdManager.EntityId.ManualAssign(e.id);
                    foreach (EventQueue eq in e.queues)
                    {
                        IdManager.QueueId.ManualAssign(eq.id);
                    }

                    Rectangle rectangle = EntityRectangle(e);
                    //int z = GameToCanvasZ((float)Math.Floor(e.z));
                    if (!(e.x >= Width || e.y >= Height))
                    {
                        Point3D point3D = new Point3D(Math.Round(e.x), Math.Round(e.y), e.z);
                        if (!entities.ContainsKey(point3D))
                        {
                            entities.Add(point3D, new Tuple <Entity, Rectangle>(e, rectangle));
                        }
                    }
                }

                Entities = entities;

                return(true);
            }
            catch (Exception e)
            {
                MessageBox.Show("Error loading file : " + e.Message + "\n" + e.StackTrace);
            }

            return(true);
        }
Beispiel #2
0
 private void ResetTiles()
 {
     Tiles.Clear();
     TileHistory.Clear();
 }