Added copy operators for game's static instance

Player
CommandParser can be swapped but not copied.
This commit is contained in:
2026-08-28 20:38:11 -05:00
parent ef54fa6609
commit 45c365e317
6 changed files with 99 additions and 12 deletions
+7
View File
@@ -22,6 +22,13 @@ public:
std::vector<std::string> Verb(std::stringstream &sentences); std::vector<std::string> Verb(std::stringstream &sentences);
std::vector<std::string> Noun(std::stringstream &sentences); std::vector<std::string> 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"); } static inline void CommandUnrecognized() { printf("I don't know how to do that.\n"); }
int Parse(); int Parse();
}; };
+15 -5
View File
@@ -2,6 +2,7 @@
#include <filesystem> #include <filesystem>
#include <fstream> #include <fstream>
#include <string> #include <string>
#include <utility>
#include <vector> #include <vector>
#include "CommandParser.h" #include "CommandParser.h"
#include "game.h" #include "game.h"
@@ -37,6 +38,9 @@ private:
public: public:
Game(); Game();
Game(Game& rhs);
Game& operator=(Game& rhs);
inline bool GetIsActive() const { return isActive; } inline bool GetIsActive() const { return isActive; }
bool Init(); bool Init();
void Run(); void Run();
@@ -54,18 +58,24 @@ public:
void SetIsActive(const bool active) { isActive = active; } void SetIsActive(const bool active) { isActive = active; }
static void SetGameInstance(Game * g) {
game_ = g;
}
static Game &Instance() { static Game &Instance() {
if (game_ == nullptr) { if (game_ == nullptr) {
game_ = new Game(); game_ = new Game();
game_->Init();
} }
return *game_; 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 // Commands
+21 -3
View File
@@ -1,12 +1,12 @@
#pragma once #pragma once
#include <iostream>
#include <math.h> #include <math.h>
#include <string> #include <utility>
#include "creature.h" #include "creature.h"
class Player : Creature { class Player : Creature {
private: private:
// Inventory inventory; // Inventory inventory;
@@ -15,11 +15,29 @@ private:
int prevRoomID = -1; int prevRoomID = -1;
public: public:
Player();
Player(Player &rhs);
Player& operator=(Player& rhs);
void Init() override; void Init() override;
void LevelUp() override; void LevelUp() override;
void Die() override; void Die() override;
void TakeDamage(int damageType, float damage) override; void TakeDamage(int damageType, float damage) override;
float GetHealth() override; float GetHealth() override;
void ChangeRoom(int roomID); 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();
}; };
+19
View File
@@ -2,6 +2,7 @@
#include <cstdio> #include <cstdio>
#include <filesystem> #include <filesystem>
#include <fstream> #include <fstream>
#include "player.h"
std::vector<std::vector<Room>> rooms; std::vector<std::vector<Room>> rooms;
@@ -48,6 +49,18 @@ bool Game::Init() {
return true; 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() { void Game::Run() {
while (isActive) { while (isActive) {
parser.Parse(); parser.Parse();
@@ -149,3 +162,9 @@ int Look(const std::string &c) {
cur.PrintAvalibleExits(); cur.PrintAvalibleExits();
return 0; return 0;
} }
Game::~Game()
{
isActive = false;
progress = 0;
}
+35 -1
View File
@@ -1,9 +1,32 @@
#include "player.h" #include "player.h"
#include <algorithm>
#include <string>
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() { void Player::Init() {
// initialize stats, room location, inventory and item data // initialize stats, room location, inventory and item data
stats = Stats{100.0f, 50.00f, 150.00f, 50.00f}; stats = Stats{100.0f, 50.00f, 150.00f, 50.00f};
name = "Player"; name = "Player";
} }
@@ -29,3 +52,14 @@ void Player::ChangeRoom(int roomID) {
curRoomID = roomID; curRoomID = roomID;
} }
} }
Player::~Player()
{
id = -1;
level = -1;
stats = {};
name.erase();
description.erase();
curRoomID = -1;
prevRoomID = -1;
}
+1 -2
View File
@@ -4,8 +4,7 @@ int main(int argc, char *argv[]) {
Game game; // Calls default constructor which Initializes GAME Game game; // Calls default constructor which Initializes GAME
if (!game) { if (!game) {
game.Init(); game = Game::Instance();
Game::SetGameInstance(&game);
} }
while (game.GetIsActive()) { while (game.GetIsActive()) {
game.Run(); game.Run();