Estoy tratando de aprender sobre programación de vídeojuegos, para eso estoy usando la librería SFML2. La idea es desarrollar un juego de naves y disparos.
La cuestión es que quiero hacer que la nave del jugador dispare.
Hasta ahora tengo el siguiente código:
Clase Aircraft.h:
class Aircraft {
private:
sf::Texture texture;
sf::Sprite mPlayer;
float playerSpeed;
sf::Vector2f playerDirection;
public:
Aircraft();
void Render(sf::RenderWindow *wnd);
void update(sf::Time deltaTime);
sf::Vector2f getDirection(){
return playerDirection;
}
void Up();
void Down();
void Left();
void Right();
}
Aircraft.cpp:
Aircraft::Aircraft() {
texture.loadFromFile("SFML-Game-Development-Book-master/02_Resources/Media/Textures/Eagle.png");
mPlayer.setTexture(texture);
mPlayer.setPosition(200.0f, 200.0f);
playerDirection.x = 0.0f;
playerDirection.y = 0.0f;
playerSpeed = 220.0f;
}
void Aircraft::Render(sf::RenderWindow *wnd){
wnd->draw(mPlayer);
}
void Aircraft::update(sf::Time dt){
mPlayer.move(playerDirection * playerSpeed * dt.asSeconds());
playerDirection.x = 0.0f;
playerDirection.y = 0.0f;
}
void Aircraft::Up(){
playerDirection.y = -1;
}
void Aircraft::Down(){
playerDirection.y = 1;
}
void Aircraft::Left(){
playerDirection.x = -1;
}
void Aircraft::Right(){
playerDirection.x = 1;
}
Clase Bullet.h para los disparos:
class Bullet {
private:
sf::Texture texture;
sf::Sprite sprite;
float speed = -300.0f;
int posX, posY;
public:
Bullet(int _posX, int _posY);
void update(sf::Time deltaTime, int _posX, int _posY);
void render(sf::RenderWindow *_wnd);
};
Bullet.cpp:
Bullet::Bullet(int _posX, int _posY) {
texture.loadFromFile("SFML-Game-Development-Book-master/10_Network/Media/Textures/misil1.png");
sprite.setTexture(texture);
sprite.rotate(-90);
sprite.setScale(0.5f, 0.5f);
posX = _posX;
posY = _posY;
}
void Bullet::update(sf::Time deltaTime, int _posX, int _posY){
posX = _posX;
posY = _posY;
sprite.setPosition(posX, posY);
sprite.move(0, speed * deltaTime.asSeconds());
}
void Bullet::render(sf::RenderWindow *_wnd){
_wnd->draw(sprite);
}
La clase Game.h:
class Game {
private:
sf::RenderWindow *mWindow;
ScrollingBackground background;
Aircraft aircraft;
std::vector<Enemy> enemies;
EnemyFactory enemy;
Bullet bullet;
private:
void proccesEvent();
void update(sf::Time deltaTime);
void render();
public:
Game();
void run();
};
Game.cpp:
Game::Game():bullet(0,0){
mWindow = new sf::RenderWindow(sf::VideoMode(640,480), "ventana SFML");
for (int i=0; i<5; i++){
enemies.push_back(enemy.create(640,480));
}
}
void Game::proccesEvent(){
sf::Event event;
while(mWindow->pollEvent(event)){
if(event.type == sf::Event::Closed){
mWindow->close();
}
}
}
void Game::update(sf::Time deltaTime){
background.update(deltaTime);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)){
aircraft.Up();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)){
aircraft.Down();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)){
aircraft.Left();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)){
aircraft.Right();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space)){
bullet.update(deltaTime, aircraft.getDirection().x, aircraft.getDirection().y);
}
aircraft.update(deltaTime);
for (int i=0; i<enemies.size(); i++){
enemies[i].update(deltaTime);
}
}
void Game::render(){
mWindow->clear();
background.render(mWindow);
aircraft.Render(mWindow);
for (int i=0; i<enemies.size(); i++){
enemies[i].render(mWindow);
}
bullet.render(mWindow);
mWindow->display();
}
void Game::run(){
sf::Clock clock;
while(mWindow->isOpen()){
sf::Time deltaTime = clock.restart();
proccesEvent();
update(deltaTime);
render();
}
}
La idea es que al apretar la barra se cree la bala en la posición de la nave y se mueva solo verticalmente. El problema es que la bala no aparece en ningún momento. Las texturas, se cargan correctamente así que no creo que esté ahí el error.
main
? Hay un buen tutorial sobre el problema logico (y no grafico de ninguna manera) sobre openclassroom.comGame
.