Je n'ai pas fait ça dans un code minimal mais complet seulement
#include "Scene.h"
sf::Clock horloge;
sf::Time tempsFrame;
Scene::Scene()
{
sf::RenderWindow window(sf::VideoMode(), "Warrior Of Space", sf::Style::Fullscreen);
window.setKeyRepeatEnabled(true);
window.setFramerateLimit(60);
sf::Texture image;
if(!image.loadFromFile("Ressources/Background.png"))
std::cout<<"Error"<<std::endl;
sf::Sprite background(image);
Spaceship spaceship;
Gui gui(&spaceship);
sf::View camera(sf::FloatRect(0, 0, 800, 600));
ProjectileManager projectile;
sf::RectangleShape rectangle;
rectangle.setSize(100, 100);
rectangle.setFillColor(sf::Color::Red);
rectangle.setPosition(200, 300);
float timer;
while (window.isOpen())
{
timer += m_tempsFrame.asSeconds();
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
if (event.type == sf::Event::Resized)
{
sf::FloatRect visibleArea(0, 0, event.size.width, event.size.height);
window.setView(sf::View(visibleArea));
}
if ( (event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Escape) )
{
window.close();
}
if ( (event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Right) )
{
spaceship.Move(5, 0);
}
if ( (event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Left) )
{
spaceship.Move(-5, 0);
}
if ( (event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Z) )
{
spaceship.addHealth(+5);
}
if ( (event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::A) )
{
spaceship.addHealth(-5);
}
if ( (event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Space) )
{
projectile.addProjectile();
}
}
window.clear();
window.draw(rectangle);
window.draw(spaceship);
projectile.displayProjectile(window);
window.draw(gui);
gui.Update(&spaceship);
m_tempsFrame = m_horloge.restart();
projectile.moveProjectile(timer);
window.setView(camera);
window.display();
}
}
#ifndef SCENE_H
#define SCENE_H
#include <SFML/Graphics.hpp>
#include <iostream>
#include "Spaceship.h"
#include "Projectile.h"
#include "Gui.h"
#include "ProjectileManager.h"
class Scene
{
public:
Scene();
private:
sf::Clock m_horloge;
sf::Time m_tempsFrame;
};
#endif // SCENE_H
#include "Projectile.h"
Projectile::Projectile() : sf::Sprite()
{
Projectile_Blue.setSize(sf::Vector2f(10, 10));
Projectile_Blue.setPosition(300, 400);
Projectile_Blue.setFillColor(sf::Color::Blue);
}
#ifndef PROJECTILE_H
#define PROJECTILE_H
#include <SFML/Graphics.hpp>
#include <iostream>
class Projectile : public sf::Sprite
{
public:
Projectile();
private:
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
{
target.draw(Projectile_Blue, states);
}
sf::RectangleShape Projectile_Blue;
};
#endif // PROJECTILE_H
#include "ProjectileManager.h"
ProjectileManager::ProjectileManager()
{
//ctor
}
ProjectileManager::~ProjectileManager()
{
//dtor
}
void ProjectileManager::addProjectile()
{
m_projectiles.push_back(Projectile());
}
void ProjectileManager::deleteProjectile()
{
for (unsigned int i = 0; i < m_projectiles.size(); i++)
if (m_projectiles[i].getPosition().y > 500)
m_projectiles.erase(m_projectiles.begin() + i);
}
void ProjectileManager::moveProjectile(float &timer)
{
for (unsigned int i = 0; i < m_projectiles.size(); ++i)
{
m_projectiles[i].move(10, 5 *timer);
int y = m_projectiles[i].getPosition().y;
int x = m_projectiles[i].getPosition().x;
std::cout << x << " | " << y << std::endl;
}
}
void ProjectileManager::displayProjectile(sf::RenderWindow& window)
{
for (unsigned int i = 0; i < m_projectiles.size(); ++i)
window.draw(m_projectiles[i]);
}
#ifndef PROJECTILEMANAGER_H
#define PROJECTILEMANAGER_H
#include <SFML/Graphics.hpp>
#include <iostream>
#include "Projectile.h"
class ProjectileManager
{
public:
ProjectileManager();
~ProjectileManager();
void addProjectile();
void deleteProjectile();
void moveProjectile(float &timer);
void displayProjectile(sf::RenderWindow& window);
private:
std::vector<Projectile> m_projectiles;
};
#endif // PROJECTILEMANAGER_H