// ---------------------------------------------------------------------------
// 
//   Lily Engine alpha
// 
//   Copyright (c) 2010 Clifford Jolly
//     http://cliffjolly.com
//     cliff@cliffjolly.com
// 
// ---------------------------------------------------------------------------
// 
//   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; either version 2 of the License, or
//   (at your option) any later version.
//  
//   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, write to the Free Software
//   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
//   USA
// -------------------------- END HEADER -------------------------------------

#include "../hacks/winhacks.h"

#include "../tinyxml/xmlstuff.h"

#include "terrainblock.h"
#include "editorui.h"
#include "world.h"
#include "renderer.h"
#include "GL/gl.h"

TerrainBlock::TerrainBlock(World *world) : Entity(world), box(0, 0, 1, 1) {
	blocker = true;
	blockable = false;
	gravityInfluence = 0;
	blocksDownOnly = false;
	setAlpha(0.5);
	editorVisibleOnly = true;
}

TerrainBlock::TerrainBlock(World *world, TiXmlElement *xml) : Entity(world, xml) {
	Polygon p;
	blocksDownOnly = false;
	
	p.offset = getVectorFromXml(xml, "polyOffset");
	
	TiXmlElement *pointXml = xml->FirstChildElement("Point");
	while(pointXml) {
		
		Vector2D newPoint = getVectorFromXml(pointXml, "loc");
		p.points.push_back(newPoint);
		
		pointXml = pointXml->NextSiblingElement("Point");
	}
	
	poly = p;
	
	blocksDownOnly = getBoolAttributeFromXml(xml, "blocksDownOnly");
	
}

TiXmlElement *TerrainBlock::toXml(void) const {
	TiXmlElement *e = Entity::toXml();
	
	e->SetValue("TerrainBlock");
	
	setVectorInXml(e, "polyOffset", poly.offset);
	
	for(unsigned int i = 0; i < poly.points.size(); i++) {
		TiXmlElement *pointEl = new TiXmlElement("Point");
		
		setVectorInXml(pointEl, "loc", poly.points[i]);
		
		e->LinkEndChild(pointEl);
	}
	
	setBoolAttributeInXml(e, "blocksDownOnly", blocksDownOnly);
	
	return e;
}

void TerrainBlock::draw(void) const {
	World *world = getWorld();
	Renderer *renderer = world->getRenderer();
	
	if(!world->getEditor()) return;
	if(!world->getEditor()->getVisible() && editorVisibleOnly && !world->getEditor()->overrideEditorOnlyVisibility) {
		return;
	}
	
	if(getLayer() != world->getEditor()->junkInterface.getActiveLayer()) {
		return;
	}
	
	if(blocksDownOnly) {
		renderer->setColor(1, 0, 0, getAlpha());
	} else {
		renderer->setColor(0, 0, 1, getAlpha());
	}
	
	renderer->unbindTexture();
	poly.drawSolid(renderer);
}

void TerrainBlock::update(void) {
	// Don't call Entity::update() here. This is not a normal Entity.
}

BoundingBox TerrainBlock::getBoundingBox(void) const {
	return box;
}

Polygon TerrainBlock::getCollisionArea(void) const {
	return poly;
}

void TerrainBlock::setPos(const Vector2D &pos) {
	Entity::setPos(pos);
	poly.offset = pos;
	buildBoundingBox();
}

void TerrainBlock::buildBoundingBox(void) {
	box = BoundingBox(poly);
}

void TerrainBlock::collided(Entity *otherEnt) {
}

void TerrainBlock::updateEditor(EditorUI *ui) {
	Entity::updateEditor(ui);
	
	ui->junkInterface.addAttribute("TerrainBlock properties", INSPECTOR_DIVIDER);	
	ui->junkInterface.addAttribute("blocksDownOnly", INSPECTOR_BOOL);
	ui->junkInterface.setBoolAttribute("blocksDownOnly", blocksDownOnly);
}

void TerrainBlock::updateFromEditor(EditorUI *ui) {
	Entity::updateFromEditor(ui);
	
	blocksDownOnly = ui->junkInterface.getBoolAttribute("blocksDownOnly");
}
