[go: up one dir, main page]

Menu

[r9]: / Item.cpp  Maximize  Restore  History

Download this file

129 lines (116 with data), 3.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#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;
}
}