
#include "stdafx.h"
#include "Game.h"
#include "../WCode/Math/Math.h"

const float CONST_sip_zivotu = 0.01f;	// zed
const int CONST_koule_zivotu = 300;		// jednotkz
const float CONST_koule_zed = 0.3f;	// zed


//////////////////////////////////////////////////////////////////////////
// Kouzla - vsechny mozny kouzla
//////////////////////////////////////////////////////////////////////////

int g_kam, g_pen, g_kam2, g_pen2;
bool g_bDrawSur=0, g_bDrawSur2;
int g_kdo, g_kdo2;
static float y, y2;
uint texture_odpad;

CParticleEmitor emSur, emSur2;
CParticleEmitor emKou;

void DelSur()
{
	g_bDrawSur=0;
	g_bDrawSur2=0;
}

void DrawBBox( TBBox b )
{
//	glPushMatrix();
//	glLoadIdentity();
	glDisable(GL_TEXTURE_2D);
	glColor3f(0, 1, 0);
	glBegin( GL_LINE_STRIP );
		glVertex3f(b.xMin, b.yMax, b.zMin);
		glVertex3f(b.xMax, b.yMax, b.zMin);
		glVertex3f(b.xMax, b.yMax, b.zMax);
		glVertex3f(b.xMin, b.yMax, b.zMax);

		glVertex3f(b.xMin, b.yMax, b.zMax);
		glVertex3f(b.xMax, b.yMax, b.zMax);
		glVertex3f(b.xMax, b.yMin, b.zMax);
		glVertex3f(b.xMin, b.yMin, b.zMax);

		glVertex3f(b.xMax, b.yMax, b.zMax);
		glVertex3f(b.xMax, b.yMax, b.zMin);
		glVertex3f(b.xMax, b.yMin, b.zMin);
		glVertex3f(b.xMax, b.yMin, b.zMax);

		glVertex3f(b.xMin, b.yMax, b.zMin);
		glVertex3f(b.xMin, b.yMax, b.zMax);
		glVertex3f(b.xMin, b.yMin, b.zMax);
		glVertex3f(b.xMin, b.yMin, b.zMin);

		glVertex3f(b.xMin, b.yMin, b.zMin);
		glVertex3f(b.xMax, b.yMin, b.zMin);
		glVertex3f(b.xMax, b.yMin, b.zMax);
		glVertex3f(b.xMin, b.yMin, b.zMax);

		glVertex3f(b.xMin, b.yMax, b.zMin);
		glVertex3f(b.xMax, b.yMax, b.zMin);
		glVertex3f(b.xMax, b.yMin, b.zMin);
		glVertex3f(b.xMin, b.yMin, b.zMin);

	glEnd();
	glColor3f(1,1,1);
//	glPopMatrix();
}

void PridejSuroviny(THrac &hrac, int kdo, uint texture) // 0-hrac, 1 -prot
{
	hrac.kameny += (g_aktKolo/2 +1) * 1200;
	hrac.penize += (g_aktKolo/2 +1) * 1000;
	textREC.Add("kameni + ", (g_aktKolo/2 +1) * 1200);
	if(kdo == 0)
	{
		g_kam = (g_aktKolo/2 +1) * 1200;
		g_pen = (g_aktKolo/2 +1) * 1000;
		InfoText.Add( W_GetString("+ %d zlata, + %d kamení", g_pen, g_kam ), RGBf_GREEN );
		g_bDrawSur = 1;
		g_kdo = kdo;
		y = 0;
		// emitor
		TRGBf color[2] = {1,1,1, 1,1,1};
		emSur.DeleteParticle();
		emSur.SetColor(color[0], color[1]);
		emSur.SetAtribut( CVector3(-4,0,0), 0, 20, 0.01f, 1.0f );
		emSur.SetTexture(texture);
		emSur.Play();
		emSur.Create( 20 );
		emSur.Stop();
	}else
	if(kdo == 1)
	{
		g_kam2 = (g_aktKolo/2 +1) * 1200;
		g_pen2 = (g_aktKolo/2 +1) * 1000;
		g_bDrawSur2 = 1;
		g_kdo2 = kdo;
		y2 = 0;
		// emitor
		TRGBf color[2] = {1,1,1, 1,1,1};
		emSur2.DeleteParticle();
		emSur2.SetColor(color[0], color[1]);
		emSur2.SetAtribut( CVector3(4,0,0), 0, 20, 0.01f, 1.0f );
		emSur2.SetTexture(texture);
		emSur2.Play();
		emSur2.Create(20);
		emSur2.Stop();
	}
}

