Généralisation de la barre d'action au jeu

This commit is contained in:
Mattéo Delabre 2016-04-27 22:23:44 +02:00
parent b7ebc8e91d
commit 656bd60643
13 changed files with 123 additions and 103 deletions

View File

@ -4,7 +4,6 @@
#include <SFGUI/Widgets.hpp>
#include <SFML/Graphics.hpp>
#include "../objects/object.hpp"
#include "../states/level.hpp"
/**
* Barre d'outils pour les actions en jeu

View File

@ -5,8 +5,8 @@
#include <SFML/Graphics.hpp>
#include <map>
#include <functional>
#include "../states/level.hpp"
#include "../objects/object.hpp"
#include "../utility.hpp"
#include "object_button.hpp"
/**
@ -43,7 +43,7 @@ private:
Object::Ptr createSwitchBlock();
Object::Ptr createFinishBlock();
Object::Ptr createKillBlock();
Object::Ptr createGravityBlock(GravityDirection direction);
Object::Ptr createGravityBlock(Utility::Direction direction);
public:
ObjectToolbar();

View File

@ -3,10 +3,10 @@
#include <SFML/Graphics.hpp>
#include <memory>
#include "../utility.hpp"
#include "block.hpp"
class Game;
enum class GravityDirection;
/**
* Le bloc de gravité est un bloc spécial qui change l'orientation
@ -19,7 +19,7 @@ public:
typedef std::shared_ptr<GravityBlock> Ptr;
private:
GravityDirection gravity_direction;
Utility::Direction gravity_direction;
sf::Sprite icon_sprite;
float opacity;
bool used;
@ -73,12 +73,12 @@ public:
/**
* Récupère la direction de gravité du bloc changeur de gravité
*/
GravityDirection getGravityDirection() const;
Utility::Direction getGravityDirection() const;
/**
* Modifie la direction de gravité du bloc
*/
void setGravityDirection(GravityDirection set_gravity_direction);
void setGravityDirection(Utility::Direction set_gravity_direction);
};
#endif

View File

@ -1,7 +1,6 @@
#ifndef __SKIZZLE_EDITOR_HPP__
#define __SKIZZLE_EDITOR_HPP__
#include "../gui/action_toolbar.hpp"
#include "../gui/object_toolbar.hpp"
#include "level.hpp"
@ -22,10 +21,7 @@ private:
sf::Vector2f *drag_control_point;
DragMode drag_mode;
ActionToolbar action_toolbar;
ObjectToolbar object_toolbar;
sfg::Button::Ptr mute_button;
/**
* Renvoie l'objet pointé à la position donnée
@ -78,11 +74,16 @@ private:
void selectAll();
protected:
/**
* Demande le dessin d'une frame
*/
void frame() override;
/**
* Dessine tous les objets, le fond et
* l'interface de l'éditeur
*/
virtual void draw();
void draw() override;
public:
Editor(Manager& manager);
@ -98,11 +99,6 @@ public:
*/
void processEvent(const sf::Event& event) override;
/**
* Demande le dessin d'une frame
*/
void frame() override;
/**
* Lance le test du niveau
*/

View File

