//@STATE=嶌惉拞
//@DESCRIPTION ---------------------------------------------------------------
// MyGroundBullets.cpp: MyGroundBullets 僋儔僗偺僀儞僾儕儊儞僥乕僔儑儞
//	棜楌丗
// $Log: MyGroundBullets.cpp,v $
// Revision 1.1  2001/10/28 21:40:36  fujiwara
// DLL偵暘妱
//
// Revision 1.4  2001/07/09 20:30:09  fujiwara
// 椺奜張棟偺慻傒崬傒
//
// Revision 1.3  2001/07/01 21:30:34  fujiwara
// 儊儌儕儕乕僋僠僃僢僋尒捈偟
//
// Revision 1.2  2001/07/01 01:03:44  fujiwara
// const 偺尒捈偟
// 攝抲僄僨傿僞偼搑拞宱夁
//
// Revision 1.1  2001/06/10 21:17:34  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"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

#include "MyGroundBullets.h"
#include "Target.h"
#include "Effects.h"
#include "Explosion.h"
#include "Enemys.h"


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

//////////////////////////////////////////////////////////////////////
// GroundBullets 
//////////////////////////////////////////////////////////////////////

const int GroundBullets::INIT_GROUNDBULLETS = 1;

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

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

int GroundNormal::mSoundID = 0;

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

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

	mMax = INIT_GROUNDBULLETS;
	mCount = 0;
}// GroundBullets()

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

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


}// ~GroundBullets()

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

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

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

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

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

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

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

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

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

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

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

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

const float GroundNormal::mTextureHeight = 32.0f;
const float GroundNormal::mTextureWidth = 32.0f;
const float GroundNormal::INIT_SPEED = -3.0f;
const float GroundNormal::INIT_ACCEL = 1.0f;
std::auto_ptr<sf::system::console::sprite::Sprite> GroundNormal::mpSprite;

// 弶婜壔 亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅
void GroundNormal::initialize()
{
	mHitRect.offsetTop(-4.0f);
	mHitRect.offsetLeft(-4.0f);
	mHitRect.offsetBottom(4.0f);
	mHitRect.offsetRight(4.0f);

	mHitRect.isChecking(false);
	mHitRect.characterPtr(this);
	mbHit = false;
	
	mObj.x(x());
	mObj.y(y());
	mObj.z(z() + GROUND_Z_OFFSET);

	mObj.color(0xc0ffffff);
	mObj.drawMode(DRAW_ADD);

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

	mObj.scalingX(1.0f);
	mObj.scalingY(1.0f);

	mObjTarget.x(x());
	mObjTarget.y(y() + Target::OFFSET_Y);
	mObjTarget.z(z() + GROUND_Z_OFFSET);

	mObjTarget.color(0x40ff0000);
	mObjTarget.drawMode(DRAW_ADD);

	mObjTarget.sprite(Target::sprite());
	mObjTarget.visibility(true);

	mObjTarget.scalingX(1.0f);
	mObjTarget.scalingY(1.0f);
	
	speed(INIT_SPEED * sf::system::instance()->deltaTime());
	
	mOffsetY = - Target::OFFSET_Y;

	direction(sf::system::PI);
	life(1);
	sf::system::instance()->sound()->play(mSoundID,1,false,false);
	mState = MOVE;
}// GroundNormal::initialize

// 抏偺摦偒 亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅亅
void GroundNormal::move(void){
	
	
	switch(mState){
	case MOVE:
		main();
		return;
	case BOMB:
		die();
		return;
	}


}// GroundNormal::move()

void GroundNormal::main(void)
{
		y(y() + speed());
		mObj.angle(mObj.angle() + 0.25f);
		mObj.y(y());
		mObj.scalingX(mObj.scalingX() - 0.02f);
		mObj.scalingY(mObj.scalingY() - 0.02f);
		
		mObjTarget.y(mObjTarget.y() + game()->scrollSpeed());
		
		mOffsetY += speed();
		mOffsetY -= game()->scrollSpeed();

		if(mOffsetY < 0.0f)
		{
			mState = BOMB;
			mHitRect.isChecking(true);
//			mTimer = 3;
		}
		

		if(mbHit || y() < (viewport::Y - mTextureHeight / 2.0f)){
			delete this;
			return;
		}
		
		// 摉傝敾掕偺曗惓 

		mHitRect.offsetBottom(mHitRect.offsetTop() - speed() );
}// main()

void GroundNormal::die(void)
{
	using namespace sf::application;
	direction(3.0f * sf::system::PI / 2.0f );
	speed( - game()->scrollSpeed());

	effect::Explosion *pExp = (effect::Explosion*)effect::Effects::instance()->create(effect::Effects::EXPLOSION,x(),y(),enemy::Z_OFFSET_ARMYS - 0.01f,game(),this);
	if(pExp){
		pExp->scaling(0.8f);
	}
	delete this;

}// die()

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

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