void DrawEfektSuroviny()
{
	char bFog = glIsEnabled( GL_FOG ); //new
	if(bFog) glDisable( GL_FOG );
	if(g_kdo == 0 && g_bDrawSur)
	{	
		glColor3ub(255, 0xD7, 00);
		//g_font.Print( W_PrevedIntXNaScreen(30, SCRWidth), W_PrevedIntYNaScreen(500-y, SCRHeight), " +%d kamení ", g_kam);
		//g_font.Print( W_PrevedIntXNaScreen(30, SCRWidth), W_PrevedIntYNaScreen(480-y, SCRHeight), " +%d zlato", g_pen);
		y += (1*60)/g_fps.GetFPS();
		if(y >= 150 && emSur.IsShowParticle()==0)
		{
			g_bDrawSur=0;
			y=0;
		}
		glDisable(GL_ALPHA_TEST);
		emSur.Render( g_rotate.x, g_rotate.y );
		if( !g_pausa ) emSur.Update( g_fps.GetFPS() );
	}
	if(g_kdo2 == 1 && g_bDrawSur2)
	{
		glColor3ub(255, 0xD7, 00);
		//g_font.Print( W_PrevedIntXNaScreen(SCRWidth-140, SCRWidth), W_PrevedIntYNaScreen(500-y2, SCRHeight), " +%d kamení ", g_kam2);
		//g_font.Print( W_PrevedIntXNaScreen(SCRWidth-140, SCRWidth), W_PrevedIntYNaScreen(480-y2, SCRHeight), " +%d zlato", g_pen2);
		y2 += (1*60)/g_fps.GetFPS();
		if(y2 >= 150 && emSur2.IsShowParticle()==0 )
		{
			g_bDrawSur2=0;
			y2=0;
		}
		glDisable(GL_ALPHA_TEST);
		emSur2.Render( g_rotate.x, g_rotate.y );
		if( !g_pausa ) emSur2.Update( g_fps.GetFPS() );
	}
	if(bFog) glEnable(GL_FOG);
}

CKouzloKC::CKouzloKC(THrac &j)
{
	_scena = &j;
	_stav = 0;
	_citac = 0;
}

CKouzloKC::~CKouzloKC()
{

}

void CKouzloKC::Draw(uint texture_id)
{
	if(!_stav) return;
	TRect screen={0, 0, 640, 480};
	screen = W_PrevedRectNaScreen(screen, SCRWidth, SCRHeight);
	C_2DOBJECT_0 obj(texture_id, screen);
	static float cit=0.2;
	static float ink_cit=0.005f;
	cit += ink_cit;
	if(cit > 0.6f) ink_cit = -ink_cit;
	if(cit < 0.1f) ink_cit = -ink_cit;
	glColor4f(1, 1, 1, cit);
	obj.SetAplha(0);
	glEnable(GL_BLEND);
	obj.Render();
	glDisable(GL_BLEND);

	static uint old_cas=0, new_cas=0;
	new_cas = timeGetTime();
	int kolik=0;
	if(old_cas != 0) kolik = new_cas - old_cas;
	_citac += kolik;
	g_silaPlus = 1;
	if(_citac >= 30000) 
	{
		_stav = 0;		// 30 secund
		_citac=0;
		g_silaPlus = 0;
	}
	old_cas = timeGetTime();

	// zmena hodnot	
}

void CKouzloKC::Enable()
{
	_stav = 1;
	_citac = 0;
}

void CKouzloKC::Disable()
{
	_stav = 0;
	_citac = 0;
}

void Vysati_soupere(THrac &ja, THrac &souper, int kdo, uint texture_id )
{
	int p = souper.penize/4;
	int k = souper.kameny/4;
	int m = souper.mana/5;

	souper.penize -= p;
	souper.kameny -= k;
	souper.mana	  -= m;

	ja.penize += p;
	ja.kameny += k;
	ja.mana += m;

	// Efekt vlny //
	if(kdo==0) g_scenaVlna.AddVlna( texture_id, -4.5, 0.015 );	
	if(kdo==1) g_scenaVlna.AddVlna( texture_id, 3.8, -0.015 );
	if(kdo==0) InfoText.Add( W_GetString("+ %d zlata, + %d kamení, + %d many", p, k, m ), RGBf_GREEN );
	if(kdo==1) InfoText.Add( W_GetString("- %d zlata, - %d kamení, - %d many ", p, k, m ), RGBf_RED );
	g_zvuk.Play( g_idZvuk[13] );
}

// SPOMAL
cKouzloSpomal::cKouzloSpomal(THrac &j) // prtoivnik
{
	_scena = &j;
	_stav = 0;
}

cKouzloSpomal::~cKouzloSpomal()
{
}

void cKouzloSpomal::Draw()
{
	if(!_stav) return;
	// efekt nic, jenom casovac
	static uint old_cas=0, new_cas=0;
	new_cas = timeGetTime();
	int kolik=0;
	if(old_cas != 0) kolik = new_cas - old_cas;
	
	_citac += kolik;
	if(_citac >= 12000) 
	{
		_stav = 0;		
		_citac=0;
		_scena->speedPlus = 0;
	}
	old_cas = timeGetTime();
}

