Ejemplo n.º 1
0
        /// <summary>
        /// Metoda pro nahrani vsech map a textur a vsech defaultnich veci
        /// </summary>
        /// <param name="mapName"></param>
        public static void LoadMapAndTextures(string mapName)
        {
            //Nahrani mapy
            #region Tile Sets
            font = GameRef.Content.Load<SpriteFont>(@"Fonts\editorFont");

            Texture2D tilesetTexture = GameRef.Content.Load<Texture2D>(@"Tiles\walkable");
            tilesetTexture.Name = "Walkable";
            Tileset tileset1 = new Tileset(tilesetTexture, 10, 10, 32, 32);
            tilesetTexture = GameRef.Content.Load<Texture2D>(@"Tiles\tileset2");
            tilesetTexture.Name = "tileset2";
            Tileset tileset2 = new Tileset(tilesetTexture, 10, 10, 32, 32);
            List<Tileset> tilesets = new List<Tileset>();
            tilesets.Add(tileset1);
            tilesets.Add(tileset2);

            Texture2D pathSetText = GameRef.Content.Load<Texture2D>(@"Tiles\pathSet");
            pathSetText.Name = "pathSet";
            Tileset pathSet = new Tileset(pathSetText, 5, 5, 32, 32);
            tilesets.Add(pathSet);

            charTexture = GameRef.Content.Load<Texture2D>(@"Sprites\characterSet");
            keyTexture = GameRef.Content.Load<Texture2D>(@"Sprites\keySet");

            #endregion

            Serializer ser = new Serializer();
            MapSerialize ob = ser.DeSerializeMap(mapName);
            Session.MapProps = ob.mapProps;

            #region Back Map

            List<MapLayer> backlayers = new List<MapLayer>();
            backlayers.Add(ob.backLayer);
            backlayers.Add(ob.backSplatter);
            Session.BackMap = new TileMap(tilesets, backlayers, Session.MapProps.BackSizeX, Session.MapProps.BackSizeY, Session.MapProps.BackEngineX, Session.MapProps.BackEngineY);

            #endregion

            #region FrontMap

            List<MapLayer> frontlayers = new List<MapLayer>();
            frontlayers.Add(ob.frontLayer);
            frontlayers.Add(ob.frontSplatter);
            Session.FrontMap = new TileMap(tilesets, frontlayers, Session.MapProps.FrontSizeX, Session.MapProps.FrontSizeY, Session.MapProps.FrontEngineX, Session.MapProps.FrontEngineY);

            #endregion

            Session.FrontMap.mapLayers[0].map[25, 7].obj = 1;
            Session.FrontMap.mapLayers[0].map[17, 25].obj = 2;
            Session.FrontMap.mapLayers[0].map[12, 31].obj = 3;
            Session.FrontMap.mapLayers[0].map[26, 34].obj = 4;
            Session.FrontMap.mapLayers[0].map[49, 17].obj = "The End";

            LoadBases();

            Units = new List<Unit>();
            foreach (Unit item in ob.units)
            {
                Units.Add(baseUnits[item.Name].Clone(item.Notoriety, item.Cell.X, item.Cell.Y));
            }

            Keys = new List<GameKey>();
            foreach (GameKey item in ob.keys)
            {
                Keys.Add(baseKeys[item.index].Clone(item.Cell.X, item.Cell.Y));
            }
        }
        protected override void LoadContent()
        {
            ram = new Rectangle(770, 250, 220, 220);

            //Nacteni fontu, ramecku pro tileset a pozadi
            font = Content.Load<SpriteFont>(@"Fonts\editorFont");
            ramTexture = Content.Load<Texture2D>(@"Pictures\ram");
            background = new BackgroundComponent(GameRef, Content.Load<Texture2D>(@"Backgrounds\editorscreen"), DrawMode.Center);

            #region ovladaci prvky

            spriteFont = Content.Load<SpriteFont>(@"Fonts\menuFont");
            string[] LayermenuItems = { "Back Map", "Back Splatter", "Front Map", "Front Splatter" };
            LayerMenu = new MenuComponent(font, LayermenuItems, false);
            LayerMenu.SetPostion(new Vector2(770, 30));
            LayerMenu.SelectedColor = Color.Yellow;
            LayerMenu.HiliteColor = Color.White;

            spriteFont = Content.Load<SpriteFont>(@"Fonts\menuFont");
            string[] ActionmenuItems = { "Add", "Move", "Pick", "Delete" };
            ActionMenu = new MenuComponent(font, ActionmenuItems, false);
            ActionMenu.SetPostion(new Vector2(900, 30));
            ActionMenu.SelectedColor = Color.Yellow;
            ActionMenu.HiliteColor = Color.White;

            spriteFont = Content.Load<SpriteFont>(@"Fonts\menuFont");

            LinkLabel linkLabel1 = new LinkLabel(spriteFont);
            linkLabel1.Text = "Save";
            linkLabel1.Size = linkLabel1.SpriteFont.MeasureString(linkLabel1.Text);
            linkLabel1.Position = new Vector2(770,720);
            linkLabel1.Selected += new EventHandler(linkLabel1_Selected);
            Controls.Add(linkLabel1);

            LinkLabel linkLabel2 = new LinkLabel(spriteFont);
            linkLabel2.Text = "Load";
            linkLabel2.Size = linkLabel2.SpriteFont.MeasureString(linkLabel2.Text);
            linkLabel2.Position = new Vector2(940,720);
            linkLabel2.Selected += new EventHandler(linkLabel2_Selected);
            Controls.Add(linkLabel2);
            Controls.editor = true;

            label1 = new Label(spriteFont);
            label1.Position = new Vector2(845, 720);
            label1.Visible = false;
            Controls.Add(label1);

            Session.LoadMapAndTextures("map.xml");

            string[] CharacterStrings = new string[Session.baseUnits.Keys.Count];
            Session.baseUnits.Keys.CopyTo(CharacterStrings, 0);

            CharacterMenu = new MenuComponent(font, CharacterStrings, false);
            CharacterMenu.SetPostion(new Vector2(770, 200));
            CharacterMenu.SelectedColor = Color.Yellow;
            CharacterMenu.HiliteColor = Color.White;

            int[] KeyInts = new int[Session.baseKeys.Keys.Count];
            Session.baseKeys.Keys.CopyTo(KeyInts, 0);
            string[] KeyStrings = new string[Session.baseKeys.Keys.Count];
            for (int i = 0; i < KeyInts.Length; i++)
            {
                KeyStrings[i] = KeyInts[i].ToString();
            }

            KeyMenu = new MenuComponent(font, KeyStrings, false);
            KeyMenu.SetPostion(new Vector2(770, 200));
            KeyMenu.SelectedColor = Color.Yellow;
            KeyMenu.HiliteColor = Color.White;

            string[] TileSetmenuItems = new string[Session.FrontMap.tilesets.Count + 2];
            for (int i = 0; i < Session.FrontMap.tilesets.Count; i++)
            {
                TileSetmenuItems[i] = Session.FrontMap.tilesets[i].Texture.Name;  //files[i].Name.Substring(0, files[i].Name.Length - 4);
            }
            TileSetmenuItems[TileSetmenuItems.Length - 2] = "Characters";
            TileSetmenuItems[TileSetmenuItems.Length - 1] = "Keys";

            TileSetMenu = new MenuComponent(font, TileSetmenuItems, false);
            TileSetMenu.SetPostion(new Vector2(770, 100));
            TileSetMenu.SelectedColor = Color.Yellow;
            TileSetMenu.HiliteColor = Color.White;

            #endregion

            //Nastaveni prvniho itemu u LayerMenu
            switch (LayerMenu.clickedIndex)
            {
                case 0:
                    CurrentMap = Session.BackMap;
                    CurrentLayer = Session.BackMap.mapLayers[0];
                    break;
                case 1:
                    CurrentMap = Session.BackMap;
                    CurrentLayer = Session.BackMap.mapLayers[1];
                    break;
                case 2:
                    CurrentMap = Session.FrontMap;
                    CurrentLayer = Session.FrontMap.mapLayers[0];
                    break;
                case 3:
                    CurrentMap = Session.FrontMap;
                    CurrentLayer = Session.FrontMap.mapLayers[1];
                    break;
            }

            //Nastaveni bazoveho policka pro akce
            CurrentTile = new Tile(0, 0, -1, -1);
            CurrentTileSet = Session.FrontMap.tilesets[CurrentTile.Tileset];

            //Nacteni vstupnich ramecku pro obraz TileSetu
            int tiles = CurrentTileSet.TilesWide * CurrentTileSet.TilesHigh;
            sourceRectangles = new Rectangle[tiles];
            float Scale = (float)ram.Width / (float)Session.FrontMap.tilesets[CurrentTile.Tileset].Texture.Width;
            int tile = 0;
            for (int y = 0; y < CurrentTileSet.TilesHigh; y++)
                for (int x = 0; x < CurrentTileSet.TilesWide; x++)
                {
                    sourceRectangles[tile] = new Rectangle(
                    (int)(x * CurrentTileSet.TileWidth * Scale),
                    (int)(y * CurrentTileSet.TileWidth * Scale),
                    (int)(CurrentTileSet.TileWidth * Scale),
                    (int)(CurrentTileSet.TileHeight * Scale));
                    tile++;
                }

            base.LoadContent();
        }
        /// <summary>
        /// Metoda pro stlaceni tlacitka Load.
        /// Nacte mapu a na chvili oznami ze je mapa nactena
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void linkLabel2_Selected(object sender, EventArgs e)
        {
            Session.LoadMapAndTextures("map.xml");

            switch (LayerMenu.clickedIndex)
            {
                case 0:
                    CurrentMap = Session.BackMap;
                    CurrentLayer = Session.BackMap.mapLayers[0];
                    break;
                case 1:
                    CurrentMap = Session.BackMap;
                    CurrentLayer = Session.BackMap.mapLayers[1];
                    break;
                case 2:
                    CurrentMap = Session.FrontMap;
                    CurrentLayer = Session.FrontMap.mapLayers[0];
                    break;
                case 3:
                    CurrentMap = Session.FrontMap;
                    CurrentLayer = Session.FrontMap.mapLayers[1];
                    break;
            }

            lvisible = true;
            label1.Text = "Loaded";
            label1.Visible = true;
        }
        public override void Update(GameTime gameTime)
        {
            if (KeyboardInput.KeyReleased(Keys.Escape))
                manager.PopScreen();

            //Oznamovaci label
            #region Time label

            if (lvisible)
            {
                lvisible = false;
                end = gameTime.TotalGameTime + TimeSpan.FromSeconds(1);
            }
            if (end.TotalMilliseconds < gameTime.TotalGameTime.TotalMilliseconds)
            {
                label1.Visible = false;
            }

            #endregion

            //Osetreni pohybu po mape klavesnici
            #region Posun + camera

            Vector2 motion = new Vector2();

            if (KeyboardInput.KeyDown(Keys.Up))
                motion.Y = -1;
            if (KeyboardInput.KeyDown(Keys.Down))
                motion.Y = 1;
            if (KeyboardInput.KeyDown(Keys.Left))
                motion.X = -1;
            if (KeyboardInput.KeyDown(Keys.Right))
                motion.X = 1;

            Session.Camera.Position += motion * Session.Camera.Speed;
            Session.Camera.LockCamera();

            #endregion

            //Osetreni vsech menicek jejich aktualni hodnoty a nasledne vykresleni tilesetu
            #region Tlacitka + menicka

            if (LayerMenu.isMouseOver() && MouseInput.LeftButtonReleased())
            {
                switch (LayerMenu.SelectedIndex)
                {
                    case 0:
                        LayerMenu.clickedIndex = 0;
                        CurrentMap = Session.BackMap;
                        CurrentLayer = Session.BackMap.mapLayers[0];
                        break;
                    case 1:
                        LayerMenu.clickedIndex = 1;
                        CurrentMap = Session.BackMap;
                        CurrentLayer = Session.BackMap.mapLayers[1];
                        break;
                    case 2:
                        LayerMenu.clickedIndex = 2;
                        CurrentMap = Session.FrontMap;
                        CurrentLayer = Session.FrontMap.mapLayers[0];
                        break;
                    case 3:
                        LayerMenu.clickedIndex = 3;
                        CurrentMap = Session.FrontMap;
                        CurrentLayer = Session.FrontMap.mapLayers[1];
                        break;
                }
            }
            LayerMenu.Update();

            if (ActionMenu.isMouseOver() && MouseInput.LeftButtonReleased() && toMove == null)
            {
                switch (ActionMenu.SelectedIndex)
                {
                    case 0:
                        ActionMenu.clickedIndex = 0;
                        action = Action.Add;
                        break;
                    case 1:
                        ActionMenu.clickedIndex = 1;
                        action = Action.Move;
                        break;
                    case 2:
                        ActionMenu.clickedIndex = 2;
                        action = Action.Pick;
                        break;
                    case 3:
                        ActionMenu.clickedIndex = 3;
                        action = Action.Delete;
                        break;
                }
            }
            ActionMenu.Update();

            if(TileSetMenu.clickedIndex == 3)
            {
                if (CharacterMenu.isMouseOver() && MouseInput.LeftButtonReleased())
                {
                    CharacterMenu.clickedIndex = CharacterMenu.SelectedIndex;
                }
                CharacterMenu.Update();
            }
            if (TileSetMenu.clickedIndex == 4)
            {
                if (KeyMenu.isMouseOver() && MouseInput.LeftButtonReleased())
                {
                    KeyMenu.clickedIndex = KeyMenu.SelectedIndex;
                }
                KeyMenu.Update();
            }

            if (TileSetMenu.isMouseOver() && MouseInput.LeftButtonReleased())
            {
                TileSetMenu.clickedIndex = TileSetMenu.SelectedIndex;
                if (TileSetMenu.clickedIndex < 3)
                {
                    CurrentTile.TileIndex = 0;
                    CurrentTile.Tileset = TileSetMenu.clickedIndex;
                    CurrentTileSet = Session.FrontMap.tilesets[CurrentTile.Tileset];

                    int tiles = CurrentTileSet.TilesWide * CurrentTileSet.TilesHigh;
                    sourceRectangles = new Rectangle[tiles];
                    float Scale = (float)ram.Width / (float)Session.FrontMap.tilesets[CurrentTile.Tileset].Texture.Width;
                    int tile = 0;
                    for (int y = 0; y < CurrentTileSet.TilesHigh; y++)
                        for (int x = 0; x < CurrentTileSet.TilesWide; x++)
                        {
                            sourceRectangles[tile] = new Rectangle(
                            (int)(x * CurrentTileSet.TileWidth * Scale),
                            (int)(y * CurrentTileSet.TileWidth * Scale),
                            (int)(CurrentTileSet.TileWidth * Scale),
                            (int)(CurrentTileSet.TileHeight * Scale));
                            tile++;
                        }
                }
            }
            TileSetMenu.Update();
            Controls.Update(gameTime);

            #endregion

            //Osetreni akci provedenych do mapy
            #region Actions

            switch (action)
            {
                case Action.Delete:
                    {
                        if (MouseInput.MouseOver(Session.ViewportRect.X, Session.ViewportRect.Y, Session.ViewportRect.Width, Session.ViewportRect.Height) && MouseInput.MouseState.LeftButton == ButtonState.Pressed)
                        {
                            if (TileSetMenu.clickedIndex < 3)
                            {
                                Tile tmp = CurrentLayer.GetTile(CurrentMap.MouseToPoint());
                                CurrentLayer.SetTile(tmp.X, tmp.Y, -1, -1, true);
                            }
                            else if (TileSetMenu.clickedIndex == 3 && MouseInput.LeftButtonPressed())
                            {
                                Tile tmp = Session.FrontMap.GetTile(Session.FrontMap.MouseToPoint());
                                Unit u = (Unit)tmp.obj;
                                Session.Units.Remove(u);
                                tmp.obj = null;
                            }
                            else if (TileSetMenu.clickedIndex == 4 && MouseInput.LeftButtonPressed())
                            {
                                Tile tmp = Session.FrontMap.GetTile(Session.FrontMap.MouseToPoint());
                                GameKey u = (GameKey)tmp.obj;
                                Session.Keys.Remove(u);
                                tmp.obj = null;
                            }
                        }

                        break;
                    }
                case Action.Add:
                    {
                        if (MouseInput.MouseOver(Session.ViewportRect.X, Session.ViewportRect.Y, Session.ViewportRect.Width, Session.ViewportRect.Height) && MouseInput.MouseState.LeftButton == ButtonState.Pressed)
                        {
                            if (TileSetMenu.clickedIndex < 3)
                            {
                                Tile tmp = CurrentLayer.GetTile(CurrentMap.MouseToPoint());
                                if (CurrentTile.Tileset == 1)
                                    CurrentLayer.SetTile(tmp.X, tmp.Y, CurrentTile.TileIndex, CurrentTile.Tileset, false);
                                else
                                    CurrentLayer.SetTile(tmp.X, tmp.Y, CurrentTile.TileIndex, CurrentTile.Tileset, true);
                            }
                            else if (TileSetMenu.clickedIndex == 3 && MouseInput.LeftButtonPressed())
                            {
                                Tile tmp = Session.FrontMap.GetTile(Session.FrontMap.MouseToPoint());
                                Session.Units.Add(Session.baseUnits[CharacterMenu.menuItems[CharacterMenu.clickedIndex]].Clone(Notoriety.Enemy, tmp.X, tmp.Y));
                            }
                            else if (TileSetMenu.clickedIndex == 4 && MouseInput.LeftButtonPressed())
                            {
                                Tile tmp = Session.FrontMap.GetTile(Session.FrontMap.MouseToPoint());
                                Session.Keys.Add(Session.baseKeys[KeyMenu.clickedIndex + 1].Clone(tmp.X, tmp.Y));
                            }
                        }
                        break;
                    }
                case Action.Pick:
                    {
                        if (MouseInput.MouseOver(Session.ViewportRect.X, Session.ViewportRect.Y, Session.ViewportRect.Width, Session.ViewportRect.Height) && MouseInput.MouseState.LeftButton == ButtonState.Pressed)
                        {
                            Tile tmp = CurrentLayer.GetTile(CurrentMap.MouseToPoint());
                            CurrentTile = new Tile(tmp.TileIndex, tmp.Tileset, -1, -1);
                            TileSetMenu.clickedIndex = tmp.Tileset;
                        }
                        break;
                    }
                case Action.Move:
                    {
                        if (MouseInput.MouseOver(Session.ViewportRect.X, Session.ViewportRect.Y, Session.ViewportRect.Width, Session.ViewportRect.Height) && MouseInput.LeftButtonPressed())
                        {
                            if (toMove == null)
                            {
                                toMove = CurrentLayer.GetTile(CurrentMap.MouseToPoint());
                            }
                            else
                            {
                                Tile tmp = CurrentLayer.GetTile(CurrentMap.MouseToPoint());
                                if (CurrentTile.Tileset == 1)
                                    CurrentLayer.SetTile(tmp.X, tmp.Y, toMove.TileIndex, toMove.Tileset, false);
                                else
                                    CurrentLayer.SetTile(tmp.X, tmp.Y, CurrentTile.TileIndex, CurrentTile.Tileset, true);
                                CurrentLayer.SetTile(toMove.X, toMove.Y, -1, -1, true);
                                toMove = null;
                            }
                        }
                        break;
                    }
            }

            #endregion

            //Osetreni kliku do tilesetu a nastaveni bazoveho policka pro upravu
            #region Get CurrentTile
            if (MouseInput.MouseOver(ram.X, ram.Y, ram.Width, ram.Height))
            {
                if (MouseInput.LeftButtonReleased())
                {
                    Point p = new Point(MouseInput.MouseState.X, MouseInput.MouseState.Y);
                    p.X -= ram.X;
                    p.Y -= ram.Y;
                    int tiles = CurrentTileSet.TilesWide * CurrentTileSet.TilesHigh;
                    for (int i = 0; i < tiles; i++)
                    {
                        if (sourceRectangles[i].Contains(p))
                        {
                            CurrentTile.TileIndex = i;
                            break;
                        }
                    }
                }
            }

            #endregion

            //Update vsech jednotek a klicu
            Session.UpdateMap(gameTime);
            base.Update(gameTime);
        }