using System;

namespace Assets.src.rpg
{
	public class CMapRPG
	{
		public const int size = 24;

		public const int T_EMPTY = 0;

		public const int T_BOTTOM = 1;

		public const int T_LEFT = 2;

		public const int T_RIGHT = 3;

		public const int T_TOP = 4;

		public const int T_BLACK = 5;

		public static Image img;

		public static int gssx;

		public static int gssy;

		public static int gssxe;

		public static int gssye;

		public static int tmw;

		public static int tmh;

		public static int pxw;

		public static int pxh;

		private sbyte[] map;

		public int[] type;

		public static Image[] imgMapItem;

		public static void initMapImage()
		{
			if (img == null)
			{
				img = CCanvas.loadImage("/map/t0.png");
			}
			if (!BackgroundNew.lowGraphic && imgMapItem == null)
			{
				imgMapItem = new Image[27];
				for (int i = 0; i < imgMapItem.Length; i++)
				{
					imgMapItem[i] = CCanvas.loadImage("/itemrpg/" + (i + 1) + ".png");
				}
			}
		}

		public int tileTypeAtPixel(int px, int py)
		{
			try
			{
				return type[py / 24 * tmw + px / 24];
			}
			catch (Exception)
			{
				return 1000;
			}
		}

		public bool checkTileCollisionLand(int x, int y)
		{
			int num = x / 24;
			int num2 = y / 24;
			if (num > tmw - 1 || num2 > tmh - 1)
			{
				return true;
			}
			int num3 = type[num2 * tmw + num];
			if (num3 == 4 || num3 == 2 || num3 == 3)
			{
				return true;
			}
			return false;
		}

		public bool checkTileCollisionLR(int x, int y)
		{
			int num = x / 24;
			int num2 = y / 24 - 1;
			if (num > tmw - 1 || num2 > tmh - 1)
			{
				return true;
			}
			if (num2 < 0)
			{
				num2 = 0;
			}
			int num3 = type[num2 * tmw + num];
			if (num3 == 2 || num3 == 3 || num3 == 5)
			{
				return true;
			}
			return false;
		}

		public void loadMap(string name)
		{
			gssx = 0;
			gssy = 0;
			tmw = 50;
			tmh = 20;
			gssxe = tmw;
			gssye = tmh;
			pxw = tmw * 24;
			pxh = tmh * 24;
			DataInputStream dataInputStream = null;
			try
			{
				dataInputStream = MyStream.readFile("/rpg/" + name);
				map = new sbyte[tmw * tmh];
				type = new int[tmw * tmh];
				for (int i = 0; i < tmw * tmh; i++)
				{
					map[i] = (sbyte)dataInputStream.read();
					if (map[i] > 0 && map[i] < 4)
					{
						type[i] = 4;
					}
					else if (map[i] == 9)
					{
						type[i] = 5;
					}
				}
			}
			catch (Exception ex)
			{
				ex.StackTrace.ToString();
			}
		}

		public void paintTilemap(mGraphics g)
		{
			if (map == null)
			{
				return;
			}
			try
			{
				for (int i = gssx; i < gssxe; i++)
				{
					for (int j = gssy; j < gssye; j++)
					{
						int num = map[j * tmw + i] - 1;
						if (num != -1)
						{
							g.drawRegion(img, 0, num * 24, 24, 24, 0, i * 24, j * 24, 0, false);
						}
					}
				}
			}
			catch (Exception)
			{
			}
		}

		public void paintMapItem(mGraphics g)
		{
			for (int i = gssx; i < gssxe; i++)
			{
				for (int j = gssy; j < gssye; j++)
				{
					int num = map[j * tmw + i] - 1;
					if (num != -1 && num - 1 >= 0)
					{
						int num2 = num + 1;
						switch (num2)
						{
						case 10:
							num2 = 27;
							break;
						case 11:
							num2 = 9;
							break;
						case 12:
							num2 = 7;
							break;
						case 13:
							num2 = 10;
							break;
						}
						g.drawImage(imgMapItem[num2 - 1], i * 24, j * 24 + 24, mGraphics.BOTTOM | mGraphics.LEFT, false);
					}
				}
			}
		}

		public void unload()
		{
			img = null;
			if (imgMapItem != null)
			{
				for (int i = 0; i < imgMapItem.Length; i++)
				{
					imgMapItem[i] = null;
				}
			}
			imgMapItem = null;
			map = null;
			type = null;
		}
	}
}
