//@STATE=嶌惉拞
//@DESCRIPTION ---------------------------------------------------------------
// ZK01.cpp: ZK01 僋儔僗偺僀儞僾儕儊儞僥乕僔儑儞
//	棜楌丗
// $Log: ZK01.cpp,v $
// Revision 1.1  2001/10/28 21:40:37  fujiwara
// DLL偵暘妱
//
// Revision 1.9  2001/07/09 20:30:10  fujiwara
// 椺奜張棟偺慻傒崬傒
//
// Revision 1.8  2001/07/01 21:30:34  fujiwara
// 儊儌儕儕乕僋僠僃僢僋尒捈偟
//
// Revision 1.7  2001/07/01 01:03:45  fujiwara
// const 偺尒捈偟
// 攝抲僄僨傿僞偼搑拞宱夁
//
// Revision 1.6  2001/06/03 09:22:32  fujiwara
// 抧忋揋僋儔僗偍傛傃嶨嫑揋偺旝挷惍
//
// Revision 1.5  2001/05/29 01:12:10  fujiwara
// 嶨嫑僉儍儔(旘峴暔乯偺嶌惉丅攝抲僄僨傿僞偺僶僌廋惓
//
// Revision 1.4  2001/05/17 12:31:29  fujiwara
// 抧忋揋僒儞僾儖嶌惉
//
// Revision 1.3  2001/05/13 20:04:24  fujiwara
// 儊儌儕乕儕乕僋偑敪惗偟偨偺偱庢傝彍偔丅
// 帺抏偺僒僂儞僪僥僗僩
//
// Revision 1.2  2001/05/12 00:48:35  fujiwara
// 俠倁俽傪摫擖偟丄儘僌昞帵傪捛壛
//
//@DESCRIPTION_END -----------------------------------------------------------
//@AUTHOR=S.F.
// Copyright (C) 2000 Satoshi Fujiwara. All Rights Reserved.
///////////////////////////////////////////////////////////////////////////////
#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 "AbstractEnemy.h"
#include "EnemyBullets.h"
#include "DispositionControl.h"
#include "Effects.h"
#include "Explosion.h"


#include "ZK01.h"
#include "EB01.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

using namespace sf::application;
using namespace sf::application::enemy;

sf::system::console::sprite::PSprite ZK01::mspSprite;

int ZK01::mBalletTimer = 0;
int ZK01::mBalletTimerInit = 0;

const sf::system::console::sprite::Info ZK01::msSpriteInfo = 
{
	"media\\zk01.png",0.0f,0.0f,64.0f,64.0f,0xff000000,console::sprite::SPRITE2D
};

const int ZK01::SCORE = 20;

// 僐儞僗僩儔僋僞 亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅
ZK01::ZK01()
{
}// ZK01()

// 僨僗僩儔僋僞 亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅
ZK01::~ZK01()
{
	Enemys::instance()->clear(index());
}// ~ZK01()


// 弶婜壔 亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅
void ZK01::initialize()
{
	// 摉傝敾掕 
	mHitRect.offsetTop(-24.0f);
	mHitRect.offsetBottom(24.0f);
	mHitRect.offsetLeft(-24.0f);
	mHitRect.offsetRight(24.0f);

	mHitRect.isChecking(true);
	mHitRect.characterPtr(this);
	direction((float)atan2((double)(game()->player()->y() - y()),(double)(game()->player()->x() - x())));
	
 	mDeltaTime = sf::system::instance()->deltaTime();
	mDeltaTimeRev = 1.0f / mDeltaTime;
	speed(3.0f * mDeltaTime);

	mDx = cosf(direction()) * speed();
	mDy = sinf(direction()) * speed();
	
	// Body 晹暘 
	mObj.index(0);

	if(!mspSprite.get())
		load();

	mObj.sprite(mspSprite.get());
	
	mObj.z(z() + (float)index() * 0.0001f );
	mObj.x(x());
	mObj.y(y());
	mObj.visibility(true);
	mObj.color(0xffffffff);
	mObj.drawMode(sf::system::console::DRAW_TRANS);
	mObj.angle(direction());

	// Light晹暘
	mObjLight.z(z() + (float)index() * 0.0001f + 0.00001f);
	mObjLight.x(x());
	mObjLight.y(y());

	initLight();
	
	mbHit = false;
	mDirCount = 18.0f * mDeltaTime;
	mDirSpeed = (rand() & 1) ? -0.02f * mDeltaTime : 0.02f * mDeltaTime;
	
	mState = MOVE;
	
	mShootTimer = (int)((20.0f + (float)(rand() & 0x7)) * mDeltaTimeRev);
	mbShoot = false;
	mAccel = 0.2f * mDeltaTime;

	if(!mBalletTimerInit)
	{
		mBalletTimer = mBalletTimerInit = (3.0f - game()->currentDifficulty()) * mDeltaTimeRev;
		mbShoot = false;
	} else {
		mBalletTimer--;
		if(!mBalletTimer){
			mBalletTimer = mBalletTimerInit;
			mBalletTimerInit = (3.0f - game()->currentDifficulty()) * mDeltaTimeRev;
			mbShoot = false;
		} else {
			mbShoot = true;
		}
	}

}// initialize()

