//particles.cpp                      //contains particle functions
#include <allegro.h>
#include <math.h>
#include "globals.h"
#include "classes.h"

particle::particle(int xx, int yy, int dd, int rd){
    x = xx;
    tx = xx;
    y = yy;
    ty = yy;
    dir = dd;
    dist = rd;}

void handle_parts(){  //checks for recently destroyed ships, creates particles and manages existing particles
    int tdir = 0;
    int dis;
    
    //*****************particle system creation*********************//
    for (int c1 = 0; c1<bots.size(); c1++){
        if (bots[c1].health<=0 && bots[c1].isdead==false){
            if (mute==1)
            play_sample(expd, 125, 128, 1000, 0);
            while (tdir<=32){
                parts.push_back(particle(bots[c1].x, bots[c1].y, tdir, bots[c1].rad));
                tdir++;}}}
                
    if (mode==2){
        tdir = 0;
        if (p1.health<=0 && p1.isdead==false){
            if (mute==1)
            play_sample(expd, 125, 128, 1000, 0);
            p1.isdead = true;
            while (tdir<=32){
                parts.push_back(particle(p1.x, p1.y, tdir, p1.rad));
                tdir++;}}
                
        if (p2.health<=0 && p2.isdead==false){
            if (mute==1)
            play_sample(expd, 125, 128, 1000, 0);
            p2.isdead = true;
            tdir = 0;
            while (tdir<=32){
            parts.push_back(particle(p2.x, p2.y, tdir, p2.rad));
            tdir++;}}}
                
    //*******************particle destruction************************//
    for (int c1 = 0; c1<parts.size(); c1++){
        dis = abs( sqrt( sqrd(parts[c1].y-parts[c1].ty) + sqrd(parts[c1].x-parts[c1].tx)));
        if (dis>=parts[c1].dist)
        parts.erase(parts.begin() + c1);}
        
    //********************particle updating****************************//
    for (int c1 = 0; c1<parts.size(); c1++){
        parts[c1].change_xy(parts[c1].dir, 1);}}
