-
Notifications
You must be signed in to change notification settings - Fork 0
/
BulletManager.cpp
81 lines (71 loc) · 2.57 KB
/
BulletManager.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
#include "BulletManager.h"
#include "CollisionDetection.h"
namespace Snowy
{
BulletManager::BulletManager(Tmpl8::Sprite* bulletSprite, int maxOnScreen, int bulletSpeed_min, int bulletSpeed_max)
: maxOnScreen(maxOnScreen), bulletSpeed_min(bulletSpeed_min), bulletSpeed_max(bulletSpeed_max),
bulletSprite(bulletSprite), bulletArray({})
{
}
void BulletManager::SpawnBullets()
{
for (int i = 0; i < (maxOnScreen - static_cast<int>(bulletArray.size())); i++)
{
bulletArray.emplace_back(bulletSprite);
bulletArray[bulletArray.size() - 1].SetPosition(static_cast<float>(IRand(ScreenWidth - 50)), static_cast<float>(IRand(300)));
bulletArray[bulletArray.size() - 1].SetVelocity(0.f, static_cast<float>(rand() % bulletSpeed_min + bulletSpeed_max));
bulletArray[bulletArray.size() - 1].setHitboxDiameter(25.f);
}
}
void BulletManager::BorderCheck(float deltaTime)
{
for (auto iter = bulletArray.begin(); iter != bulletArray.end(); )
{
auto& bullet = *iter;
bullet.SetPosition(bullet.GetPosition() + bullet.GetVelocity() * deltaTime);
if (bullet.GetPosition().x > 0 &&
bullet.GetPosition().x < ScreenWidth - 25 &&
bullet.GetPosition().y > 0 &&
bullet.GetPosition().y < ScreenHeight - 25)
{
++iter;
}
else
{
iter = bulletArray.erase(iter);
}
}
}
bool BulletManager::DetectCollisions(Kinematicbody playerBody) const
{
if (bulletArray.size() != 0)
{
for (const Snowy::Kinematicbody& bulletIT : bulletArray)
{
if (Snowy::detectCollision(bulletIT, playerBody))
{
return true;
}
}
return false;
}
}
void BulletManager::DrawBullets(Tmpl8::Surface* screen)
{
for (auto iter = bulletArray.begin(); iter != bulletArray.end(); )
{
auto& bullet = *iter;
bullet.DrawBody(static_cast<int>(25.f), static_cast<int>(25.f), screen);
/*screen->Line(bullet.GetPosition().x + bullet.getHitboxRadius(), bullet.GetPosition().y + bullet.getHitboxRadius(), bullet.GetPosition().x + bullet.getHitboxRadius(), ScreenHeight, 0xFF0000);*/
++iter;
}
}
void BulletManager::ClearArray()
{
bulletArray.clear();
}
std::vector<Snowy::Kinematicbody> BulletManager::getBulletArray()
{
return bulletArray;
}
}