void cKouzloSpomal::Enable()
{
	_scena->speedPlus = -1;
	_stav = 1;
	_citac = 0;
}

void C_FIREBOOL_0::NewFirebool(uint tex_id1, uint tex_id2, CVector3 pos_od, CVector3 pos_do, CVector3 pPos, float wh, int kdo)//0hrac,1prot
{
	TFirebool temp;
	temp.pos = pos_od;
	temp.kam = pos_do;
	temp.pPos = pPos;
	temp.texId = tex_id2;
	temp.wh = wh;
	temp.show = 1;
	temp.kdo = kdo;
	if(kdo == 0) 
	{
		pos_od.x +=4;
		temp.em1.SetAtribut( pos_od, -90, 30, 0.05f, 1.2f);
	}
	if(kdo == 1) 
	{
		pos_od.x -=4;
		temp.em1.SetAtribut( pos_od, 90, 30, 0.05f, 1.2f);
	}
	temp.col = 0;
	temp.em1.SetTexture( tex_id1 );
	TRGBf color[2] = {1,1,0, 1,0,0};
	temp.em1.SetColor(color[0], color[1]);
	temp.em1.SetStartVel(0.5f);
	temp.em1.Create(60);
	_vFireBool.push_back(temp);
	g_zvuk.Play( g_idZvuk[12] );
}

void C_FIREBOOL_0::Render()
{
	char bFog = glIsEnabled(GL_FOG); //new
	if(bFog) glDisable(GL_FOG);
	if(g_light) glDisable(GL_LIGHTING);
	std::vector<TFirebool>::iterator i = _vFireBool.begin();
	for(; i != _vFireBool.end(); i++)
	{
		if(!i->show) continue;
		float w  = i->wh;
		float h = w;
		CVector3 p = i->pos;
		TVector3 vec[4] = {p.x,	p.y,	p.z,
						p.x+w,	p.y,	p.z,
						p.x+w,	p.y+h,	p.z,
						p.x,	p.y+h,	p.z};

		i->em1.SetEmitorPos( CVector3(p.x, 0, 0) );
		glDisable(GL_ALPHA_TEST);
		i->em1.Render( g_rotate.x, g_rotate.y );
		if( !g_pausa ) i->em1.Update( g_fps.GetFPS() );
	}
	if(g_light) glEnable(GL_LIGHTING);
	if(bFog) glEnable(GL_FOG);
}

void C_FIREBOOL_0::Move(float time)
{
	std::vector<TFirebool>::iterator i = _vFireBool.begin();
	for(; i != _vFireBool.end(); i++)
	{
		if( i->show != 1) continue;
		i->pos = i->pos+(i->pPos*60)/time; 
	}
}

void C_FIREBOOL_0::Coli(float zed, float hrad, THrac &prot, int kdo)
{
	std::vector<TFirebool>::iterator i = _vFireBool.begin();
	for(; i != _vFireBool.end(); i++)
	{	
		if( i->show == 0) continue;
		if( (( (i->pos.x+i->wh/2 > zed) && (kdo==0) && (kdo==i->kdo)) || ( (i->pos.x+i->wh/2 < zed) && (kdo == 1) && (kdo==i->kdo) )) && prot.vyska_zed > 1.5f)
		{
			// Colise zed //
			i->show = 2;
			// new saetings for emitor
			i->em1.SetAtribut( i->em1.GetStartPos(), 0, 360, 0.01f, 0.5f );
			i->em1.Create( 40 );
			i->em1.Stop();
			if( i->em1.IsShowParticle() == 0 ) { i->show = 0; }
			if(!i->col)
			{
				prot.vyska_zed -= 1.0f;
				i->col =1;
				g_zvuk.Play( g_idZvuk[14] );
			}
		}else
		if( (i->pos.x+(i->wh/2) > hrad && kdo==0) || (i->pos.x+(i->wh/2) < hrad && kdo == 1) )
		{
			// Colise hrad //
			i->show = 2;
			// new saetings for emitor
			i->em1.SetAtribut( i->em1.GetStartPos(), 0, 360, 0.01f, 0.5f );
			i->em1.Create( 40 );
			i->em1.Stop();

			if( i->em1.IsShowParticle() == 0 ) { i->show = 0; }
			if(!i->col)
			{
				if(kdo==0) prot.Hrad -= 1000 + g_hrac.pocMag*100; 
				if(kdo==1) prot.Hrad -= 1000 + g_prot.pocMag*100;
				i->col =1;
				g_zvuk.Play( g_idZvuk[14] );
			}
		}
		
	}
}

//////////////////////////////////////////////////////////////////////////
// Bonus
//////////////////////////////////////////////////////////////////////////

