-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShootingEnemy.cpp
63 lines (57 loc) · 1.46 KB
/
ShootingEnemy.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
//
// Created by s5304091
//
#include "ShootingEnemy.h"
#include <iostream>
ShootingEnemy::ShootingEnemy(const ngl::Vec3 &startPosition, float speed, int health, float radius) : Enemy(
startPosition, speed, health, radius),
m_fireRate(1.5f),
m_timeSinceLastShot(0.0f)
{
}
void ShootingEnemy::update(float deltaTime)
{
// std::cout <<"Enemy shoot delta time update " << deltaTime << std::endl;
Enemy::update(deltaTime);
m_timeSinceLastShot += deltaTime;
// std::cout <<"Enemy shoot time update " << m_timeSinceLastShot << std::endl;
if (m_timeSinceLastShot >= m_fireRate)
{
std::cout <<"Enemy shoot!\n";
firelaser();
m_timeSinceLastShot = 0.0f;
}
for (auto& laser : m_lasers)
{
laser.update(deltaTime);
}
}
void ShootingEnemy::firelaser()
{
ngl::Vec3 laserStartPosition = m_pos;
laserStartPosition.m_y -= 1.0f;
m_lasers.emplace_back(laserStartPosition, ngl::Vec3(0, -1, 0),10.0f);
}
const std::vector<EnemyLaser> &ShootingEnemy::getLaser() const
{
return m_lasers;
}
bool ShootingEnemy::checkLaserCollision(SpaceShip &ship)
{
bool hit = false;
auto it = m_lasers.begin();
while(it != m_lasers.end())
{
if (ship.checkCollision(it->getPosition(),it->getRadius()))
{
hit = true;
ship.decreaseHealth(1);
it = m_lasers.erase(it);
}
else
{
++it;
}
}
return hit;
}