#include "stdafx.h"


using namespace std;
//cShell obeject is made up of several particles( the number of particle is defined as global variable Particlenumber, and in program the number of partcile is 
//defined as global variable shellnumber

cShell::cShell(){
	//
	int xrand =rand() % 100;
	if (xrand < 50) {
		this->s_vel_x = -0.2*(rand()%6/10.0);
	}
	else {
		this->s_vel_x = 0.2 * (rand() % 6 / 10.0);
	}
	this->s_vel_y = 0.45;
	this->s_fly_active = false;
	this->s_recycle = true;
	//All shells will be given initial speed when they are declared.Although they will not move, this value will be passed to the particle 
	//array belonging to them.If you want to modify the speed of particles before explosion, you can easily modify it here


}
cShell::~cShell() {

}
void cShell::Init() {

	//declare an array of particles
	particle = new cParticle[particlenumber];

	//random its initial postion as well as its life
	this->s_init_x = rand() % 300 + 100;
	this->s_init_y = rand() % 50+10;
	this->s_life= rand() % 500 + 1500;

	//after the Init function, every shell has its own explode random type, it will be useful when changing the speed of the particles
	this->randtype = rand() % 6+1;

	//after the init function, the of the particles are allowed to move
	this->s_fly_active = true;
	for (int i = 0; i < particlenumber; i++) {

		//give all the particles the initial value
		particle[i].c_posx= s_init_x;
		particle[i].c_posy = s_init_y;
		particle[i].c_vel_x = s_vel_x;
		particle[i].c_vel_y = s_vel_y;
		particle[i].c_life = s_life;
		particle[i].c_starttime = this->getCurrentTime();
	}
	

}

unsigned int cShell::getCurrentTime() {
	this->s_currentime = GetTickCount();
	return s_currentime;
}
void cShell::changevel(cShell shell) {
	double pi = 3.1415926;
	//different type of the particles
	if (shell.randtype==1)
	{
		for (int i = 0; i < particlenumber; i++) {
			shell.particle[i].c_vel_x = 0.4 * (cos(pi * i * 2 / particlenumber));
			shell.particle[i].c_vel_y = 0.4 * (sin(pi * i * 2 / particlenumber));
		}
	}
	else if (shell.randtype == 2)
	{
		
		for (int i = 0; i < particlenumber/2; i++) {
			shell.particle[i].c_vel_x = 0.41 * (cos(pi * i * 4 / particlenumber));
			shell.particle[i].c_vel_y = 0.41 * (sin(pi * i * 4 / particlenumber));
		}
		for (int i = particlenumber / 2; i < particlenumber; i++) {
			shell.particle[i].c_vel_x = 0.21 * (cos(pi * i * 4 / particlenumber));
			shell.particle[i].c_vel_y = 0.21* (sin(pi * i * 4 / particlenumber));
		}
	}
	else if (shell.randtype == 3) 
	{
		for (int i = 0; i < particlenumber; i++)
		{
			shell.particle[i].c_vel_x = 0.15* i/10*(cos(pi * i * 2 / particlenumber));
			shell.particle[i].c_vel_y = 0.15* i/10*(sin(pi * i * 2 / particlenumber));
		}
	}
	else if (shell.randtype == 4)
	{
		for (int i = 0; i < particlenumber; i++)
		{
			shell.particle[i].c_vel_x = 0.15 * i / 10 * (cos(-pi * i * 2 / particlenumber));
			shell.particle[i].c_vel_y = 0.15* i / 10 * (sin(-pi * i * 2 / particlenumber));
		}
	}
	else if(shell.randtype==5)
	{
		for (int i = 0; i < particlenumber /3; i++) {
			shell.particle[i].c_vel_x = 0.35 * (cos(pi * i * 6 / particlenumber));
			shell.particle[i].c_vel_y = 0.35 * (sin(pi * i * 6 / particlenumber));
		}
		for (int i = particlenumber / 3; i < particlenumber*2/3; i++) {
			shell.particle[i].c_vel_x = 0.22 * (cos(pi * i * 6 / particlenumber));
			shell.particle[i].c_vel_y = 0.22 * (sin(pi * i * 6 / particlenumber));
		}
		for (int i = particlenumber* 2/ 3; i < particlenumber; i++) {
			shell.particle[i].c_vel_x = 0.11 * (cos(pi * i * 6 / particlenumber));
			shell.particle[i].c_vel_y = 0.11 * (sin(pi * i * 6 / particlenumber));
		}
	}
	else if (shell.randtype == 6)
	{
		for (int i = 0; i < particlenumber / 4; i++) 
		{
			shell.particle[i].c_vel_x = 0.22*cos(pi*(particlenumber/4-i)/6) ;
			shell.particle[i].c_vel_y = 0.22 * sin(pi * (particlenumber/4 - i) / 6);
		}
		for (int i = particlenumber/4; i < particlenumber / 2; i++)
		{
			shell.particle[i].c_vel_x = -0.22 * sin(pi * (particlenumber/2 - i) / 6);
			shell.particle[i].c_vel_y =- 0.22 * sin(pi * (particlenumber/2 - i) / 6);
		}
		for (int i = particlenumber/2; i < particlenumber *3/ 4; i++)
		{
			shell.particle[i].c_vel_x = -0.22 * sin(pi * (particlenumber*3/4 - i) / 6);
			shell.particle[i].c_vel_y = 0.22 * sin(pi * (particlenumber*3/4 - i) / 6);
		}
		for (int i = particlenumber*3/4; i < particlenumber ; i++)
		{
			shell.particle[i].c_vel_x = 0.22 * sin(pi * (particlenumber - i) / 6);
			shell.particle[i].c_vel_y = -0.22 * sin(pi * (particlenumber - i) / 6);
			
		}
	}
}

void cShell::particlemove() {
	for (int i = 0; i < particlenumber; i++) {
		if (particle[i].c_life <= particle[i].getCurrentTime() - particle[i].c_starttime|| particle[i].c_posx <= 0
			|| particle[i].c_posx >= 500 || particle[i].c_posy <= 0||particle[i].c_posy>=500) {
			delete[]particle;
			s_fly_active = false;
			break;
			//if any particle of a shell has run out its life or touched the screen, the array of particles belonging to this shell will be deleted to free memory
		}
	}


	//for (int i = 0; i < particlenumber; i++) {
	//	if (particle[i].whetherdelete == true) {
	//		delete[]particle;
	//		cout << "c\n";
	//	}
	//}
	if (s_fly_active) {
		for (int i = 0; i < particlenumber; i++) {
			this->particle[i].c_posx += particle[i].c_vel_x;
			this->particle[i].c_posy += particle[i].c_vel_y;
		//the particle moves
		}
	}
}
