-
Notifications
You must be signed in to change notification settings - Fork 0
/
object.cpp
105 lines (84 loc) · 2.13 KB
/
object.cpp
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "object.h"
#include "renderer.h"
Object::Object(const std::string& name, const std::string& description) :
m_name(name),
m_description(description)
{
}
std::string Object::Open()
{
return "This cannot be opened.\n";
}
std::string Object::UseWith(ObjectPtr obj)
{
return "This doesn't work.\n";
}
std::string Object::SetLock(bool lock)
{
return "This doesn't work.\n";
}
std::string Object::AddItem(ObjectPtr object)
{
return "This item cannot store anything.\n";
}
// ========== Hammer ===============
std::string Hammer::Examine()
{
return "This is an old hammer. Probably not worth 1 gp.";
}
std::string Hammer::Open()
{
return "Can't open a hammer.";
}
std::string Hammer::UseWith(ObjectPtr obj)
{
return "This is not useful";
}
std::shared_ptr<Object> Hammer::GetObjectByName(const std::string& name)
{
if(Object::m_name == name) return shared_from_this();
return std::shared_ptr<Object>();
}
// =============== Chest =============
Chest::Chest():
Object("a chest", "This is a sturdy wooden chest."),
m_container(),
m_locked(true) {}
std::string Chest::Examine()
{
std::string description = GetDescription();
if(m_locked) description += "\nThe chest seems to be locked.";
if(!m_container.empty() && !m_locked)
{
description += "\nThe chest is not empty. It contains : ";
for(auto& pObject : m_container) description += pObject->GetName() + ", ";
return description;
}
}
std::string Chest::Open()
{
m_locked = false;
return "You unlocked the chest. Now you could examine its content.";
}
std::string Chest::AddItem(ObjectPtr object)
{
m_container.push_back(object);
return "";
}
std::shared_ptr<Object> Chest::GetObjectByName(const std::string& name)
{
if(m_name == name) return shared_from_this();
if(!m_locked)
{
for(auto p_object : m_container)
{
auto p_foundObject = p_object->GetObjectByName(name);
if(p_foundObject) return p_foundObject;
}
}
return std::shared_ptr<Object>();
}
std::string Chest::UseWith(ObjectPtr obj)
{
return Object::UseWith(obj);
}