/*
 * Particle.cpp
 *
 *  Created on: 2009-07-11
 *      Author: wasyl
 */

#include <allegro5/allegro.h>
#include "Particle.h"
#include "../prep/res/Resources.h"
#include "../prep/misc/Vector2D.h"

Particle::Particle(const char *bmp, Vec pos, Vec vel, double life, double braking, int blend)
	: mVel(vel), mLife(life), mBraking(braking), mBlend(blend)
{
	mPos = pos;
	mInitial = mLife;
	Resources::load(mBmp, bmp);
}

void Particle::update(double dt)
{
	mPos += dt * mVel;
	mLife -= dt;
	mVel -= mVel * mBraking;

	if (mLife <= 0.0)
		discard();
}

void Particle::render()
{
	al_set_blender(ALLEGRO_ALPHA, mBlend, al_map_rgba(255,255,255,mLife*255.0/mInitial));
	al_draw_bitmap(mBmp, mPos.x, mPos.y, 0);
	al_set_blender(ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA, al_map_rgb(255,255,255));
}