CBonus::CBonus()
{
	_full = 0;
	_pos = CV3_NULL;
}

void CBonus::Delete()
{
	_full = 0;
	_pos = CV3_NULL;
}

void CBonus::Add(CWfoModel &model, int bonus, float speed, int typ)
{
	if(_full) return;
	_model = &model;
	_bonus = bonus;
	_speed = speed;
	_pos.y = 7;
	_pos.z = -2.5;
	_full = 1;
	_stav = 1;
	_typ = typ;
}

void CBonus::Draw()
{
	if(!_full) return;
	static float blend=1.0f;
	glPushMatrix();
	glPushAttrib( GL_ENABLE_BIT );
	glTranslatef(_pos.x, _pos.y, _pos.z);
	if( _stav == 3 )
	{
		glDisable( GL_LIGHTING );
		glColor4f(1, 1, 1, blend);
		glEnable( GL_BLEND );
		glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
		blend -= (0.03*60)/g_fps.GetFPS();
		if( blend <= 0.0f ) 
		{
			_full = 0;
			_stav = 0;
			blend = 1.0f;
		}
	}
	_model->SetAlpha( 0 );
	_model->Render();
	glPopAttrib();
	glPopMatrix();
}

void CBonus::Refresh(float time)
{
	if(_full == 1 && _stav != 3 )
	{
		_pos.y -= (_speed*60)/time;
		if(_pos.y < 0) 
		{
			_pos.y = 0;
			_stav=2;
		}
	}
}
// penize
// kameni
// mana
// ved. body
void CBonus::Colise(THrac &hrac, C_SCENAJED_0 &jed, int kdo )
{
	if(_stav != 2) return;
	std::vector<TJednotka>::iterator i = jed._hracJed.begin();
	for(; i != jed._hracJed.end(); i++)
	{
		if(!i->show || i->smrt != 0 ) continue;
		float vel = 0.4f;
		if( (i->pos.x+i->w > 0-vel) && (i->pos.x < 0+vel) || (i->pos.x < 0+vel) && (i->pos.x+i->w > 0-vel) )
		{
			g_partKruh.Add( TM.GetIdTexture( g_textures[34] ), CVector3(0,0.1f,i->pos.z), 0.4f, 0.08f+0.2f, 0.01f );
			// COLISE !!!!
			//_full = 0;
			_stav = 3;
			if(_typ == 1)
			{
				hrac.penize += 1000 + 500*g_aktKolo;
				if( kdo == 0 ) InfoText.Add( W_GetString("Ziskal jsi bonus + %d zlata",1000 + 500*g_aktKolo), RGBf_GREEN );
				if( kdo == 1 ) InfoText.Add( W_GetString("Nepritel ziskal bonus + %d zlata",1000 + 500*g_aktKolo), RGBf_RED );
			}else
			if(_typ == 2)
			{
				hrac.kameny += 1200 + 600*g_aktKolo;
				if( kdo == 0 ) InfoText.Add( W_GetString("Ziskal jsi bonus + %d kameni",1200 + 600*g_aktKolo), RGBf_GREEN );
				if( kdo == 1 ) InfoText.Add( W_GetString("Nepritel ziskal bonus + %d kameni",1500 + 600*g_aktKolo), RGBf_RED );
			}else
			if(_typ == 3)
			{
				hrac.mana += 50 + 20*g_aktKolo;
				if( kdo == 0 ) InfoText.Add( W_GetString("Ziskal jsi bonus + %d many",50 + 20*g_aktKolo), RGBf_GREEN );
				if( kdo == 1 ) InfoText.Add( W_GetString("Nepritel ziskal bonus + %d many",50 + 20*g_aktKolo), RGBf_RED );
			}else
			if(_typ == 4)
			{
				hrac.vedBody += 10 + 10*g_aktKolo;
				if( kdo == 0 ) InfoText.Add( W_GetString("Ziskal jsi bonus + %d bodu pro vyzkum",10 + 10*g_aktKolo), RGBf_GREEN );
				if( kdo == 1 ) InfoText.Add( W_GetString("Nepritel ziskal bonus + %d bodu pro vyzkum",10 + 10*g_aktKolo), RGBf_RED );
			}
			return;
		}
	}
	return;
}


//////////////////////////////////////////////////////////////////////////
// SIP
//////////////////////////////////////////////////////////////////////////

void CScenaSip::Add( uint textureId, CVector3 pos, int kdo, int sila )
{
	TSip temp;
	temp.show = 1;
	temp.pos = pos;
	temp.kdo = kdo;
	temp.h = 0.16f;
	temp.w = 0.4f;
	if( kdo == 1 ) temp.w = -0.4f;
	temp.sila = sila;
	temp.textureId = textureId;
	_vSip.push_back( temp );
}

