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

namespace pcd_g1
{
	class BanakeCharacterStageObject : CharacterStageObject
	{
		private double movespeed_max = 8;
		private double jump_max = 8;
		private double brake_max = 0.2;

		private int hp;//ヒットポイント
		private const int DEFAULT_HP = 5;//初期のヒットポイント


		public BanakeCharacterStageObject(int x, int y, Stage stg):base(x,y,stg)
		{
			isFloating = false;

			hp = DEFAULT_HP;

			X = x; Y = y;
		}

		private enum Direction
		{
			Left = 2, Right = 1, Null = 0
		}

		private Direction moveDirection = Direction.Left;

		int actionCount = 0;
		public override void Update(int keyStatus, out int vx, out int vy)
		{
			base.Update(keyStatus, out vx, out vy);

			//speedX = 0;
			//speedY = 0;
			if (++actionCount > 150)
			{
				actionCount = 0;
				if (moveDirection == Direction.Left)
				{
					speedX = -movespeed_max;
				}
				else
				{
					speedX = movespeed_max;
				}
				speedY = -jump_max;
			}

			if (moveDirection == Direction.Left)
			{
				speedX += brake_max;
				if (speedX > 0) speedX = 0;
			}
			else
			{
				speedX -= brake_max;
				if (speedX < 0) speedX = 0;
			}

			vx = (int)speedX; vy = (int)speedY;
		}
		
		bool falled = false;
		string imageName = "960-1";
		public override void Draw(Graphics g, int width, int height)
		{
			if (falled)
			{
				if (moveDirection == Direction.Left)
				{
					imageName = "960-1";
				}
				else
				{
					imageName = "961-1";
				}
				falled = false;
			}else if (moveDirection == Direction.Left)
			{
				if (actionCount == 150)
				{
					imageName = "960-3";
				}else if (actionCount > 120)
				{
					imageName = "960-2";
				}
			}
			else
			{
				if (actionCount == 150)
				{
					imageName = "961-3";
				}
				else if (actionCount > 120)
				{
					imageName = "961-2";
				}
			}

			g.DrawImage(ImageManager.Search(imageName), 0, 0);

			//HPゲージの描画
			g.FillRectangle(Brushes.White, 0, -10, width, 5);
			g.FillRectangle(new SolidBrush(Color.FromArgb((int)(255f*(1-hp/DEFAULT_HP)),(int)(255f*hp/DEFAULT_HP),0)), 0, -10, (float)width * hp / DEFAULT_HP, 5);
		}

		public override bool EventProc(EventType type, IStageObject sender, CollideDirection direction)
		{
			switch (type)
			{
				case EventType.Collided:
					
					if (direction == CollideDirection.Left)
					{
						moveDirection = Direction.Right;
						
					}
					else if(direction == CollideDirection.Right)
					{
						moveDirection = Direction.Left;
					}
					else if (direction == CollideDirection.Down)
					{
						falled = true;
					}
					break;
				case EventType.BeTouched:

					if (sender is MainCharacterStageObject)
					{
						sender.SignalProc(SignalType.SIGDAMAGE, 10, 0);
					}
					break;
				case EventType.BeCollided:
					break;
				case EventType.Touched:

					if (sender is MainCharacterStageObject)
					{
						sender.SignalProc(SignalType.SIGDAMAGE, 10, 0);
					}
					break;
			}
			

			return base.EventProc(type, sender, direction);
		}
		bool putskill = false;
		public override void SignalProc(SignalType type, int arg1, int arg2)
		{
			base.SignalProc(type,arg1,arg2);

			if (type == SignalType.SIGDAMAGE)
			{
					hp -= arg1;

					if (hp <= 0)
					{
						owner.Remove(this);
						if (!putskill)
						{
							putskill = true;
							owner.Add(new SkillItemStageObject(X, Y, owner));
						}
					}
			}
		}
	}
}
