#pragma once
#include "DXFrameWork.h"

namespace DXFW
{
/// <summary>愢柧</summary>
enum class Belong
{
	Ally,
	EnemyF,
	EnemyG,
	Item,
	Block,
	Etc
};

/// <summary>愢柧</summary>
class Object : public Model
{
friend class Layer;

protected:
	/// <summary>愢柧</summary>
	virtual bool RemoveCheck(Rect* area)
	{
		if(
			timer == this->lifeTime ||
			(isOutCheck && !figure->Hit( area ) )
			)
		{
			this->isRemove = true;
		}

		if (isRemove) Remove();

		return this->isRemove;
	}

	virtual void Attack(Object *hit )
	{
		hit->Damaged( power , attribute);
	}

public:
	bool isRemove;
	bool isOutCheck;
	double  power;
	int  attribute;
	int  timer;
	int  lifeTime;
	const Belong belong;

	Object( Figure *figure , Sprite *sprite , double power = 0 , Belong belong = Belong::Etc):
		Model( figure , sprite ),
		isOutCheck(true),
		isRemove(false),
		timer(0),
		lifeTime(-1),
		power(power),
		belong(belong)
	{}

	virtual ~Object(){}

	virtual void Update()
	{
		this->timer++;
		AnimeUpdate();
		this->Act();
	}

	int  GetTimer()
	{
		return this->timer;
	}
	void SetTimer(int timer)
	{
		this->timer = timer;
	}

	bool GetRemoveFlag()
	{
		return isRemove;
	}

	/// <summary>愢柧</summary>
	virtual void Act()
	{

	}

	/// <summary>愢柧</summary>
	virtual void Remove()
	{

	}

	/// <summary>愢柧</summary>
	virtual void React()
	{

	}

	/// <summary>愢柧</summary>
	virtual void Damaged(double damage, int attribute)
	{
		
	}

	/// <summary>愢柧</summary>
	void 慜恑(double 懍搙)
	{
		MovePolar(懍搙, GetAngle());
	}

	void 捈恑(double 懍搙 , double 妏搙)
	{
		MovePolar(懍搙, 妏搙);
	}

	void 挼偹曉傝(double 懍搙, double &妏搙, Rect &堏摦斖埻)
	{
		const double PAI = 3.1415926535897931;

		//斖埻奜偵偄傞
		if ( GetX() < 堏摦斖埻.GetLeft())
		{
			if (妏搙 > PAI / 2 && 妏搙 < PAI * 3 / 2)
			{
				妏搙 = -PAI - 妏搙;
			}
		}

		if ( GetX() > 堏摦斖埻.GetRight())
		{
			if (妏搙 < PAI / 2 || 妏搙 > PAI * 3 / 2)
			{
				妏搙 = PAI - 妏搙;
			}
		}
		else if (GetY() < 堏摦斖埻.GetTop())
		{
			if (妏搙 > PAI)
			{
				妏搙 = PAI * 2 - 妏搙;
			}
		}
		else if (GetY() > 堏摦斖埻.GetBottom())
		{
			if (妏搙 < PAI)
			{
				妏搙 = -妏搙;
			}
		}

		if (妏搙 < 0) 妏搙 += PAI * 2;
		if (妏搙 > PAI * 2) 妏搙 -= PAI * 2;

		MovePolar(懍搙, 妏搙);
	}

	bool 巜掕堏摦(double 懍搙, double 栚昗X, double 栚昗Y)
	{
		const double lx = 栚昗X - GetX();
		const double ly = 栚昗Y - GetY();

		if (lx * lx + ly * ly <= 懍搙 * 懍搙 )
		{
			SetPos(栚昗X, 栚昗Y);
			return true;
		}
		else{
			const double angle = atan2(ly, lx);
			MovePolar(懍搙, angle);
			return false;
		}		
	}

	struct 墌婳摴忬懺
	{
		墌婳摴忬懺( double 弶妏 ):
			妏搙(弶妏)
		{}

		double 妏搙;
		double 慜埵抲X;
		double 慜埵抲Y;		
		bool   弶婜壔僼儔僌 = false;
	};

	void 墌婳摴(double 妏懍搙 , 墌婳摴忬懺 &忬懺, double 敿宎X, double 敿宎Y)
	{
		if ( !忬懺.弶婜壔僼儔僌 )
		{
			忬懺.慜埵抲X = cos(忬懺.妏搙) * 敿宎X;
			忬懺.慜埵抲Y = sin(忬懺.妏搙) * 敿宎Y;
		}

		忬懺.妏搙 += 妏懍搙;

		const double nextX = cos(忬懺.妏搙) * 敿宎X;
		const double nextY = sin(忬懺.妏搙) * 敿宎Y;

		Move(nextX - 忬懺.慜埵抲X, nextY - 忬懺.慜埵抲Y);

		忬懺.慜埵抲X = nextX;
		忬懺.慜埵抲Y = nextY;
	}

	struct 怳摦忬懺
	{
		double 堏摦検X = 0;
		double 堏摦検Y = 0;
	};
	void 怳摦(怳摦忬懺 &忬懺 , double X怳暆 , double Y怳暆)
	{
		忬懺.堏摦検X = Rand::Get(-X怳暆, X怳暆) - 忬懺.堏摦検X;
		忬懺.堏摦検Y = Rand::Get(-Y怳暆, Y怳暆) - 忬懺.堏摦検Y;
		Move( 忬懺.堏摦検X , 忬懺.堏摦検Y );
	}

};
}