Added copy operators for game's static instance
Player CommandParser can be swapped but not copied.
This commit is contained in:
@@ -22,6 +22,13 @@ public:
|
||||
std::vector<std::string> Verb(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"); }
|
||||
int Parse();
|
||||
};
|
||||
|
||||
+15
-5
@@ -2,6 +2,7 @@
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
#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
|
||||
|
||||
+22
-4
@@ -1,13 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <math.h>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#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();
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user