void CScenaSip::Render()
{
	std::vector<TSip>::iterator i =  _vSip.begin();
	for(; i != _vSip.end(); i++)
	{
		if( !i->show ) continue;
		C_3DOBJECT_0 obj(i->textureId, i->pos.x, i->pos.y,i->pos.z, i->w, i->h, 0.0);
		glColor3f(1, 1, 1);
		if( g_light ) glDisable( GL_LIGHTING );
		obj.SetAlpha(1);
		obj.Render();
		if( g_light ) glEnable( GL_LIGHTING );
	}
}

void CScenaSip::Delete()
{
	_vSip.clear();
}

void CScenaSip::Move(float time)
{
	float speedSip = 0.1f;
	std::vector<TSip>::iterator i =  _vSip.begin();
	for(; i != _vSip.end(); i++)
	{
		if(i->kdo == 0)
		{
			i->pos.x += (speedSip*60)/g_fps.GetFPS();
		}else
		if(i->kdo == 1)
		{
			i->pos.x -= (speedSip*60)/g_fps.GetFPS();
		}
	}
}

void CScenaSip::Coli()
{
	std::vector<TSip>::iterator i =  _vSip.begin();
	for(; i != _vSip.end(); i++)
	{
		if(i->kdo == 0 && i->pos.x > 8.0f) i->show = 0;
		if(i->kdo == 1 && i->pos.x < -8.0f) i->show = 0;
	}
}

void CScenaSip::Coli( C_SCENAJED_0 &h, int mKdo, float &prot_zed )
{
	// Colision sip vs. Zed
	std::vector<TSip>::iterator k =  _vSip.begin();
	for(; k != _vSip.end(); k++)
	{
		if( !k->show ) continue;
		// protihrac
		if( k->kdo == 1 && g_hrac.vyska_zed > 0.5f && k->pos.x <= dalkaZed_x+sirkaZed*2)
		{
			g_hrac.vyska_zed -= CONST_sip_zivotu;
			if( g_hrac.vyska_zed < 0 ) g_hrac.vyska_zed = 0.0f;
			k->show = 0;
		}else
		// hrac
		if( k->kdo == 0 && g_prot.vyska_zed > 0.5f && k->pos.x + 0.8f >= -dalkaZed_x+sirkaZed)
		{
			g_prot.vyska_zed -= CONST_sip_zivotu;
			if( g_prot.vyska_zed < 0 ) g_prot.vyska_zed = 0.0f;
			k->show = 0;
		}
	}
	
	// Colison sip vs. jednotka hrace
	std::vector<TJednotka>::iterator i =  h._hracJed.begin();
	for(; i!=h._hracJed.end(); i++)
	{
		if( i->smrt > 0 ) continue;
		std::vector<TSip>::iterator j =  _vSip.begin();
		for(; j != _vSip.end(); j++)
		{
	
			if( !j->show ) continue;
			TPOINT a1 = { j->pos.x, j->pos.y };	// sip pos begin
			TPOINT a2 = { j->pos.x+0.8f, j->pos.y }; // sip pos end
			TRect rect = { i->pos.x, i->pos.y, i->w/2, i->h };	// jednotka
			float moje_delka = dalkaZed_x;
			if( mKdo == 0 ) moje_delka = -moje_delka;
			TRect rectZed = { moje_delka, 0, sirkaZed, prot_zed };	// zed
			
			// Colise
			if(j->kdo != mKdo && j->show==1 && i->show==1)
			{
				if( ColPointToRect(a1, rect) || ColPointToRect(a2, rect) )
				{
					// !! COLISE
					i->live -= j->sila;
					if( g_kouzloBoziOchrana && i->kdo == 0 ) i->live += j->sila; 
					j->show = 0;
					if(i->live <= 0)
					{
						// smrt
						if(i->smrt == 0) {i->smrt = 1;i->Set2();}
					}
				}
			}
		}
	}
}
//////////////////////////////////////////////////////////////////////////
// CScenaKoule
//////////////////////////////////////////////////////////////////////////

CScenaKoule::CScenaKoule()
{
	_init=0;
}

void CScenaKoule::Add( uint textureId, CVector3 pos, int kdo )
{
	TKoule temp;
	temp.show = 1;
	temp.pos = pos;
	temp.kdo = kdo;
	temp.h = 0.3f;
	temp.w = 0.3f;
	temp.uhel = 0;
	temp.plus_pos.x = (rand()%50)/1000;
	temp.plus_pos.y = (rand()%20)/1000;
	temp.plus_pos.z = 0;
	temp.textureId = textureId;
	_vKoule.push_back( temp );
}

