| |
| using System; |
| using System.IO; |
| using System.Collections.Generic; |
| using Microsoft.Xna.Framework; |
| using Microsoft.Xna.Framework.Audio; |
| using Microsoft.Xna.Framework.Content; |
| using Microsoft.Xna.Framework.GamerServices; |
| using Microsoft.Xna.Framework.Graphics; |
| using Microsoft.Xna.Framework.Input; |
| using Microsoft.Xna.Framework.Net; |
| using Microsoft.Xna.Framework.Storage; |
| |
| namespace Calculus |
| { |
| public class LevelEditor |
| { |
| SpriteBatch spriteBatch; |
| List<Grid> grid = new List<Grid>(); |
| int screenWidth = Game1.screenWidth; |
| int screenHeight = Game1.screenHeight; |
| ItemType chosentype = ItemType.Wall; |
| int xmax = Game1.screenWidth / 25; |
| int ymax = Game1.screenHeight / 25; |
| string[ line; |
| int x = 0; |
| int y = 0; |
| |
| public LevelEditor(SpriteBatch spriteBatch) |
| { |
| this.spriteBatch = spriteBatch; |
| xmax = screenWidth / 25; |
| line = new string[ymax]; |
| } |
| |
| public void BuildGrid() |
| { |
| Grid tmp; |
| if (grid.Count == 0) |
| { |
| for (int h = 0; h < screenHeight; h += 25) |
| { |
| for (int w = 0; w < screenWidth; w += 25) |
| { |
| tmp.rect = new Rectangle(w, h, 25, 25); |
| tmp.x = w / 25; |
| tmp.y = h / 25; |
| tmp.imgrect = new Rectangle((int)ItemType.Grid * 25, 0, 25, 25); |
| tmp.zindex = 0.0f; |
| tmp.text = Game1.items; |
| tmp.itemtype = ItemType.Grid; |
| |
| grid.Add(tmp); |
| } |
| } |
| } |
| else |
| foreach (Grid g in grid) |
| { |
| spriteBatch.Draw(g.text, g.rect, g.imgrect, Color.White, 0, new Vector2(), SpriteEffects.None, g.zindex); |
| } |
| } |
| int pl; |
| public void PlaceBlock() |
| { |
| int x = Game1.mouseState.X; |
| int y = Game1.mouseState.Y; |
| |
| Rectangle rect = new Rectangle(x, y, 1, 1); |
| |
| for (int i = 0; i < grid.Count; i++) |
| { |
| if (grid[i].rect.Contains(new Point(x, y))) |
| { |
| if (chosentype == ItemType.Player) |
| { |
| if(pl != 1) |
| { |
| pl++; |
| Grid tmp = new Grid(); |
| tmp.rect = new Rectangle(grid[i].x * 25, grid[i].y * 25, 25, 25); |
| tmp.imgrect = new Rectangle((int)MoveDirection.Up * 25, 0, 25, 25); |
| tmp.x = grid[i].x; |
| tmp.y = grid[i].y; |
| tmp.zindex = 0.5f; |
| tmp.text = Game1.player; |
| tmp.itemtype = ItemType.Player; |
| Grid tmp2; |
| tmp2 = grid[i]; |
| grid.Remove(tmp2); |
| grid.Insert(i, tmp); |
| } |
| } |
| else |
| { |
| Grid tmp = new Grid(); |
| tmp.rect = new Rectangle(grid[i].x * 25, grid[i].y * 25, 25, 25); |
| tmp.imgrect = new Rectangle((int)chosentype * 25, 0, 25, 25); |
| tmp.x = grid[i].x; |
| tmp.y = grid[i].y; |
| tmp.zindex = 0.5f; |
| tmp.text = Game1.items; |
| tmp.itemtype = chosentype; |
| Grid tmp2; |
| tmp2 = grid[i]; |
| if (tmp2.itemtype == ItemType.Player) |
| pl--; |
| grid.Remove(tmp2); |
| grid.Insert(i, tmp); |
| } |
| break; |
| } |
| } |
| } |
| |
| public void DeleteBlock() |
| { |
| int x = Game1.mouseState.X; |
| int y = Game1.mouseState.Y; |
| |
| Rectangle rect = new Rectangle(x, y, 1, 1); |
| |
| for (int i = 0; i < grid.Count; i++) |
| { |
| if (grid[i].rect.Contains(new Point(x, y))) |
| { |
| if (grid[i].itemtype == ItemType.Player) |
| pl--; |
| Grid tmp = new Grid(); |
| tmp.rect = new Rectangle(grid[i].x * 25, grid[i].y * 25, 25, 25); |
| tmp.imgrect = new Rectangle((int)ItemType.Grid * 25, 0, 25, 25); |
| tmp.x = grid[i].x; |
| tmp.y = grid[i].y; |
| tmp.zindex = 0.0f; |
| tmp.text = Game1.items; |
| tmp.itemtype = ItemType.Grid; |
| Grid tmp2; |
| tmp2 = grid[i]; |
| grid.Remove(tmp2); |
| grid.Insert(i, tmp); |
| break; |
| } |
| } |
| } |
| |
| public void Draw() |
| { |
| spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.FrontToBack, SaveStateMode.None); |
| spriteBatch.Draw(Game1.mouseTexture, new Rectangle(Game1.mouseState.X, Game1.mouseState.Y, 13, 13), new Rectangle(0, 0, 13, 13), Color.White, 0, new Vector2(), SpriteEffects.None, 1.0f); |
| BuildGrid(); |
| |
| CheckCommands(); |
| |
| if(Game1.mouseState.LeftButton == ButtonState.Pressed) |
| PlaceBlock(); |
| |
| if (Game1.mouseState.RightButton == ButtonState.Pressed) |
| DeleteBlock(); |
| |
| if (Game1.keyboardState.IsKeyDown(Keys.H)) |
| DrawHelp(); |
| |
| spriteBatch.End(); |
| } |
| |
| public void CheckCommands() |
| { |
| if (Game1.keyboardState.IsKeyDown(Keys.W)) |
| { |
| chosentype = ItemType.Wall; |
| } |
| if (Game1.keyboardState.IsKeyDown(Keys.F)) |
| { |
| chosentype = ItemType.Floor; |
| } |
| if (Game1.keyboardState.IsKeyDown(Keys.C)) |
| { |
| chosentype = ItemType.Rock; |
| } |
| if (Game1.keyboardState.IsKeyDown(Keys.P)) |
| { |
| chosentype = ItemType.Player; |
| } |
| if (Game1.keyboardState.IsKeyDown(Keys.G)) |
| { |
| chosentype = ItemType.Goal; |
| } |
| if (Game1.keyboardState.IsKeyDown(Keys.Q)) |
| { |
| SaveToFile(); |
| Game1.createlevel = false; |
| } |
| |
| } |
| |
| public void DrawHelp() |
| { |
| string helpmsg = "Commands (Press em buttons):\nLeft Click - Place Block\nRight Click - Delete Block\nP - Set Player Spawnpoint\nW - Select 'Wall' Block\nC - Select 'Crate' Block\nF - Select 'Floor' Block\nG - Select 'Goal' Block\nS - Save & Exit\nH - Show this..."; |
| spriteBatch.DrawString(Game1.smallfont, helpmsg, new Vector2( |
| screenWidth / 2 - (int)Game1.smallfont.MeasureString(helpmsg).X / 2, |
| screenHeight / 2 - (int)Game1.smallfont.MeasureString(helpmsg).Y / 2), Color.White, 0, new Vector2(), 1, SpriteEffects.None, 1.0f); |
| |
| spriteBatch.Draw(Game1.overlay, new Rectangle( |
| screenWidth / 2 - (int)Game1.smallfont.MeasureString(helpmsg).X / 2, |
| screenHeight / 2 - (int)Game1.smallfont.MeasureString(helpmsg).Y / 2, |
| (int)Game1.smallfont.MeasureString(helpmsg).X, screenHeight / 2 - (int)Game1.smallfont.MeasureString(helpmsg).Y / 2), new Rectangle(0, 0, 1, 1), Color.White, 0, new Vector2(), SpriteEffects.None, 0.95f); |
| } |
| |
| public void SaveToFile() |
| { |
| foreach (Grid g in grid) |
| { |
| ConvertToText(g); |
| } |
| |
| string mapname = string.Empty; |
| for (int i = 1; i < 999; i++) |
| { |
| if (!File.Exists(BuildLevel.mapDirectory + i.ToString("000") + ".map")) |
| { |
| mapname = i.ToString("000") + ".map"; |
| break; |
| } |
| } |
| |
| using (StreamWriter sw = new StreamWriter(BuildLevel.mapDirectory + mapname)) |
| { |
| for (int i = 0; i < y; i++) |
| { |
| sw.WriteLine(line[i]); |
| } |
| } |
| } |
| |
| private void ConvertToText(Grid g) |
| { |
| x++; |
| switch (g.itemtype) |
| { |
| case ItemType.Player: |
| line[y] += "P"; |
| break; |
| |
| case ItemType.Grid: |
| line[y] += "S"; |
| break; |
| |
| case ItemType.Wall: |
| line[y] += "W"; |
| break; |
| |
| case ItemType.Floor: |
| line[y] += "F"; |
| break; |
| |
| case ItemType.Goal: |
| line[y] += "G"; |
| break; |
| |
| case ItemType.Rock: |
| line[y] += "R"; |
| break; |
| } |
| |
| if (x >= xmax) |
| { |
| y++; |
| x = 0; |
| } |
| } |
| } |
| } |