#include "Item.h"
// Constructor
Item::Item(int i, int aTime, int dDegree, int locationX, int locationY, int type)
{
activeSurface = NULL;
destroySurface = NULL;
activeAnimate = new Animation();
destroyAnimate = new Animation();
setType(i);
setItemType(type);
activeAnimate->setFrameRate(aTime);
destroyAnimate->setFrameRate(100);
damageDegree = dDegree;
setLocation(locationX, locationY);
}
// Default destructor
Item::~Item()
{
}
void Item::init()
{
setStatus(SPRITEACTIVE);
setWH(0,0);
setControlType(0);
}
void Item::loadActiveAnimatedClips(string fileName, int maxFrame, int w, int h)
{
activeSurface = loadImage(fileName);
if (activeSurface == NULL)
{
cout << "Unable to load the animate file: " << fileName << endl;
exit(1);
}
activeAnimateClips = loadAnimatedClips(fileName, maxFrame, w, h, activeAnimate);
}
void Item::loadDestroyAnimatedClips(string fileName, int maxFrame, int w, int h)
{
destroySurface = loadImage(fileName);
if (destroySurface == NULL)
{
cout << "Unable to load the animated file: " << fileName << endl;
exit(1);
}
destroyAnimateClips = loadAnimatedClips(fileName, maxFrame, w, h, destroyAnimate);
}
vector<SDL_Rect> Item::loadAnimatedClips(string fileName, int maxFrame, int w, int h, Animation *animate)
{
vector<SDL_Rect> clips;
animate->setMaxFrame(maxFrame);
for (int i = 0; i < maxFrame; ++i)
{
SDL_Rect frame;
frame.x = i * w;
frame.y = 0;
frame.w = w;
frame.h = h;
clips.push_back(frame);
}
return clips;
}
void Item::draw(SDL_Surface *screen, SDL_Rect *camera)
{
int status = getStatus();
switch(status)
{
case SPRITEACTIVE:
applySurface(getX() - camera->x, getY() - camera->y, activeSurface, screen, &activeAnimateClips[activeAnimate->getCurrentFrame()]);
break;
case SPRITEDESTROY:
applySurface(getX() - camera->x, getY() - camera->y, destroySurface, screen, &destroyAnimateClips[destroyAnimate->getCurrentFrame()]); // Center
for (int i = 1; i <= damageDegree; ++i)
{
applySurface(getX() - (destroyAnimateClips[0].w * i) - camera->x, getY() - camera->y, destroySurface, screen, &destroyAnimateClips[destroyAnimate->getCurrentFrame()]); // left
applySurface(getX() + (destroyAnimateClips[0].w * i) - camera->x, getY() - camera->y, destroySurface, screen, &destroyAnimateClips[destroyAnimate->getCurrentFrame()]); // right
applySurface(getX() - camera->x, getY() - (destroyAnimateClips[0].h * i) - camera->y, destroySurface, screen, &destroyAnimateClips[destroyAnimate->getCurrentFrame()]); // top
applySurface(getX() - camera->x, getY() + (destroyAnimateClips[0].h * i) - camera->y, destroySurface, screen, &destroyAnimateClips[destroyAnimate->getCurrentFrame()]); // Bottom
}
break;
case SPRITEIDLE:
break;
}
}
int Item::handleInput(SDL_Event *event)
{
return 0;
}
void Item::update()
{
int status = getStatus();
int type = getType();
bool isStop = false;
switch(status)
{
case SPRITEACTIVE:
if (activeAnimate->getCurrentFrame() == activeAnimate->getMaxFrame() - 1)
isStop = true;
if (isStop)
setStatus(SPRITEDESTROY);
else
activeAnimate->onAnimate();
break;
case SPRITEDESTROY:
if (type == SPRITEBOMB)
{
if (destroyAnimate->getCurrentFrame() == destroyAnimate->getMaxFrame() - 1)
setStatus(SPRITEDIE);
else
destroyAnimate->onAnimate();
}
else
setStatus(SPRITEDIE);
break;
case SPRITEIDLE:
break;
}
}