Hello, I have problem with collisions, my character ignores conficlting sprites.
I have also problem because he can jumps if he is in air
Please correct my code, I have to pass my C++ project on university.
Player.cpp
#include <SFML/Graphics.hpp>
#include "Player.h"
#include "Level.h"
void Player::Draw(sf::RenderWindow & gameWindow)
{
const float gravity = 0.9;
int groundHeight = 368;
sf::Vector2f velocity(sf::Vector2f(0, 0));
float moveSpeed = 2.0f;
float jumpSpeed = 10.0f;
Level level;
enum Direction { Down,Left, Right, Up }; //{0,1}
sf::Vector2i source(1, Down);
gameWindow.setKeyRepeatEnabled(false);
sf::Texture playerImage;
playerImage.loadFromFile("C:/Users/Marcin/Desktop/Teddies/Release/Images/player.png");
sf::Sprite player(playerImage);
player.setPosition(0, 50);
while (gameWindow.isOpen()) {
sf::Event event;
while (gameWindow.pollEvent(event)) {
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
source.y = Right;
if (level.Collision(level.cabinets) || level.Collision(level.microwaves) || level.Collision(level.fridges) ||
level.Collision(level.boxs) || level.Collision(level.chairs) || level.Collision(level.ovens) ||
level.Collision(level.tables))
velocity.x = 0;
else
velocity.x = +moveSpeed;
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
source.y = Left;
if (level.Collision(level.cabinets) || level.Collision(level.microwaves) || level.Collision(level.fridges) ||
level.Collision(level.boxs) || level.Collision(level.chairs) || level.Collision(level.ovens) ||
level.Collision(level.tables))
velocity.x = 0;
else
velocity.x = -moveSpeed;
}
else
{
source.y = Down;
velocity.x = 0;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
velocity.y = -jumpSpeed;
if (event.key.code == sf::Keyboard::Escape)
return;
break;
}
player.move(velocity.x, velocity.y);
if (player.getPosition().y < groundHeight || velocity.y < 0)
{
velocity.y += gravity;
}
else
{
player.setPosition(player.getPosition().x, groundHeight - player.getScale().y);
velocity.y = 0;
}
source.x++;
if (source.x * 32 >= playerImage.getSize().x)
source.x = 0;
player.setTextureRect(sf::IntRect(source.x * 32, source.y * 32, 32, 32));
level.Draw(gameWindow);
gameWindow.draw(player);
gameWindow.display();
gameWindow.clear();
}
}
Level.cpp
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include "Level.h"
#include "Player.h"
void Level::Load1(sf::RenderWindow& gameWindow) {
Player player;
Draw(gameWindow);
player.Draw(gameWindow);
}
void Level::Draw(sf::RenderWindow& gameWindow) {
sf::Texture cabinet,bg,microwave,fridge,table,oven,box, chair;
cabinet.loadFromFile("C:/Users/Marcin/Desktop/Teddies/Release/Images/cabinet.png");
bg.loadFromFile("C:/Users/Marcin/Desktop/Teddies/Release/Images/bg.png");
microwave.loadFromFile("C:/Users/Marcin/Desktop/Teddies/Release/Images/microwave.png");
fridge.loadFromFile("C:/Users/Marcin/Desktop/Teddies/Release/Images/fridge.png");
sf::Sprite cabinets(cabinet);
sf::Sprite bgs(bg);
sf::Sprite microwaves(microwave);
sf::Sprite fridges(fridge);
bgs.setPosition(0, 0);
cabinets.setPosition(0, 405);
fridges.setPosition(137, 295);
microwaves.setPosition(30, 367);
gameWindow.draw(bgs);
gameWindow.draw(cabinets);
gameWindow.draw(microwaves);
gameWindow.draw(fridges);
}
//DODANE DLA SPROBOWANIA KOLIZJI
bool Level::Collision(sf::Sprite x)
{
Player p;
sf::IntRect playerRect(
p.player.getPosition().x,
p.player.getPosition().y,
p.player.getGlobalBounds().width,
p.player.getGlobalBounds().height
);
sf::IntRect spriteRect(
x.getPosition().x,
x.getPosition().y,
x.getGlobalBounds().width,
x.getGlobalBounds().height
);
if (playerRect.intersects(spriteRect))
return true;
else
return false;
}
////
Level.h
#pragma once
class Level
{
public:
void Load1(sf::RenderWindow& gameWindow);
void Draw(sf::RenderWindow& gameWindow);
//DODANE DLA SPROBOWANIA KOLIZJI
sf::Sprite cabinets;
sf::Sprite microwaves;
sf::Sprite fridges;
bool Collision(sf::Sprite x);
////
};
Player.h
#pragma once
class Player
{
public:
void Draw(sf::RenderWindow& gameWindow);
//DODANE DLA SPROBOWANIA KOLIZJI
sf::Sprite player;
////
};