Skip to content

Commit

Permalink
vector platforme fara pointeri
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdan-velicu committed Nov 1, 2023
1 parent 322b227 commit 46ebcb1
Show file tree
Hide file tree
Showing 14 changed files with 340 additions and 145 deletions.
21 changes: 21 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(lldb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/doodle_jump",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb"
}

]
}
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ endif()
###############################################################################

# NOTE: update executable name in .github/workflows/cmake.yml:25 when changing name here
add_executable(${PROJECT_NAME} main.cpp doodle_jump/game/Game.cpp doodle_jump/platform/Platform.cpp doodle_jump/player/Player.cpp doodle_jump/screen/GameScreen.cpp doodle_jump/screen/MainMenu.cpp doodle_jump/screen/PlayScreen.cpp doodle_jump/screen/GameOver.cpp)
add_executable(${PROJECT_NAME} main.cpp doodle_jump/game/Game.cpp doodle_jump/platform/Platform.cpp doodle_jump/player/Player.cpp doodle_jump/screen/GameScreen.cpp doodle_jump/screen/MainMenu.cpp doodle_jump/screen/PlayScreen.cpp doodle_jump/screen/GameOver.cpp doodle_jump/game_object/GameObject.cpp doodle_jump/powerups/Powerups.cpp)

###############################################################################

Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@

#### Cerințe
- [x] separarea codului din clase în `.h` (sau `.hpp`) și `.cpp`
- [ ] moșteniri:
- [x] moșteniri:
- minim o clasă de bază și **3 clase derivate**
- ierarhia trebuie să fie cu bază proprie, nu derivată dintr-o clasă predefinită
- [ ] clasă cu atribut de tip pointer la o clasă de bază cu derivate
- [x] clasă cu atribut de tip pointer la o clasă de bază cu derivate
- [ ] funcții virtuale (pure) apelate prin pointeri de bază din clasa care conține atributul de tip pointer de bază
- minim o funcție virtuală va fi **specifică temei** (e.g. nu simple citiri/afișări)
- constructori virtuali (clone): sunt necesari, dar nu se consideră funcții specifice temei
Expand All @@ -42,7 +42,7 @@
- [ ] excepții
- [ ] ierarhie proprie cu baza `std::exception` sau derivată din `std::exception`; minim 2 clase pentru erori specifice
- [ ] utilizare cu sens: de exemplu, `throw` în constructor, `try`/`catch` în `main`
- [x] funcții și atribute `static`
- [ ] funcții și atribute `static`
- [ ] STL
- [x] cât mai multe `const`
- [ ] la sfârșit: commit separat cu adăugarea unei noi clase derivate fără a modifica restul codului, pe lângă cele 3 derivate deja adăugate
Expand Down
194 changes: 92 additions & 102 deletions doodle_jump/game/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,78 +13,39 @@ Game::Game() {
currentScreen = ScreenType::MAIN_MENU;
score = 0;
maxScore = 0;
player = new Player();
platforms = std::vector<Platform*>();

platformTextures = std::vector<sf::Texture>();
initPlatformTextures();

platforms = std::vector<Platform>();
powerups = std::vector<Powerups>();
powerup = nullptr;

lastPlatform = new Platform();

for (int i = 0; i < 15; i++) {
auto platform = new Platform();
platform->useGenerator(lastPlatform->getSprite().getPosition());
lastPlatform = platform;
for (int i = 0; i < 20; i++) {
auto platform = Platform();
platform.useGenerator(lastPlatform->getSprite().getPosition());
platform.assignTexture(pickTexture(platform.getType()));
platforms.push_back(platform);
lastPlatform = &platforms[i];
}

font = sf::Font();
font.loadFromFile("fonts/Valoon.ttf");

scoreText = new sf::Text();
scoreText->setFont(font);
scoreText->setString(std::to_string(score));
scoreText->setCharacterSize(36);
scoreText->setFillColor(sf::Color::Black);
scoreText->setPosition(20, 20);
scoreText.setFont(font);
scoreText.setString(std::to_string(score));
scoreText.setCharacterSize(36);
scoreText.setFillColor(sf::Color::Black);
scoreText.setPosition(20, 20);

window.create(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT), windowTitle, sf::Style::Default);
window.setFramerateLimit(120);
window.setFramerateLimit(100);
}

