Esempio n. 1
0
        private void GenerateTiles()
        {
            for (int x = 0; x < gridTiles.ColumnDefinitions.Count; x++)
            {
                for (int y = 0; y < gridTiles.RowDefinitions.Count; y++)
                {
                    Button tile = new Button();

                    // auto-size
                    tile.Width  = Double.NaN;
                    tile.Height = Double.NaN;

                    tile.PreviewMouseLeftButtonDown  += new MouseButtonEventHandler(tile_MouseLeftDown);
                    tile.PreviewMouseRightButtonDown += new MouseButtonEventHandler(tile_MouseRightDown);

                    TileTag tag = new TileTag
                    {
                        X = x,
                        Y = y
                    };

                    tile.Tag = tag;

                    Grid.SetRow(tile, y);
                    Grid.SetColumn(tile, x);

                    gridTiles.Children.Add(tile);
                }
            }
        }
Esempio n. 2
0
        public TileType GetTileTypeForTag(TileTag tag)
        {
            TileType type = TileType.None;

            if (map != null && map.ContainsKey(tag))
            {
                map.TryGetValue(tag, out type);
            }

            return(type);
        }
Esempio n. 3
0
        void tile_MouseLeftDown(object sender, MouseButtonEventArgs e)
        {
            Button  btn = (Button)sender;
            TileTag tag = (TileTag)btn.Tag;

            btn.Content = CreateImageForResource(selectedTileType.GetImageResourceName());

            bool levelChanged = level.SetTileTypeForTag(tag, selectedTileType);

            if (levelChanged)
            {
                this.progressSaved = false;
                UpdateUnsavedIndicator();
            }
        }
Esempio n. 4
0
        private void PanMap(Direction dir)
        {
            Debug.WriteLine("Panning map in direction: " + dir.ToString());

            foreach (UIElement child in gridTiles.Children)
            {
                if (child is Button)
                {
                    Button  btn = (Button)child;
                    TileTag tag = (TileTag)btn.Tag;

                    tag     = UpdateTileTag(tag, dir);
                    btn.Tag = tag;

                    RenderTile(btn, level.GetTileTypeForTag(tag));
                }
            }
        }
Esempio n. 5
0
        void tile_MouseRightDown(object sender, MouseButtonEventArgs e)
        {
            Button  btn = (Button)sender;
            TileTag tag = (TileTag)btn.Tag;

            if (btn.Content != null)
            {
                ((Image)btn.Content).Visibility = Visibility.Hidden;
            }

            bool levelChanged = level.DeleteTileAtTag(tag);

            if (levelChanged)
            {
                this.progressSaved = false;
                UpdateUnsavedIndicator();
            }
        }
Esempio n. 6
0
        public bool DeleteTileAtTag(TileTag tag)
        {
            if (map != null && map.ContainsKey(tag))
            {
                map.Remove(tag);

                // check if the tile MAY have been on the outskirts of the level, which would require
                // us to re-determine the min/max x and y.
                if (tag.X == MinX || tag.X == MaxX || tag.Y == MinY || tag.Y == MaxY)
                {
                    RecalculateLevelBoundaries();
                }

                return(true);
            }

            return(false);
        }
Esempio n. 7
0
        private void ResetViewport()
        {
            foreach (UIElement child in gridTiles.Children)
            {
                if (child is Button)
                {
                    int x = Grid.GetColumn(child);
                    int y = Grid.GetRow(child);

                    Button  btn = (Button)child;
                    TileTag tag = new TileTag();
                    tag.X   = x;
                    tag.Y   = y;
                    btn.Tag = tag;

                    RenderTile(btn, level.GetTileTypeForTag(tag));
                }
            }
        }
Esempio n. 8
0
        public static Level Load(string path)
        {
            StreamReader file  = new StreamReader(path);
            Level        level = new Level();

            // LINE 1: dimensions
            string[] levelDimens = file.ReadLine().Split(',');
            int      w           = int.Parse(levelDimens[0]);
            int      h           = int.Parse(levelDimens[1]);

            // LINE 2: number of materials
            int numMaterials = int.Parse(file.ReadLine());

            string[] materials = new string[numMaterials];

            // LINE 3--numMaterials: materials
            for (int m = 0; m < numMaterials; m++)
            {
                materials[m] = file.ReadLine();
            }

            // REMAINING LINES: level dimensions
            for (int y = 0; y < h; y++)
            {
                char[] line = file.ReadLine().ToCharArray();
                for (int x = 0; x < w; x++)
                {
                    if (line[x] != ' ')
                    {
                        TileTag tag = new TileTag();
                        tag.X = x;
                        tag.Y = y;

                        level.SetTileTypeForTag(tag, TileTypeMethods.GetTileTypeForChar(line[x]));
                    }
                }
            }

            file.Close();

            return(level);
        }
Esempio n. 9
0
        private TileTag UpdateTileTag(TileTag tag, Direction dir)
        {
            switch (dir)
            {
            case Direction.Up:
                tag.Y--;
                break;

            case Direction.Down:
                tag.Y++;
                break;

            case Direction.Left:
                tag.X--;
                break;

            case Direction.Right:
                tag.X++;
                break;
            }

            return(tag);
        }
Esempio n. 10
0
        public bool SetTileTypeForTag(TileTag tag, TileType type)
        {
            if (map == null)
            {
                map = new Dictionary <TileTag, TileType>();
            }

            bool changed = false;

            if (!map.ContainsKey(tag))
            {
                map.Add(tag, type);
                changed = true;
            }

            // re-calculate min/max
            MinX = Math.Min(MinX, tag.X);
            MinY = Math.Min(MinY, tag.Y);
            MaxX = Math.Max(MaxX, tag.X);
            MaxY = Math.Max(MaxY, tag.Y);

            return(changed);
        }