//
//  Tile.cpp
//  GlobalGameJam15
//
//  Created by Cesar Parent on 24/01/2015.
//  Copyright (c) 2015 Cesar Parent. All rights reserved.
//

#include <math.h>
#include "Tile.h"
#include "MapLayer.h"
#include "Defines.h"

Tile::Tile(Uint8 tileIndex, int tileX, int tileY) :
	SpriteEmitter("assets/maps/tileset.png")
{
	_index = tileIndex;
	_colorMod = {0xff, 0xdd, 0x99, 0xff};
	
	setX((tileX+0.5)*kTileSize);
	setY((tileY+0.5)*kTileSize);
	
	int tileSetX = (tileIndex % kTileSetWidth)*kTileSize;
	int tileSetY = (tileIndex / kTileSetWidth)*kTileSize;
	
	setClipFrame(tileSetX, tileSetY, kTileSize, kTileSize);
}

Tile::~Tile()
{
	
}

Particle* Tile::emit(int timeInterval)
{
	return nullptr;
}

void Tile::draw(SDL_Renderer *renderer)
{
	if(_parent == nullptr) return;
	MapLayer* map = (MapLayer*)_parent;
	
	float opacity = 1.0f - map->distanceToPlayer(_position.x, _position.y) /
					(float)(kVisionRadius*kTileSize);
	
	opacity = (opacity > 0)? opacity : 0.0f;
	opacity = (opacity < 1)? opacity : 1.0f;
	
	SDL_SetTextureColorMod(_texture,
						   _colorMod.red*map->flicker*opacity,
						   _colorMod.green*map->flicker*opacity,
						   _colorMod.blue*map->flicker*opacity);
	SpriteEmitter::draw(renderer);
}