using System;

public class Airplane
{
	public class Node
	{
		public int x1;

		public int y1;

		public int x2;

		public int y2;

		public Node(int x1, int y1, int x2, int y2)
		{
			this.x1 = x1;
			this.x2 = x2;
			this.y1 = y1;
			this.y2 = y2;
		}
	}

	public const sbyte TRUCTHANG = 0;

	public const sbyte CANHQUAT = 1;

	public const sbyte PHANLUC = 2;

	public const sbyte LEFT = 0;

	public const sbyte RIGHT = 2;

	public int x;

	public int y;

	public int w;

	public int h;

	public int hw;

	public int hh;

	public int speed;

	public static Image mb00;

	public static Image mb01;

	public static Image mb02;

	public static Image mb03;

	public static Image mb10;

	public static Image mb11;

	public static Image mb2;

	public static FrameImage khoi;

	public bool isVisible;

	private int sleep;

	public Node soixich;

	private int type;

	private int iFrame;

	private int nFrame;

	private int direction;

	private int iFrameKhoi;

	private int delay;

	public Airplane(sbyte type, sbyte direction)
	{
		this.type = type;
		this.direction = direction;
		nFrame = 1;
		switch (type)
		{
		case 0:
			nFrame = 2;
			w = 64;
			h = 48;
			speed = 1;
			break;
		case 1:
			nFrame = 2;
			w = 59;
			h = 47;
			speed = 10;
			break;
		case 2:
			if (mb2 == null)
			{
				mb2 = CCanvas.loadImage("/mb/mb2.png");
			}
			w = 64;
			h = 48;
			speed = 30;
			break;
		}
		hw = w >> 1;
		hh = h >> 1;
	}

	public Airplane(sbyte direction, int yy)
	{
		type = 0;
		this.direction = direction;
		nFrame = 1;
		if (type == 0)
		{
			nFrame = 2;
			w = 64;
			h = 48;
			speed = 1;
		}
		soixich = new Node(PM.getMyPlayer().x, yy - 108, PM.getMyPlayer().x, yy);
		hw = w >> 1;
		hh = h >> 1;
	}

	public static void loadImage()
	{
		try
		{
			mb00 = CCanvas.loadImage("/mb/mb00.png");
			mb01 = CCanvas.loadImage("/mb/mb01.png");
			mb02 = CCanvas.loadImage("/mb/mb02.png");
			mb03 = CCanvas.loadImage("/mb/mb03.png");
			mb10 = CCanvas.loadImage("/mb/mb10.png");
			mb11 = CCanvas.loadImage("/mb/mb11.png");
		}
		catch (Exception)
		{
		}
	}

	public void setPos(int x, int y)
	{
		this.x = x;
		this.y = y;
	}

	public void doSleep(int sleepTime)
	{
		sleep = sleepTime;
	}

	public void update()
	{
		if (sleep > 0)
		{
			sleep--;
			return;
		}
		if (type == 0)
		{
			soixich.x1 = x + 30;
			soixich.x2 = soixich.x1;
			if (!CCanvas.gameScr.isSplash)
			{
				if (soixich.y2 - 5 < soixich.y1)
				{
					soixich.y2 = soixich.y1;
					speed = 3;
					if (Camera.mode != 0 && Camera.mode != 9 && speed == 0)
					{
						GameScr.cam.setPlayerMode(PM.curP);
					}
				}
				else
				{
					soixich.y2 -= 5;
				}
			}
			if (direction == 2)
			{
				x += speed;
			}
			else
			{
				x -= speed;
			}
		}
		else if (direction == 2)
		{
			x += speed;
		}
		else
		{
			x -= speed;
		}
		if (type == 2)
		{
			iFrameKhoi++;
			if (iFrameKhoi > 8)
			{
				iFrameKhoi = 0;
			}
		}
		if (nFrame != 1)
		{
			iFrame++;
			if (iFrame > nFrame - 1)
			{
				iFrame = 0;
			}
		}
	}

