Exemplo n.º 1
0
        // Places, edits, deletes tiles in the world
        public void EditTiles(KeyboardState _keyboardState, MouseState _mouseState, MouseState _oldMouseState, Vector2 _tilePosition)
        {
            //_tilePosition += L1TileTool.TileOffset;
            // Check which panel is active
            // ===========================================================================
            if (activeTool == EditorTools.L1TilePlacer)         // ======= LAYER 1 =======
            {
                // Toggle tile selection menu
                if (l1TileTool.TileSelectionMenuShow != activeToolMenuShow)
                {
                    l1TileTool.TileSelectionMenuShow = activeToolMenuShow;

                    if (activeToolMenuShow)
                    {
                        ignoredMenuAreas = new List <Rectangle>()
                        {
                            l1TileTool.RectangleMenuOpen
                        }
                    }
                    ;
                    else
                    {
                        ignoredMenuAreas = new List <Rectangle>()
                        {
                            l1TileTool.RectangleMenuClosed
                        }
                    };
                }

                // Tile selection
                selectedTileTexture = l1TileTool.SelectedTileTexture;

                // Tile placement
                if (_mouseState.LeftButton == ButtonState.Pressed)
                {
                    listL1Tiles = l1TileTool.PlaceTile(listL1Tiles, _tilePosition);
                }
            }
            else if (activeTool == EditorTools.L2TilePlacer)         // ======= LAYER 2 =======
            {
                // Toggle tile selection menu
                if (l2TileTool.TileSelectionMenuShow != activeToolMenuShow)
                {
                    l2TileTool.TileSelectionMenuShow = activeToolMenuShow;

                    if (activeToolMenuShow)
                    {
                        ignoredMenuAreas = new List <Rectangle>()
                        {
                            l2TileTool.RectangleMenuOpen
                        }
                    }
                    ;
                    else
                    {
                        ignoredMenuAreas = new List <Rectangle>()
                        {
                            l2TileTool.RectangleMenuClosed
                        }
                    };
                }

                // Tile selection
                selectedTileTexture = l2TileTool.SelectedTileTexture;

                // Tile placement
                if (_mouseState.LeftButton == ButtonState.Pressed)
                {
                    listL2Tiles = l2TileTool.PlaceTile(listL2Tiles, _tilePosition);
                }
            }
            else if (activeTool == EditorTools.TileDelete)
            {
                while (_mouseState.LeftButton == ButtonState.Pressed && _oldMouseState.LeftButton == ButtonState.Released)
                {
                    // Tries to remove a tile if found in each list. Throws error on blank tiles.
                    // Removal is done from the highest layer to the lowest in order to not accidentally remove
                    // any tiles underneath.
                    try
                    {
                        listL2Tiles.RemoveAt(listL2Tiles.FindIndex(a => a.TilePosition == mouseMapTilePosition));
                        break;
                    }
                    catch
                    {
                        Console.WriteLine("No tile to delete.");
                    }
                    try
                    {
                        listL1Tiles.RemoveAt(listL1Tiles.FindIndex(a => a.TilePosition == mouseMapTilePosition));
                        break;
                    }
                    catch
                    {
                        Console.WriteLine("No tile to delete.");
                    }
                    break;
                }
            }
            else if (activeTool == EditorTools.TileMover)
            {
                if (_mouseState.LeftButton == ButtonState.Pressed && _oldMouseState.LeftButton == ButtonState.Released)
                {
                    try
                    {
                        selectedTileCoords = listL2Tiles[listL2Tiles.FindIndex(a => a.TilePosition == mouseMapTilePosition)].TilePosition;
                        Console.WriteLine("selected tile: " + selectedTileCoords);
                    }
                    catch
                    {
                        Console.WriteLine("No tile selected.");
                        selectedTileCoords = new Vector2(0.0001f, 0);
                    }
                }

                if (_keyboardState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Up))
                {
                    try
                    {
                        L2Tile tile = listL2Tiles[listL2Tiles.FindIndex(a => a.TilePosition == selectedTileCoords)];
                        listL2Tiles.Insert(listL2Tiles.Count, tile);
                        listL2Tiles.Remove(tile);
                    }
                    catch { }
                }
                else if (_keyboardState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Down))
                {
                    try
                    {
                        L2Tile tile = listL2Tiles[listL2Tiles.FindIndex(a => a.TilePosition == selectedTileCoords)];
                        listL2Tiles.Remove(tile);
                        listL2Tiles.Insert(0, tile);
                    }
                    catch { }
                }
            }
        }
