aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CMakeLists.txt17
-rw-r--r--backend/CMakeLists.txt2
-rw-r--r--frontend/GameController.h3
-rw-r--r--frontend/cmd/cheat.cpp23
-rw-r--r--frontend/cmd/equip.cpp20
-rw-r--r--frontend/cmd/get.cpp8
-rw-r--r--frontend/cmd/use.cpp18
-rw-r--r--test/CMakeLists.txt3
-rw-r--r--test/string.cpp24
9 files changed, 92 insertions, 26 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index c71af78..fdbdfd4 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -4,20 +4,31 @@ set(CMAKE_CXX_STANDARD 20)
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
set(CMAKE_BUILD_TYPE Debug)
+project(main CXX)
+
find_package(pugixml REQUIRED)
+find_package(GTest REQUIRED)
-project(main CXX)
+add_library(backend STATIC)
+target_include_directories(backend PUBLIC .)
add_executable(main)
-
-target_include_directories(main PRIVATE .)
+add_executable(test_main EXCLUDE_FROM_ALL)
add_subdirectory(backend)
add_subdirectory(frontend)
+add_subdirectory(test)
target_link_libraries(main
+ PRIVATE backend
PUBLIC pugixml
PUBLIC sqlite3
PUBLIC readline
)
+target_link_libraries(test_main
+ PRIVATE gtest
+ PRIVATE gtest_main
+ PRIVATE backend
+)
+
diff --git a/backend/CMakeLists.txt b/backend/CMakeLists.txt
index 814d7e9..1bff63b 100644
--- a/backend/CMakeLists.txt
+++ b/backend/CMakeLists.txt
@@ -1,4 +1,4 @@
-target_sources(main PUBLIC
+target_sources(backend PUBLIC
EnemyFactory.cpp
LocationFactory.cpp
Object.cpp
diff --git a/frontend/GameController.h b/frontend/GameController.h
index 9f0d6ee..ca19972 100644
--- a/frontend/GameController.h
+++ b/frontend/GameController.h
@@ -30,6 +30,7 @@ private:
private:
void cmdset_default();
void cmdset_death();
+ void cmdset_cheats();
private:
std::unordered_map<std::string, void(GameController::*)(std::string &)> cmds;
@@ -48,6 +49,8 @@ private:
Cmd cmd_quit;
Cmd cmd_restart;
+ Cmd cmd_give;
+
private:
std::unique_ptr<Dungeon> dungeon = nullptr;
bool playing = true;
diff --git a/frontend/cmd/cheat.cpp b/frontend/cmd/cheat.cpp
index 8c0af8d..981bca1 100644
--- a/frontend/cmd/cheat.cpp
+++ b/frontend/cmd/cheat.cpp
@@ -1,13 +1,36 @@
+#include <memory>
+
#include "backend/print.h"
#include "backend/Dungeon.h"
#include "../GameController.h"
+#include "../GameData.h"
using namespace std;
+constexpr const char * cmd_give_key = "Manifesteer";
+void GameController::cmd_give(string & item_name) {
+ Player & player = this->dungeon->get_player();
+ GameData & gamedata = GameData::get_instance();
+ auto object = unique_ptr<Object>(gamedata.create_object(item_name));
+ lprtf("Object aangemaakt: %s\n", object->get_displayname().c_str());
+ player.inventory.push_back(object.release());
+}
+
void GameController::cmd_cheat(string &) {
Player & player = this->dungeon->get_player();
player.cheating = !player.cheating;
lprtf("Cheats staan nu %s.\n", player.cheating ? "aan" : "uit");
+ this->cmdset_cheats();
+}
+
+void GameController::cmdset_cheats() {
+ Player & player = this->dungeon->get_player();
+
+ if (player.cheating) {
+ this->cmds[cmd_give_key] = &GameController::cmd_give;
+ } else {
+ this->cmds.erase(cmd_give_key);
+ }
}
diff --git a/frontend/cmd/equip.cpp b/frontend/cmd/equip.cpp
index fe951b1..8f37c0c 100644
--- a/frontend/cmd/equip.cpp
+++ b/frontend/cmd/equip.cpp
@@ -16,21 +16,21 @@ void GameController::cmd_equip(string & target_name) {
Object & object = **it;
- WeaponObject * weapon = dynamic_cast<WeaponObject *>(&object);
- if (weapon != nullptr) {
- player.inventory.remove(weapon);
- player.equip(weapon);
+ try {
+ WeaponObject & weapon = dynamic_cast<WeaponObject &>(object);
+ player.inventory.remove(&weapon);
+ player.equip(&weapon);
this->dungeon->update();
return;
- }
+ } catch (std::bad_cast &) {};
- ArmorObject * armor = dynamic_cast<ArmorObject *>(&object);
- if (armor != nullptr) {
- player.inventory.remove(armor);
- player.equip(armor);
+ try {
+ ArmorObject & armor = dynamic_cast<ArmorObject &>(object);
+ player.inventory.remove(&armor);
+ player.equip(&armor);
this->dungeon->update();
return;
- }
+ } catch (std::bad_cast &) {};
throw Exception("object \"%s\" is niet draagbaar", target_name.c_str());
}
diff --git a/frontend/cmd/get.cpp b/frontend/cmd/get.cpp
index e774168..ea1f69b 100644
--- a/frontend/cmd/get.cpp
+++ b/frontend/cmd/get.cpp
@@ -22,13 +22,13 @@ void GameController::cmd_get(string & target_name) {
location.remove_visible_object(object.get());
// gold objects are collected and (implicitly) destroyed
- GoldObject * gold = dynamic_cast<GoldObject *>(object.get());
- if (gold != nullptr) {
- int count = gold->get_count();
+ try {
+ GoldObject & gold = dynamic_cast<GoldObject &>(*object);
+ int count = gold.get_count();
player.gold += count;
lprtf("Je bent %d goudstuk%s rijker.\n", count, count == 1 ? "" : "ken");
return;
- }
+ } catch (std::bad_cast &) {};
// other objects go in the inventory
lprtf("Je voegt %s toe aan je bezit.\n", object->get_name().c_str());
diff --git a/frontend/cmd/use.cpp b/frontend/cmd/use.cpp
index 3134210..78d8233 100644
--- a/frontend/cmd/use.cpp
+++ b/frontend/cmd/use.cpp
@@ -1,4 +1,4 @@
-#include <algorithm>
+#include <memory>
#include "backend/ConsumableObject.h"
#include "backend/Exception.h"
@@ -14,16 +14,18 @@ void GameController::cmd_use(string & target_name) {
Location & location = player.get_location();
auto it = find_if_range(player.inventory.range(), by_name_case_insensitive(target_name));
-
if (!it)
throw Exception("object \"%s\" niet gevonden", target_name.c_str());
+ Object & object = **it;
- auto consumable = unique_ptr<ConsumableObject>(dynamic_cast<ConsumableObject *>(*it));
- if (consumable == nullptr)
- throw Exception("%s is niet consumeerbaar", (*it)->get_name().c_str());
+ try {
+ auto consumable = unique_ptr<ConsumableObject>(&dynamic_cast<ConsumableObject &>(object));
+ player.inventory.remove(consumable.get());
- lprtf("Je drinkt %s.\n", consumable->get_displayname().c_str());
- player.inventory.remove(consumable.get());
- consumable->consume(player);
+ lprtf("Je drinkt %s.\n", consumable->get_displayname().c_str());
+ consumable->consume(player);
+ } catch (std::bad_cast &) {
+ throw Exception("%s is niet consumeerbaar", object.get_name().c_str());
+ }
}
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
new file mode 100644
index 0000000..bba1d07
--- /dev/null
+++ b/test/CMakeLists.txt
@@ -0,0 +1,3 @@
+target_sources(test_main PUBLIC
+ string.cpp
+)
diff --git a/test/string.cpp b/test/string.cpp
new file mode 100644
index 0000000..dc329e3
--- /dev/null
+++ b/test/string.cpp
@@ -0,0 +1,24 @@
+#include <gtest/gtest.h>
+
+#include "backend/String.h"
+
+TEST(StringTest, OperatorEqual) {
+ String a = "foo";
+ String b = "foo";
+ ASSERT_TRUE(a == b);
+
+ b = "bar";
+ ASSERT_FALSE(a == b);
+}
+
+TEST(StringTest, OperatorNotEqual) {
+ String a = "foo";
+ String b = "foo";
+ ASSERT_FALSE(a != b);
+
+ b = "bar";
+ ASSERT_TRUE(a != b);
+}
+
+
+