/
CyraxWComponent.cpp
89 lines (86 loc) · 2.42 KB
/
CyraxWComponent.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
#include "CyraxWComponent.h"
#include <vector>
#include "bullet.h"
CyraxWComponent::CyraxWComponent(Game *cipher)
{
bulletList.reserve(10);
if (!WbulletTexture.initialize(cipher->getGraphics(), CYRAXW_IMAGE))
throw(GameError(gameErrorNS::FATAL_ERROR, "Error initializing cyrax W image"));
}
CyraxWComponent::~CyraxWComponent()
{
bulletList.clear();
}
void CyraxWComponent::update(float frameTime)
{
for (int i = 0; i < bulletList.size(); i++)
{
bulletList[i]->update(frameTime);
if (bulletList[i]->getActive())
{
if (bulletList[i]->getCurrRange() == 30)
{
bulletList[i]->setCurrentFrame(CyraxWComponentNS::WBULLET_END_FRAME);
VECTOR2 newSpeed;
if (bulletList[i]->getMotion()->getVelocity().x >= 0)
{
newSpeed.x = -CyraxWComponentNS::WBULLET_MAX_SPEED;
newSpeed.y = 0;
}
else
{
newSpeed.x = CyraxWComponentNS::WBULLET_MAX_SPEED;
newSpeed.y = 0;
}
bulletList[i]->setDirection(newSpeed);
}
if (bulletList[i]->getCurrRange() == CyraxWComponentNS::WBULLET_MAX_RANGE)
{
bulletList.erase(bulletList.begin() + i);
}
}
}
}
void CyraxWComponent::draw()
{
for (int i = 0; i < bulletList.size(); i++)
{
bulletList[i]->draw();
}
}
void CyraxWComponent::releaseAll()
{
WbulletTexture.onLostDevice();
}
void CyraxWComponent::resetAll()
{
WbulletTexture.onResetDevice();
}
void CyraxWComponent::activate(bool facingRight, float x, float y, Game *cipher)
{
Bullet *newBullet = new Bullet();
newBullet->setBulletSprite(CyraxWComponentNS::WIDTH, CyraxWComponentNS::HEIGHT, CyraxWComponentNS::WBULLET_START_FRAME, CyraxWComponentNS::WBULLET_END_FRAME, 100);
newBullet->setCurrentFrame(CyraxWComponentNS::WBULLET_START_FRAME);
if (!newBullet->initialize(cipher, CyraxWComponentNS::WIDTH, CyraxWComponentNS::HEIGHT, CyraxWComponentNS::TEXTURE_COLS, &WbulletTexture))
{
throw(GameError(gameErrorNS::FATAL_ERROR, "Error initializing cyrax W"));
}
newBullet->setX(x);
newBullet->setY(y);
newBullet->setActive(true);
VECTOR2 direction;
if (facingRight) //shoot right
{
direction.x = CyraxWComponentNS::WBULLET_MIN_SPEED;
direction.y = CyraxWComponentNS::WBULLET_MIN_SPEED;
newBullet->flipHorizontal(true);
newBullet->setDirection(direction);
}
else if (!facingRight) //shoot left
{
direction.x = -CyraxWComponentNS::WBULLET_MIN_SPEED;
direction.y = CyraxWComponentNS::WBULLET_MIN_SPEED;
newBullet->setDirection(direction);
}
bulletList.push_back(newBullet);
}