-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameWorld.h
More file actions
188 lines (171 loc) · 5.6 KB
/
GameWorld.h
File metadata and controls
188 lines (171 loc) · 5.6 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//
// Created by Ben Dickson on 5/9/17.
//
#ifndef LIBRARY_GAMEWORLD_H
#define LIBRARY_GAMEWORLD_H
#include <cstdlib>
#include <sstream>
#include <fstream>
#include <iostream>
#include <string>
#include "Point.h"
#include "Entity.h"
#include "NonPlayerCharacter.h"
using namespace std;
class GameWorld
{
private:
CollisionPipeline collisionPipeline;
CollisionPipeline movingPipeline;
RenderPipeline renderLayer[5];
map<string,Entity*> entities; // UID , Entity
list<NonPlayerCharacter> npc;
double gravity = 10.0;
string name;
public:
static Rotation angles;
void loadLevel(stringstream levelStream);
bool hasEntityID(string uid)
{
return entities.find(uid)==entities.end();
}
Entity* createEntity(string uid)
{
if(!hasEntityID(uid))
{
Entity* newEnt = new Entity(uid);
entities.insert(pair<string,Entity*>(uid,newEnt));
return newEnt;
}
else
{
return nullptr;
}
}
inline string getName()
{
return name;
}
GameWorld(string ln)
{
name = ln;
}
};
/**
* Level importer
* @param is
* @param gw
* @return
*/
inline std::ifstream& operator>>(ifstream &is, GameWorld& gw)
{
string lineString;
Entity* currentEnt = nullptr;
PhysicalModel* currentPM = nullptr;
RenderModel* currentRM = nullptr;
CollisionModel* currentCM = nullptr;
string entName;
while(getline(is,lineString))
{
lineString = trim(lineString);
int lineLen = lineString.length();
cout << "linelength " << lineLen <<": ";
if(lineLen > 2)
{
cout << " >> ";
if (lineString.at(0)=='<' && lineString.at(lineLen-1)=='>')
{
entName = lineString.substr(1,lineLen-2);
currentEnt = gw.createEntity(entName);
currentPM = nullptr;
currentRM = nullptr;
currentCM = nullptr;
cout << "Entity name '" << entName << "'";
}
else if(currentEnt != nullptr)
{
if(lineString.at(0)=='[')
{
if(lineString.at(lineLen-1) == ']')
{
string pmName = lineString.substr(1,lineLen-2);
currentPM = new PhysicalModel(pmName);
if(!currentEnt->addPhysicalModel(pmName,currentPM))
{
free(currentPM);
currentPM = nullptr;
}
cout << "PhysicalModel name '" << pmName << "'";
}
}
else if(lineString.at(0)=='(' && currentPM != nullptr)
{
if(lineLen > 18 && currentPM->getCollider() == nullptr && lineString.substr(0,17)=="(CollisionModel::"
&& lineString.at(lineLen-1)==')')
{
string colType = lineString.substr(17,lineLen-18);
CollisionModel::Shape s;
if(colType=="BOX")
{
s = CollisionModel::BOX;
}
else if(colType=="CIRCLE")
{
s = CollisionModel::CIRCLE;
}
currentCM = new CollisionModel(s,currentPM);
cout << "PhysicalModel type '" << colType << "'";
}
else if(lineLen > 15 && currentPM->getRenderer() == nullptr && lineString.substr(0,14)=="(RenderModel::"
&& lineString.at(lineLen-1)==')')
{
string renType = lineString.substr(14,lineLen-15);
RenderModel::Shape s;
if(renType=="BOX")
{
s = RenderModel::BOX;
}
else if(renType=="CIRCLE")
{
s = RenderModel::CIRCLE;
}
currentRM = new RenderModel(s,currentPM);
cout << "PhysicalModel type '" << renType << "'";
}
else
{
currentPM = nullptr;
currentRM = nullptr;
currentCM = nullptr;
}
}
else
{
pair<string,string> lineVar = getEquals(lineString);
if(currentCM != nullptr && currentPM != nullptr)
{
currentCM->parseConstructorPair(lineVar);
}
else if(currentRM != nullptr && currentPM != nullptr)
{
currentRM->parseConstructorPair(lineVar);
}
else if(currentPM != nullptr)
{
currentPM->parseConstructorPair(lineVar);
}
else
{
currentPM = nullptr;
currentRM = nullptr;
currentCM = nullptr;
}
cout << "equals line '" << lineVar.first << "' is equal to '" << lineVar.second << "'";
}
}
}
cout << endl;
}
return is;
}
#endif //LIBRARY_GAMEWORLD_H