//CParticleManager.cpp

#include <winsock2.h>
#include <iostream.h> // include important C/C++ stuff
#include <conio.h>
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h> 
#include <math.h>
#include <io.h>
#include <fcntl.h>
#include <fstream>
#include <cstdlib>



#include "stringc.h"
#include "CGraphicSystem.h"
#include "Assert.h"
#include "CTextReader.h"
#include "CImage.h"


#include "CParticleManager.h"

void CParticleManager::Init()
{
	
	for(int i=0; i < MaxParticles; i++)
	{
		Particles[i].isDead=true;
		
		
	}
	

}

void CParticleManager::ShutDown()
{


}



void CParticleManager::Draw()
{
	//g_Console.AddMessage(STRINGC("Xfac:") + STRINGC(x_fac));
	

	for(int i=0; i < MaxParticles; i++)
	{
		if(Particles[i].isDead==false)
		{
			Assert(Particles[i].Img, "Particle Img not loaded")			
			
			int screen_x = (int)(Particles[i].x);
			int screen_y = (int)(Particles[i].y);

			//int screen_x = Particles[i].x;
			//int screen_y = 1*(Particles[i].y+Particles[i].z)/2 ;			

			//g_Console.AddMessage(STRINGC("Particle at: ") + STRINGC(screen_x) + STRINGC(",") + STRINGC(screen_y) + STRINGC(" x_fac:") + STRINGC(x_fac));
			//g_Console.AddMessage(STRINGC("Particle at: ") + STRINGC(z_fac));

			g_GraphicSystem.DrawCImage(Particles[i].Img, screen_x, screen_y);

		}
	}


}

void CParticleManager::Update()
{

	for(int i=0; i < MaxParticles; i++)
	{
		if(Particles[i].isDead==false)
		{
			Particles[i].Update();
		}
	}

}

void CParticleManager::Create4WayExplosion(int x, int y, int magnitude)
{	
	Create1WayExplosion(x, y, magnitude, 1, 0);
	Create1WayExplosion(x, y, magnitude, -1, 0);
	Create1WayExplosion(x, y, magnitude, 0, 1);
	Create1WayExplosion(x, y, magnitude, 0, -1);

}

void CParticleManager::Create1WayExplosion(int x, int y, int magnitude, int xv, int yv)
{
	int size=20;

	CImage* Img = g_GraphicSystem.LoadCImage("./art/steam.dds");

	for(int i=0; i < size; i++)
	{

		int NextPart = getNextParticleIndex();
		if(NextPart==-1)
			return;

		float speed = float((float)i/(float)(size))*magnitude/15;

		Particles[NextPart].Init(x, y, xv * speed, yv * speed, 15, Img);


	}

	

}

void CParticleManager::CreateFizzle(int x, int y)
{
	CImage* Img = g_GraphicSystem.LoadCImage("./art/steam.dds");

	for(int i=0; i < 10; i++)
	{
		int NextPart = getNextParticleIndex();
		if(NextPart==-1)
			return;

		int dx = (rand() % 4);
		int dy = (rand() % 4);

		int LifeSpan = i + 5;		

		Particles[NextPart].Init(x+dx, y+dy, 0, 0, LifeSpan, Img);



	}
}

void CParticleManager::CreateExplosion(int x, int y, STRINGC File)
{

	//CTextReader::getString("Image", File)
	CImage* Img = g_GraphicSystem.LoadCImage("./art/steam.dds");
	int minSize = 7;//CTextReader::getInt("MinVel", File);
	int maxSize = 19;//CTextReader::getInt("MaxVel", File);
	int size = 25;//CTextReader::getInt("Size", File);
	
	float Gravity = .25f;//CTextReader::getFloat("Gravity", File);
	
	int range = maxSize - minSize;


	for(int i=0; i < size; i++)
	{

		int NextPart = getNextParticleIndex();
		if(NextPart==-1)
			return;

		int nx = x; //+ ((rand() % 100)) - 50;

		int xv = ((rand() % 5));
		int yv = ((rand() % range) + minSize);
		
		int d = rand() % 2;
		

		if(d==0)
			d=-1;

		xv *= d;

		
		d = rand() % 2;
		if(d==0)
			d=-1;

		yv *= -1; //d;

		int LifeSpan = ((rand() % 50)) + 2; //CTextReader::getInt("LifeSpan", File);
		


//		g_Console.AddMessage("Init Particle");
		//g_GraphicSystem.PrintText("Create Particle",1,300);

		Particles[NextPart].Init(nx, y, xv, yv, LifeSpan, Img);

		///////////////////

		Particles[NextPart].ya = Gravity;		

		////////////////////


	}

}


int CParticleManager::getNextParticleIndex()
{
	for(int i=0; i < MaxParticles; i++)
	{
		if(Particles[i].isDead==true)
			return i;
	}

//	g_Console.AddMessage("Could not find open particle");

	return -1;

}
