/*
 * ForceField.cpp
 *
 *  Created on: 2009-07-12
 *      Author: wasyl
 */

#include "ForceField.h"

#include <allegro5/allegro.h>
#include <allegro5/a5_primitives.h>

#include "../prep/res/Resources.h"
#include "../misc/Controls.h"
#include "../prep/misc/Vector2D.h"
#include "../misc/Physics.h"
#include "../prep/misc/Random.h"
#include "Level.h"
#include "Game.h"
#include "Particle.h"

ForceField::ForceField(bool vertical)
	: mVertical(vertical)
{
	mWorld = NULL;
}

ForceField::~ForceField()
{
}

void ForceField::update(double dt)
{
	Entity *avatar = mWorld->entity(ID("avat"));

	bool contains;
	if (mVertical)
		contains = avatar->bbox().contains(mPos + Vec(10,10)) ||
		 avatar->bbox().contains(mPos + Vec(10,20)) ||
		 avatar->bbox().contains(mPos + Vec(10,00));
	else
		contains = avatar->bbox().contains(mPos + Vec(10,10)) ||
		 avatar->bbox().contains(mPos + Vec(00,10)) ||
		 avatar->bbox().contains(mPos + Vec(20,10));

	if (contains)
		((Avatar*)avatar)->die("got baked by a force field");
}

void ForceField::render()
{
	al_set_blender(ALLEGRO_ALPHA, ALLEGRO_ONE, al_map_rgb(255,255,255));
	if (mVertical)
	{
		al_draw_filled_rectangle(mPos.x + 8, mPos.y, mPos.x + 13, mPos.y + 20, al_map_rgba(255, 255, 0, 36));
		al_draw_filled_rectangle(mPos.x + 10, mPos.y, mPos.x + 11, mPos.y + 20, al_map_rgba(255, 255, 255, 128));
	}
	else
	{
		al_draw_filled_rectangle(mPos.x, mPos.y + 8, mPos.x + 20, mPos.y + 13, al_map_rgba(255, 255, 0, 36));
		al_draw_filled_rectangle(mPos.x, mPos.y + 10, mPos.x + 20, mPos.y + 11, al_map_rgba(255, 255, 255, 128));
	}
	al_set_blender(ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA, al_map_rgb(255,255,255));
}

void ForceField::load(FILE *f)
{
	Entity::load(f);
	mVertical = loadByte(f);
}

void ForceField::save(FILE *f)
{
	Entity::save(f);
	saveByte(f, mVertical);
}