Game::~Game() {
std::cout << "Game destructor called\n";
delete player;
for (auto& platform : platforms) {
delete platform;
}
delete lastPlatform;
delete scoreText;
}

Game::Game(const Game& game) {
std::cout << "Game copy constructor called\n";
currentScreen = game.currentScreen;

if (window.isOpen())
window.close();
window.create(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT), windowTitle, sf::Style::Default);
window.setFramerateLimit(120);

font = game.font;
score = game.score;
maxScore = game.maxScore;
scoreText = game.scoreText;
player = game.player;
platforms = game.platforms;
lastPlatform = game.lastPlatform;
}

Game& Game::operator=(const Game& game) {
std::cout << "Game copy assignment operator called\n";
if (this != &game) {
currentScreen = game.currentScreen;

if (window.isOpen())
window.close();
window.create(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT), windowTitle, sf::Style::Default);
window.setFramerateLimit(120);

font = game.font;
score = game.score;
maxScore = game.maxScore;
scoreText = game.scoreText;
player = game.player;
platforms = game.platforms;
lastPlatform = game.lastPlatform;
}
return *this;
}

std::ostream& operator<<(std::ostream& out, const Game& game) {
Expand All @@ -95,20 +56,18 @@ std::ostream& operator<<(std::ostream& out, const Game& game) {
}

void Game::reset() {
delete player;
player = new Player();
for (auto& platform : platforms) {
delete platform;
}
platforms.clear();

lastPlatform = new Platform();
powerups.clear();
powerup = nullptr;

for (int i = 0; i < 15; i++) {
auto platform = new Platform();
platform->useGenerator(lastPlatform->getSprite().getPosition());
lastPlatform = platform;
for (int i = 0; i < 20; i++) {
auto platform = Platform();
platform.useGenerator(lastPlatform->getSprite().getPosition());
platform.assignTexture(pickTexture(platform.getType()));
platforms.push_back(platform);
lastPlatform = &platforms[i];
}
score = 0;
maxScore = 0;
Expand Down Expand Up @@ -174,7 +133,7 @@ void Game::run() {

void Game::checkCollision() {
// Get only lower part of player sprite
sf::FloatRect lowerPlayerBounds = player->getSprite().getGlobalBounds();
sf::FloatRect lowerPlayerBounds = player.getSprite().getGlobalBounds();
lowerPlayerBounds.top += lowerPlayerBounds.height - 5;
lowerPlayerBounds.height = 5;

Expand All @@ -184,39 +143,40 @@ void Game::checkCollision() {
changeScreen(ScreenType::GAME_OVER);
}
else
player->jump();
player.jump();
}

// sf::FloatRect playerBounds = player->getSprite().getGlobalBounds();
for (auto& platform : platforms) {
// Get only upper part of platform sprite
sf::FloatRect upperPlatformBounds = platform->getSprite().getGlobalBounds();
sf::FloatRect upperPlatformBounds = platform.getSprite().getGlobalBounds();
upperPlatformBounds.height = 5;

// sf::FloatRect platformBounds = platform->getSprite().getGlobalBounds();
if (lowerPlayerBounds.intersects(upperPlatformBounds)) {
if (player->getSprite().getPosition().y < platform->getSprite().getPosition().y) {
if (player.getSprite().getPosition().y < platform.getSprite().getPosition().y) {
// Jump only if player is falling
if (player->getYVelocity() > 0.2f) {
player->jump();

if (platform->getType() == PlatformType::BREAKABLE) {
delete platform;
auto plat = new Platform();
plat->useGenerator(lastPlatform->getSprite().getPosition());
lastPlatform = plat;
platform = plat;
if (player.getYVelocity() > 0.2f) {
player.jump();

if (platform.getType() == PlatformType::BREAKABLE) {
// delete platform;
auto plat = Platform();
plat.useGenerator(lastPlatform->getSprite().getPosition());
plat.assignTexture(pickTexture(plat.getType()));
platforms[&platform - &platforms[0]] = plat;
lastPlatform = &platforms[&platform - &platforms[0]];
}
else if (platform->getType() == PlatformType::BOOST) {
player->setYVelocity(-20.0f);
else if (platform.getType() == PlatformType::BOOST) {
player.setYVelocity(-20.0f);
}
}
}
}

// Handle moving platforms
if (platform->getType() == PlatformType::MOVING) {
platform->animateMovement();
if (platform.getType() == PlatformType::MOVING) {
platform.animateMovement();
}
}
}
Expand All @@ -234,48 +194,78 @@ void Game::checkCollision() {
// }

void Game::play() {
player->update();
player.update();
checkCollision();
sf::Vector2f velocity = player->getVelocity();
sf::Vector2f velocity = player.getVelocity();

if (player->getSprite().getPosition().y < 300 && maxScore - score < 200) {
if (player.getSprite().getPosition().y < 300 && maxScore - score < 200) {
for (auto& platform : platforms) {
if (platform->getSprite().getPosition().y > 750.0f) {
delete platform;
auto plat = new Platform();
plat->useGenerator(lastPlatform->getSprite().getPosition());
lastPlatform = plat;
platform = plat;
if (platform.getSprite().getPosition().y > 750.0f) {
// delete platform;
auto plat = Platform();
plat.useGenerator(lastPlatform->getSprite().getPosition());
plat.assignTexture(pickTexture(plat.getType()));
platforms[&platform - &platforms[0]] = plat;
lastPlatform = &platforms[&platform - &platforms[0]];
}

float yVelocity = player->getYVelocity();
platform->moveSprite({0.0f, -yVelocity});
float yVelocity = player.getYVelocity();
platform.moveSprite({0.0f, -yVelocity});
}
}
else {
player->moveSprite({0.0f, velocity.y});
player.moveSprite({0.0f, velocity.y});
}
player->moveSprite({velocity.x, 0.0f});
player.moveSprite({velocity.x, 0.0f});

window.clear(sf::Color::White);

window.draw(player->getSprite());
player.draw(window, player.getSprite());
for (auto& platform : platforms) {
window.draw(platform->getSprite());
platform.draw(window, platform.getSprite());
}
displayScore();
// displayDebugInfo();

window.display();
}

void Game::initPlatformTextures() {
platformTextures.emplace_back();
platformTextures[0].loadFromFile("assets/platform.png");

platformTextures.emplace_back();
platformTextures[1].loadFromFile("assets/platform_break.png");

platformTextures.emplace_back();
platformTextures[2].loadFromFile("assets/platform_moving.png");

platformTextures.emplace_back();
platformTextures[3].loadFromFile("assets/platform_boost.png");
}

sf::Texture& Game::pickTexture(PlatformType platformType) {
switch (platformType) {
case PlatformType::NORMAL:
return platformTextures[0];
case PlatformType::BREAKABLE:
return platformTextures[1];
case PlatformType::MOVING:
return platformTextures[2];
case PlatformType::BOOST:
return platformTextures[3];
default:
return platformTextures[0];
}
}

void Game::displayScore() {
score -= player->getYVelocity();
score -= player.getYVelocity();
if (score > maxScore)
maxScore = score;

scoreText->setString("Score: " + std::to_string((int)maxScore));
window.draw(*scoreText);
scoreText.setString("Score: " + std::to_string((int)maxScore));
window.draw(scoreText);
}

void Game::changeScreen(ScreenType screenType) {
Expand Down
18 changes: 12 additions & 6 deletions doodle_jump/game/Game.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "./../platform/Platform.h"
#include "./../player/Player.h"
#include "./../screen/GameScreen.h"
#include "./../powerups/Powerups.h"

#include <queue>
#include <vector>
Expand All @@ -14,21 +15,26 @@ class Game {
sf::Font font;
float score;
float maxScore;
sf::Text *scoreText;
Player *player;
std::vector<Platform*> platforms;
Platform* lastPlatform;
sf::Text scoreText;
Player player;
std::vector<sf::Texture> platformTextures;
std::vector<Platform> platforms;
std::vector<Powerups> powerups;
GameObject *powerup;
Platform *lastPlatform;
public:
Game();
~Game();
Game(const Game& game); // for cppcheck
Game& operator=(const Game& game); // for cppcheck
Game(const Game& game) = delete;
Game& operator=(const Game& game) = delete;
friend std::ostream& operator<<(std::ostream& out, const Game& game);
void run();
void changeScreen(ScreenType screenType);
void play();
void reset();
// void displayDebugInfo();
void initPlatformTextures();
sf::Texture& pickTexture(PlatformType platformType);
void displayScore();
void checkCollision();
};
Loading

0 comments on commit 46ebcb1

Please sign in to comment.