#include "Door.h"
#include "List.h"

Door::Door(List* info, int x, int y)
:Object(info,x,y),_open(false),_locked(true)
{
	_type = DOOR;
	load(info);
}

void Door::load(List *info, bool fromConstructor)
{
	if(!fromConstructor)
		Object::load(info,fromConstructor);

	while(!info->empty()) {
		List *current = info->firstList();

		if(current->firstString() == "openSprite") {
			_openSprite = Sprite::loadSprite(current->rest()->firstString());
		}
		else if(current->firstString() == "closedSprite") {
			_closedSprite = Sprite::loadSprite(current->rest()->firstString());
		}
		else if(current->firstString() == "unlocked")
			_locked = false;
		else if(current->firstString() == "locked")
			_locked = true;
		else if(current->firstString() == "open")
			_open = true;

		info = info->rest();
	}

	setOpen(_open);
}

Object *Door::copy()
{
	return new Door(*this);
}

void Door::collide( Object* o )
{
	if((o->getType() == PLAYER || o->getType() == ENEMY) && !_locked ) {
		_open = true;
		_collidable = false;
	}
}

void Door::draw()
{
	if(_open)
		_openSprite->draw(_box.x,_box.y);
	else
		_closedSprite->draw(_box.x,_box.y);
		
}

void Door::setOpen(bool open) 
{
	_open = open;
	
	if(_open) {
		_box.h = _openSprite->height();
		_box.w = _openSprite->width();
		_collidable = false;
	}
	else {
		_box.h = _closedSprite->height();
		_box.w = _closedSprite->width();
		_collidable = true;
	}
}