//This code is copyright Andrew Story (Kaslai/Aslai) All rights reserved.
//This and derivative works can be distributed as seen fit,
//so long as this notice remains unchanged and present

#include "engine/tile.h"
#include "engine/types.h"

std::vector<Tile*> tiles;
std::map<const char*, int, map_char> tile_map;

void Tile::draw( int x, int y )
{
    int frame = 0;
    parent->draw_image_part( x, y, (frame%frames) * w, 0, w, h, img );
}

Tile::Tile(  CImage image, Context* prnt, int framecnt, int wid, int hig, int stat, char* ID   )
{
    frames = framecnt;
    w = wid;
    h = hig;
    img = image;
    parent = prnt;
    id = (char*) malloc( strlen( ID ) + 1 );
    strcpy( id, ID );
}


Tile::~Tile(){
    free( id );
}

Tile* tile_add( CImage image, Context* prnt, int framecnt, int wid, int hig, int stat, char* ID )
{
    Tile* a = new Tile( image, prnt, framecnt, wid, hig, stat, ID );
    int pos = tiles.size();
    tiles.push_back( a );
    tile_map[a->id] = pos;
    a->pos = pos;
    return a;
}



void tile_rem( Tile* ani )
{
    delete tiles[ani->pos];
    tiles[ani->pos] = 0;
}

Tile* tile_get_by_id( char* ID )
{

    return tiles[tile_map[ID]];
}