Beispiel #1
0
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            mousePosition  = new Vector2(ms.X, ms.Y);
            aKeyboardState = Keyboard.GetState();
            if (aKeyboardState.IsKeyDown(Keys.Escape))
            {
                this.Exit();
            }
            ms = Mouse.GetState();
            if (ms.LeftButton == ButtonState.Pressed && ms.LeftButton != previousMS.LeftButton)
            {
                if (!objectSelected)
                {
                    foreach (LevelObject tmpObject in baseObjectList)
                    {
                        if (tmpObject.Rectangle.Contains(new Point(ms.X, ms.Y)))
                        {
                            cursorTexture            = tmpObject.Texture;
                            selectedObject           = new LevelObject();
                            selectedObject.Texture   = tmpObject.Texture;
                            selectedObject.Position  = tmpObject.Position;
                            selectedObject.Origin    = tmpObject.Origin;
                            selectedObject.Bodytype  = tmpObject.Bodytype;
                            selectedObject.Rectangle = tmpObject.Rectangle;
                            selectedObject.Rotation  = tmpObject.Rotation;
                            selectedObject.Name      = tmpObject.Name;

                            objectSelected = true;
                        }
                    }
                    foreach (LevelObject tmpObject in levelObjectList)
                    {
                        if (tmpObject.Rectangle.Contains(new Point(ms.X, ms.Y)))
                        {
                            if (tmpObject.Bodytype == FarseerPhysics.Dynamics.BodyType.Dynamic)
                            {
                                tmpObject.Bodytype = FarseerPhysics.Dynamics.BodyType.Static;
                            }
                            else
                            {
                                tmpObject.Bodytype = FarseerPhysics.Dynamics.BodyType.Dynamic;
                            }
                        }
                    }
                }
                else
                {
                    levelObjectList.Add(selectedObject);
                    objectSelected = false;
                    cursorTexture  = originalCursorTexture;
                }
            }
            else if (ms.RightButton == ButtonState.Pressed && ms.RightButton != previousMS.RightButton)
            {
                foreach (LevelObject tmpObject in levelObjectList)
                {
                    if (tmpObject.Rectangle.Contains(new Point(ms.X, ms.Y)))
                    {
                        levelObjectList.Remove(tmpObject);
                        break;
                    }
                }
            }
            else if (ms.ScrollWheelValue != prevScrollWheelValue)
            {
                if (objectSelected && aKeyboardState.IsKeyUp(Keys.LeftControl))
                {
                    selectedObject.Rotation += ms.ScrollWheelValue - prevScrollWheelValue;
                }
                else if (objectSelected && aKeyboardState.IsKeyDown(Keys.LeftControl))
                {
                    selectedObject.Rotation += ((float)(ms.ScrollWheelValue - prevScrollWheelValue) / 700);
                    Console.WriteLine(selectedObject.Rotation.ToString());
                }
                prevScrollWheelValue = ms.ScrollWheelValue;
            }
            else if (aKeyboardState.IsKeyDown(Keys.Q) && aKeyboardState != previousKeyboardState)
            {
                selectedObject.Rotation -= (float)((5.0f / 180.0f) * float.Parse(Math.PI.ToString()));
            }
            else if (aKeyboardState.IsKeyDown(Keys.E) && aKeyboardState != previousKeyboardState)
            {
                selectedObject.Rotation += (float)((5.0f / 180.0f) * float.Parse(Math.PI.ToString()));
            }
            else if (aKeyboardState.IsKeyDown(Keys.LeftControl) && aKeyboardState.IsKeyDown(Keys.S))
            {
                svDialog.ShowDialog();
                if (svDialog.FileName != "")
                {
                    TextWriter tw = new StreamWriter(svDialog.FileName);
                    foreach (LevelObject theObject in levelObjectList)
                    {
                        string currLine = "";
                        currLine  = theObject.getShapeFromTextureName() + ",";
                        currLine += theObject.Name + ",";

                        if (theObject.Bodytype == FarseerPhysics.Dynamics.BodyType.Dynamic)
                        {
                            currLine += "dynamic,";
                        }
                        else
                        {
                            currLine += "static,";
                        }
                        currLine += ConvertUnits.ToSimUnits(theObject.Position.X).ToString().Replace(',', '.') + "," + ConvertUnits.ToSimUnits(theObject.Position.Y).ToString().Replace(',', '.') + "," + theObject.Rotation.ToString().Replace(',', '.');
                        tw.WriteLine(currLine);
                    }
                    tw.Close();
                }
            }

            if (objectSelected)
            {
                selectedObject.Position  = new Vector2(ms.X, ms.Y);
                selectedObject.Rectangle = new Rectangle(ms.X, ms.Y, selectedObject.Texture.Width, selectedObject.Texture.Height);
            }
            prevScrollWheelValue  = ms.ScrollWheelValue;
            previousMS            = ms;
            previousKeyboardState = aKeyboardState;
            base.Update(gameTime);
        }