trabajando los items
This commit is contained in:
235
source/item.cpp
235
source/item.cpp
@@ -2,120 +2,107 @@
|
||||
#include "item.h"
|
||||
|
||||
// Constructor
|
||||
Item::Item(LTexture *texture, SDL_Renderer *renderer, Uint8 type, float x, float y)
|
||||
Item::Item(SDL_Renderer *renderer, Asset *asset, Uint8 type, float x, float y)
|
||||
{
|
||||
mSprite = new AnimatedSprite(texture, renderer);
|
||||
|
||||
mClass = type;
|
||||
mEnabled = true;
|
||||
mTimeToLive = 600;
|
||||
mPosX = x;
|
||||
mPosY = y;
|
||||
mWidth = 16;
|
||||
mHeight = 16;
|
||||
mVelX = -1.0f + ((rand() % 5) * 0.5f);
|
||||
mVelY = -4.0f;
|
||||
mAccelX = 0.0f;
|
||||
mAccelY = 0.2f;
|
||||
mFloorCollision = false;
|
||||
mCollider.r = mWidth / 2;
|
||||
shiftColliders();
|
||||
|
||||
mSprite->setAnimationFrames(0, 0, 0, 48, mWidth, mHeight);
|
||||
mSprite->setAnimationFrames(0, 1, 0, 64, mWidth, mHeight);
|
||||
mSprite->setCurrentFrame(0);
|
||||
mSprite->setAnimationCounter(0);
|
||||
mSprite->setAnimationSpeed(0, 10);
|
||||
mSprite->setAnimationLoop(0, true);
|
||||
mSprite->setSpriteClip(mSprite->getAnimationClip(0, 0));
|
||||
mSprite->setPosX(mPosX);
|
||||
mSprite->setPosY(mPosY);
|
||||
|
||||
std::string filePNG;
|
||||
std::string fileANI;
|
||||
switch (type)
|
||||
{
|
||||
case NO_KIND:
|
||||
mEnabled = false;
|
||||
mTimeToLive = 0;
|
||||
mPosX = 0;
|
||||
mPosY = 0;
|
||||
mWidth = 0;
|
||||
mHeight = 0;
|
||||
mVelX = 0;
|
||||
mVelY = 0;
|
||||
break;
|
||||
|
||||
case ITEM_POINTS_1_DISK:
|
||||
mSprite->setAnimationFrames(0, 0, 16 * 0, 16 * 0, mWidth, mHeight);
|
||||
mSprite->setAnimationFrames(0, 1, 16 * 0, 16 * 1, mWidth, mHeight);
|
||||
filePNG = asset->get("item_points1_disk.png");
|
||||
fileANI = asset->get("item_points1_disk.ani");
|
||||
break;
|
||||
|
||||
case ITEM_POINTS_2_GAVINA:
|
||||
mSprite->setAnimationFrames(0, 0, 16 * 1, 16 * 0, mWidth, mHeight);
|
||||
mSprite->setAnimationFrames(0, 1, 16 * 1, 16 * 1, mWidth, mHeight);
|
||||
filePNG = asset->get("item_points2_gavina.png");
|
||||
fileANI = asset->get("item_points2_gavina.ani");
|
||||
break;
|
||||
|
||||
case ITEM_POINTS_3_PACMAR:
|
||||
mSprite->setAnimationFrames(0, 0, 16 * 2, 16 * 0, mWidth, mHeight);
|
||||
mSprite->setAnimationFrames(0, 1, 16 * 2, 16 * 1, mWidth, mHeight);
|
||||
filePNG = asset->get("item_points3_pacmar.png");
|
||||
fileANI = asset->get("item_points3_pacmar.ani");
|
||||
break;
|
||||
|
||||
case ITEM_CLOCK:
|
||||
mSprite->setAnimationFrames(0, 0, 16 * 3, 16 * 0, mWidth, mHeight);
|
||||
mSprite->setAnimationFrames(0, 1, 16 * 3, 16 * 1, mWidth, mHeight);
|
||||
filePNG = asset->get("item_clock.png");
|
||||
fileANI = asset->get("item_clock.ani");
|
||||
break;
|
||||
|
||||
case ITEM_COFFEE:
|
||||
mSprite->setAnimationFrames(0, 0, 16 * 5, 16 * 0, mWidth, mHeight);
|
||||
mSprite->setAnimationFrames(0, 1, 16 * 5, 16 * 1, mWidth, mHeight);
|
||||
filePNG = asset->get("item_coffee.png");
|
||||
fileANI = asset->get("item_coffee.ani");
|
||||
break;
|
||||
|
||||
case ITEM_COFFEE_MACHINE:
|
||||
mWidth = 32;
|
||||
mHeight = 32;
|
||||
mPosX = (((int)x + (PLAY_AREA_WIDTH / 2)) % (PLAY_AREA_WIDTH - mWidth - 5)) + 2;
|
||||
mPosY = PLAY_AREA_TOP - mHeight;
|
||||
mVelX = 0.0f;
|
||||
mVelY = -0.1f;
|
||||
mAccelY = 0.1f;
|
||||
mSprite->setAnimationFrames(0, 0, 32 * 0, 16 * 2, mWidth, mHeight);
|
||||
mSprite->setAnimationFrames(0, 1, 32 * 1, 16 * 2, mWidth, mHeight);
|
||||
mSprite->setAnimationFrames(0, 2, 32 * 2, 16 * 2, mWidth, mHeight);
|
||||
mSprite->setAnimationFrames(0, 3, 32 * 3, 16 * 2, mWidth, mHeight);
|
||||
mSprite->setPosX(mPosX);
|
||||
mSprite->setPosY(mPosY);
|
||||
mCollider.r = 10;
|
||||
shiftColliders();
|
||||
filePNG = asset->get("item_coffee_machine.png");
|
||||
fileANI = asset->get("item_coffee_machine.ani");
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
texture = new LTexture(renderer, filePNG);
|
||||
sprite = new AnimatedSprite(texture, renderer, fileANI);
|
||||
|
||||
this->type = type;
|
||||
enabled = true;
|
||||
timeToLive = 600;
|
||||
accelX = 0.0f;
|
||||
floorCollision = false;
|
||||
|
||||
if (type == ITEM_COFFEE_MACHINE)
|
||||
{
|
||||
width = 32;
|
||||
height = 32;
|
||||
posX = (((int)x + (PLAY_AREA_WIDTH / 2)) % (PLAY_AREA_WIDTH - width - 5)) + 2;
|
||||
posY = PLAY_AREA_TOP - height;
|
||||
velX = 0.0f;
|
||||
velY = -0.1f;
|
||||
accelY = 0.1f;
|
||||
collider.r = 10;
|
||||
}
|
||||
else
|
||||
{
|
||||
width = 16;
|
||||
height = 16;
|
||||
posX = x;
|
||||
posY = y;
|
||||
velX = -1.0f + ((rand() % 5) * 0.5f);
|
||||
velY = -4.0f;
|
||||
accelY = 0.2f;
|
||||
collider.r = width / 2;
|
||||
}
|
||||
|
||||
sprite->setPosX(posX);
|
||||
sprite->setPosY(posY);
|
||||
shiftColliders();
|
||||
}
|
||||
|
||||
// Destructor
|
||||
Item::~Item()
|
||||
{
|
||||
delete mSprite;
|
||||
mSprite = nullptr;
|
||||
delete texture;
|
||||
delete sprite;
|
||||
}
|
||||
|
||||
// Centra el objeto en la posición X
|
||||
void Item::allignTo(int x)
|
||||
{
|
||||
mPosX = float(x - (mWidth / 2));
|
||||
posX = float(x - (width / 2));
|
||||
|
||||
if (mPosX < PLAY_AREA_LEFT)
|
||||
if (posX < PLAY_AREA_LEFT)
|
||||
{
|
||||
mPosX = PLAY_AREA_LEFT + 1;
|
||||
posX = PLAY_AREA_LEFT + 1;
|
||||
}
|
||||
else if ((mPosX + mWidth) > PLAY_AREA_RIGHT)
|
||||
else if ((posX + width) > PLAY_AREA_RIGHT)
|
||||
{
|
||||
mPosX = float(PLAY_AREA_RIGHT - mWidth - 1);
|
||||
posX = float(PLAY_AREA_RIGHT - width - 1);
|
||||
}
|
||||
|
||||
// Posición X,Y del sprite
|
||||
mSprite->setPosX(int(mPosX));
|
||||
mSprite->setPosY(int(mPosY));
|
||||
sprite->setPosX(int(posX));
|
||||
sprite->setPosY(int(posY));
|
||||
|
||||
// Alinea el circulo de colisión con el objeto
|
||||
shiftColliders();
|
||||
@@ -124,15 +111,15 @@ void Item::allignTo(int x)
|
||||
// Pinta el objeto en la pantalla
|
||||
void Item::render()
|
||||
{
|
||||
if (mEnabled)
|
||||
if (enabled)
|
||||
{
|
||||
if (mTimeToLive > 200)
|
||||
if (timeToLive > 200)
|
||||
{
|
||||
mSprite->render();
|
||||
sprite->render();
|
||||
}
|
||||
else if (mTimeToLive % 20 > 10)
|
||||
else if (timeToLive % 20 > 10)
|
||||
{
|
||||
mSprite->render();
|
||||
sprite->render();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -140,71 +127,73 @@ void Item::render()
|
||||
// Actualiza la posición y estados del objeto
|
||||
void Item::move()
|
||||
{
|
||||
mFloorCollision = false;
|
||||
floorCollision = false;
|
||||
|
||||
// Calcula la nueva posición
|
||||
mPosX += mVelX;
|
||||
mPosY += mVelY;
|
||||
posX += velX;
|
||||
posY += velY;
|
||||
|
||||
// Aplica las aceleraciones a la velocidad
|
||||
mVelX += mAccelX;
|
||||
mVelY += mAccelY;
|
||||
velX += accelX;
|
||||
velY += accelY;
|
||||
|
||||
// Si queda fuera de pantalla, corregimos su posición y cambiamos su sentido
|
||||
if ((mPosX < PLAY_AREA_LEFT) || (mPosX + mWidth > PLAY_AREA_RIGHT))
|
||||
if ((posX < PLAY_AREA_LEFT) || (posX + width > PLAY_AREA_RIGHT))
|
||||
{
|
||||
// Corregir posición
|
||||
mPosX -= mVelX;
|
||||
posX -= velX;
|
||||
|
||||
// Invertir sentido
|
||||
mVelX = -mVelX;
|
||||
velX = -velX;
|
||||
}
|
||||
|
||||
// Si se sale por arriba rebota (excepto la maquina de café)
|
||||
if ((mPosY < PLAY_AREA_TOP) && !(mClass == ITEM_COFFEE_MACHINE))
|
||||
if ((posY < PLAY_AREA_TOP) && !(type == ITEM_COFFEE_MACHINE))
|
||||
{
|
||||
// Corrige
|
||||
mPosY = PLAY_AREA_TOP;
|
||||
posY = PLAY_AREA_TOP;
|
||||
|
||||
// Invierte el sentido
|
||||
mVelY = -mVelY;
|
||||
velY = -velY;
|
||||
}
|
||||
|
||||
// Si el objeto se sale por la parte inferior
|
||||
if (mPosY + mHeight > PLAY_AREA_BOTTOM)
|
||||
if (posY + height > PLAY_AREA_BOTTOM)
|
||||
{
|
||||
// Corrige
|
||||
mPosY -= mVelY;
|
||||
posY -= velY;
|
||||
|
||||
// Detiene el objeto
|
||||
mVelY = 0;
|
||||
mVelX = 0;
|
||||
mAccelX = 0;
|
||||
mAccelY = 0;
|
||||
mPosY = PLAY_AREA_BOTTOM - mHeight;
|
||||
if (mClass == ITEM_COFFEE_MACHINE)
|
||||
mFloorCollision = true;
|
||||
velY = 0;
|
||||
velX = 0;
|
||||
accelX = 0;
|
||||
accelY = 0;
|
||||
posY = PLAY_AREA_BOTTOM - height;
|
||||
if (type == ITEM_COFFEE_MACHINE)
|
||||
{
|
||||
floorCollision = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza la posición del sprite
|
||||
mSprite->setPosX(int(mPosX));
|
||||
mSprite->setPosY(int(mPosY));
|
||||
sprite->setPosX(int(posX));
|
||||
sprite->setPosY(int(posY));
|
||||
shiftColliders();
|
||||
}
|
||||
|
||||
// Pone a cero todos los valores del objeto
|
||||
void Item::erase()
|
||||
void Item::disable()
|
||||
{
|
||||
// init(NO_KIND, 0, 0, nullptr, nullptr);
|
||||
enabled = false;
|
||||
}
|
||||
|
||||
// Actualiza el objeto a su posicion, animación y controla los contadores
|
||||
void Item::update()
|
||||
{
|
||||
if (mEnabled)
|
||||
if (enabled)
|
||||
{
|
||||
move();
|
||||
shiftColliders();
|
||||
mSprite->animate();
|
||||
sprite->animate();
|
||||
updateTimeToLive();
|
||||
checkTimeToLive();
|
||||
}
|
||||
@@ -213,76 +202,70 @@ void Item::update()
|
||||
// Actualiza el contador
|
||||
void Item::updateTimeToLive()
|
||||
{
|
||||
if (mTimeToLive > 0)
|
||||
if (timeToLive > 0)
|
||||
{
|
||||
mTimeToLive--;
|
||||
timeToLive--;
|
||||
}
|
||||
}
|
||||
|
||||
// Comprueba si el objeto sigue vivo
|
||||
void Item::checkTimeToLive()
|
||||
{
|
||||
if (mTimeToLive == 0)
|
||||
erase();
|
||||
if (timeToLive == 0)
|
||||
disable();
|
||||
}
|
||||
|
||||
// Obtiene del valor de la variable
|
||||
float Item::getPosX()
|
||||
{
|
||||
return mPosX;
|
||||
return posX;
|
||||
}
|
||||
|
||||
// Obtiene del valor de la variable
|
||||
float Item::getPosY()
|
||||
{
|
||||
return mPosY;
|
||||
return posY;
|
||||
}
|
||||
|
||||
// Obtiene del valor de la variable
|
||||
int Item::getWidth()
|
||||
{
|
||||
return mWidth;
|
||||
return width;
|
||||
}
|
||||
|
||||
// Obtiene del valor de la variable
|
||||
int Item::getHeight()
|
||||
{
|
||||
return mHeight;
|
||||
return height;
|
||||
}
|
||||
|
||||
// Obtiene del valor de la variable
|
||||
int Item::getClass()
|
||||
{
|
||||
return mClass;
|
||||
return type;
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
bool Item::isEnabled()
|
||||
{
|
||||
return mEnabled;
|
||||
}
|
||||
|
||||
// Establece el valor de la variable
|
||||
void Item::setEnabled(bool value)
|
||||
{
|
||||
mEnabled = value;
|
||||
return enabled;
|
||||
}
|
||||
|
||||
// Obtiene el circulo de colisión
|
||||
circle_t &Item::getCollider()
|
||||
{
|
||||
return mCollider;
|
||||
return collider;
|
||||
}
|
||||
|
||||
// Alinea el circulo de colisión con la posición del objeto
|
||||
void Item::shiftColliders()
|
||||
{
|
||||
mCollider.x = int(mPosX + (mWidth / 2));
|
||||
mCollider.y = int(mPosY + (mHeight / 2));
|
||||
collider.x = int(posX + (width / 2));
|
||||
collider.y = int(posY + (height / 2));
|
||||
}
|
||||
|
||||
// Informa si el objeto ha colisionado con el suelo
|
||||
bool Item::floorCollision()
|
||||
bool Item::isOnFloor()
|
||||
{
|
||||
return mFloorCollision;
|
||||
return floorCollision;
|
||||
}
|
||||
Reference in New Issue
Block a user