void CScenaKoule::Render()
{
	std::vector<TKoule>::iterator i =  _vKoule.begin();
	for(; i != _vKoule.end(); i++)
	{
		if( !i->show ) continue;
		C_3DOBJECT_0 obj(i->textureId, i->pos.x, i->pos.y,i->pos.z, i->w, i->h, 0.0);
		glColor3f(1, 1, 1);
		glEnable( GL_ALPHA_TEST );
		if(g_light) glDisable( GL_LIGHTING );
		obj.SetAlpha( 1 );
		glEnable( GL_BLEND );
		obj.Render();
		if(g_light) glEnable( GL_LIGHTING );
		glDisable( GL_ALPHA_TEST );
	}
}

void CScenaKoule::Delete()
{
	_vKoule.clear();
	_init=0;
}

void CScenaKoule::Move(float time)
{
	std::vector<TKoule>::iterator i =  _vKoule.begin();
	for(; i != _vKoule.end(); i++)
	{
		if(i->kdo == 0)
		{
			i->pos.x += (0.045f*60)/g_fps.GetFPS();
			i->pos.y += ((float(cos( DegToRad(i->uhel) )/10.0f))*60)/g_fps.GetFPS();
			i->uhel += (1*60+(rand() %10))/g_fps.GetFPS();
			//i->pos = i->pos+i->plus_pos;
		}else
		if(i->kdo == 1)
		{
			i->pos.x += -(0.045f*60)/g_fps.GetFPS();
			i->pos.y += ((float(cos( DegToRad(i->uhel) )/10.0f))*60)/g_fps.GetFPS();
			i->uhel += (1*60+(rand() %10))/g_fps.GetFPS();
			//i->pos = i->pos+i->plus_pos;
		}
	}
}

void CScenaKoule::Coli( )
{
	std::vector<TKoule>::iterator i =  _vKoule.begin();
	for(; i != _vKoule.end(); i++)
	{
		if(i->kdo == 0 && i->pos.x > 8.0f) i->show = 0;
		if(i->kdo == 1 && i->pos.x < -8.0f) i->show = 0;
	}
}

void CScenaKoule::Coli( CWfoModel &m, THrac &prot, int kdo )
{
	static TBBox box, box_c, temp;;
	double matrix[16];
	glGetDoublev( GL_MODELVIEW_MATRIX, matrix );
	//if( !_init )
	{
		box = m.GetBBox();
		temp = box;
		_init=1;
	}

	//DrawBBox( box );
	box.xMax = box.zMax;
	box.xMin = box.zMin;
/*	textREC.Add("X min", box.xMin);
	textREC.Add("X max", box.xMax);
	textREC.Add("Y min", box.yMin);
	textREC.Add("Y max", box.yMax);*/
	
	std::vector<TKoule>::iterator i =  _vKoule.begin();
	for(; i != _vKoule.end(); i++)
	{
		if( !i->show ) continue;
		if( i->kdo == kdo )
		{
			TAshlar kvadr = W_PrevedBBoxNaAshlar( box );
			TPoint bod = { i->pos.x+i->w/2, i->pos.y+i->h/2};
			//textREC.Add("x ", bod.x );
			//textREC.Add("y ", bod.y );
			/*textREC.Add("width ", kvadr.width );
			textREC.Add("height ", kvadr.height );*/
			kvadr.y = 0;
			kvadr.height = kvadr.height + box.yMin;
			kvadr.x = matrix[12]+box.xMin-0.1f;
			//kvadr.width = 3;
			if( ColAshlarToPoint( kvadr, bod ) ) 
			{
				i->show=0;
				TRGBf color[2] = {1,1,0, 1,0,0};
				emKou.SetColor(color[0], color[1]);
				emKou.SetStartVel(0.4f);
				emKou.SetTexture( texture_trosky );
				emKou.SetAtribut(  CVector3( bod.x, bod.y, i->pos.z ), 0, 360, 0.005f, 0.4f );
				//emKou.SetEmitorPos( CVector3( bod.x, bod.y, i->pos.z ) );
				//emKou.DeleteParticle();
				emKou.Play();
				//emKou.SetBlendBlack();
				emKou.Create( 50 );
				emKou.Stop();
				emKou.SetBlend(1);
				if( kdo == 1 ) g_hrac.Hrad -= 100*g_prot.dilny1;
				if( kdo == 0 ) g_prot.Hrad -= 100*g_hrac.dilny1;
			}//kdo
		}//show
	}//for i
}
void CScenaKoule::Coli( C_SCENAJED_0 &h, int mKdo, float &prot_zed )
{
	// Colison koule vs. jednotka hrace
	std::vector<TJednotka>::iterator i =  h._hracJed.begin();
	for(; i!=h._hracJed.end(); i++)
	{
		if( i->smrt > 0 ) continue;
		std::vector<TKoule>::iterator k =  _vKoule.begin();
		for(; k < _vKoule.end(); k++)
		{
			if( !k->show ) continue;
			if( k->kdo != mKdo )	// neni to hracova vlastni koule
			{
				TRect rect = {i->pos.x, i->pos.y, i->w, i->h - (i->h/3) };
				TPoint bod = { k->pos.x+0.15f, k->pos.y+0.15f };
				if( ColPointToRect( bod, rect ) ) 
				{
					k->show=0;
					TRGBf color[2] = {1,1,0, 1,0,0};
					emKou.SetColor(color[0], color[1]);
					emKou.SetStartVel(0.4f);
					emKou.SetTexture( texture_trosky );
					emKou.SetAtribut( CVector3( bod.x, bod.y, i->pos.z ), 0, 360, 0.005f, 0.1f );
					//emKou.DeleteParticle();
					emKou.Play();
					//emKou.SetBlendBlack();
					emKou.Create( 10 );
					emKou.Stop();
					emKou.SetBlend(1);
					if( mKdo == 1) i->live -= CONST_koule_zivotu*g_hrac.dilny1;
					if( mKdo == 0 && !g_kouzloBoziOchrana ) i->live -= CONST_koule_zivotu*g_prot.dilny1;

			}//kdo
		}//show
		}
	}

	// COLISE KOULE SE ZDI
	std::vector<TKoule>::iterator k =  _vKoule.begin();
	for(; k < _vKoule.end(); k++)
	{
		if( !k->show ) continue;
		if( k->kdo == mKdo )	// neni to hracova vlastni koule
		{
			float moje_delka = dalkaZed_x;
			if( mKdo == 0 ) moje_delka = -moje_delka;
			TRect rectZed = { moje_delka, 0, sirkaZed, prot_zed };	// zed
			TPoint bod = { k->pos.x+0.3f, k->pos.y+0.3f }; // koule
			if( ColPointToRect( bod, rectZed ) ) 
			{
				prot_zed -= CONST_koule_zed;
				if( prot_zed < 0 ) prot_zed = 0.0f;
				k->show = 0;

				TRGBf color[2] = {1,1,0, 1,0,0};
				emKou.SetColor(color[0], color[1]);
				emKou.SetStartVel(0.4f);
				emKou.SetTexture( texture_trosky );
				emKou.SetAtribut( CVector3( bod.x, bod.y, i->pos.z ), 0, 360, 0.005f, 0.15f );
				//emKou.DeleteParticle();
				emKou.Play();
				//emKou.SetBlendBlack();
				emKou.Create( 15 );
				emKou.Stop();
				emKou.SetBlend(1);
			}
		}
	}
}

