aboutsummaryrefslogtreecommitdiff
path: root/frontend/cmd/get.cpp
blob: 652e9891ac0e60b854eb91343cc0619b98a550bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <memory>

#include "../GameController.h"
#include "../strings.h"

#include "backend/print.h"
#include "backend/GoldObject.h"
#include "backend/Location.h"

using namespace std;

FollowupAction GameController::cmd_get(string & target_name) {
	Location & location = this->player.get_location();
	unique_ptr<Object> target = nullptr;

	for (Object * object : location.get_visible_objects()) {
		if (str_lower(object->get_name().c_str()) != str_lower(target_name)) continue;
		target = unique_ptr<Object>(object);
		location.remove_visible_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->player.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_name().c_str());
	this->player.inventory.push_back(target.release());
	return FollowupAction::NONE;
}