コード例 #1
0
ファイル: MapEditor.cs プロジェクト: HendrikMennen/Dash2D
 public void cmd(string[] param)
 {
     if (param[0] == "!map") updateMap();
     if (param[0] == "!loadmap")
     {
         Map map = SaveGame.DeserializeMap<Map>(param[1]);
         level.LoadMap(map);
         ChangeLayer(0);
         ChangeTileset(0);
     }
     if (param[0] == "!addtileset")
     {
         AddTileset("maps/tilesets/" + param[1]);
     }
     if (param[0] == "!addlayer")
     {
         AddLayer(bool.Parse(param[1]));
     }
     if (param[0] == "!newmap")
     {
         NewMap(new Map(param[1], int.Parse(param[2]), int.Parse(param[3]), int.Parse(param[4]), int.Parse(param[5])));
     }
     if (param[0] == "!setlayer")
     {
         ChangeLayer(Int32.Parse(param[1]));
     }
     if (param[0] == "!savemap")
     {
         SaveGame.SerializeMap<object>(param[1], (object)level.CurrentMap);
     }
     if (param[0] == "!fill")
     {
         FillLayer(0);
     }
     if (param[0] == "!addtp")
     {
         teleporterparams = new Teleporter(new Rectangle(), int.Parse(param[1]), new Vector2(float.Parse(param[2]), float.Parse(param[3])));
         currentEditorMode = EditorMode.ObjectAdd;
         currentObjectAddmode = ObjectAddMode.Teleporter;
     }
 }
