-
Notifications
You must be signed in to change notification settings - Fork 0
/
npc.cpp
110 lines (107 loc) · 2.71 KB
/
npc.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
106
107
108
109
110
#include <string>
#include "npc.h"
using namespace std;
void NPC::print(){
//TODO add logic to print different things depending on state (dead, passified, agressive etc)
if (health <= 0){
cout << deadDescription << endl;
} else{
cout << description << endl;
}
}
void NPC::talkTo(){
if (health >0){
cout << talk << endl;
} else {
cout << "You start to have a lovely conversation with the creature, until you realize you have been talking to a corpse for the past few minutes.\n";
}
}
NPC::NPC(string npcId){
ifstream myfile ("./npcs/"+npcId+".npc");
if (myfile.is_open()){
struct NPC* npc = (struct NPC*)malloc(sizeof(struct NPC));
if(!npc){
cerr << "Error in malloc\n";
exit(1);
}
id = npcId;
description = "";
string line;
//read in id
if ( getline (myfile, line)) {
id = line;
} else {
cerr << "NPC File: " << npcId << " is incorrectly formatted\n";
exit(1);
}
//read in name
if ( getline (myfile, line)) {
name = line;
} else {
cerr << "NPC File: " << npcId << " is incorrectly formatted\n";
exit(1);
}
//read in room
if ( getline (myfile, line)) {
whichRoom = line;
} else {
cerr << "NPC File: " << npcId << " is incorrectly formatted\n";
exit(1);
}
//read in description
if ( getline (myfile, line)) {
description = line;
} else {
cerr << "NPC File: " << npcId << " is incorrectly formatted\n";
exit(1);
}
//read in talking text
if ( getline (myfile, line)) {
talk = line;
} else {
cerr << "NPC File: " << npcId << " is incorrectly formatted\n";
exit(1);
}
//read in deadDescription
if ( getline (myfile, line)) {
deadDescription = line;
} else {
cerr << "NPC File: " << npcId << " is incorrectly formatted\n";
exit(1);
}
//read in health
if ( getline (myfile, line)) {
size_t stringLen = line.length();
health = stoi(line, &stringLen);
} else {
cerr << "NPC File: " << npcId << " is incorrectly formatted\n";
exit(1);
}
//read in strength
if ( getline (myfile, line)) {
size_t stringLen = line.length();
strength = stoi(line, &stringLen);
} else {
cerr << "NPC File: " << npcId << " is incorrectly formatted\n";
exit(1);
}
//read in accuracy
if ( getline (myfile, line)) {
size_t stringLen = line.length();
accuracy = stoi(line, &stringLen);
} else {
cerr << "NPC File: " << npcId << " is incorrectly formatted\n";
exit(1);
}
//read in relationship_status (friend, wary, enemy)
if ( getline (myfile, line)) {
relationship_status = line;
} else {
cerr << "NPC File: " << npcId << " is incorrectly formatted\n";
exit(1);
}
myfile.close();
} else{
id = "-1";
}
}