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

namespace pcd_g1
{
	delegate void CharacterStageObjectMovedEventHandler(CharacterStageObject sender);

	abstract class CharacterStageObject : IStageObject
	{
		const double MAX_SPEED_Y = 100.0;

		public int X { get; set; }
		public int Y { get; set; }
		public int Width { get; protected set; }
		public int Height { get; protected set; }
		public bool Visibility { get; private set; }
		public bool CanCrash { get; private set; }
		public CollideRange Range { get; protected set; }

		protected Stage owner{get; private set;}
		protected Double speedX = 0, speedY = 0;
		protected bool isFloating;
		protected bool isSet = false;

		public CharacterStageObject(int x, int y, Stage stg)
		{
			owner = stg;
			X = x; Y = y;
			Width = 32;
			Height = 32;
			Visibility = true;
			isFloating = false;
			CanCrash = false;
			Range = new FreeRectangleCollideRange(this);
			((FreeRectangleCollideRange)Range).Rect = new RectangleF(3f, 3f, 29f, 29f);
		}

		public virtual void Update(int keyStatus, out int vx, out int vy)
		{
			isSet = owner.IsOn(this, CollideDirection.Down);

			if (isSet)
			{
				//speedY = 0;
			}
			else
			{
				if (!isFloating)
				{
					//重力
					if (speedY < MAX_SPEED_Y)
					{
						speedY += owner.IsLake ? 0.3 : 0.5;
					}
				}
			}

			vx = (int)speedX; vy = (int)speedY;
		}

		//Stageからの通知用
		public virtual bool EventProc(EventType type, IStageObject sender, CollideDirection direction)
		{
			switch(type)
			{
				case EventType.Collided:
				   if (direction == CollideDirection.Down || direction == CollideDirection.Up)
					{
						speedY = 0;
					}
					break;
			}
			return true;
		}

		//Character同士の連絡用
		public virtual void SignalProc(SignalType type, int arg1, int arg2)
		{
			if (type == SignalType.SIGKILL)
			{
				SignalProc(SignalType.SIGDAMAGE, 99999, 0);
				if(owner.Contains(this)) owner.Remove(this);
			}
			else if (type == SignalType.SIGPUSH)
			{
				speedX += arg1;
				speedY += arg2;
			}
		}

		public virtual void Draw(Graphics g, int width, int height)
		{
			g.DrawRectangle(Pens.Red, 0, 0, width, height);
			g.DrawLine(Pens.Red, 0, 0, width, height);
			g.DrawLine(Pens.Red, 0, height, width, 0);
		}
	}

	
}
