public LevelEditorScreen(GraphicsDevice graphicsDevice, Game1 game1, string levelDir, bool load) : base(graphicsDevice, game1) { this.levelDir = levelDir; key = Keyboard.GetState(); mouse = Mouse.GetState(); blockWidth = graphicsDevice.Viewport.Height / 15; List <List <List <GameObject> > > levelGrid = new List <List <List <GameObject> > >(); level = new Level(levelGrid, 0, blockWidth, graphicsDevice, () => { game1.SetScreen(null); }); font = LoadedContent.mainMenuFont; camera = new Vector2(0, -graphicsDevice.Viewport.Height); current = 0; objectRow = 0; objectsPerRow = graphicsDevice.Viewport.Width / (int)blockWidth; state = State.Draw; if (load) { Load(); } }
public static void shufleList(ArrayList list) { System.Random rng = new System.Random(); int n = list.Count; while (n > 1) { n--; int k = rng.Next(n + 1); GameObjectsEnum value = (GameObjectsEnum)list[k]; list[k] = list[n]; list[n] = value; } }
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 void AddTile(GameObjectsEnum gameObject, int x, int y) { switch (gameObject) { case GameObjectsEnum.Ground: level.AddGameObject(new Ground(level, blockWidth, new Vector2(x, -y)), x, y); break; case GameObjectsEnum.fallingPlatform: level.AddGameObject(new FallingPlatform(level, blockWidth, new Vector2(x, -y)), x, y); break; case GameObjectsEnum.risingPlatform: level.AddGameObject(new RisingPlatform(level, blockWidth, new Vector2(x, -y)), x, y); break; case GameObjectsEnum.Arrows: level.AddGameObject(new Arrows(level, blockWidth, new Vector2(x, -y)), x, y); break; case GameObjectsEnum.Player: level.AddGameObject(new Player(level, blockWidth, new Vector2(x, -y)), x, y); break; case GameObjectsEnum.Spikes: level.AddGameObject(new Spikes(level, blockWidth, new Vector2(x, -y)), x, y); break; case GameObjectsEnum.Block: level.AddGameObject(new Block(level, blockWidth, new Vector2(x, -y)), x, y); break; case GameObjectsEnum.Button: level.AddGameObject(new Button(level, blockWidth, new Vector2(x, -y)), x, y); break; case GameObjectsEnum.FloatingText: level.AddGameObject(new FloatingText(level, blockWidth, new Vector2(x, -y)), x, y); break; case GameObjectsEnum.Goal: level.AddGameObject(new Goal(level, blockWidth, new Vector2(x, -y)), x, y); break; case GameObjectsEnum.Arrow2: level.AddGameObject(new Arrow2(level, blockWidth, new Vector2(x, -y)), x, y); 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; }
public void createGameObjectsOnFloor() { float xFloor = floor.transform.position.x; float zFloor = floor.transform.position.z; System.Random random = new System.Random(); // generate from 5 to 10 objects int objectsNum = 10; // random number of laser radars from 1 to 5 int obstaclesNum = random.Next(1, 5); objectsNum -= obstaclesNum; // random number of obstacles from 1 to 5 int laserRadarNum = random.Next(1, 3); objectsNum -= laserRadarNum; // number of coins will be the rest of objects available int coinNum = objectsNum; ArrayList gameObjects = new ArrayList(); for (int j = 0; j < objectsNum; j++) { if (laserRadarNum > 0) { gameObjects.Add(GameObjectsEnum.laserRadar); laserRadarNum--; continue; } if (obstaclesNum > 0) { gameObjects.Add(GameObjectsEnum.Obstacle); obstaclesNum--; continue; } if (coinNum > 0) { gameObjects.Add(GameObjectsEnum.Coin); coinNum--; } } // shuffle list shufleList(gameObjects); GameObjectsEnum[,] map = new GameObjectsEnum[20, 3]; int xTranslation, index; GameObjectsEnum o; GameObject g; // populate map for (int i = 0; i < 20; i += 4) { for (int j = 0; j < 3; j++) { if (gameObjects.Count == 0) { return; } index = random.Next(0, gameObjects.Count); o = (GameObjectsEnum)gameObjects[index]; if (isValidPlace(map, new Position(i, j), o)) { g = null; gameObjects.RemoveAt(index); if (o.Equals(GameObjectsEnum.laserRadar)) { g = Instantiate(laserRadar); g.transform.position = new Vector3(0, 1f, zFloor + i - 5); map[i, 0] = o; } else { } if (o.Equals(GameObjectsEnum.Obstacle)) { g = Instantiate(obstacle); xTranslation = getXTranslation(j); g.transform.position = new Vector3(xFloor + xTranslation, 1, zFloor + i - 5); map[i, j] = o; } if (o.Equals(GameObjectsEnum.Coin)) { g = Instantiate(coin); xTranslation = getXTranslation(j); g.transform.position = new Vector3(xFloor + xTranslation, 1, zFloor + i - 5); map[i, j] = o; } if (g != null) { g.GetComponent <MeshRenderer>().enabled = true; g.transform.parent = floor.transform; } } } } }
/** * Map is 20*3 matrix */ private bool isValidPlace(GameObjectsEnum[,] map, Position newPosition, GameObjectsEnum o) { if (o.Equals(GameObjectsEnum.Obstacle)) { // no laser Rader at this row if (map[newPosition.getRow(), 0].Equals(GameObjectsEnum.laserRadar)) { return(false); } // make sure that if o is an obstacle, that we cannot have three obstacle one row if (newPosition.getColumn() == 0 && map[newPosition.getRow(), 1].Equals(GameObjectsEnum.Obstacle) && map[newPosition.getRow(), 2].Equals(GameObjectsEnum.Obstacle)) { return(false); } if (newPosition.getColumn() == 1 && map[newPosition.getRow(), 0].Equals(GameObjectsEnum.Obstacle) && map[newPosition.getRow(), 2].Equals(GameObjectsEnum.Obstacle)) { return(false); } if (newPosition.getColumn() == 2 && map[newPosition.getRow(), 0].Equals(GameObjectsEnum.Obstacle) && map[newPosition.getRow(), 1].Equals(GameObjectsEnum.Obstacle)) { return(false); } } else { if (o.Equals(GameObjectsEnum.laserRadar)) { // check that all columns in row r donot have any game objects, where r = newPosition.getRow() if (!map[newPosition.getRow(), 0].Equals(GameObjectsEnum.Empty) || !map[newPosition.getRow(), 1].Equals(GameObjectsEnum.Empty) || !map[newPosition.getRow(), 2].Equals(GameObjectsEnum.Empty)) { return(false); } // check that the nearest laserRadar is far 8 in z-axis (8 rows) if (newPosition.getRow() - 4 >= 0 && map[newPosition.getRow() - 4, 0].Equals(GameObjectsEnum.laserRadar)) { return(false); } } else { if (o.Equals(GameObjectsEnum.Coin)) { // check that there is not laser radar on same row of newPosition.getRow() if (map[newPosition.getRow(), 0].Equals(GameObjectsEnum.laserRadar)) { return(false); } // check that there is no coins on the same row if (newPosition.getColumn() == 0 && map[newPosition.getRow(), 1].Equals(GameObjectsEnum.Coin) && map[newPosition.getRow(), 2].Equals(GameObjectsEnum.Coin)) { return(false); } if (newPosition.getColumn() == 1 && map[newPosition.getRow(), 0].Equals(GameObjectsEnum.Coin) && map[newPosition.getRow(), 2].Equals(GameObjectsEnum.Coin)) { return(false); } if (newPosition.getColumn() == 2 && map[newPosition.getRow(), 0].Equals(GameObjectsEnum.Coin) && map[newPosition.getRow(), 1].Equals(GameObjectsEnum.Coin)) { return(false); } } } } return(true); }