//CTile.cpp


#include <winsock2.h>
#include <iostream.h> // include important C/C++ stuff
#include <conio.h>
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h> 
#include <math.h>
#include <io.h>
#include <fcntl.h>
#include <fstream>
#include <cstdlib>


#include "CTile.h"




		/************************/


void CTile::Clear()
{

}

void CTile::Init(STRINGC img, int x, int y)
{

	image = g_GraphicSystem.LoadCImage(img);
	
	Assert( image , "Image==null" )	
	
	mapCords.x=x;
	mapCords.y=y;	
	overlay=NULL;
	isGreen=false;


}

void CTile::Draw()
{

	if(NeedToDraw())
	{
		g_GraphicSystem.DrawCImage(image,worldCords.x,worldCords.y);

		//check overlay
		if(overlay)
		{
			g_GraphicSystem.DrawCImage(overlay,worldCords.x,worldCords.y);
		}

	}

}


void CTile::Scroll(POINT p)
{


	worldCords.x += p.x;
	worldCords.y += p.y;
	OffsetRect(&worldRect,p.x,p.y);

}

	

void CTile::setAnchor(POINT p)
{
	//Point p is where tile 0,0 is.
	int w=image->getWidth();
	int h=image->getHeight();

	//These two lines are for iso tiles
	//worldCords.x=((mapCords.x-mapCords.y)*w/2) +(p.x-w/2);
	//worldCords.y=((mapCords.x+mapCords.y)*h/2) +(p.y);
	worldCords.x=((mapCords.x*w) +(p.x));
	worldCords.y=((mapCords.y*h) +(p.y));

	SetRect(&worldRect,worldCords.x, worldCords.y, worldCords.x+image->getWidth(), worldCords.y+image->getHeight());

	//Set the bounding box
	SetRect(&m_boundingBox,worldCords.x, worldCords.y, worldCords.x+image->getWidth(), worldCords.y+image->getHeight());
	



}

void CTile::DrawSelection(CImage* cursorImage)
{
	g_GraphicSystem.DrawCImage(cursorImage,worldCords.x,worldCords.y);

}



bool CTile::NeedToDraw()
{

	return true;

	/*
	bool draw=false;

	RECT tmp;
	IntersectRect(&tmp,&worldRect,&g_D3DManager.GameRect);
	
	if(!IsRectEmpty(&tmp))
	{
		draw=true;
	}

	return draw;
	*/

}