public void setGuiControls(GameItem item)
 {
     itemPolygonType.SelectedItem = item.polygonType.ToString();
     itemTextureText.Text = item.name;
     itemWeightBox.Text = item.weight.ToString();
     itemRadiusText.Text = item.polygonType == PhysicsPolygonType.Circle ? item.radius.ToString() :
         item.sideLengths.X.ToString() + "," + item.sideLengths.Y.ToString();
     itemRotationTextBox.Text = item.rotation.ToString();
     itemDepthSlider.Value = (int)item.startdepth;
     itemWidthSlider.Value = (int)item.width;
     itemImmovableBox.Checked = item.immovable;
 }
 private void itemSpawnButton_Click(object sender, EventArgs e)
 {
     try
     {
         GameItem item = new GameItem();
         item.name = this.itemTextureText.Text;
         item.polygonType = (PhysicsPolygonType)Enum.Parse(typeof(PhysicsPolygonType), this.itemPolygonType.Text);
         if (item.polygonType == PhysicsPolygonType.Circle)
             item.radius = uint.Parse(this.itemRadiusText.Text);
         else
         {
             String[] sideLengths = this.itemRadiusText.Text.Split(',');
             item.sideLengths = new Vector2(int.Parse(sideLengths[0]), int.Parse(sideLengths[1]));
         }
         item.immovable = this.itemImmovableBox.Checked;
         item.startdepth = (uint)this.itemDepthSlider.Value;
         item.weight = uint.Parse(this.itemWeightBox.Text);
         item.width = (uint)this.itemWidthSlider.Value;
         item.rotation = float.Parse(this.itemRotationTextBox.Text);
         item.loc = gameref.currentLocation;
         gameref.CurrentLevel.items.Add(item);
     }
     catch (Exception exceptionSpawnItem)
     {
         MessageBox.Show("Exception from game item:\n" + exceptionSpawnItem.StackTrace);
     }
 }