// 僥僋僗僠儍偺儘乕僪 亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅
void ZK01::load(void){
	using namespace sf::system::console::sprite;
	if(!mspSprite.get()){
		sf::system::console::sprite::PSprite ptemp(sf::system::instance()->console()->createSprite(&msSpriteInfo)); 
		mspSprite = ptemp;
	}
	Bullets::instance()->load(Bullets::EB01);
	AbstractEnemy::loadLight();
}// load()

// 僥僋僗僠儍偺傾儞儘乕僪 亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅
void ZK01::unload(void){
	using namespace sf::system::console::sprite;
	PSprite ptmp(NULL);
	mspSprite = ptmp;
}// unload()

// 揋偺摦嶌 亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅
void ZK01::move()
{
	using namespace sf::application;
	float tx,ty;

	y(y() + mDy);
	x(x() + mDx);

	switch(mState){
	case MOVE:
		// 摦偒偺儊僀儞
		if(!--mDirCount){
			mDirCount = (int)(2.0f * mDeltaTimeRev);
			direction(direction() + mDirSpeed);
			speed(speed() + mAccel);
			mDx = cosf(direction()) * speed();
			mDy = sinf(direction()) * speed();
		}

		// 嵗昗埵抲丄妏搙偺愝掕		
		mObj.x(x());
		mObj.y(y());
		mObj.angle(direction());

		mObjLight.x(x());
		mObjLight.y(y());
		moveLight();
		
		// 抏敪幩 
		if(!mbShoot){
			if(!(mShootTimer--) ){
				tx = x() + 20.0f * cosf(direction());
				ty = y() + 20.0f * sinf(direction());
				EB01 *peb = (EB01 *)Bullets::instance()->create(Bullets::EB01,tx,ty,z() + 0.01f,game());
				peb->direction(Util::calcDirection(x(),y(),game()->player()->x(),game()->player()->y()));
				mbShoot = true;
			}
		}
		
		// 抏偵偁偨偭偨偐偳偆偐偺敾掕
		if(mbHit)
		{
			die();
			return;
		}
		break;// case MOVE:

	case DEAD:
		if(!--mDirCount){
			delete this;
			return;
		}
		mObj.x(x());
		mObj.y(y());
		break;
	}

	// 夋柺徚嫀敾掕
	if(Util::checkArea(x(),y(),64.0f,64.0f))
		delete this;
}//move()



// 抏偵偁偨偭偨偲偒偺張棟 亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅
void ZK01::hit(Character * const pDest)
{
	mHitRect.isChecking(false);
	mbHit = true;

};

// 揋偺惗惉 亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅
sf::application::Character * const ZK01::create(float x,float y ,float z,sf::application::scene::Game * const pGame,int index)
{
	ZK01 * ptemp = new ZK01;
	ptemp->x(x);
	ptemp->y(y);
	ptemp->z(z);
	ptemp->index(index);
	ptemp->game(pGame);
	ptemp->initialize();
	return ptemp;
;}// create

//丂巰傫偩偲偒偺張棟  亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅
void ZK01::die(void)
{
	effect::Explosion *pExp = (effect::Explosion*)effect::Effects::instance()->create(effect::Effects::EXPLOSION,x(),y(),0.1f,game(),this);
	if(pExp){
		pExp->scaling(1.5f);
	}
	mDirCount = 2;
	mState = DEAD;
	game()->score(game()->score() + SCORE);

};// die()

//  慡敋敪僀儀儞僩 亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅
void ZK01::allBomb(void)
{
	die();
}// allBomb()