#include "door.h"

SDoor* CreateDoor(SObject* parent, SObjectEx* target_obj, vector2d& closed_position, vector2d& open_position)
{
	SDoor* door = new SDoor();
	InitObject(door, parent);
	door->SetPosition(closed_position.x, closed_position.y);
	door->close_pos = closed_position;
	door->open_pos = open_position;
	door->desired_pos = closed_position;
	door->velocity.Set(0, 0);
	door->factor = 0.5;
	door->image = FindBitmap("door.tga");
	door->target_object = target_obj;
	return door;	
}

int SDoor::Handle()
{
	vector2d diff = desired_pos - position;
	
	if(diff.GetLengthSQ() < 1.0f)
	{
		velocity.Set(0, 0);
		if(desired_pos == open_pos)
		{
			desired_pos = close_pos;
			return 0;
		}
	}
	else
	{
		velocity = diff * factor;
	}
	
	position += velocity * g_fDelta;
	
	SetPosition(position.x, position.y);
	
	return 0;
}

void SDoor::Draw()
{
	SetModelMatrix(target_object->net_transform);	
	
	DrawModeMask();
	DrawBitmap(image, -image->w / 2, -image->h /2, -8);
	DrawModeSolid();
	
	SetModelMatrix(g_trIdentity);
}

void SDoor::Open()
{
	desired_pos = open_pos;	
}
