aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-10-30 16:34:29 +0100
committerLoek Le Blansch <loek@pipeframe.xyz>2024-10-30 16:34:29 +0100
commit71380426426dffe787d1704a8fd639c4b1bbfad3 (patch)
treef550cc1dfa100e208712e20b4442687cc2a7f157
parenta3c1ba7b49e4c5901d7c9dd917049744ad20fc96 (diff)
cmd_get complete
-rw-r--r--backend/GoldObject.cpp5
-rw-r--r--backend/GoldObject.h3
-rw-r--r--backend/List.h3
-rw-r--r--backend/List.hpp16
-rw-r--r--backend/Location.cpp3
-rw-r--r--backend/Location.h1
-rw-r--r--frontend/Player.h2
-rw-r--r--frontend/cmd/get.cpp33
8 files changed, 60 insertions, 6 deletions
diff --git a/backend/GoldObject.cpp b/backend/GoldObject.cpp
index 624fdf9..cf97b2e 100644
--- a/backend/GoldObject.cpp
+++ b/backend/GoldObject.cpp
@@ -1,6 +1,9 @@
#include "GoldObject.h"
-void GoldObject::set_count(int count) {
+void GoldObject::set_count(const int & count) {
this->count = count;
}
+int GoldObject::get_count() const {
+ return this->count;
+}
diff --git a/backend/GoldObject.h b/backend/GoldObject.h
index ea473d4..03a074e 100644
--- a/backend/GoldObject.h
+++ b/backend/GoldObject.h
@@ -6,7 +6,8 @@ class GoldObject : public Object {
using Object::Object;
public:
- void set_count(int count);
+ void set_count(const int & count);
+ int get_count() const;
private:
int count = 0;
diff --git a/backend/List.h b/backend/List.h
index 2d3d6b1..1d8893e 100644
--- a/backend/List.h
+++ b/backend/List.h
@@ -20,7 +20,8 @@ class List {
public:
size_t size();
- void push_back(T el);
+ void push_back(T & el);
+ void remove(const T & val);
void pop_back();
diff --git a/backend/List.hpp b/backend/List.hpp
index df075f9..a0d9289 100644
--- a/backend/List.hpp
+++ b/backend/List.hpp
@@ -8,7 +8,7 @@ size_t List<T>::size() {
}
template <typename T>
-void List<T>::push_back(T el) {
+void List<T>::push_back(T & el) {
ListLink<T> * link = static_cast<ListLink<T> *>(malloc(sizeof(ListLink<T>)));
link->prev = this->head;
link->next = nullptr;
@@ -20,6 +20,20 @@ void List<T>::push_back(T el) {
}
template <typename T>
+void List<T>::remove(const T & target) {
+ ListLink<T> * link = nullptr;
+ for (link = this->tail; link != nullptr; link = link->next) {
+ if (link->value == target) break;
+ }
+ if (link == nullptr) return; // target not in list
+
+ if (link->next != nullptr) link->next->prev = link->prev;
+ if (link->prev != nullptr) link->prev->next = link->next;
+ free(link);
+ this->length--;
+}
+
+template <typename T>
void List<T>::pop_back() {
if (this->head == nullptr) return; // empty list
diff --git a/backend/Location.cpp b/backend/Location.cpp
index a26d530..9add524 100644
--- a/backend/Location.cpp
+++ b/backend/Location.cpp
@@ -40,6 +40,9 @@ Location * Location::get_exit(Direction dir) {
void Location::add_object(Object * object) {
this->objects.push_back(object);
}
+void Location::remove_object(Object * object) {
+ this->objects.remove(object);
+}
ListRange<Object *> Location::get_objects() {
return this->objects.range();
}
diff --git a/backend/Location.h b/backend/Location.h
index 59a531f..fb0abfc 100644
--- a/backend/Location.h
+++ b/backend/Location.h
@@ -23,6 +23,7 @@ public:
void set_exit(Direction dir, Location * location = nullptr);
Location * get_exit(Direction dir);
void add_object(Object *);
+ void remove_object(Object *);
ListRange<Object *> get_objects();
void add_enemy(Enemy *);
ListRange<Enemy *> get_enemies();
diff --git a/frontend/Player.h b/frontend/Player.h
index 09e3a73..6d333f5 100644
--- a/frontend/Player.h
+++ b/frontend/Player.h
@@ -3,6 +3,7 @@
#include <unordered_map>
#include <string>
#include <memory>
+#include <vector>
#include "backend/WeaponObject.h"
#include "backend/ArmorObject.h"
@@ -29,6 +30,7 @@ private:
std::unique_ptr<ArmorObject> armor = nullptr;
Location & location;
bool cheating = false;
+ std::vector<std::unique_ptr<Object>> inventory = {};
public:
Player(Dungeon & dungeon);
diff --git a/frontend/cmd/get.cpp b/frontend/cmd/get.cpp
index 77ad727..78c6ec1 100644
--- a/frontend/cmd/get.cpp
+++ b/frontend/cmd/get.cpp
@@ -1,9 +1,38 @@
#include "../Player.h"
+#include "../strings.h"
+#include "../print.h"
+
+#include "backend/GoldObject.h"
+#include "backend/Location.h"
using namespace std;
-FollowupAction Player::cmd_get(string & argv) {
- // TODO
+FollowupAction Player::cmd_get(string & target_name) {
+ unique_ptr<Object> target = nullptr;
+ for (Object * object : this->location.get_objects()) {
+ if (object->get_hidden() == true) continue;
+ if (str_lower(object->get_name()) != str_lower(target_name)) continue;
+ target = unique_ptr<Object>(object);
+ this->location.remove_object(object);
+ break;
+ }
+ if (target == nullptr) {
+ lprtf("Object \"%s\" niet gevonden\n", target_name.c_str());
+ return FollowupAction::NONE;
+ }
+
+ // gold objects are collected and (implicitly) destroyed
+ GoldObject * gold = dynamic_cast<GoldObject *>(target.get());
+ if (gold != nullptr) {
+ int count = gold->get_count();
+ this->gold += count;
+ lprtf("Je bent %d goudstuk%s rijker.\n", count, count == 1 ? "" : "ken");
+ return FollowupAction::NONE;
+ }
+
+ // other objects go in the inventory
+ lprtf("Je voegt %s toe aan je bezit.\n", target->get_displayname());
+ this->inventory.push_back(std::move(target));
return FollowupAction::NONE;
}