-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBullet.cpp
46 lines (45 loc) · 1.63 KB
/
Bullet.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
#include "Bullet.hpp"
#include "Collider.hpp"
#include "Enemy.hpp"
#include "GameEngine.hpp"
#include "Group.hpp"
#include "IObject.hpp"
#include "IScene.hpp"
#include "PlayScene.hpp"
#include "Point.hpp"
#include "Sprite.hpp"
#include "PoisonTurret.h"
PlayScene* Bullet::getPlayScene() {
return dynamic_cast<PlayScene*>(Engine::GameEngine::GetInstance().GetActiveScene());
}
void Bullet::OnExplode(Enemy* enemy) {
}
Bullet::Bullet(std::string img, float speed, float damage, Engine::Point position, Engine::Point forwardDirection, float rotation, Turret* parent) :
Sprite(img, position.x, position.y), speed(speed), damage(damage), parent(parent) {
Velocity = forwardDirection * speed;
Rotation = rotation;
CollisionRadius = 4;
}
void Bullet::Update(float deltaTime) {
Sprite::Update(deltaTime);
PlayScene* scene = getPlayScene();
// Can be improved by Spatial Hash, Quad Tree, ...
// However simply loop through all enemies is enough for this program.
for (auto& it : scene->EnemyGroup->GetObjects()) {
Enemy* enemy = dynamic_cast<Enemy*>(it);
if (!enemy->Visible)
continue;
if (Engine::Collider::IsCircleOverlap(Position, CollisionRadius, enemy->Position, enemy->CollisionRadius)) {
if(enemy->GetSpeed()>=15&&parent->GetPrice()==PoisonTurret::Price){
enemy->minusSpeed(15);
}
OnExplode(enemy);
enemy->Hit(damage);
getPlayScene()->BulletGroup->RemoveObject(objectIterator);
return;
}
}
// Check if out of boundary.
if (!Engine::Collider::IsRectOverlap(Position - Size / 2, Position + Size / 2, Engine::Point(0, 0), PlayScene::GetClientSize()))
getPlayScene()->BulletGroup->RemoveObject(objectIterator);
}