Skip to content

Commit

Permalink
base class GameObject added
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdan-velicu committed Nov 7, 2023
1 parent b2382ec commit 1d14062
Show file tree
Hide file tree
Showing 13 changed files with 158 additions and 179 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@
- minim o clasă de bază și **3 clase derivate**
- ierarhia trebuie să fie cu bază proprie, nu derivată dintr-o clasă predefinită
- [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ă
- [x] 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
- [ ] apelarea constructorului din clasa de bază din constructori din derivate
- [ ] smart pointers (recomandat, opțional)
- [ ] `dynamic_cast`/`std::dynamic_pointer_cast` pentru downcast cu sens
- [ ] suprascris cc/op= pentru copieri/atribuiri corecte, copy and swap
- [x] apelarea constructorului din clasa de bază din constructori din derivate
- [x] smart pointers (recomandat, opțional)
- [x] `dynamic_cast`/`std::dynamic_pointer_cast` pentru downcast cu sens
- [x] suprascris cc/op= pentru copieri/atribuiri corecte, copy and swap
- [ ] 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`
Expand Down
25 changes: 17 additions & 8 deletions doodle_jump/game/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "./../screen/PlayScreen.h"
#include "./../screen/GameOver.h"
#include <iostream>
#include <memory>
#include <string>

const int SCREEN_WIDTH = 800;
Expand All @@ -21,13 +22,20 @@ Game::Game() {
currentScreen = ScreenType::MAIN_MENU;
score = 0;
maxScore = 0;

gameObject = &player;

This comment has been minimized.

Copy link
@mcmarius

mcmarius Dec 17, 2023

Dacă gameObject nu va fi altceva, mai simplu să folosești direct clasa Player, iar partea de moșteniri/virtual/dynamic cast ar fi doar la platforme (sau altă ierarhie)


auto player = dynamic_cast<Player*>(gameObject);
if (player) {
player->setHealth(4);
} else {
std::cout << "gameObject is not a Player\n";
}

initTextures();

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

lastPlatform = new Platform();

Expand Down Expand Up @@ -77,7 +85,6 @@ void Game::reset() {

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

for (int i = 0; i < 20; i++) {
auto platform = Platform();
Expand Down Expand Up @@ -136,7 +143,7 @@ void Game::run() {
Game::play();
break;
case ScreenType::GAME_OVER:
GameOver::render(window);
GameOver::render(window, maxScore);
break;
case ScreenType::CLOSE:
window.close();
Expand Down Expand Up @@ -253,7 +260,9 @@ void Game::play() {
}
}
else {
player.moveSprite({0.0f, velocity.y});
// funcție virtuala apelata prin pointer
gameObject->moveSprite({0.0f, velocity.y});

This comment has been minimized.

Copy link
@mcmarius

mcmarius Dec 23, 2023

Nu punctez cerințe "corecte" dpdv "tehnic" dacă nu au sens. Pe logica de acum, gameObject nu va fi altceva decât un player, deci nu are sens să faci apel prin pointer "doar ca să fie".

// player.moveSprite({0.0f, velocity.y});
}
player.moveSprite({velocity.x, 0.0f});

Expand All @@ -262,12 +271,12 @@ void Game::play() {
window.draw(backgroundSprite[0]);
window.draw(backgroundSprite[1]);

player.draw(window, player.getSprite());
player.draw(window);
for (auto& platform : platforms) {
platform.draw(window, platform.getSprite());
platform.draw(window);
}
for (auto& powerup : powerups) {
powerup.draw(window, powerup.getSprite());
powerup.draw(window);
}
displayScore();
// displayDebugInfo();
Expand Down
42 changes: 3 additions & 39 deletions doodle_jump/game/Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,7 @@
#include <vector>
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>

class PowerDrawer {
private:
GameObject* gameObject;
public:
PowerDrawer() {
gameObject = nullptr;
}

void setGameObject(GameObject* gameObject_) {
gameObject = gameObject_;
}

void assignTexture(sf::Texture& texture) {
if (gameObject != nullptr)
gameObject->assignTexture(texture);
}

sf::Texture& getTexture() {
if (gameObject != nullptr)
return gameObject->getTexture();
}

sf::Sprite getSprite() {
if (gameObject != nullptr)
return gameObject->getSprite();
}

void draw(sf::RenderWindow& window, const sf::Sprite& sprite) {
std::cout << "PowerDrawer draw called\n";

if (gameObject != nullptr)
gameObject->draw(window, sprite);
else
window.draw(sprite);
}
};
#include <memory>

class Game {
ScreenType currentScreen;
Expand All @@ -61,8 +25,8 @@ class Game {

std::vector<Platform> platforms;
std::vector<Powerups> powerups;
PowerDrawer powerDrawer;
// GameObject *powerup;
GameObject *gameObject;

Platform *lastPlatform;
public:
Game();
Expand Down
62 changes: 58 additions & 4 deletions doodle_jump/game_object/GameObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,65 @@ GameObject::~GameObject() {
std::cout << "GameObject destructor called\n";
}

GameObject::GameObject(const GameObject& gameObject) {
std::cout << "GameObject copy constructor called\n";
// texture = new sf::Texture();
texture = gameObject.texture;
sprite = sf::Sprite();
sprite.setPosition(gameObject.sprite.getPosition());
sprite.setTexture(*texture);
}

GameObject& GameObject::operator=(const GameObject& gameObject) {
std::cout << "GameObject copy assignment operator called\n";
if (this != &gameObject) {
// texture = new sf::Texture();
texture = gameObject.texture;
sprite = sf::Sprite();
sprite.setPosition(gameObject.sprite.getPosition());
sprite.setTexture(*texture);
}
return *this;
}

void GameObject::assignTexture(sf::Texture& texture_) {
texture = texture_;
sprite.setTexture(texture);
texture = &texture_;
sprite.setTexture(*texture);
}

void GameObject::draw(sf::RenderWindow& window) {
window.draw(sprite);
}

sf::Texture& GameObject::getTexture() {
return *texture;
}

void GameObject::loadTexture(const std::string& path) {
texture->loadFromFile(path);
sprite.setTexture(*texture);
}

sf::Sprite GameObject::getSprite() const {
return sprite;
}

void GameObject::setSpritePos(const sf::Vector2f& coordinates) {
sprite.setPosition(coordinates);
}

void GameObject::setSpriteScale(const sf::Vector2f& scale) {
sprite.setScale(scale);
}

void GameObject::setSpriteOrigin(const sf::Vector2f& origin) {
sprite.setOrigin(origin);
}

void GameObject::moveSprite(const sf::Vector2f& coordinates_) {
sprite.move(coordinates_);
}

void GameObject::draw(sf::RenderWindow &window, const sf::Sprite& sprite_) {
window.draw(sprite_);
void GameObject::setSpritePosition(const sf::Vector2f& coordinates) {
sprite.setPosition(coordinates);
}
25 changes: 13 additions & 12 deletions doodle_jump/game_object/GameObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@

class GameObject {
private:
sf::Texture texture;
sf::Sprite sprite;
sf::Texture *texture = new sf::Texture();
sf::Sprite sprite = sf::Sprite();
public:
GameObject();
virtual ~GameObject();
GameObject(const GameObject& gameObject);
GameObject& operator=(const GameObject& gameObject);
virtual void draw(sf::RenderWindow& window);
virtual void assignTexture(sf::Texture& texture_);

virtual sf::Texture& getTexture() {
return texture;
}

virtual sf::Sprite getSprite() {
return sprite;
}

virtual void draw(sf::RenderWindow& window, const sf::Sprite& sprite_);
virtual sf::Texture& getTexture();
virtual void loadTexture(const std::string& path);
virtual sf::Sprite getSprite() const;
virtual void setSpritePos(const sf::Vector2f& coordinates);
virtual void setSpriteScale(const sf::Vector2f& scale);
virtual void setSpriteOrigin(const sf::Vector2f& origin);
virtual void moveSprite(const sf::Vector2f& coordinates_);
virtual void setSpritePosition(const sf::Vector2f& coordinates);

This comment has been minimized.

Copy link
@mcmarius

mcmarius Dec 17, 2023

Ierarhia asta pare să fie utilă doar ca să ții atribute și funcții comune, însă e prea generală să o folosești pentru virtualizare. Ar veni cu destructor protected și non-virtual și partea de virtualizare în altă ierarhie.

Problema acum e că ai funcții virtuale, dar nu ai de fapt nevoie să suprascrii ceva în derivate. În plus, nu văd să ai undeva nevoie de un gameObject la modul general, indiferent de tipul derivat.

};
32 changes: 6 additions & 26 deletions doodle_jump/platform/Platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,24 @@ Platform::Platform() {
int x = 0;
int y = 800;

sprite.setPosition((float)x, (float)y);
setSpritePosition({(float)x, (float)y});
}

Platform::~Platform() {
std::cout << "Platform destructor called\n";
}

Platform::Platform(const Platform& platform) {
Platform::Platform(const Platform& platform) : GameObject(platform) {
std::cout << "Platform copy constructor called\n";
texture = new sf::Texture();
sprite = platform.sprite;
type = platform.type;
updateCount = platform.updateCount;
}

Platform& Platform::operator=(const Platform& platform) {
std::cout << "Platform copy assignment operator called\n";
if (this != &platform) {
GameObject::operator=(platform);
type = platform.type;
texture = platform.texture;
sprite = platform.sprite;
updateCount = platform.updateCount;
}
return *this;
Expand Down Expand Up @@ -67,12 +64,7 @@ void Platform::useGenerator(const sf::Vector2f& lastPlatformCoordinates) {

type = platformType;

sprite.setPosition((float)x, (float)y);
}

void Platform::assignTexture(sf::Texture& texture_) {
texture = &texture_;
sprite.setTexture(*texture);
setSpritePosition({(float)x, (float)y});
}

std::ostream& operator<<(std::ostream& os, const PlatformType& platformType) {
Expand Down Expand Up @@ -102,16 +94,12 @@ std::ostream& operator<<(std::ostream& os, const Platform& platform) {
return os;
}

void Platform::moveSprite(const sf::Vector2f& coordinates_) {
sprite.move(coordinates_);
}

void Platform::animateMovement() {
if (updateCount < 100) {
sprite.move({ -1.0f, 0.0f });
moveSprite({ -1.0f, 0.0f });
}
else if (updateCount < 200) {
sprite.move({ 1.0f, 0.0f });
moveSprite({ 1.0f, 0.0f });
}
else {
updateCount = 0;
Expand All @@ -121,12 +109,4 @@ void Platform::animateMovement() {

PlatformType Platform::getType() const {
return type;
}

sf::Sprite Platform::getSprite() const {
return sprite;
}

void Platform::draw(sf::RenderWindow& window, const sf::Sprite& _sprite) {
window.draw(_sprite);
}
12 changes: 5 additions & 7 deletions doodle_jump/platform/Platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,18 @@ std::ostream& operator<<(std::ostream& os, const PlatformType& platformType);
class Platform : public GameObject {
private:
PlatformType type;
sf::Texture *texture = new sf::Texture();
sf::Sprite sprite = sf::Sprite();
// sf::Texture *texture = new sf::Texture();
// sf::Sprite sprite = sf::Sprite();
int updateCount;
public:
Platform();
~Platform();
~Platform() override;
Platform(const Platform& platform);
Platform& operator=(const Platform& platform);
void useGenerator(const sf::Vector2f& lastPlatformCoordinates);
friend std::ostream& operator<<(std::ostream& os, const Platform& platform);
PlatformType getType() const;
sf::Sprite getSprite() const;
void moveSprite(const sf::Vector2f& coordinates_);
// sf::Sprite getSprite() const;
// void moveSprite(const sf::Vector2f& coordinates_);
void animateMovement();
void assignTexture(sf::Texture& texture_);
void draw(sf::RenderWindow& window, const sf::Sprite& sprite) override;
};
Loading

0 comments on commit 1d14062

Please sign in to comment.