/**
 *  one room arena
 *  Copyright (C) 2016 POSITIVE MENTAL ATTITUDE
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, version 3 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


#include <cmath>
#include "Map.hpp"

Map::Map()
{
	_img.loadFromFile("data/bg.png");
	_tex.loadFromImage(_img);
	this->setTexture(_tex);
	this->setPosition(0.f, 0.f);
}

Map::~Map()
{
	
}

Vector2u Map::getSize() const
{
	return _img.getSize();
}

bool Map::collision(const Vector2f coords) const
{
	if(coords.x < 0.f || coords.x > _img.getSize().x
	|| coords.y < 0.f || coords.y > _img.getSize().y)
		return false;
	return _img.getPixel(coords.x, coords.y).r > 100;
}

void Map::destroy(const Vector2f coords, int radius)
{
	for(int i = -radius; i <= radius; ++i)
	{
        for(int j = -radius; j <= radius; ++j)
		{
			float currentRadius = std::sqrt(i*i+j*j);
			int transparency = 0;
			if(currentRadius < radius &&
			i + coords.x >= 10 && i + coords.x < _img.getSize().x - 10 && j + coords.y >= 10 && j + coords.y < _img.getSize().y - 10)
			{
				if(radius - currentRadius < 60.f)
					transparency = _img.getPixel(i + coords.x, j + coords.y).r - (radius - currentRadius) * 255 / 60;
				if(transparency < 0)
					transparency = 0;
				_img.setPixel(i + coords.x, j + coords.y, Color(transparency, transparency, transparency, 255));
			}
		}
	}
	_tex.loadFromImage(_img);
}