@ -43,11 +43,16 @@ private:
bool isInZone(Object::Ptr object);
protected:
/**
* Demande le dessin d'une frame
*/
void frame() override;
/**
* Dessine tous les objets, le fond et
* l'interface de jeu
*/
virtual void draw();
void draw() override;
/**
* S'assure que la caméra est centrée sur les joueurs
@ -61,17 +66,12 @@ public:
/**
* Active l'état
*/
virtual void enable();
void enable() override;
/**
* Traite l'événement donné
*/
virtual void processEvent(const sf::Event& event);
/**
* Demande le dessin d'une frame
*/
virtual void frame();
void processEvent(const sf::Event& event) override;
/**
* Tue l'objet donné

View File

@ -3,15 +3,14 @@
#include <fstream>
#include <vector>
#include "../gui/action_toolbar.hpp"
#include "../utility.hpp"
#include "../objects/object.hpp"
#include "../objects/player.hpp"
#include "state.hpp"
class Manager;
// liste des directions de la gravité
enum class GravityDirection {NORTH, EAST, SOUTH, WEST};
/**
* La classe Level est une abstraction des
* classes affichant une collection d'objets, comme
@ -21,7 +20,7 @@ class Level : public State {
private:
sf::View camera;
float camera_angle;
GravityDirection gravity_direction;
Utility::Direction gravity_direction;
sf::String name;
std::string path;
@ -36,7 +35,16 @@ private:
std::vector<Player::Ptr> players;
std::vector<sf::Vector2f> zone;
sfg::Button::Ptr mute_button;
protected:
ActionToolbar action_toolbar;
/**
* Demande le dessin d'une frame
*/
virtual void frame();
/**
* Dessine tous les objets et le fond à l'écran
*/
@ -146,12 +154,12 @@ public:
/**
* Récupère la direction de la gravité
*/
GravityDirection getGravityDirection();
Utility::Direction getGravityDirection();
/**
* Modifie la direction de la gravité
*/
void setGravityDirection(GravityDirection set_gravity_direction);
void setGravityDirection(Utility::Direction set_gravity_direction);
/**
* Récupère la liste des objets

View File

@ -10,6 +10,11 @@ namespace Utility {
* avec la vitesse donnée
*/
float animateValue(float current, float speed, float goal);
/**
* Liste des directions
*/
enum class Direction {NORTH, EAST, SOUTH, WEST};
}
#endif

Binary file not shown.

View File

