//@STATE=嶌惉拞
//@DESCRIPTION ---------------------------------------------------------------
// MyBullets.cpp: MyBullets 僋儔僗偺僀儞僾儕儊儞僥乕僔儑儞
//	棜楌丗
// $Log: MyBullets.cpp,v $
// Revision 1.1  2001/10/28 21:40:36  fujiwara
// DLL偵暘妱
//
// Revision 1.8  2001/07/09 20:30:09  fujiwara
// 椺奜張棟偺慻傒崬傒
//
// Revision 1.7  2001/07/01 21:30:34  fujiwara
// 儊儌儕儕乕僋僠僃僢僋尒捈偟
//
// Revision 1.6  2001/07/01 01:03:44  fujiwara
// const 偺尒捈偟
// 攝抲僄僨傿僞偼搑拞宱夁
//
// Revision 1.5  2001/06/10 21:17:01  fujiwara
// 僞乕僎僢僩捛壛乮抧忋梡乯
//
// Revision 1.4  2001/05/29 01:12:35  fujiwara
// 嶨嫑僉儍儔(旘峴暔乯偺嶌惉丅攝抲僄僨傿僞偺僶僌廋惓
//
// Revision 1.3  2001/05/13 20:04:24  fujiwara
// 儊儌儕乕儕乕僋偑敪惗偟偨偺偱庢傝彍偔丅
// 帺抏偺僒僂儞僪僥僗僩
//
// Revision 1.2  2001/05/12 00:49:34  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"
#include "MyBullets.h"

using namespace sf::application;
using namespace sf::system::console;
using namespace sf::application::player::bullet;

//////////////////////////////////////////////////////////////////////
// Bullets 
//////////////////////////////////////////////////////////////////////

const int Bullets::INIT_BULLETS = 3;

// 僆僽僕僃僋僩惗惉儊僜僢僪傊偺億僀儞僞 
const Bullets::CreateFuncPtr Bullets::mpFunctions[MAX_TYPE] = {
	&Normal::create
};

// 僥僋僗僠儍儘乕僟乕傊偺億僀儞僞
const Bullets::LoaderFuncPtr Bullets::mpLoaders[MAX_TYPE] = {
	&Normal::load
};

int Normal::mSoundID = 0;
int Normal::mSoundIDB = 0;

// 僔儞僌儖僩儞僷僞乕儞傪抂愜傞偨傔偺儅僋儘
IMPLEMENT_SINGLETON(Bullets,Bullets)

// 僐儞僗僩儔僋僞 亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅
Bullets::Bullets()
{
	int i;
	for(i = 0;i < MAX_BULLETS;i++){
		mpBullets[i] = NULL;
	}

	mMax = INIT_BULLETS;
	mCount = 0;
}// Bullets()

// 僨僗僩儔僋僞 亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅
Bullets::~Bullets()
{
	int i;

	for(i = 0;i < MAX_BULLETS;i++)
	{
		if(mpBullets[i]){
			delete mpBullets[i];
			mpBullets[i] = NULL;
		}
	}


}// ~Bullets()

// 弶婜壔 亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅
void Bullets::initialize(){
	
	int i;
	for(i = 0;i < MAX_BULLETS;i++)
	{
		if(mpBullets[i]){
			delete mpBullets[i];
			mpBullets[i] = NULL;
		}
	}

	// 價僢僩儅僢僾偺儘乕僪
	for(i = 0;i < MAX_TYPE;i++){
		(*mpLoaders[i])();
	}
	mCount = 0;
}// initialize()

// 抏偺堏摦 亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅
void Bullets::move(void)
{
	int count = 0;
	for(int i = 0;i < MAX_BULLETS;i++)
	{
		if(mpBullets[i]){
			mpBullets[i]->move();
		}
	}
}// move()

