This repository has been archived by the owner on Mar 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBulletHandler.cpp
74 lines (64 loc) · 2.01 KB
/
BulletHandler.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
#include "BulletHandler.h"
#include "Game.h"
#include <iostream>
BulletHandler *BulletHandler::s_pInstance = nullptr;
void BulletHandler::addPlayerBullet(int x, int y, int width, int height, std::string textureID,
int numFrames, Vector2D heading)
{
PlayerBullet *pPlayerBullet = new PlayerBullet();
pPlayerBullet->load(std::unique_ptr<LoaderParams>(new LoaderParams(x, y, width, height, textureID, numFrames)), heading);
// std::cerr << x << " " << y << " " << width << " " << height << " " << textureID << " " << heading.getX() << " " << heading.getY() << std::endl;
m_playerBullets.push_back(pPlayerBullet);
}
void BulletHandler::addEnemyBullet(int x, int y, int width, int height, std::string textureID,
int numFrames, Vector2D heading)
{
EnemyBullet *pEnemyBullet = new EnemyBullet();
pEnemyBullet->load(std::unique_ptr<LoaderParams>(new LoaderParams(x, y, width, height, textureID, numFrames)), heading);
m_enemyBullets.push_back(pEnemyBullet);
}
void BulletHandler::updateBullets()
{
for (auto it = m_playerBullets.begin(); it != m_playerBullets.end();)
{
if ((*it)->getPosition().getX() < 0 || (*it)->getPosition().getX() > TheGame::Instance()->getGameWidth() ||
(*it)->getPosition().getY() < 0 || (*it)->getPosition().getY() > TheGame::Instance()->getGameHeight() ||
(*it)->dead())
{
delete *it;
it = m_playerBullets.erase(it);
}
else
{
(*it)->update();
++it;
}
}
for (auto it = m_enemyBullets.begin(); it != m_enemyBullets.end();)
{
if ((*it)->getPosition().getX() < 0 || (*it)->getPosition().getX() > TheGame::Instance()->getGameWidth() ||
(*it)->getPosition().getY() < 0 || (*it)->getPosition().getY() > TheGame::Instance()->getGameHeight() ||
(*it)->dead())
{
delete *it;
it = m_enemyBullets.erase(it);
}
else
{
(*it)->update();
++it;
}
}
}
void BulletHandler::drawBullets()
{
for (auto i : m_playerBullets)
i->draw();
for (auto i : m_enemyBullets)
i->draw();
}
void BulletHandler::clearBullets()
{
m_enemyBullets.clear();
m_playerBullets.clear();
}