Example #3
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)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                this.Exit();
            }
            switch (currentState)
            {
                #region Leveleditor
            case Enums.State.Leveleditor:
                Vector2 currentLocation = new Vector2(Mouse.GetState().X + offset.X, resolution.ScreenHeight - (Mouse.GetState().Y + offset.Y));
                #region Moving gameitem
                if (ButtonState.Pressed == Mouse.GetState().LeftButton&&
                    ButtonState.Pressed != previousMouseState.LeftButton &&
                    Mouse.GetState().X > 0 && Mouse.GetState().Y > 0)
                {
                    this.currentLocation = currentLocation;
                    control.updateCurrentPositionLabel();
                    if (Keyboard.GetState().IsKeyDown(Keys.LeftControl))
                    {
                        movingItem = CurrentLevel.getGameItemAtLocation(currentLocation.X, currentLocation.Y);
                    }
                    else if (Keyboard.GetState().IsKeyDown(Keys.LeftAlt))
                    {
                        movingSpawn = CurrentLevel.getSpawnAtLocation(currentLocation.X, currentLocation.Y, this);
                    }
                    else if (Keyboard.GetState().IsKeyDown(Keys.LeftShift))
                    {
                        BackgroundItemStruct?str = CurrentLevel.getBackgroundItemAtLocation(currentLocation.X, currentLocation.Y, this);
                        if (str.HasValue)
                        {
                            movingBackgroundItem = str.Value;
                        }
                    }
                }
                if (ButtonState.Pressed == Mouse.GetState().LeftButton&&
                    Mouse.GetState().X > 0 && Mouse.GetState().Y > 0)
                {
                    if (movingItem != null)
                    {
                        movingItem.loc = currentLocation;
                    }
                    if (movingSpawn != null)
                    {
                        movingSpawn.loc = currentLocation;
                    }
                    movingBackgroundItem.location = currentLocation;
                }
                if (ButtonState.Pressed != Mouse.GetState().LeftButton&&
                    ButtonState.Pressed == previousMouseState.LeftButton &&
                    Mouse.GetState().X > 0 && Mouse.GetState().Y > 0)
                {
                    movingItem  = null;
                    movingSpawn = null;
                }
                #endregion
                #region Remove level item
                if (ButtonState.Pressed == Mouse.GetState().RightButton&&
                    ButtonState.Pressed != previousMouseState.RightButton &&
                    Mouse.GetState().X > 0 && Mouse.GetState().Y > 0)
                {
                    if (Keyboard.GetState().IsKeyDown(Keys.LeftControl))
                    {
                        GameItem item = CurrentLevel.getGameItemAtLocation(currentLocation.X, currentLocation.Y);
                        if (item != null)
                        {
                            CurrentLevel.items.Remove(item);
                            control.setGuiControls(item);
                        }
                    }
                    if (Keyboard.GetState().IsKeyDown(Keys.LeftAlt))
                    {
                        SpawnPoint spawn = CurrentLevel.getSpawnAtLocation(currentLocation.X, currentLocation.Y, this);
                        if (spawn != null)
                        {
                            CurrentLevel.spawns.Remove(spawn);
                            control.setGuiControls(spawn);
                        }
                    }
                    if (Keyboard.GetState().IsKeyDown(Keys.LeftShift))
                    {
                        BackgroundItemStruct?str = CurrentLevel.getBackgroundItemAtLocation(currentLocation.X, currentLocation.Y, this);
                        if (str.HasValue)
                        {
                            CurrentLevel.backgroundItems.Remove(str.Value);
                            control.setGuiControls(str.Value);
                        }
                    }
                }
                #endregion
                break;

                #endregion
                #region Worldeditor
            case Enums.State.Worldeditor:
                //if clicked on a level, choose that, otherwise make a new one
                Vector2 point = new Vector2(Mouse.GetState().X, Mouse.GetState().Y);
                int     indexOfClosestLevel = -1;
                float   distance            = float.MaxValue;
                foreach (Level level in levels)
                {
                    float newdistance = Vector2.Distance(level.loc, point);
                    if (newdistance < distance)
                    {
                        distance            = newdistance;
                        indexOfClosestLevel = level.number;
                    }
                }
                if (ButtonState.Pressed == Mouse.GetState().LeftButton&&
                    ButtonState.Pressed != previousMouseState.LeftButton &&
                    Mouse.GetState().X > 0 && Mouse.GetState().Y > 0)
                {
                    if (distance < DISTANCE_FOR_SELECTION)
                    {
                        control.setLevelInfo(levels[indexOfClosestLevel].loc.X, levels[indexOfClosestLevel].loc.Y,
                                             levels[indexOfClosestLevel].adjacent, levels[indexOfClosestLevel].prereq,
                                             levels[indexOfClosestLevel].name, levels[indexOfClosestLevel].number,
                                             levels[indexOfClosestLevel].autoProgress);
                        if (Keyboard.GetState().IsKeyDown(Keys.LeftControl))
                        {
                            levels.RemoveAt(indexOfClosestLevel);
                            foreach (Level level in levels)
                            {
                                if (level.number >= indexOfClosestLevel)
                                {
                                    --level.number;
                                }
                                for (int i = 0; i < level.adjacent.Count; i++)
                                {
                                    if (level.adjacent[i] == indexOfClosestLevel)
                                    {
                                        level.adjacent.RemoveAt(i);
                                    }
                                }
                            }
                            selectedLevelIndex = 0;
                        }
                        else if (selectedLevelIndex == indexOfClosestLevel)
                        {
                            switchState();
                        }
                        else
                        {
                            selectedLevelIndex = indexOfClosestLevel;
                        }
                    }
                    else
                    {
                        addLevel();
                    }
                }
                break;
                #endregion
            }
            previousMouseState = Mouse.GetState();
            base.Update(gameTime);
        }
 public static GameItem fromXML(XmlElement itemnode)
 {
     GameItem gameItem = new GameItem();
     gameItem.copyValues(itemnode);
     return gameItem;
 }
 /// <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)
 {
     if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
         this.Exit();
     switch (currentState)
     {
         #region Leveleditor
         case Enums.State.Leveleditor:
             Vector2 currentLocation = new Vector2(Mouse.GetState().X + offset.X, resolution.ScreenHeight - (Mouse.GetState().Y + offset.Y));
             #region Moving gameitem
             if (ButtonState.Pressed == Mouse.GetState().LeftButton &&
                 ButtonState.Pressed != previousMouseState.LeftButton &&
                 Mouse.GetState().X > 0 && Mouse.GetState().Y > 0)
             {
                 this.currentLocation = currentLocation;
                 control.updateCurrentPositionLabel();
                 if (Keyboard.GetState().IsKeyDown(Keys.LeftControl))
                     movingItem = CurrentLevel.getGameItemAtLocation(currentLocation.X, currentLocation.Y);
                 else if (Keyboard.GetState().IsKeyDown(Keys.LeftAlt))
                     movingSpawn = CurrentLevel.getSpawnAtLocation(currentLocation.X, currentLocation.Y, this);
                 else if (Keyboard.GetState().IsKeyDown(Keys.LeftShift))
                 {
                     BackgroundItemStruct? str = CurrentLevel.getBackgroundItemAtLocation(currentLocation.X, currentLocation.Y, this);
                     if(str.HasValue)
                         movingBackgroundItem = str.Value;
                 }
             }
             if (ButtonState.Pressed == Mouse.GetState().LeftButton &&
                 Mouse.GetState().X > 0 && Mouse.GetState().Y > 0)
             {
                 if (movingItem != null)             movingItem.loc = currentLocation;
                 if (movingSpawn != null)            movingSpawn.loc = currentLocation;
                 movingBackgroundItem.location = currentLocation;
             }
             if (ButtonState.Pressed != Mouse.GetState().LeftButton &&
                 ButtonState.Pressed == previousMouseState.LeftButton &&
                 Mouse.GetState().X > 0 && Mouse.GetState().Y > 0)
             {
                 movingItem = null;
                 movingSpawn = null;
             }
             #endregion
             #region Remove level item
             if (ButtonState.Pressed == Mouse.GetState().RightButton &&
                 ButtonState.Pressed != previousMouseState.RightButton &&
                 Mouse.GetState().X > 0 && Mouse.GetState().Y > 0)
             {
                 if (Keyboard.GetState().IsKeyDown(Keys.LeftControl))
                 {
                     GameItem item = CurrentLevel.getGameItemAtLocation(currentLocation.X, currentLocation.Y);
                     if (item != null)
                     {
                         CurrentLevel.items.Remove(item);
                         control.setGuiControls(item);
                     }
                 }
                 if (Keyboard.GetState().IsKeyDown(Keys.LeftAlt))
                 {
                     SpawnPoint spawn = CurrentLevel.getSpawnAtLocation(currentLocation.X, currentLocation.Y, this);
                     if (spawn != null)
                     {
                         CurrentLevel.spawns.Remove(spawn);
                         control.setGuiControls(spawn);
                     }
                 }
                 if (Keyboard.GetState().IsKeyDown(Keys.LeftShift))
                 {
                     BackgroundItemStruct? str = CurrentLevel.getBackgroundItemAtLocation(currentLocation.X, currentLocation.Y, this);
                     if (str.HasValue)
                     {
                         CurrentLevel.backgroundItems.Remove(str.Value);
                         control.setGuiControls(str.Value);
                     }
                 }
             }
             #endregion
             break;
         #endregion
         #region Worldeditor
         case Enums.State.Worldeditor:
             //if clicked on a level, choose that, otherwise make a new one
             Vector2 point = new Vector2(Mouse.GetState().X, Mouse.GetState().Y);
             int indexOfClosestLevel = -1;
             float distance = float.MaxValue;
             foreach (Level level in levels)
             {
                 float newdistance = Vector2.Distance(level.loc, point);
                 if (newdistance < distance)
                 {
                     distance = newdistance;
                     indexOfClosestLevel = level.number;
                 }
             }
             if (ButtonState.Pressed == Mouse.GetState().LeftButton &&
                 ButtonState.Pressed != previousMouseState.LeftButton &&
                 Mouse.GetState().X > 0 && Mouse.GetState().Y > 0)
             {
                 if (distance < DISTANCE_FOR_SELECTION)
                 {
                     control.setLevelInfo(levels[indexOfClosestLevel].loc.X, levels[indexOfClosestLevel].loc.Y,
                         levels[indexOfClosestLevel].adjacent, levels[indexOfClosestLevel].prereq,
                         levels[indexOfClosestLevel].name, levels[indexOfClosestLevel].number,
                         levels[indexOfClosestLevel].autoProgress);
                     if (Keyboard.GetState().IsKeyDown(Keys.LeftControl))
                     {
                         levels.RemoveAt(indexOfClosestLevel);
                         foreach (Level level in levels)
                         {
                             if (level.number >= indexOfClosestLevel)
                                 --level.number;
                             for (int i = 0; i < level.adjacent.Count; i++)
                                 if (level.adjacent[i] == indexOfClosestLevel)
                                     level.adjacent.RemoveAt(i);
                         }
                         selectedLevelIndex = 0;
                     }
                     else if (selectedLevelIndex == indexOfClosestLevel)
                         switchState();
                     else
                         selectedLevelIndex = indexOfClosestLevel;
                 }
                 else
                     addLevel();
             }
             break;
         #endregion
     }
     previousMouseState = Mouse.GetState();
     base.Update(gameTime);
 }