// 抏傪惗惉偡傞 亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅
const bool Bullets::create(const TYPE id,const float x,const float y,const float z,sf::application::scene::Game * const pGame)
{
	if(id >= MAX_TYPE)
		return false;

	if(mCount >= mMax)
		return false;
	
	for(int i = 0 ; i < MAX_BULLETS; i++){
		if(mpBullets[i] == NULL){
			mpBullets[i] = (*mpFunctions[id])(x,y,z,pGame,i);
			mCount++;
			return true;
		}
	}
	return false;
}// create()

// 摉傝敾掕 亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅
const bool Bullets::hitCheck(Character * const pDest){
	bool return_ = false;

	for(int i = 0;i < MAX_BULLETS;i++){
		if(mpBullets[i]){
			if(mpBullets[i]->hitCheck(pDest)){
				mpBullets[i]->hit(pDest);
				pDest->hit(mpBullets[i]);
				return_ = true;
			};
		}
	}
	return return_;
}// hitCheck(Character * const pDest)

const bool Bullets::hitCheck(Characters * const pDest){
	bool return_ = false;

	for(int i = 0;i < MAX_BULLETS;i++){
		if(mpBullets[i]){
			if(pDest->hitCheck(mpBullets[i]))
				return_ = true;
		}
	}

	return return_;
}// hitCheck(Characters * const pDest)


// 亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅
// 僲乕儅儖抏
// 亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅

const console::sprite::Info Normal::mSpriteInfo = {
	"media\\normal.png",0.0f,0.0f,0.0f,0.0f,0xff000000,console::sprite::SPRITE2D
};

const console::sprite::Info Normal::mSpriteBInfo = {
	"media\\normalb.png",0.0f,0.0f,32.0f,32.0f,0xff000000,console::sprite::SPRITE2D
};

const float Normal::mTextureHeight = 32.0f;
const float Normal::mTextureWidth = 32.0f;

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

// 弶婜壔 亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅
void Normal::initialize()
{
	mHitRect.offsetTop(-8.0f);
	mHitRect.offsetLeft(-2.0f);
	mHitRect.offsetBottom(8.0f);
	mHitRect.offsetRight(2.0f);
	mHitRect.isChecking(true);
	mHitRect.characterPtr(this);
	mbHit = false;
	mObj.x(x());
	mObj.y(y());
	mObj.z(z() + Z_OFFSET);
	mObj.color(0xc0ffffff);
	mObj.drawMode(DRAW_ADD);

	mObj.sprite(mpSprite.get());
	mObj.visibility(true);
	
	speed(-48.0f * sf::system::instance()->deltaTime());
	direction(sf::system::PI / 2.0f);
	mObj.angle(direction());
	mBeforeY = y();
	sf::system::instance()->sound()->play(mSoundID,1,false,false);
	life(1);
	mState = MOVE;
}// Normal::initialize

// 抏偺摦偒 亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅
void Normal::move(void){
	
	switch(mState)
	{
	case MOVE:
		mBeforeY = y();
		y(y() + speed());
		mObj.y(y());
	
		if(y() < (viewport::Y - mTextureHeight / 2.0f)){
			delete this;
			return;
		}
		// 摉傝敾掕偺曗惓 
		mHitRect.offsetBottom(mHitRect.offsetTop() - speed() );
		break;
	case BOUND:
		mAnimCount++;
		if(mAnimCount > 7.0f){
			mState = DEAD;
			break;
		}
		mObj.index(mAnimCount);
		break;
	case DEAD:
		delete this;
		return;
	}
}// Normal::move()

// 僥僋僗僠儍丒僒僂儞僪偺儘乕僪 亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅
void Normal::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(&mSpriteInfo)); 
		mpSprite = ptemp;
		std::auto_ptr<sf::system::console::sprite::Sprite> ptemp1(sf::system::instance()->console()->createSprite(&mSpriteBInfo)); 
		mpSpriteB = ptemp1;
	}
	using namespace sf::system;
	mSoundID = instance()->sound()->load("beam.wav");
	mSoundIDB = instance()->sound()->load("bound.wav");

}// Normal::load()

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