So guys I'm making a shooting game in SFML where the player sprite shoots at 5 enemies who are randomly generated on the screen once destroyed. I was just testing out my code and I noticed that my bullet class isn't working. Everytime I press spacebar the sprite slows down but no bullets shoot. I am fairly new to SFML (started last week) so I guess I made a very stupid mistake (and also I might be following an old tutorial. I couldn't find a viable tutorial to rely on so I chose an old but good one). Here's the code :-
(dunno how to do the code thing)
Bullet.h :-
#pragma once
#include <SFML/Graphics.hpp>
#include "Player.h"
class Bullet {
public:
Bullet(sf::Vector2f size, sf::Color color) {
bullet.setSize(size);
bullet.setFillColor(color);
}
void setPosition(sf::Vector2f position) {
bullet.setPosition(position);
}
void playerShoot(Player& player, float velocity){
rotation = player.getRotation();
fire.x = sin(rotation * (3.14 / 180)) * velocity;
fire.y = -cos(rotation * (3.14 / 180)) * velocity;
bullet.move(fire);
}
void enemyShoot() {
}
void draw(sf::RenderWindow& window) {
window.draw(bullet);
}
int getTop() {
return bullet.getPosition().x;
}
int getRight() {
return bullet.getPosition().x + bullet.getSize().x;
}
int getBottom() {
return bullet.getPosition().y + bullet.getSize().y;
}
int getLeft() {
return bullet.getPosition().x;
}
private:
sf::RectangleShape bullet;
sf::Vector2f fire;
double rotation;
};
Player.h :-
pragma once
#include<SFML/Graphics.hpp>
class Player {
public:
Player(sf::Vector2f size) {
player.setSize(size);
player.setRotation(0);
}
sf::Vector2f getSize() {
return player.getSize();
}
void setPosition(sf::Vector2f position) {
player.setPosition(position);
}
sf::Vector2f getPosition() {
return player.getPosition();
}
void setOrigin(sf::Vector2f origin) {
player.setOrigin(origin);
}
sf::Vector2f getOrigin() {
return player.getOrigin();
}
void setRotation(double rotation) {
player.setRotation(rotation);
}
double getRotation() {
return player.getRotation();
}
void setTexture(sf::Texture &texture) {
player.setTexture(&texture);
}
void setTexture(sf::IntRect texture) {
player.setTextureRect(texture);
}
void move(double angle, float velocity) {
rotation = angle;
vec.x = sin(rotation * (3.14 / 180)) * velocity;
vec.y = -cos(rotation * (3.14 / 180)) * velocity;
player.move(vec);
}
void draw(sf::RenderWindow& window) {
window.draw(player);
}
private:
sf::RectangleShape player;
sf::Vector2f vec;
double rotation;
};
main.cpp :-
#include<SFML\Graphics.hpp>
#include<SFML\Audio.hpp>
#include<iostream>
#include "Player.h"
#include "Bullet.h"
#include "Enemy.h"
int main() {
int i = 0;
bool isShooting = false;
std::vector<sf::Vector2f> position;
sf::Texture backgroundTex;
sf::Sprite background;
if (!backgroundTex.loadFromFile("space.jpg")) std::cout << "There was an unprecedented problem";
background.setTexture(backgroundTex);
sf::RenderWindow window(sf::VideoMode(700, 700), "Test", sf::Style::Default);
Player player(sf::Vector2f(49, 56));
player.setOrigin(sf::Vector2f(player.getSize().x / 2, player.getSize().y / 2));
player.setPosition(sf::Vector2f(350, 350));
player.setRotation(36);
sf::Texture tex;
if (!tex.loadFromFile("sheet.png")) {
std::cout << "Error loading texture" << std::endl;
}
player.setTexture(tex);
sf::Vector2u texSize = tex.getSize();
texSize.x /= 3;
texSize.y /= 3;
sf::IntRect texture(texSize.x * 2, texSize.y * 1, texSize.x, texSize.y);
player.setTexture(texture);
sf::Vector2f bulletInitPos(player.getOrigin().x, player.getOrigin().y + player.getSize().y / 2);
std::vector<Bullet> bulletVec;
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
switch (event.type) {
case sf::Event::Closed:
window.close();
break;
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
player.move(player.getRotation(), 0.5);
}if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
player.move(player.getRotation(), -0.5);
}if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
player.setRotation(player.getRotation() - 0.5);
}if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
player.setRotation(player.getRotation() + 0.5);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) {
isShooting = true;
}
if (isShooting == true) {
Bullet newBullet(sf::Vector2f(30, 5), sf::Color::Green);
newBullet.setPosition(sf::Vector2f(player.getPosition().x, player.getPosition().y));
bulletVec.push_back(newBullet);
isShooting = false;
}
for (int i = 0; i < bulletVec.size(); i++) {
if (i == 0) {
bulletVec.pop_back();
}
else {
bulletVec[i].draw(window);
bulletVec[i].playerShoot(player, 3);
}
}
window.clear();
window.draw(background);
player.draw(window);
window.display();
}
}