-
Notifications
You must be signed in to change notification settings - Fork 0
/
powerUp.cpp
91 lines (75 loc) · 2.93 KB
/
powerUp.cpp
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
#include "powerUp.h"
//=============================================================================
// constructor
//=============================================================================
PowerUp::PowerUp() : Entity()
{
//startFrame = powerUpNS::START_FRAME; // first frame of ship animation
//endFrame = powerUpNS::END_FRAME; // last frame of ship animation
//currentFrame = startFrame;
radius = powerUpNS::WIDTH/2.0;
collisionType = entityNS::CIRCLE;
velocity.x = 0;
velocity.y = 0;
active = false;
visible = false;
}
//=============================================================================
// Initialize the Entity.
// Pre: *gamePtr = pointer to Game object
// width = width of Image in pixels (0 = use full texture width)
// height = height of Image in pixels (0 = use full texture height)
// ncols = number of columns in texture (1 to n) (0 same as 1)
// *textureM = pointer to TextureManager object
// Post: returns true if successful, false if failed
//=============================================================================
bool PowerUp::initialize(Game *gamePtr, int width, int height, int ncols,
TextureManager *textureM)
{
float xLoc;
short powerType;
active = true;
visible = true;
std::default_random_engine generator(static_cast<unsigned int>(time(0)));
// randomly generate the x coord of where the power up will spawn as well as what power up it is
std::uniform_int_distribution<float> distributionXLoc(0, GAME_WIDTH - powerUpNS::WIDTH * spriteData.scale);
std::uniform_int_distribution<int> distributionPowerType(0, 1);
xLoc = distributionXLoc(generator);
powerType = distributionPowerType(generator);
if (powerType == 0)
{
powerUpType = powerUpNS::INVINCIBILITY;
startFrame = powerUpNS::START_FRAME_INVINCIBILITY;
endFrame = powerUpNS::END_FRAME_INVINCIBILITY;
}
else
{
powerUpType = powerUpNS::BLACK_HOLE;
startFrame = powerUpNS::START_FRAME_BLACK_HOLE;
endFrame = powerUpNS::END_FRAME_BLACK_HOLE;
}
setFrames(startFrame, endFrame);
setCurrentFrame(startFrame);
// power up does not move in the x direction
velocity.x = 0.0f;
spriteData.x = float(xLoc);
spriteData.y = -spriteData.scale * height;
return(Entity::initialize(gamePtr, width, height, ncols, textureM));
}
//=============================================================================
// update
// typically called once per frame
// frameTime is used to regulate the speed of movement and animation
//=============================================================================
void PowerUp::update(float frameTime)
{
if(!active)
return;
velocity.y = powerUpNS::SPEED * frameTime;
spriteData.x += frameTime * velocity.x; // move power up along X
spriteData.y += frameTime * velocity.y; // move power up along Y
// the power up went off screen. Deactivate it.
if (spriteData.y > GAME_HEIGHT)
active = false;
Entity::update(frameTime);
}