Example #1
0
        public EditorState()
        {
            ChosenFile = false;
            ChosenMethod = false;
            LoadFile = false;
            GoMenu = false;
            FileLoadError = false;

            BuildHoriz = true;
            BuildAmount = 1;

            InputString = "";
            Input = new InputManager();
            camera = new Camera();
            AvailableTextures = new Dictionary<string, Texture2D>();
            MapTiles = new List<Sprite>();            
            map = new Map();

            CurrentTexture = 0;
            CurrentDepth = 1.0f;
        }
Example #2
0
        public void Update(GameTime gameTime)
        {
            Input.Update();

            if (!ChosenFile)
            {
                if (!ChosenMethod)
                {
                    if (Input.IsNewKeyDown(Keys.D1))
                    {
                        ChosenMethod = true;
                        LoadFile = true;
                    }
                    else if (Input.IsNewKeyDown(Keys.D2))
                    {
                        ChosenMethod = true;
                        LoadFile = false;
                    }
                }
                else
                {
                    InputString += Input.GetInputString();

                    if (Input.IsNewKeyDown(Keys.Back))
                    {
                        if (InputString.Length > 0)
                        {
                            InputString = InputString.Remove(InputString.Length - 1);
                        }
                    }

                    if (Input.IsNewKeyDown(Keys.Enter))
                    {
                        if (LoadFile)
                        {
                            Stream stream = File.Open("Maps\\" + InputString + ".entm", FileMode.Open);
                            BinaryFormatter bFormatter = new BinaryFormatter();
                            map = (Map)bFormatter.Deserialize(stream);
                            stream.Close();
                             

                            //Build list...
                            for (int x = 0; x < map.MapTiles.Count; x++)
                            {
                                Sprite sp = new Sprite();
                                sp.Initialize(AvailableTextures[map.MapTiles[x].TextureName], map.MapTiles[x].Position, map.MapTiles[x].Depth);
                                MapTiles.Add(sp);
                            }
                        }

                        ChosenFile = true;
                    }

                }
            }
            else
            {

                //Editor logic here.

                KeyboardState currentKeyboard = Input.GetCurrentKeyboardState();

                if (currentKeyboard.IsKeyDown(Keys.W))
                {
                    camera.Move(new Vector2(0.0f, -10.0f));
                }
                if (currentKeyboard.IsKeyDown(Keys.S))
                {
                    camera.Move(new Vector2(0.0f, 10.0f));
                }

                if (currentKeyboard.IsKeyDown(Keys.A))
                {
                    camera.Move(new Vector2(-10.0f, 0.0f));
                }
                if (currentKeyboard.IsKeyDown(Keys.D))
                {
                    camera.Move(new Vector2(10.0f, 0.0f));
                }
                
                if(currentKeyboard.IsKeyDown(Keys.R))
                {
                    camera.SetZoom(1.0f);
                }

                if (Input.GetCurrentMouseState().ScrollWheelValue < Input.GetPreviousMouseState().ScrollWheelValue)
                {
                    float zoom = camera.GetZoom() - 0.2f;
                    camera.SetZoom(zoom);
                }
                else if (Input.GetCurrentMouseState().ScrollWheelValue > Input.GetPreviousMouseState().ScrollWheelValue)
                {
                    float zoom = camera.GetZoom() + 0.2f;
                    camera.SetZoom(zoom);
                }

                if(Input.IsNewKeyDown(Keys.PageUp))
                {
                    if (CurrentTexture != AvailableTextures.Keys.Count - 1)
                    {
                        CurrentTexture++;
                    }
                }
                else if (Input.IsNewKeyDown(Keys.PageDown))
                {
                    if (CurrentTexture != 0)
                    {
                        CurrentTexture--;
                    }
                }

                if (Input.IsNewKeyDown(Keys.Add))
                {
                    CurrentDepth += 0.1f;

                    if (CurrentDepth > 1.0f)
                        CurrentDepth = 1.0f;
                }
                else if (Input.IsNewKeyDown(Keys.Subtract))
                {
                    CurrentDepth -= 0.1f;

                    if (CurrentDepth < 0.1f)
                        CurrentDepth = 0.1f;
                }

                if(Input.IsNewKeyDown(Keys.Z))
                {
                    if (MapTiles.Count > 0)
                    {
  
                            MapTiles.RemoveAt(MapTiles.Count - 1);
                    }
                }

                if (Input.IsNewKeyDown(Keys.Multiply))
                {
                    BuildAmount++;
                }
                else if (Input.IsNewKeyDown(Keys.Divide))
                {
                    BuildAmount--;

                    if (BuildAmount < 0)
                        BuildAmount = 0;
                }

                if (Input.IsNewKeyDown(Keys.B))
                    BuildHoriz = BuildHoriz ? false : true;

                if (Input.IsNewKeyDown(Keys.H))
                    Hide = Hide ? false : true;

                if (Input.GetCurrentMouseState().RightButton == ButtonState.Pressed)
                {
                    if (SelectingIndex == -1)
                    {
                        //Get mouse pos
                        Vector2 mousePos = new Vector2(Input.GetCurrentMouseState().X, Input.GetCurrentMouseState().Y);
                        mousePos = Vector2.Transform(mousePos, Matrix.Invert(camera.GetTransform()));

                        for (int v = 0; v < MapTiles.Count; v++)
                        {
                            Sprite tile = MapTiles[v];
                            if (tile.GetDepth() == CurrentDepth)
                            {
                                //Transform position to same coordinates as mouse
                                Vector2 pos = tile.GetPosition();
                                if (mousePos.X >= pos.X && mousePos.X <= (pos.X + tile.SpriteTexture.Width))
                                {
                                    if (mousePos.Y >= pos.Y && mousePos.Y <= (pos.Y + tile.SpriteTexture.Height))
                                    {
                                        SelectingIndex = v;
                                        break;
                                    }
                                }

                            }
                        }
                    }
                    else
                    {
                        Sprite st = MapTiles[SelectingIndex];
                        float texWidth = (st.SpriteTexture.Width / 2) * camera.GetZoom();
                        float texHeight = (st.SpriteTexture.Height / 2) * camera.GetZoom();

                        Vector2 loc = new Vector2(Input.GetCurrentMouseState().X - texWidth, Input.GetCurrentMouseState().Y - texHeight);
                        loc = Vector2.Transform(loc, Matrix.Invert(camera.GetTransform()));
                        st.SetPosition(loc);

                        if (Input.IsNewKeyDown(Keys.Delete))
                        {
                            MapTiles.RemoveAt(SelectingIndex);
                            SelectingIndex = -1;
                        }
                    }
                }
                else if(Input.GetCurrentMouseState().RightButton == ButtonState.Released)
                {
                    SelectingIndex = -1;
                }
 

                if (Input.IsNewLeftMouseDown() && !Hide)
                {
                    for (int x = 0; x < BuildAmount; x++)
                    {
                        Sprite ns = new Sprite();
                        ns.Initialize(AvailableTextures.ElementAt(CurrentTexture).Value, new Vector2(0, 0), CurrentDepth);

                        float texWidth = (ns.SpriteTexture.Width / 2) * camera.GetZoom();
                        float texHeight = (ns.SpriteTexture.Height / 2) * camera.GetZoom();

                        Vector2 loc = new Vector2(Input.GetCurrentMouseState().X - texWidth, Input.GetCurrentMouseState().Y - texHeight);
                        if (BuildHoriz)
                        {
                            loc.X = loc.X + (texWidth * x);
                        }
                        else
                        {
                            loc.Y += texHeight * x;
                        }

                        loc = Vector2.Transform(loc, Matrix.Invert(camera.GetTransform()));
                        ns.SetPosition(loc);
                        MapTiles.Add(ns);
                    }
                }

                if (Input.IsNewKeyDown(Keys.J))
                {
                    Map buildMap = new Map();
                    buildMap.NameOfMap = InputString;
                    
                    for(int z = 0; z < MapTiles.Count; z++)
                    {
                        MapTile tile = new MapTile();
                        tile.Position = MapTiles[z].GetPosition();
                        tile.TextureName = MapTiles[z].SpriteTexture.Name.ToString();
                        tile.Depth = MapTiles[z].GetDepth();

                        buildMap.MapTiles.Add(tile);
                    }

                    Stream stream = File.Open("Maps\\" + InputString + ".entm", FileMode.Create);
                    BinaryFormatter bFormatter = new BinaryFormatter();
                    bFormatter.Serialize(stream, buildMap);
                    stream.Close();
                }
            }
        }