void RenderVybuchHrad( float time )
{
	glPushAttrib( GL_ENABLE_BIT );
	glDisable(GL_LIGHTING);
	
	emKou.Render( g_rotate.x, g_rotate.y );
	emKou.Update( time );

	glPopAttrib();
}

//////////////////////////////////////////////////////////////////////////
// CInfoText
//////////////////////////////////////////////////////////////////////////
	
void CInfoText::Add( std::string text, TRGBf color )
{
	TInfoText temp;
	temp.text = text;
	temp.color = color;
	temp.live = 1.0f;
	temp.pos = _startPos;
	_lData.push_back( temp );
}

void CInfoText::Render( C_FREETYPE_0 &font )
{
	std::list<TInfoText>::iterator i=_lData.begin();
	int iVal=0;
	for(; i != _lData.end(); i++ )
	{
		if( iVal > 7 ) continue;
		glEnable( GL_BLEND );
		glColor4f(i->color.r, i->color.g, i->color.b, i->live );
		glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
		int plus=0;
		if( _lData.size() > 7 ) plus = _lData.size()-7;
		font.Print( i->pos.x, i->pos.y-16*(_lData.size()-iVal)+16*+plus, i->text.c_str() );
		glDisable( GL_BLEND );
		iVal++;
		i->live -= (0.002*60)/g_fps.GetFPS();
		if( i->live <= 0 ) i = _lData.erase( i );
	}
}

void CInfoText::DeleteAll()
{
	_lData.clear();
}

void CScenaVlna::AddVlna( uint texture_id, float start, float mx )
{
	TVlna t;
	t.em.SetTexture( texture_id );
	TRGBf color[2] = {0.4,0.4,1, 0,0,0};
	t.em.SetColor( color[0], color[1] );
	t.em.SetStartVel( 2.0f );
	if( mx < 0 ) t.em.SetAtribut( CVector3(start, 1.3, -1 ), -90, 25, -mx, 3.0 );
	else t.em.SetAtribut( CVector3(start, 1.3, -1 ), 90, 25, mx, 3.0 );
	t.em.Create( 20 );
	t.em.Stop();
	_lData.push_back( t );
}
void CScenaVlna::Render()
{
	std::list<TVlna>::iterator i=_lData.begin();
	for( ; i != _lData.end(); i++)
	{
		i->em.Render( g_rotate.x, g_rotate.y );
		i->em.Update( g_fps.GetFPS() );
	}
}
void CScenaVlna::Move()
{

}
void CScenaVlna::DeleteAll()
{
	_lData.clear();
}

