82 lines
1.7 KiB
C++
82 lines
1.7 KiB
C++
#include "item.h"
|
|
#include "sprite.h" // Para Sprite
|
|
#include "texture.h" // Para Texture
|
|
|
|
// Constructor
|
|
Item::Item(item_t item)
|
|
{
|
|
const int itemSize = 8;
|
|
|
|
// Crea objetos;
|
|
sprite = new Sprite(item.x, item.y, itemSize, itemSize, item.texture, item.renderer);
|
|
|
|
// Inicia variables
|
|
sprite->setSpriteClip((item.tile % 10) * itemSize, (item.tile / 10) * itemSize, itemSize, itemSize);
|
|
collider = sprite->getRect();
|
|
colorChangeSpeed = 4;
|
|
counter = item.counter * colorChangeSpeed;
|
|
|
|
// Inicializa los colores
|
|
color_t c = item.color1;
|
|
color.push_back(c);
|
|
color.push_back(c);
|
|
|
|
c = item.color2;
|
|
color.push_back(c);
|
|
color.push_back(c);
|
|
}
|
|
|
|
// Destructor
|
|
Item::~Item()
|
|
{
|
|
delete sprite;
|
|
}
|
|
|
|
// Pinta el objeto en pantalla
|
|
void Item::render()
|
|
{
|
|
const int index = (counter / colorChangeSpeed) % color.size();
|
|
sprite->getTexture()->setColor(color[index].r, color[index].g, color[index].b);
|
|
sprite->render();
|
|
sprite->getTexture()->setColor(255, 255, 255);
|
|
}
|
|
|
|
// Actualiza las variables del objeto
|
|
void Item::update()
|
|
{
|
|
counter++;
|
|
}
|
|
|
|
// Obtiene el rectangulo de colision del objeto
|
|
SDL_Rect &Item::getCollider()
|
|
{
|
|
return collider;
|
|
}
|
|
|
|
// Obtiene su ubicación
|
|
SDL_Point Item::getPos()
|
|
{
|
|
const SDL_Point p = {sprite->getPosX(), sprite->getPosY()};
|
|
return p;
|
|
}
|
|
|
|
// Recarga la textura
|
|
void Item::reLoadTexture()
|
|
{
|
|
sprite->getTexture()->reLoad();
|
|
}
|
|
|
|
// Asigna los colores del objeto
|
|
void Item::setColors(color_t col1, color_t col2)
|
|
{
|
|
// Reinicializa el vector de colores
|
|
color.clear();
|
|
|
|
// Añade el primer color
|
|
color.push_back(col1);
|
|
color.push_back(col1);
|
|
|
|
// Añade el segundo color
|
|
color.push_back(col2);
|
|
color.push_back(col2);
|
|
} |