/// <summary> /// Initializes the map-related functionality in /// the MainForm /// </summary> private void InitializeMap() { // Create a blank tilemap tilemap = new Tilemap("Untitled", 1, 100, 100, 50, 50); // Assign our event handlers newMapImageToolstripMenuItem.Click += NewMapClicked; loadTilemapToolStripMenuItem.Click += LoadMapClicked; saveTilemapToolStripMenuItem.Click += SaveMapClicked; hScrollBarMap.Scroll += MapScrolled; vScrollBarMap.Scroll += MapScrolled; }
/// <summary> /// Creates a new camera instance, tied to a particular tilemap /// </summary> /// <param name="graphicsDevice">The GraphicsDevice</param> /// <param name="tilemap">The Tilemap this camera is tied to</param> public Camera(GraphicsDevice graphicsDevice, Tilemap tilemap) { Viewport viewport = graphicsDevice.Viewport; effect = new BasicEffect(graphicsDevice) { TextureEnabled = true, VertexColorEnabled = true, World = Matrix.Identity, View = Matrix.Identity, Projection = Matrix.CreateOrthographicOffCenter(0, viewport.Width, viewport.Height, 0, 0, 1) }; center = new Vector2(viewport.Width / 2, viewport.Height / 2); bounds = new Rectangle( viewport.Width / 2, viewport.Height / 2, tilemap.Width * tilemap.TileWidth - viewport.Width, tilemap.Height * tilemap.TileHeight - viewport.Height ); }
/// <summary> /// Event handler for the "New Map" menu item. Launches /// a dialog to colleced the needed map information. If /// the dialog is successful, the resulting map is loaded /// into the editor. /// </summary> void NewMapMenuClicked(object sender, EventArgs e) { if (mapDirty) { if (DialogResult.Yes != MessageBox.Show("The map has not been saved. Continue creating a new map?", "Warning", MessageBoxButtons.YesNo)) return; } MapDialog mapDialog = new MapDialog(); if (mapDialog.ShowDialog(this) == DialogResult.OK) { // Store the resulting tilemap tilemap = mapDialog.Tilemap; mapViewerControl.Tilemap = tilemap; // Set the scrollbar maximums to match our map size hScrollBar2.Maximum = tilemap.Width; vScrollBar1.Maximum = tilemap.Height; // Clear our old lists tileImageList.Images.Clear(); tileListView.Items.Clear(); tilePalette.Clear(); // Mark the map as dirty until it is saved mapDirty = true; } }
/// <summary> /// Clicking the OK button validates the values provided by /// the form, and if successful, creates a new tilemap using /// those values, setting the DialogResult to true /// </summary> private void buttonOK_Click(object sender, EventArgs e) { string mapName; int mapLayers, mapWidth, mapHeight, tileWidth, tileHeight; mapName = textBoxMapName.Text; if(string.IsNullOrEmpty(mapName)) { MessageBox.Show("You must enter a map name", "Error"); return;} try { mapLayers = int.Parse(textBoxLayers.Text); } catch { MessageBox.Show("You must enter a map width", "Error"); return; } if (mapLayers <= 0) { MessageBox.Show("Map width must be greater than 0"); return; } try { mapWidth = int.Parse(textBoxMapWidth.Text); } catch { MessageBox.Show("You must enter a map width", "Error"); return;} if (mapWidth <= 0) { MessageBox.Show("Map width must be greater than 0"); return; } try { mapHeight = int.Parse(textBoxMapHeight.Text); } catch { MessageBox.Show("You must enter a map height", "Error"); return;} if (mapHeight <= 0) { MessageBox.Show("Map height must be greater than 0"); return; } try { tileWidth = int.Parse(textBoxTileWidth.Text); } catch { MessageBox.Show("You must enter a tile width", "Error"); return; } if (tileWidth <= 0) { MessageBox.Show("Tile width must be greater than 0"); return; } try { tileHeight = int.Parse(textBoxTileHeight.Text); } catch { MessageBox.Show("You must enter a tile height", "Error"); return; } if (tileHeight <= 0) { MessageBox.Show("Tile height must be greater than 0"); return; } Tilemap = new Tilemap(mapName, mapLayers, mapWidth, mapHeight, tileWidth, tileHeight); DialogResult = DialogResult.OK; }
/// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); //load tilemap tilemap = Content.Load<Tilemap>("NNmap1"); //load player player = new Player(); player.LoadContent(Content); player.Tilemap = tilemap; player.LayerIndex = tilemap.PlayerStart.Layer; //need to modify this slightly more than 1 tile to the right player.Position = tilemap.PlayerStart.Position + new Vector2(60, 20); //target 30 fps for sped-up play, minimum for normal collision function //will need to scale this with time scale: based on framerate on desktop computer this.TargetElapsedTime = TimeSpan.FromSeconds(1.0f / 60.0f); //heart = Content.Load<Texture2D>("heart"); //load camera camera = new Camera(GraphicsDevice, tilemap); camera.Target = player; //enemies = new List<Enemy>(); //if (tilemap.EnemyLocations != null) //{ // foreach (EnemyStart e in tilemap.EnemyLocations) // { // enemies.Add(new Enemy(e.MonsterType, e.Layer, e.Position)); // } //} //gems = new List<Gem>(); //if (tilemap.Gems != null) //{ // foreach (GemLocation g in tilemap.Gems) // { // gems.Add(new Gem(g.GemType, g.Layer, g.Position)); // } //} //Licensed under Creative Commons Attribution - from freemusicarchive.org //bgSong = Content.Load<Song>("Sounds/Rolemusic - Leafless_Quince_Tree"); spriteFont = Content.Load<SpriteFont>("SpriteFont1"); //MediaPlayer.Play(bgSong); gameState = GameState.Start; }
/// <summary> /// Event handler for when the "Load Map" menu item is clicked /// </summary> void LoadMapClicked(Object obj, EventArgs e) { // Make sure the current map is saved, or intentionally being discarded if (mapDirty) { string message = "The current tilemap has not been saved - opening a new one will lose any changes you made. Are you sure you want to continue?"; if (DialogResult.Yes != MessageBox.Show(message, "Warning", MessageBoxButtons.YesNo)) return; } // Create a OpenFileDialog to find our file OpenFileDialog ofd = new OpenFileDialog(); ofd.InitialDirectory = tilemapPath; ofd.DefaultExt = ".tmap"; ofd.Filter = "tilemap files (*.tmap)|*.tmap"; ofd.RestoreDirectory = true; if (ofd.ShowDialog() == DialogResult.OK) { Cursor = Cursors.WaitCursor; using (XmlReader reader = XmlReader.Create(ofd.FileName)) { TilemapContent tilemapContent = IntermediateSerializer.Deserialize<TilemapContent>(reader, null); // Create a tilemap with the same size as our loaded one tilemap = new Tilemap(tilemapContent.Name, tilemapContent.Layers.Count, tilemapContent.Width, tilemapContent.Height, tilemapContent.TileHeight, tilemapContent.TileWidth); // Clear our old lists tileImageList.Images.Clear(); tileListView.Items.Clear(); tilePalette.Clear(); // Create the tiles foreach (TileContent tileContent in tilemapContent.TilePalette) { // Load the texture through the content pipline so we can render it // in our XNA controls Texture2D texture = LoadTexture(tileContent.Image.Filename); // Create an instance of the tile and add it to the palette Tile tile = new Tile(){ Name = tileContent.Name, Image = texture }; tilemap.TilePalette.Add(tile); // Add the new tile image to the tileListView, and assign it a new image tileListView.Items.Add(tileContent.Name, tileImageList.Images.Count); // Load the new image representing our new tile tileImageList.Images.Add(Bitmap.FromFile(tileContent.Image.Filename)); // Add the TileContent to our TilePalette tilePalette.Add(tileContent); } // Create the layers tilemap.Layers.Clear(); listBoxLayers.Items.Clear(); foreach (TilemapLayerContent layerContent in tilemapContent.Layers) { // Create a corresponding TilemapLayer TilemapLayer layer = new TilemapLayer(layerContent.Name, layerContent.Width, layerContent.Height, layerContent.TileWidth, layerContent.TileHeight); layer.TileData = layerContent.TileData; // Add the layer to our tilemap tilemap.Layers.Add(layer); // Add the layer to our layer GUI list listBoxLayers.Items.Add(layer.Name); } // Deselect the selected tile index on the mapViewer mapViewerControl.SelectedTileIndex = -1; } Cursor = Cursors.Arrow; } }
/// <summary> /// Event handler for the "New Map" menu item. Launches /// a dialog to colleced the needed map information. If /// the dialog is successful, the resulting map is loaded /// into the editor. /// </summary> void NewMapClicked(object sender, EventArgs e) { if (mapDirty) { if(DialogResult.Yes != MessageBox.Show("The map has not been saved. Continue creating a new map?", "Warning", MessageBoxButtons.YesNo)) return; } MapDialog mapDialog = new MapDialog(); if (mapDialog.ShowDialog(this) == DialogResult.OK) { // Store the resulting tilemap tilemap = mapDialog.Tilemap; // Set the scrollbar maximums to match our map size hScrollBarMap.Maximum = tilemap.Width; vScrollBarMap.Maximum = tilemap.Height; // Clear our old lists tileImageList.Images.Clear(); tileListView.Items.Clear(); tilePalette.Clear(); listBoxLayers.Items.Clear(); // Add our layers to the layer list gui foreach(TilemapLayer layer in tilemap.Layers) { listBoxLayers.Items.Add(layer.Name); } // Mark the map as dirty until it is saved mapDirty = true; } }
/// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); //load tilemap tilemap = Content.Load<Tilemap>("Map2"); //load player player = new Player(); player.LoadContent(Content); player.Tilemap = tilemap; player.LayerIndex = tilemap.PlayerStart.Layer; player.Position = tilemap.PlayerStart.Position; //heart = Content.Load<Texture2D>("heart"); //load camera camera = new Camera(GraphicsDevice, tilemap); camera.Target = player; //enemies = new List<Enemy>(); //if (tilemap.EnemyLocations != null) //{ // foreach (EnemyStart e in tilemap.EnemyLocations) // { // enemies.Add(new Enemy(e.MonsterType, e.Layer, e.Position)); // } //} //gems = new List<Gem>(); //if (tilemap.Gems != null) //{ // foreach (GemLocation g in tilemap.Gems) // { // gems.Add(new Gem(g.GemType, g.Layer, g.Position)); // } //} //Licensed under Creative Commons Attribution - from freemusicarchive.org //bgSong = Content.Load<Song>("Sounds/Rolemusic - Leafless_Quince_Tree"); spriteFont = Content.Load<SpriteFont>("SpriteFont1"); //MediaPlayer.Play(bgSong); gameState = GameState.Start; }