Bonjour, je souhaiterais faire un jeu indépendent avec la SFML 2.4.2 et comme IDE Codeblocks mais les collisions sont foireuses et envoient le player dans l'espace.
Pourriez vous m'indiquer le probleme que je puisse fix.
le main.cpp
#include <SFML/Graphics.hpp>
#include <iostream>
#include "Player.hpp"
#include "Block.hpp"
#include "Map.hpp"
using namespace sf;
using namespace std;
int main()
{
RenderWindow app(VideoMode(640,320),"SpikeyGuy");
Image icon;
icon.loadFromFile("Data/icon.jpg");
app.setIcon(icon.getSize().x,icon.getSize().y,icon.getPixelsPtr());
Texture playerT;
playerT.loadFromFile("Data/you.png");
Sprite sprite(playerT);
Player player(sprite);
app.setFramerateLimit(60);
vector<Block> Blocks = {Block(0,298),Block(22,298),Block(44,298),Block(66,298),Block(88,298),Block(110,298),Block(132,298),Block(154,298),Block(88,276)};
Map map(Blocks);
while(app.isOpen())
{
Event e;
while(app.pollEvent(e))
{
if(e.type == Event::Closed)
app.close();
if(e.type == Event::KeyReleased)
{
if(e.key.code == Keyboard::Escape)
app.close();
if(e.key.code == Keyboard::D)
cout << player.GetPos().x << " " << player.GetPos().y << endl;
}
}
player.update(sf::Keyboard::isKeyPressed(Keyboard::Space),sf::Keyboard::isKeyPressed(Keyboard::Left),sf::Keyboard::isKeyPressed(Keyboard::Right),map);
app.clear(Color::White);
map.draw(app);
app.draw(player.image);
app.display();
}
return 0;
}
le Entity.hpp
#ifndef ENTITY_HPP
#define ENTITY_HPP
#include <SFML/Graphics.hpp>
class Entity
{
public:
Entity(sf::Sprite sprite);
sf::Vector2f GetPos() { return m_Pos; }
void SetPos(sf::Vector2f val) { m_Pos = val; }
sf::Vector2f GetVel() { return m_Vel; }
void setVel(sf::Vector2f val) {m_Vel = val;}
sf::Sprite image;
protected:
sf::Vector2f m_Pos;
sf::Vector2f m_Vel;
};
#endif // ENTITY_HPP
, le player.hpp:
#ifndef PLAYER_HPP
#define PLAYER_HPP
#include "Entity.hpp"
#include "Map.hpp"
class Player : public Entity
{
public:
Player(sf::Sprite sprite);
void update(bool ,bool ,bool,Map);
void collide(Map map,float Xvel,float yVel);
private:
float leftSide;
float rightSide;
float topSide;
float bottomSide;
bool isCollide = false;
};
#endif // PLAYER_HPP
,le player.cpp
#include "Player.hpp"
Player::Player(sf::Sprite sprite) : Entity(sprite)
{
//ctor
}
void Player::collide(Map map,float xVel,float yVel)
{
for(auto& i : map.m_Blocks)
{
if(image.getPosition().x+image.getLocalBounds().width > i.leftSide && image.getPosition().x < i.rightSide && image.getPosition().y+image.getLocalBounds().height > i.topSide && image.getPosition().y < i.bottomSide)
{
isCollide = true;
}else{isCollide = false;}
if(isCollide)
{
if(xVel > 0)
{
image.setPosition(i.leftSide-image.getLocalBounds().width,image.getPosition().y);
}
if(xVel < 0)
{
image.setPosition(i.rightSide,image.getPosition().y);
}
if(yVel > 0)
{
image.setPosition(image.getPosition().x,i.topSide-image.getLocalBounds().height);
}
if(yVel < 0)
{
image.setPosition(image.getPosition().x,i.bottomSide);
}
}
}
}
void Player::update(bool Jump,bool LEFT,bool RIGHT,Map map)
{
if(Jump)
{
if(m_Vel.y > -8)
m_Vel.y -= 2;
}
if(LEFT)
{
m_Vel.x = -2;
}
if(RIGHT)
{
m_Vel.x = 2;
}
if(!(RIGHT || LEFT))
{
m_Vel.x = 0;
}
m_Vel.y += 0.5;
image.move(m_Vel.x,0);
collide(map,m_Vel.x,0);
image.move(0,m_Vel.y);
collide(map,0,m_Vel.y);
m_Pos = image.getPosition();
}
, le map.hpp
#ifndef MAP_HPP
#define MAP_HPP
#include <vector>
#include "Block.hpp"
class Map
{
public:
Map(std::vector<Block> blocks);
std::vector<Block> m_Blocks;
void draw(sf::RenderWindow& window);
};
#endif // MAP_HPP
,le map.cpp
#include "Map.hpp"
Map::Map(std::vector<Block> blocks) : m_Blocks(blocks)
{
//ctor
}
void Map::draw(sf::RenderWindow& window)
{
for(auto &i : m_Blocks)
{
window.draw(i.block);
}
}
,le block.hpp
#ifndef BLOCK_HPP
#define BLOCK_HPP
#include <SFML/Graphics.hpp>
class Block
{
public:
Block(int x,int y);
sf::RectangleShape block;
float leftSide;
float rightSide;
float topSide;
float bottomSide;
};
#endif // BLOCK_HPP
et le block.cpp
#include "Block.hpp"
Block::Block(int x,int y)
{
block.setPosition(x,y);
block.setFillColor(sf::Color(0,0,0));
block.setSize(sf::Vector2f(22,22));
leftSide = x;
rightSide = x+22;
topSide = y;
bottomSide = y+22;
}
Afficher la suite