//////////////////////////////////////////////////////////////////////////
// Vyzkum
//////////////////////////////////////////////////////////////////////////

void DrawOlejProt( )
{
	static CParticleEmitor p;
	static bool nas=0;
	if( nas==0 )
	{
		p.SetStartVel( 0.6f );
		CVector3 pos_olej = CVector3( -1.0f+5, 2.7f, -2.0f);
		p.SetAtribut( CVector3(pos_olej.x-0.3, pos_olej.y, pos_olej.z), -170, 20, 0.01f, 1.3f );
		p.SetTexture( TM.GetIdTexture( g_textures[16] ) );
		TRGBf color[2] = {0.5,0.8,0.5, 0,1,0}; 
		p.SetColor( color[0], color[1] );
		p.Create( 30 );
		nas = 1;
	}else
	{
		p.Render( g_rotate.x, g_rotate.y );
		p.Update( g_fps.GetFPS() );
	}
}

void CVyzkum::Init()
{
	olej=0;sipy=0;koule=0;
	obrana=0;brneni=0;kostel=0;
	zlato=0;kameni=0;mana=0;
	domoobrana=0;trziste=0;chram=0;

	_p.SetStartVel( 0.6f );
	//CVector3 pos_olej = hradD.GetPosPoint( "POINT_ZED" );
	CVector3 pos_olej = CVector3( 1.0f-4.9, 2.7f, -2.0f);
	_p.SetAtribut( CVector3(pos_olej.x-0.3, pos_olej.y, pos_olej.z), -190, 20, 0.01f, 1.3f );
	_p.SetTexture( TM.GetIdTexture( g_textures[16] ) );
	TRGBf color[2] = {0.5,0.8,0.5, 0,1,0}; 
	_p.SetColor( color[0], color[1] );
	_p.Create( 30 );
}

void CVyzkum::DrawOlej()
{
	if(!olej) return;
	_p.Render( g_rotate.x, g_rotate.y );
	_p.Update( g_fps.GetFPS() );
}

void DrawVoda()
{
	if( g_aktKolo == 3-1 || g_aktKolo == 6-1 || g_aktKolo == 10-1 || g_aktKolo == 11-1 || g_aktKolo == 12-1)
	{
		uint tex_voda=TM.GetIdTexture( g_textures[42] );
		uint tex_lava=TM.GetIdTexture( g_textures[43] );
		static CVector3 pos;
		static CVector3 vel(50,0,100);
		uint texture=tex_voda;
		glColor4f(1,1,1,0.7f);
		if( g_aktKolo > 6 )
		{
			texture=tex_lava;
			glColor4f(1,1,1,0.89f);
		}
		if( g_aktKolo == 3-1 ) pos = CVector3(-25,-1, -50);else 
		if( g_aktKolo == 6-1 ) pos = CVector3(-25,-0.1, -50);else
		if( g_aktKolo == 10-1 ) pos = CVector3(-25,-0.1, -50);else
		if( g_aktKolo == 11-1 ) pos = CVector3(-25,-0.2, -50);else
		if( g_aktKolo == 12-1 ) pos = CVector3(-25,-2.0, -50);

		static float coord_plus_x, coord_plus_y;
		glPushAttrib( GL_ENABLE_BIT );
		glBindTexture( GL_TEXTURE_2D, texture );
		glEnable(GL_TEXTURE_2D);
		glBlendFunc( GL_SRC_ALPHA,  GL_DST_ALPHA );
		glEnable( GL_BLEND );
		glDisable( GL_ALPHA_TEST );
		glBegin( GL_QUADS );
			glTexCoord2f(0.0-coord_plus_x,0.0+coord_plus_y);					glVertex3f(pos.x, pos.y, pos.z);
			glTexCoord2f(3.0-coord_plus_x,0.0+coord_plus_y);		glVertex3f(pos.x, pos.y, pos.z+vel.z);
			glTexCoord2f(3.0-coord_plus_x,3.0+coord_plus_y);		glVertex3f(pos.x+vel.x, pos.y, pos.z+vel.z);
			glTexCoord2f(0.0-coord_plus_x,3.0+coord_plus_y);					glVertex3f(pos.x+vel.x, pos.y, pos.z);
		glEnd();
		glPopAttrib( );
		// move water - timing
		static unsigned int stupne=0;
		coord_plus_x=((0.06*60)/g_fps.GetFPS())*cos(DegToRad(stupne));
		coord_plus_y=((0.06*60)/g_fps.GetFPS())*sin(DegToRad(stupne));
		stupne++;
		if(stupne >= 360 ) stupne = 0;
	}
}