public void DrawIcon(GameObjectsEnum gameObject, GameTime gameTime, SpriteBatch spriteBatch, int x, int y) { switch (gameObject) { case GameObjectsEnum.Ground: Ground.DrawIcon(gameTime, spriteBatch, new Rectangle(x, y, (int)blockWidth, (int)blockWidth)); break; case GameObjectsEnum.fallingPlatform: FallingPlatform.DrawIcon(gameTime, spriteBatch, new Rectangle(x, y, (int)blockWidth, (int)blockWidth)); break; case GameObjectsEnum.risingPlatform: RisingPlatform.DrawIcon(gameTime, spriteBatch, new Rectangle(x, y, (int)blockWidth, (int)blockWidth)); break; case GameObjectsEnum.Arrows: Arrows.DrawIcon(gameTime, spriteBatch, new Rectangle(x, y, (int)blockWidth, (int)blockWidth)); break; case GameObjectsEnum.Player: Player.DrawIcon(gameTime, spriteBatch, new Rectangle(x, y, (int)blockWidth, (int)blockWidth)); break; case GameObjectsEnum.Spikes: Spikes.DrawIcon(gameTime, spriteBatch, new Rectangle(x, y, (int)blockWidth, (int)blockWidth)); break; case GameObjectsEnum.Block: Block.DrawIcon(gameTime, spriteBatch, new Rectangle(x, y, (int)blockWidth, (int)blockWidth)); break; case GameObjectsEnum.Button: Button.DrawIcon(gameTime, spriteBatch, new Rectangle(x, y, (int)blockWidth, (int)blockWidth), blockWidth); break; case GameObjectsEnum.FloatingText: FloatingText.DrawIcon(gameTime, spriteBatch, new Rectangle(x, y, (int)blockWidth, (int)blockWidth)); break; case GameObjectsEnum.Goal: Goal.DrawIcon(gameTime, spriteBatch, new Rectangle(x, y, (int)blockWidth, (int)blockWidth)); break; case GameObjectsEnum.Arrow2: Arrow2.DrawIcon(gameTime, spriteBatch, new Rectangle(x, y, (int)blockWidth, (int)blockWidth)); break; } }
public override void Update(GameTime gameTime) { keyi = key; key = Keyboard.GetState(); mousei = mouse; mouse = Mouse.GetState(); int x = (int)((mouse.X + camera.X) / blockWidth); if (x < 0) { x = 0; } int y = (int)-((int)(camera.Y + mouse.Y) / blockWidth) + 1; if (y < 0) { y = 0; } //left click if (mouse.LeftButton == ButtonState.Pressed) { //pick block int blockX = mouse.X / (int)blockWidth; int selectedBlock = blockX + (objectRow * objectsPerRow); if (mouse.Y < blockWidth && blockX < objectsPerRow && selectedBlock < Enum.GetNames(typeof(GameObjectsEnum)).Count()) { if (mousei.LeftButton == ButtonState.Released) { current = (GameObjectsEnum)selectedBlock; state = State.Draw; } } //select tool else if (mouse.Y < blockWidth * 3f / 2 && mouse.X < font.MeasureString("Select").X / 100 * blockWidth / 4) { state = State.Select; } else { //select block if (state == State.Select && mousei.LeftButton == ButtonState.Released) { if (level.CheckForObject(x, y)) { int layer = level.LevelObjects[x][y].Count() - 1; //if you are selecting a block that is already selected, cycle through the blocks at that location int index = level.LevelObjects[x][y].IndexOf(currentSelection); if (index != -1) { if (index > 0) { layer = index - 1; } } currentSelection = level.LevelObjects[x][y][layer]; currentSelectionX = x; currentSelectionY = y; currentSelectionLayer = layer; } else { currentSelection = null; } } //draw else if (state == State.Draw) { //draw block if there isn't a block there if (!level.CheckForObject(x, y)) { AddTile(current, x, y); } //draw a block if you are holding left ctrl and (left click wasn't down last frame or the mouse moved to a different block) if (key.IsKeyDown(Keys.LeftControl) && (mousei.LeftButton == ButtonState.Released || x != previousX || y != previousY)) { AddTile(current, x, y); } } } } //pick select tool if (state != State.Typeing && key.IsKeyDown(Keys.E) && keyi.IsKeyUp(Keys.E)) { state = State.Select; } //delet blocks if right click wasn't held for the previous frame and the mouse didn't move to a different block if (mouse.RightButton == ButtonState.Pressed && (mousei.RightButton == ButtonState.Released || x != previousX || y != previousY)) { level.RemoveGameObject(x, y); } //move camera if (state != State.Typeing) { float cameraSpeed = blockWidth; if (key.IsKeyDown(Keys.D)) { camera.X += cameraSpeed; } if (key.IsKeyDown(Keys.A)) { camera.X -= cameraSpeed; } if (key.IsKeyDown(Keys.W)) { camera.Y -= cameraSpeed; } if (key.IsKeyDown(Keys.S)) { camera.Y += cameraSpeed; } if (camera.X < 0) { camera.X = 0; } if (camera.Y > -graphicsDevice.Viewport.Height) { camera.Y = -graphicsDevice.Viewport.Height; } } //change data value if (state == State.Select && currentSelection != null) { if (key.IsKeyDown(Keys.OemComma) && keyi.IsKeyUp(Keys.OemComma)) { currentSelection.PreviousDataValue(); } if (key.IsKeyDown(Keys.OemPeriod) && keyi.IsKeyUp(Keys.OemPeriod)) { currentSelection.NextDataValue(); } } //move selected item try { if (currentSelection != null) { if (state == State.Select && mouse.LeftButton == ButtonState.Pressed && (x != previousX || y != previousY)) { if (MoveTile(currentSelection, currentSelectionX, currentSelectionY, x, y)) { currentSelectionX = x; currentSelectionY = y; } } if (key.IsKeyDown(Keys.Left) && keyi.IsKeyUp(Keys.Left)) { if (MoveTile(currentSelection, currentSelectionX, currentSelectionY, currentSelectionX - 1, currentSelectionY)) { currentSelectionX--; } } if (key.IsKeyDown(Keys.Right) && keyi.IsKeyUp(Keys.Right)) { if (MoveTile(currentSelection, currentSelectionX, currentSelectionY, currentSelectionX + 1, currentSelectionY)) { currentSelectionX++; } } if (key.IsKeyDown(Keys.Up) && keyi.IsKeyUp(Keys.Up)) { if (MoveTile(currentSelection, currentSelectionX, currentSelectionY, currentSelectionX, currentSelectionY + 1)) { currentSelectionY++; } } if (key.IsKeyDown(Keys.Down) && keyi.IsKeyUp(Keys.Down)) { if (MoveTile(currentSelection, currentSelectionX, currentSelectionY, currentSelectionX, currentSelectionY - 1)) { currentSelectionY--; } } } } catch (Exception) { } //If selecting floating text, edit text if (currentSelection is FloatingText) { FloatingText floatingText = (FloatingText)currentSelection; if (state == State.Typeing) { Keys currentKey; int i = 0; do { if (i >= key.GetPressedKeys().Count()) { currentKey = Keys.None; break; } currentKey = key.GetPressedKeys()[i]; i++; } while (currentKey == Keys.LeftShift || currentKey == Keys.RightShift || currentKey == Keys.Enter); if (keyi.IsKeyDown(currentKey)) { currentKey = Keys.None; } if (currentKey != Keys.None) { switch (currentKey) { case Keys.Back: if (floatingText.Text.Count() > 0) { floatingText.Text = floatingText.Text.Substring(0, floatingText.Text.Count() - 1); } break; case Keys.Space: floatingText.Text += ' '; break; case Keys.D1: if (key.IsKeyUp(Keys.LeftShift) && key.IsKeyUp(Keys.RightShift)) { floatingText.Text += '1'; } else { floatingText.Text += '!'; } break; case Keys.OemComma: if (key.IsKeyUp(Keys.LeftShift) && key.IsKeyUp(Keys.RightShift)) { floatingText.Text += ','; } else { floatingText.Text += '<'; } break; case Keys.OemPeriod: if (key.IsKeyUp(Keys.LeftShift) && key.IsKeyUp(Keys.RightShift)) { floatingText.Text += '.'; } else { floatingText.Text += '>'; } break; case Keys.OemQuestion: if (key.IsKeyUp(Keys.LeftShift) && key.IsKeyUp(Keys.RightShift)) { floatingText.Text += '/'; } else { floatingText.Text += '?'; } break; case Keys.OemSemicolon: if (key.IsKeyUp(Keys.LeftShift) && key.IsKeyUp(Keys.RightShift)) { floatingText.Text += ';'; } else { floatingText.Text += ':'; } break; default: if (key.IsKeyUp(Keys.LeftShift) && key.IsKeyUp(Keys.RightShift)) { floatingText.Text += currentKey.ToString().ToLower(); } else { floatingText.Text += currentKey.ToString().ToUpper(); } break; } } } if (key.IsKeyDown(Keys.Enter) && keyi.IsKeyUp(Keys.Enter)) { if (state == State.Select) { state = State.Typeing; } else if (state == State.Typeing) { state = State.Select; } } } //save if (state != State.Typeing && (key.IsKeyDown(Keys.Escape) || (key.IsKeyDown(Keys.Q) && keyi.IsKeyUp(Keys.Q)))) { Save(); } //test level if (state != State.Typeing && key.IsKeyDown(Keys.T) && keyi.IsKeyUp(Keys.T)) { game1.SetScreen(new Screens.LevelTestScreen(graphicsDevice, game1, this, level)); } //set previous x and y previousX = x; previousY = y; }