Click here to Skip to main content
15,891,708 members
Articles / Desktop Programming / Windows Forms

2D Map Editor

Rate me:
Please Sign up or sign in to vote.
4.89/5 (21 votes)
8 Oct 2009CPOL9 min read 185.9K   6.8K   92  
Create and edit 2D maps using tiles
using System;

namespace MapEditorLib
{
	/// <summary>
	/// Represents one layer of a map. MapLayer holds the actual
	/// array of tiles
	/// </summary>
	public class MapLayer : IComparable<MapLayer>
	{
		#region Variables
		private Tile[,] m_tiles;
		private string m_name;
		private int m_id;
		private bool m_bg;
		private float m_zvalue;
		private bool m_visible;
		#endregion

		#region Properties
		public string Name
		{
			get { return m_name; }
		}

		public int ID
		{
			get { return m_id; }
		}

		public bool IsBackground
		{
			get { return m_bg; }
		}

		public float Z_Value
		{
			set { m_zvalue = value; }
			get { return m_zvalue; }
		}

		public bool Visible
		{
			set { m_visible = value; }
			get { return m_visible; }
		}

		public Tile[,] Tiles
		{
			get { return m_tiles; }
		}
		#endregion

		public MapLayer(string Name, int ID, float ZValue, bool Background)
		{
			m_name = Name;
			m_id = ID;
			m_bg = Background;
			m_zvalue = ZValue;
			m_visible = true;
			m_tiles = new Tile[0, 0];
		}

		#region Public Functions
		public override string ToString()
		{
			return m_name;
		}

		public int CompareTo(MapLayer other)
		{
			int retval = 0;
			if (this.m_zvalue > other.m_zvalue)
			{
				retval = 1;
			}
			else if (this.m_zvalue < other.m_zvalue)
			{
				retval = -1;
			}

			return retval;
		}

		public void Resize(int width, int height)
		{
			//Set the new size of the map
			Tile[,] temp = new Tile[width, height];

			for (int x = 0; x < width; x++)
			{
				for (int y = 0; y < height; y++)
				{
					if (x < m_tiles.GetLength(0) && y < m_tiles.GetLength(1))
						temp[x, y] = m_tiles[x, y];
				}
			}

			m_tiles = temp;
		}

		public bool TileIsAlive(int X, int Y)
		{
			if(m_tiles[X,Y] != null && m_tiles[X,Y].IsInitialized)
				return true;
			return false;
		}
		#endregion
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
England England
*blip*

Comments and Discussions