コード例 #2
0
ファイル: MapEditor.cs プロジェクト: HendrikMennen/Dash2D
        public void Update()
        {
            Vector2 mousePos = input.MousePos;
            Vector2 tilesetPos = TilesetView.ScreenToWorld(input.MousePos);
            Vector2 mapPos = input.GetMapPos(input.MousePos);

            if (!tilesetScroll)
            {
                if (input.CurrentMouseState.ScrollWheelValue > input.PreviousMouseState.ScrollWheelValue && input.KeyDown(Keys.LeftControl)) Global.camera.AdjustZoom(0.2f);
                if (input.CurrentMouseState.ScrollWheelValue < input.PreviousMouseState.ScrollWheelValue && input.KeyDown(Keys.LeftControl)) Global.camera.AdjustZoom(-0.2f);
            }

            if (input.KeyPressed(Keys.Z) && input.KeyDown(Keys.LeftControl)) Undo();


            if (input.KeyPressed(Microsoft.Xna.Framework.Input.Keys.C) || input.MouseRightButtonPressed())
            {
                currentItemIDs = new int[0, 0];
                sourceRectangle = new Rectangle();
                currentEditorMode = EditorMode.Default;
            }

            foreach (var button in components)
            {
                button.update(input);
            }

            if (input.KeyPressed(Keys.E))
            {
                currentItemIDs = new int[1, 1] { { 0 } };
                sourceRectangle = new Rectangle();
                currentEditorMode = EditorMode.Rubber;
            }

            if (mousePos.X > TileSelectorField.X && mousePos.X < TileSelectorField.X + TileSelectorField.Width && mousePos.Y > TileSelectorField.Y && mousePos.Y < TileSelectorField.Y + TileSelectorField.Height)
            {
                if (input.CurrentMouseState.ScrollWheelValue > input.PreviousMouseState.ScrollWheelValue && input.KeyDown(Keys.LeftControl)) zoomTileset(0.2f);
                if (input.CurrentMouseState.ScrollWheelValue < input.PreviousMouseState.ScrollWheelValue && input.KeyDown(Keys.LeftControl)) zoomTileset(-0.2f);
                tilesetScroll = true;    
                if (input.ButtonDown(input.CurrentMouseState.LeftButton))
                {
                    currentEditorMode = EditorMode.TileSelector;
                }
            }else
            {
                tilesetScroll = false;
            }
            

            if(currentEditorMode == EditorMode.TileSelector)
            {
                if (mousePos.X > TileSelectorField.X && mousePos.X < TileSelectorField.X + TileSelectorField.Width && mousePos.Y > TileSelectorField.Y && mousePos.Y < TileSelectorField.Y + TileSelectorField.Height)
                {
                    if (input.ButtonDown(input.CurrentMouseState.LeftButton))
                    {
                        if (!Drag)
                        {
                            startID = ((int)tilesetPos.X / level.CurrentMap.TileWidth) + ((int)tilesetPos.Y - TileSelectorField.Y) / level.CurrentMap.TileHeight * (currentTileset.width) + 1;
                            Drag = true;
                        }
                    }
                    if (!input.ButtonDown(input.CurrentMouseState.LeftButton))
                    {
                        if (Drag)
                        {
                            endID = ((int)tilesetPos.X / level.CurrentMap.TileWidth) + ((int)tilesetPos.Y - TileSelectorField.Y) / level.CurrentMap.TileHeight * (currentTileset.width) + 1;


                            Rectangle endid = new Rectangle(level.CurrentMap.getSourceRectangle(endID, currentTileset).X / level.CurrentMap.TileWidth + 1, level.CurrentMap.getSourceRectangle(endID, currentTileset).Y / level.CurrentMap.TileHeight, level.CurrentMap.TileHeight, level.CurrentMap.TileHeight);
                            Rectangle startid = new Rectangle(level.CurrentMap.getSourceRectangle(startID, currentTileset).X / level.CurrentMap.TileWidth + 1, level.CurrentMap.getSourceRectangle(startID, currentTileset).Y / level.CurrentMap.TileHeight, level.CurrentMap.TileHeight, level.CurrentMap.TileHeight);
                            if (startID > endID || startid.X > endid.X || startid.Y > endid.Y) return;
                            currentItemIDs = new int[endid.X - startid.X + 1, endid.Y - startid.Y + 1];
                            for (int y = startid.Y; y <= endid.Y; y++)
                            {
                                for (int x = startid.X; x <= endid.X; x++)
                                {
                                    currentItemIDs[x - startid.X, y - startid.Y] = Tiles[x + y * currentTileset.width]-1;
                                }
                            };
                            sourceRectangle = new Rectangle((startid.X-1) * level.CurrentMap.TileWidth, startid.Y * level.CurrentMap.TileHeight, (endid.X - startid.X) * level.CurrentMap.TileWidth + level.CurrentMap.TileWidth, (endid.Y - startid.Y) * level.CurrentMap.TileHeight + level.CurrentMap.TileHeight);
                            Drag = false;
                            currentEditorMode = EditorMode.Place;
                        }
                    }
                }
            }

            if (input.MousePos.X < TileSelectorBackground.Width) return; // STOP IF CURSOR IS AT OVERLAY

            switch (currentEditorMode)
            {
                case EditorMode.Default:
                   
                    break;

                case EditorMode.Place:
                  
                    if (input.ButtonDown(input.CurrentMouseState.LeftButton))
                    {
                        int mapX = (int)(mapPos.X) / level.CurrentMap.TileWidth;
                        int mapY = (int)(mapPos.Y) / level.CurrentMap.TileHeight;
                        if (mapX < 0 || mapY < 0 || mapX + currentItemIDs.GetLength(0) > level.CurrentMap.MapWidth || mapY + currentItemIDs.GetLength(1) > level.CurrentMap.MapHeight) return;
                        int[,] restoreIDs = new int[currentItemIDs.GetLength(0), currentItemIDs.GetLength(1)];
                        bool change = false;

                        for (int y = mapY; y < mapY + currentItemIDs.GetLength(1); y++)
                        {
                            for (int x = mapX; x < mapX + currentItemIDs.GetLength(0); x++)
                            {
                                if (currentLayer[x + y * level.CurrentMap.MapWidth] != currentItemIDs[x - mapX, y - mapY]) change = true;
                                restoreIDs[x - mapX, y - mapY] = currentLayer[x + y * level.CurrentMap.MapWidth];
                                currentLayer[x + y * level.CurrentMap.MapWidth] = currentItemIDs[x - mapX, y - mapY];
                            }
                        }

                        if (change) undo.Push(new Backup(new Vector2(mapX, mapY), restoreIDs, currentLayerID));

                    }

                    break;

                case EditorMode.Rubber:

                    if (input.ButtonDown(input.CurrentMouseState.LeftButton))
                    {
                        int mapX = (int)(mapPos.X) / level.CurrentMap.TileWidth;
                        int mapY = (int)(mapPos.Y) / level.CurrentMap.TileHeight;
                        if (mapX < 0 || mapY < 0 || mapX + currentItemIDs.GetLength(0) > level.CurrentMap.MapWidth || mapY + currentItemIDs.GetLength(1) > level.CurrentMap.MapHeight) return;
                        int[,] restoreIDs = new int[1, 1];
                        bool change = false;

                        if (currentLayer[mapX + mapY * level.CurrentMap.MapWidth] != 0) change = true;
                        restoreIDs[0, 0] = currentLayer[mapX + mapY * level.CurrentMap.MapWidth];
                        currentLayer[mapX + mapY * level.CurrentMap.MapWidth] = 0;
                                             
                        if (change) undo.Push(new Backup(new Vector2(mapX, mapY), restoreIDs, currentLayerID));
                    }

                    break;
               
                case EditorMode.ObjectAdd:
                    if (input.ButtonDown(input.CurrentMouseState.LeftButton) && !objectdrag)
                    {
                        objectposition.X = (int)mapPos.X;
                        objectposition.Y = (int)mapPos.Y;
                        objectdrag = true;
                    }
                    if (objectdrag)
                    {
                        objectposition.Width = (int)mapPos.X - objectposition.X;
                        objectposition.Height = (int)mapPos.Y - objectposition.Y;
                    }

                    if (!input.ButtonDown(input.CurrentMouseState.LeftButton) && objectdrag)
                    {
                        if (objectposition.X < 0)
                        {
                            objectposition.Width += objectposition.X;
                            objectposition.X = 0;
                        }
                        if (objectposition.Y < 0)
                        {
                            objectposition.Height += objectposition.Y;
                            objectposition.Y = 0;
                        }
                        if (objectposition.Width + objectposition.X > level.CurrentMap.MapWidth * level.CurrentMap.TileWidth) objectposition.Width = level.CurrentMap.MapWidth * level.CurrentMap.TileWidth - objectposition.X;
                        if (objectposition.Height + objectposition.Y > level.CurrentMap.MapHeight * level.CurrentMap.TileHeight) objectposition.Height = level.CurrentMap.MapHeight * level.CurrentMap.TileHeight - objectposition.Y;

                        if (objectposition.Width > 0 && objectposition.Height > 0)
                        {
                            if (currentObjectAddmode == ObjectAddMode.Collision) level.CurrentMap.collisions.Add(objectposition);
                            else if (currentObjectAddmode == ObjectAddMode.Teleporter)
                            {
                                level.CurrentMap.teleporters.Add(new Teleporter(objectposition, teleporterparams.mapID, teleporterparams.tpCoordinates));
                                currentEditorMode = EditorMode.Default;
                            }

                            //ADD COllison or teleporter
                        }
                        objectposition = new Rectangle();
                        objectdrag = false;
                    }
                    break;

                case EditorMode.ObjectEdit:

                    if (input.MouseLeftButtonPressed())
                    {
                        Rectangle obj = new Rectangle();
                        foreach (var coll in level.CurrentMap.collisions)
                        {
                            if (CollisionDetector.Collison(coll, mapPos))
                            {
                                obj = coll;
                                break;
                            }
                        }
                        Furniture fur = null;
                        foreach (var coll in level.CurrentMap.entities)
                        {
                            if (CollisionDetector.Collison(new Rectangle((int)coll.Position.X + coll.clickbox.X, (int)coll.Position.Y + coll.clickbox.Y, coll.clickbox.Width, coll.clickbox.Height), mapPos))
                            {
                                fur = coll;
                                break;
                            }
                        }
                        Teleporter tp = null;
                        foreach (var coll in level.CurrentMap.teleporters)
                        {
                            if (CollisionDetector.Collison(coll.position, mapPos))
                            {
                                tp = coll;
                                break;
                            }
                        }
                        if (obj.Width != 0)
                        {
                            level.CurrentMap.collisions.Remove(obj);
                        }
                        if (fur != null)
                        {
                            level.CurrentMap.entities.Remove(fur);
                            level.entities.Remove(fur);
                        }
                        if (tp != null)
                        {
                            level.CurrentMap.teleporters.Remove(tp);
                        }
                    }
                    break;
            }       
        }