//@STATE=作成中
//@DESCRIPTION ---------------------------------------------------------------
// Target.cpp: Target クラスのインプリメンテーション
//	履歴：
// $Log: Target.cpp,v $
// Revision 1.1  2001/10/28 21:40:37  fujiwara
// DLLに分割
//
// Revision 1.4  2001/07/09 20:30:10  fujiwara
// 例外処理の組み込み
//
// Revision 1.3  2001/07/01 21:30:34  fujiwara
// メモリリークチェック見直し
//
// Revision 1.2  2001/07/01 01:03:45  fujiwara
// const の見直し
// 配置エディタは途中経過
//
// Revision 1.1  2001/06/10 21:16:43  fujiwara
// ターゲット追加（地上用）
//
//@DESCRIPTION_END -----------------------------------------------------------
//@AUTHOR=S.F.
///////////////////////////////////////////////////////////////////////////////
#pragma warning( disable : 4786 )	//STLの警告外し

// メモリーリーク検出用
#include "sfdebug.h"

#include <string>
#include <vector>
#include <memory>
#include <stack>
#include <list>
#include <iostream>
#include <fstream>
#include <map>

#include "stdio.h"
#include "math.h"

#include "exception.h"
#include "singleton.h"
#include "system.h"
#include "console.h"
#include "input.h"
#include "sound.h"

#include "Obj.h"
#include "Obj2D.h"
#include "Obj3D.h"
#include "Obj3DL.h"
#include "ObjRectangle.h"
#include "ObjQuadrangle.h"

#include "Background.h"
#include "Scene.h"
#include "main.h"

#include "character.h"
#include "Hit.h"
#include "MyShip.h"
#include "Game.h"
#include "Enemys.h"
#include "Armys.h"
#include "DispositionControl.h"
#include "AbstractCharacter.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

#include "Target.h"
#include "MyGroundBullets.h"
//////////////////////////////////////////////////////////////////////
// 構築/消滅
//////////////////////////////////////////////////////////////////////
using namespace sf::application::player;
using namespace sf::system;
const console::sprite::Info Target::msSpriteInfo = {
	"media\\target.png",0.0f,0.0f,0.0f,0.0f,0xff000000,console::sprite::SPRITE2D
};

std::auto_ptr<sf::system::console::sprite::Sprite> Target::mpSprite;

const float Target::OFFSET_Y = -156.0f;
int Target::mSoundID = 0;
 
Target::Target(sf::application::scene::Game *pGame)
{
	game(pGame);
}

Target::~Target()
{

}

void Target::initialize(void)
{
	
	x(game()->player()->x());
	y(game()->player()->y());
	z(game()->player()->z());

	mHitRect.offsetTop(-4.0f);
	mHitRect.offsetLeft(-4.0f);
	mHitRect.offsetBottom(4.0f);
	mHitRect.offsetRight(4.0f);
	mHitRect.isChecking(true);
	mHitRect.characterPtr(this);
	mbHit = false;
	
	mObj.x(x());
	mObj.y(y() + OFFSET_Y);
	mObj.z(z());
	mObj.color(0x80ffffff);
	mObj.drawMode(console::DRAW_TRANS);

	mObj.sprite(mpSprite.get());
	mObj.visibility(true);

	mObjMark.x(x());
	mObjMark.y(y() + OFFSET_Y);
	mObjMark.z(z());
	mObjMark.color(0x80ff0000);
	mObjMark.drawMode(console::DRAW_TRANS);

	mObjMark.sprite(mpSprite.get());
	mObjMark.visibility(false);
	
	life(0);

	speed(0.0f);
	direction(0.0f);
	mTimerPeriod = (int)( 3.0f / sf::system::instance()->deltaTime());
	mTimer = mTimerPeriod;
}// initialize()

// 動作 
void Target::move(void)
{
	// 
	mTimer--;
	hitCheck(sf::application::enemy::Armys::instance());
	if(mTimer <= 0){
		mTimer = mTimerPeriod;
		if(mbHit){
			sf::system::instance()->sound()->play(mSoundID,1,false,false);
			mbHit = false;
			if(mObj.color() == 0xc0ff0000)
				mObj.color(0x80ffffff);
			else
				mObj.color(0xc0ff0000);
			mObj.drawMode(console::DRAW_ADD);
		} else {
			mObj.color(0x80ffffff);
			mObj.drawMode(console::DRAW_TRANS);
		}
	};

	x(game()->player()->x());
	y(game()->player()->y() + OFFSET_Y );
	z(game()->player()->z());

	mObj.x(x());
	mObj.y(y());
	mObj.z(z());
	
	//

	if(sf::system::instance()->input()->buttonB(true))
	{
		bullet::GroundBullets::instance()->create(
			bullet::GroundBullets::NORMAL,
			game()->player()->x(),
			game()->player()->y(),
			game()->player()->z(),
			game());
	}

}// move()

// リソースのロード //
void Target::load(void)
{
	// ビットマップ 
	using namespace sf::system::console::sprite;
	if(!mpSprite.get()){
		std::auto_ptr<sf::system::console::sprite::Sprite> ptemp(sf::system::instance()->console()->createSprite(&msSpriteInfo)); 
		mpSprite = ptemp;
	}
	
	// サウンドデータ
	using namespace sf::system;
	mSoundID = instance()->sound()->load("target.wav");

//		mpSprite = std::auto_ptr<sf::system::console::sprite::Sprite *>(sf::system::instance()->console()->createSprite(&mSpriteInfo));
}// load
