Ajout du bloc de gravité
This commit is contained in:
parent
606451737b
commit
2515ed0d59
|
@ -36,7 +36,7 @@ public:
|
|||
/**
|
||||
* Appelé lorsque le bloc est activé par un objet
|
||||
*/
|
||||
virtual void activated(Object& object);
|
||||
virtual void activated(Level& level, Object& object);
|
||||
|
||||
/**
|
||||
* Récupère l'identifiant de type des blocs
|
||||
|
|
|
@ -0,0 +1,51 @@
|
|||
#ifndef __PTF_GRAVITY_BLOCK_HPP__
|
||||
#define __PTF_GRAVITY_BLOCK_HPP__
|
||||
|
||||
#include "block.hpp"
|
||||
#include "level.hpp"
|
||||
|
||||
class GravityBlock : public Block {
|
||||
private:
|
||||
GravityDirection gravity_direction;
|
||||
|
||||
public:
|
||||
/**
|
||||
* Identifiant unique du type "bloc de gravité"
|
||||
*/
|
||||
static const unsigned int TYPE_ID;
|
||||
|
||||
GravityBlock();
|
||||
virtual ~GravityBlock();
|
||||
|
||||
/**
|
||||
* Clone ce bloc de gravité en un bloc de gravité avec les mêmes propriétés
|
||||
*/
|
||||
virtual ObjectPtr clone() const;
|
||||
|
||||
/**
|
||||
* Appelé lorsque le bloc de gravité est activé par un objet
|
||||
*/
|
||||
virtual void activated(Level& level, Object& object);
|
||||
|
||||
/**
|
||||
* Récupère l'identifiant de type des blocs de gravité
|
||||
*/
|
||||
virtual unsigned int getTypeId() const;
|
||||
|
||||
/**
|
||||
* Chargement du bloc de gravité depuis le fichier donné
|
||||
*/
|
||||
static ObjectPtr load(std::ifstream& file);
|
||||
|
||||
/**
|
||||
* Récupère la direction de gravité du bloc changeur de gravité
|
||||
*/
|
||||
GravityDirection getGravityDirection() const;
|
||||
|
||||
/**
|
||||
* Modifie la direction de gravité du bloc
|
||||
*/
|
||||
void setGravityDirection(GravityDirection set_gravity_direction);
|
||||
};
|
||||
|
||||
#endif
|
|
@ -115,7 +115,7 @@ public:
|
|||
/**
|
||||
* Modifie la direction de la gravité
|
||||
*/
|
||||
void setGravity(GravityDirection direction);
|
||||
void setGravityDirection(GravityDirection direction);
|
||||
|
||||
/**
|
||||
* Récupère la liste des objets
|
||||
|
|
|
@ -66,7 +66,7 @@ public:
|
|||
/**
|
||||
* Appelé lorsque l'objet est activé par un autre
|
||||
*/
|
||||
virtual void activated(Object& object) = 0;
|
||||
virtual void activated(Level& level, Object& object) = 0;
|
||||
|
||||
/**
|
||||
* Récupère l'identifiant de type de cet objet
|
||||
|
@ -102,7 +102,7 @@ public:
|
|||
* et l'objet passé en paramètre selon la normale
|
||||
* donnée
|
||||
*/
|
||||
virtual void solveCollision(Object& obj, const sf::Vector2f& normal);
|
||||
virtual void solveCollision(Level& level, Object& obj, const sf::Vector2f& normal);
|
||||
|
||||
/**
|
||||
* Application de la correction positionnelle sur
|
||||
|
|
|
@ -42,7 +42,7 @@ public:
|
|||
/**
|
||||
* Appelé lorsque le joueur est activé par un objet
|
||||
*/
|
||||
virtual void activated(Object& object);
|
||||
virtual void activated(Level& level, Object& object);
|
||||
|
||||
/**
|
||||
* Récupère l'identifiant de type des joueurs
|
||||
|
|
Binary file not shown.
|
@ -37,7 +37,7 @@ void Block::draw(Level& level) {
|
|||
ResourceManager& resources = level.getResourceManager();
|
||||
sf::RenderWindow& window = level.getWindow();
|
||||
|
||||
sprite.setTexture(resources.getTexture("block.png"));
|
||||
sprite.setTexture(resources.getTexture("block.tga"));
|
||||
select_sprite.setTexture(resources.getTexture("block_select.png"));
|
||||
|
||||
// coloration du bloc en fonction de sa charge
|
||||
|
@ -59,7 +59,7 @@ void Block::draw(Level& level) {
|
|||
}
|
||||
}
|
||||
|
||||
void Block::activated(Object& object) {
|
||||
void Block::activated(Level& level, Object& object) {
|
||||
// ne rien faire si le bloc est activé.
|
||||
// Ceci est un bloc de base qui n'a pas a réagir
|
||||
// aux activations
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#include "collision.hpp"
|
||||
#include "player.hpp"
|
||||
#include "block.hpp"
|
||||
#include "gravity_block.hpp"
|
||||
#include "object.hpp"
|
||||
#include <functional>
|
||||
#include <utility>
|
||||
|
@ -189,10 +190,15 @@ std::map<
|
|||
std::pair<unsigned int, unsigned int>,
|
||||
std::function<bool(CollisionData&)>
|
||||
> collision_map = {
|
||||
{std::make_pair(Player::TYPE_ID, Block::TYPE_ID), playerToBlock},
|
||||
{std::make_pair(Block::TYPE_ID, Player::TYPE_ID), blockToPlayer},
|
||||
{std::make_pair(Player::TYPE_ID, Player::TYPE_ID), playerToPlayer},
|
||||
{std::make_pair(Block::TYPE_ID, Block::TYPE_ID), blockToBlock}
|
||||
{std::make_pair(Player::TYPE_ID, Block::TYPE_ID), playerToBlock},
|
||||
{std::make_pair(Player::TYPE_ID, GravityBlock::TYPE_ID), playerToBlock},
|
||||
{std::make_pair(Block::TYPE_ID, Block::TYPE_ID), blockToBlock},
|
||||
{std::make_pair(Block::TYPE_ID, GravityBlock::TYPE_ID), blockToBlock},
|
||||
{std::make_pair(Block::TYPE_ID, Player::TYPE_ID), blockToPlayer},
|
||||
{std::make_pair(GravityBlock::TYPE_ID, Block::TYPE_ID), blockToBlock},
|
||||
{std::make_pair(GravityBlock::TYPE_ID, GravityBlock::TYPE_ID), blockToBlock},
|
||||
{std::make_pair(GravityBlock::TYPE_ID, Player::TYPE_ID), blockToPlayer}
|
||||
};
|
||||
|
||||
CollisionData::CollisionData(Object& objA, Object& objB) :
|
||||
|
|
|
@ -18,9 +18,16 @@ Editor::Editor(Manager& manager) : Level(manager), drag_mode(DragMode::NONE),
|
|||
setTotalTime(30);
|
||||
|
||||
ResourceManager& resources = getResourceManager();
|
||||
std::shared_ptr<SelectorCategory> basic = widget_selector.addCategory("BASE");
|
||||
basic->addItem("Block", resources.getTexture("block.png"));
|
||||
basic->addItem("Player", resources.getTexture("player.tga"));
|
||||
|
||||
std::shared_ptr<SelectorCategory> basic_cat = widget_selector.addCategory("BASE");
|
||||
basic_cat->addItem("Block", resources.getTexture("block.tga"));
|
||||
basic_cat->addItem("Player", resources.getTexture("player.tga"));
|
||||
|
||||
std::shared_ptr<SelectorCategory> gravity_cat = widget_selector.addCategory(sf::String(L"GRAVITÉ"));
|
||||
gravity_cat->addItem("GravityN", resources.getTexture("gravity_block_north.tga"));
|
||||
gravity_cat->addItem("GravityE", resources.getTexture("gravity_block_east.tga"));
|
||||
gravity_cat->addItem("GravityS", resources.getTexture("gravity_block_south.tga"));
|
||||
gravity_cat->addItem("GravityW", resources.getTexture("gravity_block_west.tga"));
|
||||
}
|
||||
|
||||
Editor::~Editor() {}
|
||||
|
|
|
@ -111,7 +111,7 @@ void Game::update() {
|
|||
// résolution des collisions détectées
|
||||
for (unsigned int i = 0; i < colliding.size(); i++) {
|
||||
CollisionData& collided = colliding[i];
|
||||
collided.objA.solveCollision(collided.objB, collided.normal);
|
||||
collided.objA.solveCollision(*this, collided.objB, collided.normal);
|
||||
}
|
||||
|
||||
// intégration de la vitesse dans la position
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
#include "gravity_block.hpp"
|
||||
#include "level.hpp"
|
||||
|
||||
const unsigned int GravityBlock::TYPE_ID = 3;
|
||||
|
||||
GravityBlock::GravityBlock() : Block() {}
|
||||
GravityBlock::~GravityBlock() {}
|
||||
|
||||
ObjectPtr GravityBlock::clone() const {
|
||||
return ObjectPtr(new GravityBlock(*this));
|
||||
}
|
||||
|
||||
void GravityBlock::activated(Level& level, Object& object) {
|
||||
Block::activated(level, object);
|
||||
|
||||
// lorsque le bloc est activé, il transmet son
|
||||
// sens de gravité au niveau
|
||||
level.setGravityDirection(gravity_direction);
|
||||
}
|
||||
|
||||
unsigned int GravityBlock::getTypeId() const {
|
||||
return TYPE_ID;
|
||||
}
|
||||
|
||||
ObjectPtr GravityBlock::load(std::ifstream& file) {
|
||||
ObjectPtr object = ObjectPtr(new GravityBlock);
|
||||
std::shared_ptr<GravityBlock> block = std::dynamic_pointer_cast<GravityBlock>(object);
|
||||
|
||||
// lecture de la direction de la gravité
|
||||
char gravity_direction;
|
||||
file.read(&gravity_direction, 1);
|
||||
block->setGravityDirection((GravityDirection) gravity_direction);
|
||||
|
||||
// lecture des propriétés communes des objets
|
||||
Object::load(file, object);
|
||||
file.seekg(1, file.cur);
|
||||
|
||||
return object;
|
||||
}
|
||||
|
||||
GravityDirection GravityBlock::getGravityDirection() const {
|
||||
return gravity_direction;
|
||||
}
|
||||
|
||||
void GravityBlock::setGravityDirection(GravityDirection set_gravity_direction) {
|
||||
gravity_direction = set_gravity_direction;
|
||||
}
|
|
@ -1,11 +1,12 @@
|
|||
#include "constants.hpp"
|
||||
#include "block.hpp"
|
||||
#include "level.hpp"
|
||||
#include "player.hpp"
|
||||
#include "block.hpp"
|
||||
#include "gravity_block.hpp"
|
||||
#include <arpa/inet.h>
|
||||
#include <cstring>
|
||||
#include <queue>
|
||||
#include <utility>
|
||||
#include "level.hpp"
|
||||
|
||||
const float GRAVITY = 235;
|
||||
|
||||
|
@ -16,7 +17,8 @@ const float GRAVITY = 235;
|
|||
*/
|
||||
std::map<unsigned int, std::function<ObjectPtr(std::ifstream&)>> object_type_map = {
|
||||
{Player::TYPE_ID, Player::load},
|
||||
{Block::TYPE_ID, Block::load}
|
||||
{Block::TYPE_ID, Block::load},
|
||||
{GravityBlock::TYPE_ID, GravityBlock::load}
|
||||
};
|
||||
|
||||
Level::Level(Manager& manager) : View(manager), gravity(0, GRAVITY) {}
|
||||
|
@ -196,21 +198,21 @@ sf::Vector2f Level::getGravity() const {
|
|||
return gravity;
|
||||
}
|
||||
|
||||
void Level::setGravity(GravityDirection direction) {
|
||||
void Level::setGravityDirection(GravityDirection direction) {
|
||||
switch (direction) {
|
||||
case GravityDirection::NORTH:
|
||||
gravity = sf::Vector2f(0, -GRAVITY);
|
||||
break;
|
||||
|
||||
case GravityDirection::WEST:
|
||||
case GravityDirection::EAST:
|
||||
gravity = sf::Vector2f(GRAVITY, 0);
|
||||
break;
|
||||
|
||||
case GravityDirection::SOUTH:
|
||||
gravity = sf::Vector2f(0, GRAVITY);
|
||||
gravity = sf::Vector2f(0, -GRAVITY);
|
||||
break;
|
||||
|
||||
case GravityDirection::EAST:
|
||||
case GravityDirection::WEST:
|
||||
gravity = sf::Vector2f(-GRAVITY, 0);
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -154,7 +154,7 @@ bool Object::detectCollision(const Object& obj, CollisionData& data) const {
|
|||
return getCollisionData(data);
|
||||
}
|
||||
|
||||
void Object::solveCollision(Object& obj, const sf::Vector2f& normal) {
|
||||
void Object::solveCollision(Level& level, Object& obj, const sf::Vector2f& normal) {
|
||||
// si les deux objets sont de masse infinie, réinitialisation
|
||||
// des vitesses en tant que collision
|
||||
if (getMassInvert() == 0 && obj.getMassInvert() == 0) {
|
||||
|
@ -174,8 +174,8 @@ void Object::solveCollision(Object& obj, const sf::Vector2f& normal) {
|
|||
|
||||
// en ce point, on est bertins qu'une collision a eu lieu.
|
||||
// activation réciproque des deux objets
|
||||
activated(obj);
|
||||
obj.activated(*this);
|
||||
activated(level, obj);
|
||||
obj.activated(level, *this);
|
||||
|
||||
// on utilise le plus petit coefficient de friction entre les
|
||||
// deux objets comme le coefficient de la collision
|
||||
|
|
|
@ -86,7 +86,7 @@ void Player::draw(Level& level) {
|
|||
level.getWindow().draw(sprite);
|
||||
}
|
||||
|
||||
void Player::activated(Object& object) {
|
||||
void Player::activated(Level& level, Object& object) {
|
||||
// ne rien faire si le joueur est activé.
|
||||
// en règle générale, c'est l'objet activé par le joueur
|
||||
// qui s'occupe de la réponse
|
||||
|
|
|
@ -63,6 +63,8 @@ void WidgetSelector::draw(sf::Vector2f position, sf::Vector2f size) {
|
|||
background.setPosition(position);
|
||||
window.draw(background);
|
||||
|
||||
float total_y = PADDING;
|
||||
|
||||
for (unsigned int i = 0; i < categories.size(); i++) {
|
||||
std::shared_ptr<SelectorCategory> category = categories[i];
|
||||
|
||||
|
@ -75,22 +77,25 @@ void WidgetSelector::draw(sf::Vector2f position, sf::Vector2f size) {
|
|||
category_label.setColor(sf::Color::Black);
|
||||
category_label.setPosition(position + sf::Vector2f(
|
||||
floor(size.x / 2 - category_label.getGlobalBounds().width / 2),
|
||||
PADDING
|
||||
total_y
|
||||
));
|
||||
|
||||
window.draw(category_label);
|
||||
|
||||
for (unsigned int j = 0; j < category->items.size(); j++) {
|
||||
total_y += PADDING + 24;
|
||||
|
||||
std::shared_ptr<SelectorItem> item = category->items[j];
|
||||
sf::Vector2f sprite_position = position + sf::Vector2f(
|
||||
size.x / 2 - item->sprite.getGlobalBounds().width / 2,
|
||||
PADDING + 24 + 38 * j
|
||||
total_y
|
||||
);
|
||||
|
||||
// affichage du sprite de l'item
|
||||
item->sprite.setPosition(sprite_position);
|
||||
window.draw(item->sprite);
|
||||
}
|
||||
}
|
||||
|
||||
total_y += 64;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue