diff --git a/Engine/headers/CommandParser.h b/Engine/headers/CommandParser.h index 01de049..e9d239a 100644 --- a/Engine/headers/CommandParser.h +++ b/Engine/headers/CommandParser.h @@ -22,6 +22,13 @@ public: std::vector Verb(std::stringstream &sentences); std::vector Noun(std::stringstream &sentences); + inline friend void swap(CommandParser& lhs, CommandParser& rhs) + { + using std::swap; + swap(lhs.commands, rhs.commands); + } + + static inline void CommandUnrecognized() { printf("I don't know how to do that.\n"); } int Parse(); }; diff --git a/Engine/headers/game.h b/Engine/headers/game.h index 5380c84..1c3b68b 100644 --- a/Engine/headers/game.h +++ b/Engine/headers/game.h @@ -2,6 +2,7 @@ #include #include #include +#include #include #include "CommandParser.h" #include "game.h" @@ -37,6 +38,9 @@ private: public: Game(); + Game(Game& rhs); + Game& operator=(Game& rhs); + inline bool GetIsActive() const { return isActive; } bool Init(); void Run(); @@ -54,18 +58,24 @@ public: void SetIsActive(const bool active) { isActive = active; } - static void SetGameInstance(Game * g) { - game_ = g; - } - static Game &Instance() { if (game_ == nullptr) { game_ = new Game(); + game_->Init(); } return *game_; } - ~Game() = default; + inline friend void swap(Game &lhs, Game &rhs) + { + using std::swap; + swap(lhs.isActive, rhs.isActive); + swap(lhs.progress, rhs.progress); + swap(lhs.player, rhs.player); + swap(lhs.parser, rhs.parser); + } + + ~Game(); }; // Commands diff --git a/Engine/headers/player.h b/Engine/headers/player.h index 063eb1c..a2ea04b 100644 --- a/Engine/headers/player.h +++ b/Engine/headers/player.h @@ -1,13 +1,13 @@ #pragma once -#include #include -#include +#include #include "creature.h" class Player : Creature { -private: + + private: // Inventory inventory; // Lets keep the room a loose reference to avoid to many hardcoded details and other info @@ -15,11 +15,29 @@ private: int prevRoomID = -1; public: + + Player(); + Player(Player &rhs); + Player& operator=(Player& rhs); + void Init() override; void LevelUp() override; void Die() override; void TakeDamage(int damageType, float damage) override; float GetHealth() override; - void ChangeRoom(int roomID); + + inline friend void swap(Player& lhs, Player &rhs) noexcept + { + using std::swap; + swap(lhs.id, rhs.id); + swap(lhs.name, rhs.name); + swap(lhs.description, rhs.description); + swap(lhs.stats, rhs.stats); + swap(lhs.level, rhs.level); + swap(lhs.prevRoomID, rhs.prevRoomID); + swap(lhs.curRoomID, rhs.curRoomID); + } + + ~Player(); }; diff --git a/Engine/src/game.cpp b/Engine/src/game.cpp index 5f891e2..b39676b 100644 --- a/Engine/src/game.cpp +++ b/Engine/src/game.cpp @@ -2,6 +2,7 @@ #include #include #include +#include "player.h" std::vector> rooms; @@ -48,6 +49,18 @@ bool Game::Init() { return true; } +Game::Game(Game &rhs) +{ + player = rhs.player; + progress = rhs.progress; +} + +Game& Game::operator=(Game &rhs) +{ + swap(*this, rhs); + return *this; +} + void Game::Run() { while (isActive) { parser.Parse(); @@ -149,3 +162,9 @@ int Look(const std::string &c) { cur.PrintAvalibleExits(); return 0; } + +Game::~Game() +{ + isActive = false; + progress = 0; +} \ No newline at end of file diff --git a/Engine/src/player.cpp b/Engine/src/player.cpp index 9506594..78550c1 100644 --- a/Engine/src/player.cpp +++ b/Engine/src/player.cpp @@ -1,9 +1,32 @@ #include "player.h" +#include +#include + +Player::Player() +{ + Init(); +} + +Player::Player(Player &rhs) +{ + id = rhs.id; + level = rhs.level; + stats = rhs.stats; + name = rhs.name; + description = rhs.description; + curRoomID = rhs.curRoomID; + prevRoomID = rhs.prevRoomID; +} + +Player& Player::operator=(Player& rhs) +{ + swap(*this, rhs); + return *this; +} void Player::Init() { // initialize stats, room location, inventory and item data stats = Stats{100.0f, 50.00f, 150.00f, 50.00f}; - name = "Player"; } @@ -29,3 +52,14 @@ void Player::ChangeRoom(int roomID) { curRoomID = roomID; } } + +Player::~Player() +{ + id = -1; + level = -1; + stats = {}; + name.erase(); + description.erase(); + curRoomID = -1; + prevRoomID = -1; +} diff --git a/main.cpp b/main.cpp index e075dd2..4403e1b 100644 --- a/main.cpp +++ b/main.cpp @@ -4,8 +4,7 @@ int main(int argc, char *argv[]) { Game game; // Calls default constructor which Initializes GAME if (!game) { - game.Init(); - Game::SetGameInstance(&game); + game = Game::Instance(); } while (game.GetIsActive()) { game.Run();