New Projectiles class

Refactor of Bullet class
Different method of shooting.
This commit is contained in:
2024-12-01 17:44:25 +01:00
parent 2f6c98f4be
commit 2341c2fa6d
11 changed files with 127 additions and 62 deletions

View File

@@ -30,7 +30,7 @@ std::vector<Bullet> &Actor::getBullets() {
void Actor::updateBullets() {
for (auto& bullet : bullets) {
if(bullet.getStatus()) {
if(bullet.isOutOfBounds()) {
bullets.erase(bullets.begin());
}
}

View File

@@ -1,35 +1,9 @@
#include <iostream>
#include "../headers/Bullet.h"
Bullet::Bullet(float x, float y) {
bulletPosition.x = x;
bulletPosition.y = y;
outOfBounds = false;
bulletSprite.setTexture(Bullet::bulletTexture);
bulletSprite.setOrigin(bulletSprite.getLocalBounds().width/2, bulletSprite.getLocalBounds().height/2);
bulletSprite.setPosition(x, y);
bulletSpeed = -10.0f;
}
void Bullet::setSpeed(float speed) {
bulletSpeed = speed;
}
sf::Sprite &Bullet::getSprite() {
return bulletSprite;
}
void Bullet::update() {
bulletSprite.move(0.0f, bulletSpeed);
bulletPosition.y += int(bulletSpeed);
if(bulletPosition.y < -100) {
sprite.move(0.0f, speed);
position.y += int(speed);
if(position.y < -100) {
outOfBounds = true;
}
}
bool Bullet::getStatus() const {
return outOfBounds;
}
sf::Texture Bullet::bulletTexture = sf::Texture(); // plain init of static field
sf::Texture Bullet::rocketTexture = sf::Texture();
}

View File

@@ -3,22 +3,22 @@
// TODO: Podzielić na klasy Bullet i Rocket.
Player::Player(int x, int y, std::string path) : Actor(x, y, path) {
Bullet::bulletTexture.loadFromFile("../assets/img/bullet_left.png");
bulletTexture.loadFromFile("../assets/img/bullet_left.png");
rocketTexture.loadFromFile("../assets/img/Rocket_111.png");
};
void Player::shoot() {
auto now = std::chrono::steady_clock::now();
if (std::chrono::duration_cast<std::chrono::milliseconds>(now - lastShotTime).count() >= firerate) {
bullets.emplace_back(position.x, position.y);
bullets.emplace_back(position.x, position.y, bulletTexture);
lastShotTime = now;
}
}
// TODO: Czy strzał prawym musi mieć osobną tablicę z pociskami?
void Player::alternate_shoot() {
auto now = std::chrono::steady_clock::now();
if (std::chrono::duration_cast<std::chrono::milliseconds>(now - lastShotTime).count() >= firerate) {
rightBullets.emplace_back(position.x, position.y).getSprite().scale(1.5f, 1.5f);
rockets.emplace_back(position.x, position.y, rocketTexture).getSprite().scale(1.5f, 1.5f);
lastShotTime = now;
}
}
@@ -49,4 +49,8 @@ void Player::moveUp() {
void Player::moveDown() {
move(0.0f, moving_speed);
position.y += static_cast<int>(moving_speed);
}
}
std::vector<Rocket> &Player::getRockets() {
return rockets;
}

23
sources/Projectile.cpp Normal file
View File

@@ -0,0 +1,23 @@
#include "../headers/Projectile.h"
Projectile::Projectile(float x, float y, sf::Texture &texture) {
position.x = x;
position.y = y;
outOfBounds = false;
sprite.setTexture(texture);
sprite.setOrigin(sprite.getLocalBounds().width/2, sprite.getLocalBounds().height/2);
sprite.setPosition(x, y);
speed = -10.0f;
}
sf::Sprite &Projectile::getSprite() {
return sprite;
}
void Projectile::setSpeed(float speed) {
this->speed = speed;
}
bool Projectile::isOutOfBounds() const {
return outOfBounds;
}

9
sources/Rocket.cpp Normal file
View File

@@ -0,0 +1,9 @@
#include "../headers/Rocket.h"
void Rocket::update() {
sprite.move(0.0f, speed);
position.y += int(speed);
if(position.y < -100) {
outOfBounds = true;
}
}