using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace pcd_g1
{
	class NormalCollideRange : CollideRange
	{
		public NormalCollideRange(IStageObject item)
		{
			StageItem = item;
		}

		public override bool InCollideField(double x, double y, CollideRange range)
		{
			return true;
		}

		public override bool Judge(double x, double y, CollideRange range)
		{
			double x2 = x + StageItem.Width, y2 = y + StageItem.Height;
			if (range is NormalCollideRange)
			{
				IStageObject item = ((NormalCollideRange)range).StageItem;
				double ix = item.X, iy = item.Y, ix2 = item.X + item.Width, iy2 = item.Y + item.Height;
				return JudgeByRectangle(x, y, x2, y2, ix, iy, ix2, iy2);
			}
			else if (range is SlopeTypeBCollideRange)
			{
				IStageObject item = ((SlopeTypeBCollideRange)range).StageItem;
				double ix = item.X, iy = item.Y + item.Height / 2, ix2 = item.X + item.Width, iy2 = item.Y + item.Height;
				return JudgeByRectangle(x, y, x2, y2, ix, iy, ix2, iy2);
			}
			else if (range is FreeRectangleCollideRange)
			{
				IStageObject item = ((FreeRectangleCollideRange)range).StageItem;
				var rect = ((FreeRectangleCollideRange)range).Rect;
				double ix = item.X + rect.X, iy = item.Y + rect.Y, ix2 = ix + rect.Width, iy2 = iy + rect.Height;
				return NormalCollideRange.JudgeByRectangle(x, y, x2, y2, ix, iy, ix2, iy2);
			}
			else if (range is SlopeTypeACollideRange)
			{
				var rng = (SlopeTypeACollideRange)range;
				return SlopeTypeACollideRange.CrossLineJudge(new PointF((float)x, (float)y2), new PointF((float)x2, (float)y2),
					new PointF(rng.P1.X + rng.StageItem.X, rng.P1.Y + rng.StageItem.Y),
					new PointF(rng.P2.X + rng.StageItem.X, rng.P2.Y + rng.StageItem.Y));
			}
			return false;
		}

		public static bool JudgeByRectangle(double x, double y, double x2, double y2, double ix, double iy, double ix2, double iy2)
		{
			return (x < ix2 && y < iy2) && (x2 > ix && y2 > iy);
		}
	}
}
