#ifndef _GAME_H
#define _GAME_H

#include "stdafx.h"

#define BLOCK_SIZE 2.5

struct TBrick
{
	TPoint3 pos, drawPos;
	uint uiWait;
	bool form[4][4];
};

struct TMan
{
	TPoint3 pos;
	int angle;
	float _fFrame;
	TMan():_fFrame(0.f){}
};

enum E_EFFECT_TYPE {ET_FIRE, ET_BLOOD, ET_SMOKE};

struct TParticle
{
	TPoint3 pos;
	TColor4 color;
	float angle, scale, speed;
};

struct TEffect
{
	E_EFFECT_TYPE type;
	vector<TParticle> particles;

	TEffect(E_EFFECT_TYPE _type, TPoint3 _pos):type(_type)
	{
		for(int i = 0; i < 10+rand()%10; i++)
		{
			TParticle t;

			switch(_type)
			{
			case ET_FIRE:
				if(rand()%10 == 5)
					t.color = TColor4(255, 0, 0, 255 - rand()%100);
				else
					t.color = TColor4(255, 128, 40, 255 - rand()%150);

				t.scale = 0.5 + (float)(rand()%200)/100.f;
				t.angle = rand()%60 - 90.f - 40.f;
				t.speed = 0.1 + (float)(rand()%10)/100.f;
				break;
			default:
				t.angle = rand()%360;
				t.color = TColor4(255, 255, 255, 225 - rand()%100);
				t.speed = 0.05 + (float)(rand()%10)/100.f;
				t.scale = 0.025 + (float)(rand()%200)/100.f;
			}

			t.pos = TPoint3(_pos.x + (-0.5 + float(rand()%100)/100.f), _pos.y + (-0.5 + float(rand()%100)/100.f), _pos.z + (-0.5 + float(rand()%100)/100.f));
			
			particles.push_back(t);
		}
	}
};

class CGame
{
	TMatrixStack matrix_stack;
	IEngineCore *_pEngineCore;
	IRender *_pRender;
	IRender2D *_pRender2D;
	IRender3D *_pRender3D;
	IInput *_pInput;
	IGUIEngine *_pGUIEngine;
	
	IGUIForms *_pGUIForms;
	IDCForm	*_pQuitForm;

	ILight *_pLight1, *_pLight2;
	ITexture *_pTexBg, *_pTexCur, *_pTexBlood, *_pTexBall, *_pTexSpark, *_pRT;
	IMaterial *_pMatAsphalt, *_pMaterialHouse[5], *_pBlockMaterial;
	ITexture *_pTexAsphalt, *_pTexHouse[5], *_pTexBlock, *_pTexMan;
	IMesh *_pMeshPlane, *_pMeshHouse[5], *_pMeshBlock, *_pMeshSoplo; 
	IBitmapFont *_pMainFont, *_pBaseFont;
	IPostprocess *_pPostprocess;
	IModel *_pMan;

	float _fShaker;

	bool _bDoGame, _bDoGameOver, _bPaused;

	TBrick _stBrick;
	bool _arrPole[10][20];

	uint _uiLines[4], _uiLineWait;

	uint _uiShowScore, _uiShowPause;

	uint _uiResX, _uiResY;
	uint _uiCounter;
	uint _uiGameSpeed, _uiLevel, _uiScore, _uiVictoryCounter;
	int _uiLinesLeft;
	float _fYAngle, _fXAngle, _fZoom;

	vector<TPoint3> _stFlyBricks;
	vector<TEffect> _stParticles;
	vector<TMan> _stHuman;

	void _Process();
	void _Render();
	void _NewGame();
	void _NewBrick();
	bool _AssignBrick();
	void _RotateBrick();
	bool _BrickCollision(TPoint2 pos, float scale = 1.f);
	bool _HumanCollide(TMan man);
	static void CALLBACK _s_Process(void *pParametr);
	static void CALLBACK _s_Render(void *pParametr);

public:
	void _YesClick();
	void _NoClick();

	CGame(IEngineCore *pEngineCore, bool postprocess, bool highdetail);
	~CGame();

};

void CALLBACK _s_YesClick(void *pSelf, void *pParametr);
void CALLBACK _s_NoClick(void *pSelf, void *pParametr);


#endif