-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBullet.cpp
74 lines (72 loc) · 1.57 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
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 <SFML/Graphics.hpp>
#include "Ship.h"
#include "Enemy.h"
#include "Bullet.h"
#include <iostream>
Bullet::Bullet()
{
m_bulletRect=sf::RectangleShape(sf::Vector2f(5,5));
m_bulletRect.setFillColor(sf::Color::Yellow);
m_isAlive=true;
m_speed=400;
}
void Bullet::move(sf::Time elapsed)
{
switch(m_bulletType)
{
case BulletTypes::Ship:
{
setPosition(sf::Vector2f(getPosition().x, getPosition().y-m_speed*elapsed.asSeconds()));
}
break;
case BulletTypes::Enemy:
{
setPosition(sf::Vector2f(getPosition().x, getPosition().y+m_speed*elapsed.asSeconds()));
}
break;
}
}
void Bullet::die() { m_isAlive = false; }
void Bullet::render(sf::RenderWindow &renderWindow) { renderWindow.draw(m_bulletRect); }
bool Bullet::checkCollisionWith(const sf::FloatRect &rect)
{
sf::FloatRect bulletRect=m_bulletRect.getGlobalBounds();
if(bulletRect.intersects(rect))
{
m_isAlive = false;
return true;
}
return false;
}
void Bullet::setPosition(sf::Vector2f position)
{
m_bulletRect.setPosition(position);
}
sf::Vector2f Bullet::getPosition() const
{
return m_bulletRect.getPosition();
}
sf::Vector2f Bullet::getSize() const
{
return m_bulletRect.getSize();
}
bool Bullet::isAlive() const
{
return m_isAlive;
}
void Bullet::setBulletType(BulletTypes type)
{
m_bulletType = type;
}
BulletTypes Bullet::getBulletType()
{
return m_bulletType;
}
void Bullet::setSpeed(int speed)
{
m_speed = speed;
}
int Bullet::getSpeed()
{
return m_speed;
}