//
//  Tile.hpp
//  Meteor
//
//  Created by Cesar Parent on 11/09/2015.
//  Copyright © 2015 Cesar Parent. All rights reserved.
//

#ifndef Tile_hpp
#define Tile_hpp

#include "../Nodes/SpriteNode.h"
#include "TiledMapInfo.h"

NS_BEGIN


/*!
 * @class           Tile
 * @ingroup         tiledmap
 * @brief           A tile used in a tiled map.
 * @details         A tile is a subclass of SpriteNode. It keeps track of its GID, which in turns
 *                  defines which portion of the tileset it should draw
 * @note            You should not create a Tile manually. Instead, you should access tiles created
 *                  by TiledMap and TiledMapLayer objects by calling
 *                  TiledMapLayer::tileAtIndexPath(const ivec2&) or
 *                  TiledMapLayer::firstTileWithGID(Uint32).
 */
class Tile : public SpriteNode {
public:
    
    /*!
     * @internal
     * @brief       Creates a tile with a given GID.
     * @param       gid         The tile's tileset identifier.
     * @param       info        The tiled map's information.
     */
    Tile(Uint32 gid, const TiledMapInfo& info) : SpriteNode(info.tileSetFile), _gid(gid) {
        auto layout = texture()->size() / info.tileSize;
        float x = ((gid-1) % layout.x) * info.tileSize.x;
        float y = (((gid-1) / layout.x) % layout.y) * info.tileSize.y;
        setClipRect({x, y, float(info.tileSize.width), float(info.tileSize.height)});
    }
    
    virtual ~Tile() {}
    
    /*!
     * @brief       Returns the tile's GID.
     * @return      The tile's GID. 
     */
    Uint32
    gid() const { return _gid; }
    
private:
    
    Uint32      _gid;
};


NS_END

#endif /* Tile_hpp */