@ -29,19 +29,19 @@ ObjectToolbar::ObjectToolbar() {
addCreator("kill_block", std::bind(&ObjectToolbar::createKillBlock, this));
addCreator("gravity_block_north", std::bind(
&ObjectToolbar::createGravityBlock, this, GravityDirection::NORTH
&ObjectToolbar::createGravityBlock, this, Utility::Direction::NORTH
));
addCreator("gravity_block_east", std::bind(
&ObjectToolbar::createGravityBlock, this, GravityDirection::EAST
&ObjectToolbar::createGravityBlock, this, Utility::Direction::EAST
));
addCreator("gravity_block_south", std::bind(
&ObjectToolbar::createGravityBlock, this, GravityDirection::SOUTH
&ObjectToolbar::createGravityBlock, this, Utility::Direction::SOUTH
));
addCreator("gravity_block_west", std::bind(
&ObjectToolbar::createGravityBlock, this, GravityDirection::WEST
&ObjectToolbar::createGravityBlock, this, Utility::Direction::WEST
));
// attachement de la liste des créateurs à l'interface
@ -117,7 +117,7 @@ Object::Ptr ObjectToolbar::createKillBlock() {
return Object::Ptr(new KillBlock);
}
Object::Ptr ObjectToolbar::createGravityBlock(GravityDirection direction) {
Object::Ptr ObjectToolbar::createGravityBlock(Utility::Direction direction) {
GravityBlock::Ptr gravity_block = GravityBlock::Ptr(new GravityBlock);
gravity_block->setGravityDirection(direction);
return std::dynamic_pointer_cast<Object>(gravity_block);

View File

@ -53,7 +53,7 @@ void GravityBlock::init(std::ifstream& file, Object::Ptr object) {
// lecture de la direction de la gravité
char gravity_direction;
file.read(&gravity_direction, 1);
gravity_block->setGravityDirection((GravityDirection) gravity_direction);
gravity_block->setGravityDirection((Utility::Direction) gravity_direction);
// lecture des propriétés d'un bloc
Block::init(file, object);
@ -74,30 +74,30 @@ void GravityBlock::save(std::ofstream& file) const {
Block::save(file);
}
GravityDirection GravityBlock::getGravityDirection() const {
Utility::Direction GravityBlock::getGravityDirection() const {
return gravity_direction;
}
void GravityBlock::setGravityDirection(GravityDirection set_gravity_direction) {
void GravityBlock::setGravityDirection(Utility::Direction set_gravity_direction) {
gravity_direction = set_gravity_direction;
// sélectionne le sprite d'icône selon la direction
std::string texture;
switch (gravity_direction) {
case GravityDirection::NORTH:
case Utility::Direction::NORTH:
texture = "north";
break;
case GravityDirection::EAST:
case Utility::Direction::EAST:
texture = "east";
break;
case GravityDirection::SOUTH:
case Utility::Direction::SOUTH:
texture = "south";
break;
case GravityDirection::WEST:
case Utility::Direction::WEST:
texture = "west";
break;
}

View File

@ -35,30 +35,11 @@ inline sf::Vector2f roundVectorToGrid(sf::Vector2f input) {
Editor::Editor(Manager& manager) : Level(manager),
drag_control_point(nullptr), drag_mode(Editor::DragMode::NONE) {
// ajout des boutons dans la barre d'action
// ajout des boutons d'action de la barre d'action
action_toolbar.addButton(
*ResourceManager::get().getImage("toolbar/icon_back.tga"),
std::bind(&Manager::popState, &getManager())
*ResourceManager::get().getImage("toolbar/icon_gear.tga")
);
mute_button = action_toolbar.addButton(
*ResourceManager::get().getImage("toolbar/icon_music.tga"),
[]() {
// on inverse le drapeau de muet
ResourceManager::get().setMuted(
!ResourceManager::get().isMuted()
);
}
);
action_toolbar.addSpacer(20, true, false);
action_toolbar.addButton(
*ResourceManager::get().getImage("toolbar/icon_no_music.tga")
);
action_toolbar.addSpacer(20, true, false);
action_toolbar.addButton(
*ResourceManager::get().getImage("toolbar/icon_save.tga"),
std::bind(&Editor::save, this)
@ -69,17 +50,11 @@ Editor::Editor(Manager& manager) : Level(manager),
std::bind(&Editor::test, this)
);
action_toolbar.addButton(
*ResourceManager::get().getImage("toolbar/icon_gear.tga")
);
// signalement des sous-widgets
getManager().addWidget(action_toolbar.getWindow());
// ajout de la barre d'objets
getManager().addWidget(object_toolbar.getWindow());
}
Editor::~Editor() {
getManager().removeWidget(action_toolbar.getWindow());
getManager().removeWidget(object_toolbar.getWindow());
}
@ -93,8 +68,7 @@ void Editor::enable() {
// joue la musique de l'éditeur
ResourceManager::get().playMusic("editor.ogg");
// on affiche les barres d'outils
action_toolbar.getWindow()->Show(true);
// on affiche la barre d'objets
object_toolbar.getWindow()->Show(true);
}
@ -293,6 +267,8 @@ void Editor::frame() {
sf::RenderWindow& window = getManager().getWindow();
sf::Vector2i window_size = (sf::Vector2i) window.getSize();
Level::frame();
// dessin de la frame
draw();
@ -315,26 +291,10 @@ void Editor::frame() {
setCamera(camera);
}
// mise à jour de l'icône du mute
sf::Image image;
if (ResourceManager::get().isMuted()) {
image = *ResourceManager::get().getImage("toolbar/icon_no_music.tga");
} else {
image = *ResourceManager::get().getImage("toolbar/icon_music.tga");
}
mute_button->SetImage(sfg::Image::Create(image));
// màj du titre de la fenêtre
getManager().setTitle(sf::String(L"Édition de ") + getName());
// positionnement des barres d'outils au bon endroit
action_toolbar.getWindow()->SetAllocation(sf::FloatRect(
0, 0, window_size.x,
action_toolbar.getHeight()
));
// positionnement de la barre d'objets
object_toolbar.getWindow()->SetAllocation(sf::FloatRect(
window_size.x - object_toolbar.getWidth(),
action_toolbar.getHeight(),

View File

@ -61,6 +61,7 @@ void Game::processEvent(const sf::Event& event) {
}
void Game::frame() {
Level::frame();
sf::Time current_time = getManager().getCurrentTime();
if (current_time >= next_frame_time) {

View File

@ -48,8 +48,8 @@ namespace {
};
}
Level::Level(Manager& manager) : State(manager) {
gravity_direction = GravityDirection::SOUTH;
Level::Level(Manager& manager) : State(manager),
gravity_direction(Utility::Direction::SOUTH) {
// métadonnées par défaut
setName(sf::String("Nouveau niveau"));
@ -67,9 +67,36 @@ Level::Level(Manager& manager) : State(manager) {
// ressources par défaut
setMusic("");
setBackground("");
// ajout des boutons d'action de la barre d'action
action_toolbar.addButton(
*ResourceManager::get().getImage("toolbar/icon_back.tga"),
std::bind(&Manager::popState, &getManager())
);
mute_button = action_toolbar.addButton(
*ResourceManager::get().getImage("toolbar/icon_music.tga"),
[]() {
// on inverse le drapeau de muet
ResourceManager::get().setMuted(
!ResourceManager::get().isMuted()
);
}
);
action_toolbar.addSpacer(5, true, false);
action_toolbar.addButton(
*ResourceManager::get().getImage("toolbar/icon_no_music.tga")
);
action_toolbar.addSpacer(5, true, false);
getManager().addWidget(action_toolbar.getWindow());
}
Level::~Level() {}
Level::~Level() {
getManager().removeWidget(action_toolbar.getWindow());
}
void Level::enable() {
// positionnement par défaut de la caméra
@ -78,6 +105,9 @@ void Level::enable() {
camera.setSize(window_size.x, window_size.y);
camera.setCenter(0, 0);
camera_angle = 180.f;
// on affiche la barre d'actions
action_toolbar.getWindow()->Show(true);
}
void Level::load() {
@ -251,6 +281,27 @@ void Level::processEvent(const sf::Event& event) {
}
}
void Level::frame() {
sf::Vector2i window_size = (sf::Vector2i) getManager().getWindow().getSize();
// mise à jour de l'icône du mute en fonction de l'état
sf::Image image;
if (ResourceManager::get().isMuted()) {
image = *ResourceManager::get().getImage("toolbar/icon_no_music.tga");
} else {
image = *ResourceManager::get().getImage("toolbar/icon_music.tga");
}
mute_button->SetImage(sfg::Image::Create(image));
// positionnement de la barre d'actions
action_toolbar.getWindow()->SetAllocation(sf::FloatRect(
0, 0, window_size.x,
action_toolbar.getHeight()
));
}
void Level::draw() {
sf::RenderWindow& window = getManager().getWindow();
sf::Vector2u window_size = window.getSize();
@ -375,16 +426,16 @@ void Level::setBackground(std::string set_background) {
sf::Vector2f Level::getGravity() const {
switch (gravity_direction) {
case GravityDirection::NORTH:
case Utility::Direction::NORTH:
return sf::Vector2f(0, -GRAVITY);
case GravityDirection::EAST:
case Utility::Direction::EAST:
return sf::Vector2f(GRAVITY, 0);
case GravityDirection::SOUTH:
case Utility::Direction::SOUTH:
return sf::Vector2f(0, GRAVITY);
case GravityDirection::WEST:
case Utility::Direction::WEST:
return sf::Vector2f(-GRAVITY, 0);
}
@ -393,16 +444,16 @@ sf::Vector2f Level::getGravity() const {
sf::Vector2f Level::getLeftDirection() const {
switch (gravity_direction) {
case GravityDirection::NORTH:
case Utility::Direction::NORTH:
return sf::Vector2f(MOVE, 0);
case GravityDirection::EAST:
case Utility::Direction::EAST:
return sf::Vector2f(0, MOVE);
case GravityDirection::SOUTH:
case Utility::Direction::SOUTH:
return sf::Vector2f(-MOVE, 0);
case GravityDirection::WEST:
case Utility::Direction::WEST:
return sf::Vector2f(0, -MOVE);
}
@ -413,11 +464,11 @@ sf::Vector2f Level::getRightDirection() const {
return -1.f * getLeftDirection();
}
GravityDirection Level::getGravityDirection() {
Utility::Direction Level::getGravityDirection() {
return gravity_direction;
}
void Level::setGravityDirection(GravityDirection set_gravity_direction) {
void Level::setGravityDirection(Utility::Direction set_gravity_direction) {
gravity_direction = set_gravity_direction;
}