Exemplo n.º 2
0
        private void buttonLoad_Click(object sender, EventArgs e)
        {
            Console.WriteLine("LoadMap called.");
            tempL1Tile = new L1Tile();
            mapData    = new MapData();

            mapData.MapName = mapName;

            using (XmlReader xmlReader = XmlReader.Create(mapFolderPath + mapName + ".map"))
            {
                while (xmlReader.Read())
                {
                    if (xmlReader.IsStartElement())
                    {
                        switch (xmlReader.Name)
                        {
                        case "L1Tile":
                            tempL1Tile   = new L1Tile();
                            tempTileType = tileType.L1Tile;
                            break;

                        case "L2Tile":
                            tempL2Tile   = new L2Tile();
                            tempTileType = tileType.L2Tile;
                            break;

                        case "Type":
                            if (xmlReader.Read())
                            {
                                if (tempTileType == tileType.L1Tile)
                                {
                                    // Loads the tile type
                                    tempL1Tile.TileType = (EditorManager.L1TileType)Enum.Parse(typeof(EditorManager.L1TileType), xmlReader.Value);
                                }
                                else if (tempTileType == tileType.L2Tile)
                                {
                                    // Loads the tile type
                                    tempL2Tile.TileType = (EditorManager.L2TileType)Enum.Parse(typeof(EditorManager.L2TileType), xmlReader.Value);
                                }
                            }
                            break;

                        case "Position":
                            if (xmlReader.Read())
                            {
                                Vector2 tempVector;

                                if (tempTileType == tileType.L1Tile)
                                {
                                    // Parses the tile position from string
                                    int startInd = xmlReader.Value.IndexOf("X:") + 2;
                                    tempVector.X = float.Parse(xmlReader.Value.Substring(
                                                                   startInd,
                                                                   xmlReader.Value.IndexOf(" Y") - startInd));
                                    startInd     = xmlReader.Value.IndexOf("Y:") + 2;
                                    tempVector.Y = float.Parse(xmlReader.Value.Substring(
                                                                   startInd,
                                                                   xmlReader.Value.IndexOf("}") - startInd));
                                    tempL1Tile.TilePosition = tempVector;
                                }
                                else if (tempTileType == tileType.L2Tile)
                                {
                                    // Parses the tile position from string
                                    int startInd = xmlReader.Value.IndexOf("X:") + 2;
                                    tempVector.X = float.Parse(xmlReader.Value.Substring(
                                                                   startInd,
                                                                   xmlReader.Value.IndexOf(" Y") - startInd));
                                    startInd     = xmlReader.Value.IndexOf("Y:") + 2;
                                    tempVector.Y = float.Parse(xmlReader.Value.Substring(
                                                                   startInd,
                                                                   xmlReader.Value.IndexOf("}") - startInd));
                                    tempL2Tile.TilePosition = tempVector;
                                }
                            }
                            break;

                        case "Offset":
                            if (xmlReader.Read())
                            {
                                Vector2 tempVector;

                                if (tempTileType == tileType.L1Tile)
                                {
                                    // Parses the tile offset from string
                                    int startInd = xmlReader.Value.IndexOf("X:") + 2;
                                    tempVector.X = float.Parse(xmlReader.Value.Substring(
                                                                   startInd,
                                                                   xmlReader.Value.IndexOf(" Y") - startInd));
                                    startInd     = xmlReader.Value.IndexOf("Y:") + 2;
                                    tempVector.Y = float.Parse(xmlReader.Value.Substring(
                                                                   startInd,
                                                                   xmlReader.Value.IndexOf("}") - startInd));
                                    tempL1Tile.TileOffset = tempVector;
                                }
                                else if (tempTileType == tileType.L2Tile)
                                {
                                    // Parses the tile offset from string
                                    int startInd = xmlReader.Value.IndexOf("X:") + 2;
                                    tempVector.X = float.Parse(xmlReader.Value.Substring(
                                                                   startInd,
                                                                   xmlReader.Value.IndexOf(" Y") - startInd));
                                    startInd     = xmlReader.Value.IndexOf("Y:") + 2;
                                    tempVector.Y = float.Parse(xmlReader.Value.Substring(
                                                                   startInd,
                                                                   xmlReader.Value.IndexOf("}") - startInd));
                                    tempL2Tile.TileOffset = tempVector;
                                }
                            }
                            break;

                        case "TextureID":
                            if (xmlReader.Read())
                            {
                                if (tempTileType == tileType.L1Tile)
                                {
                                    // Loads the texture index value
                                    tempL1Tile.TextureID = xmlReader.Value;
                                }
                                else if (tempTileType == tileType.L2Tile)
                                {
                                    // Loads the texture index value
                                    tempL2Tile.TextureID = xmlReader.Value;
                                }
                            }

                            // Adds the final temporary tile to the tile list in map data
                            if (tempL1Tile != null && tempTileType == tileType.L1Tile)
                            {
                                mapData.L1Tiles.Add(tempL1Tile);
                            }
                            else if (tempL2Tile != null && tempTileType == tileType.L2Tile)
                            {
                                mapData.L2Tiles.Add(tempL2Tile);
                            }
                            break;

                        case "Tile":
                            break;
                        }
                    }
                }
            }
            Console.WriteLine("Map loaded.");
            this.Close();
        }