	public void updateGoOutScreen()
	{
		if (direction == 2)
		{
			if (x > CCanvas.w)
			{
				int sleepTime = CRes.random(100, 200);
				doSleep(sleepTime);
				if (CRes.random(0, 2) == 0)
				{
					direction = 0;
					x = CCanvas.w + 100;
				}
				else
				{
					direction = 2;
					x = -100;
				}
				y = CRes.random(20, 100);
			}
		}
		else if (x < -100)
		{
			int sleepTime2 = CRes.random(100, 200);
			doSleep(sleepTime2);
			if (CRes.random(0, 2) == 0)
			{
				direction = 0;
				x = CCanvas.w + 100;
			}
			else
			{
				direction = 2;
				x = -100;
			}
			y = CRes.random(20, 100);
		}
	}

	public void paint(mGraphics g)
	{
		switch (type)
		{
		case 0:
			if (soixich.y1 != soixich.y2)
			{
				int num = soixich.y2 - soixich.y1;
				int num2 = soixich.y1;
				if (num >= 20)
				{
					for (int i = 0; i < num / 20; i++)
					{
						g.drawImage(GameScr.daythung, soixich.x1, num2, 0, false);
						num2 += 20;
					}
				}
				int h = num % 20;
				g.drawRegion(GameScr.daythung, 0, 0, 1, h, 0, soixich.x1, num2, 0, false);
			}
			g.drawRegion(mb00, 0, 0, 43, 48, direction, (direction != 0) ? (x + w - 43) : x, y, 0, false);
			g.drawRegion((iFrame != 0) ? mb02 : mb01, 0, 0, 32, 31, direction, (direction != 0) ? x : (x + 32), y + 1, 0, false);
			g.drawRegion(mb03, 0, 0, 64, 15, (iFrame != 0) ? 2 : 0, (direction != 0) ? (x + w - 59) : (x - 5), y - 4, 0, false);
			break;
		case 1:
			g.drawRegion(mb10, 0, 0, 59, 47, direction, x, y, 0, false);
			g.drawRegion(mb11, 0, 0, 15, 32, (iFrame != 0) ? 2 : 0, (direction != 0) ? (x + w + 3 - 15) : (x - 3), y + 11, 0, false);
			break;
		case 2:
			g.drawRegion(mb2, 0, 0, 64, 48, direction, (direction != 0) ? (x + w - 64) : x, y, 0, false);
			khoi.drawFrame(iFrameKhoi >> 1, (direction != 0) ? (x + w - 63) : (x + 63), y + 23, direction, (direction != 0) ? 10 : 6, g, false);
			break;
		}
	}

	public void paint(mGraphics g, int x, int y, int anchor)
	{
		if (anchor == 3)
		{
			x -= hw;
			y -= hh;
		}
		switch (type)
		{
		case 0:
			g.drawRegion(mb00, 0, 0, 43, 48, direction, (direction != 0) ? (x + w - 43) : x, y, 0, false);
			g.drawRegion((iFrame != 0) ? mb02 : mb01, 0, 0, 32, 31, direction, (direction != 0) ? x : (x + 32), y + 1, 0, false);
			g.drawRegion(mb03, 0, 0, 64, 15, (iFrame != 0) ? 2 : 0, (direction != 0) ? (x + w - 59) : (x - 5), y - 4, 0, false);
			break;
		case 1:
			g.drawRegion(mb10, 0, 0, 59, 47, direction, x, y, 0, false);
			g.drawRegion(mb11, 0, 0, 15, 32, (iFrame != 0) ? 2 : 0, (direction != 0) ? (x + w + 3 - 15) : (x - 3), y + 11, 0, false);
			break;
		case 2:
			g.drawRegion(mb2, 0, 0, 64, 48, direction, (direction != 0) ? (x + w - 64) : x, y, 0, false);
			khoi.drawFrame(iFrameKhoi >> 1, (direction != 0) ? (x + w - 63) : (x + 63), y + 23, direction, (direction != 0) ? 10 : 6, g, false);
			break;
		}
	}
}
