forked from luke-c-sargent/ogre_3rd_person_ninjas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Level.cpp
executable file
·250 lines (215 loc) · 5.61 KB
/
Level.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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include <Level.h>
#include <stdlib.h>
#include "GameEnums.h"
Level::Level(){
}
Level::Level(Ogre::SceneManager* smptr)
{
cout <<"\n\nCreating level...\n\n";
x=y=z=0;
btTransform bt;bt.setIdentity();
ms = new btDefaultMotionState(bt);
sceneMgr=smptr;
mass=0;
name="Level";
rootNode = sceneMgr->getRootSceneNode()->createChildSceneNode("Level_node");
tileBodies = new btCompoundShape();
tileset=new Tile(rootNode, sceneMgr,tileBodies);
start=Ogre::Vector3(0,0,0);
num_monsters = 3;
num_monsters_left = num_monsters;
cout << "\n\nLEVEL MADE: "<<rootNode<<"\n\n";
}
//Added for Monster code
int Level::getTileMapSize()
{
return tile_map.size();
}
short Level::getTile(int xi, int yi, int zi)
{
return tile_map[xi + yi*x + zi*y*x]; //linear to 3d storage
}
void Level::setTile(short val, int xi, int yi, int zi)
{
tile_map[xi + yi*x + zi*y*x]=val; //linear to 3d storage
}
Ogre::Vector3 Level::getStart()
{
return start;
}
void Level::testLevelGen(){
tile_map.clear();
tile_map.resize(4,0);
x=2;y=2;z=1;
tile_map[0]=11;tile_map[1]=8;tile_map[2]=11;tile_map[3]=6;
}
//create entities and such from level data
void Level::constructLevel(){
//Ogre::Vector3 offset;//where to put tile
//std::string tileID="defaultID";
cout << "\nCONSTRUCTING LEVEL\n";
for(int k=0; k < z; k++){
for(int j=0; j < y; j++){
for(int i=0; i < x; i++){
tileset->genTile(getTile(i,j,k),i,j,k);
}
}
}
btTransform transform;
transform.setIdentity();
ms = new btDefaultMotionState( transform );
btVector3 localInertia(0,0,0);
tileBodies->calculateLocalInertia(mass,localInertia);
btRigidBody::btRigidBodyConstructionInfo comp_cInfo( mass, ms, tileBodies, localInertia );
body= new btRigidBody( comp_cInfo );
}//void constructLevel()
/*
tiles:
0. no tile 1. no wall 2. north 3. south 4. east 5. west
6. north-e 7. north-w 8. south-e 9. south-w
10. nse 11. nsw 12. new 13. sew
*/
void Level::proceduralLevelGen(int nRooms){
vector<Ogre::Vector4> rooms;
// vector4:<xbl,ybl,x,y>
tile_map.clear();
int roomx=0;
int midx=0;
int roomy=0;
int midy=0;
//room 1
roomx=2*(rand()%2+1)+1;
roomy=2*(rand()%2+1)+1;
x=2*roomx;
y=2*roomy;
z=2;
midx=roomx/2;
midy=roomy/2;
tile_map.resize(roomx*roomy*4*2,0);
cout << getTile(0,0)<<"\n";
rooms.push_back(Ogre::Vector4(roomx,roomy,0,0));
rooms.push_back(Ogre::Vector4(roomx,roomy,roomx,0));
rooms.push_back(Ogre::Vector4(roomx,roomy,0,roomy));
rooms.push_back(Ogre::Vector4(roomx,roomy,roomx,roomy));
cout << rooms[0].w << rooms[0].x << rooms[0].y << rooms[0].z<<"\n";
//just make a grid for now
for(int i=0; i < rooms.size();i++){
generateRoom(rooms[i].x,rooms[i].y,rooms[i].z,rooms[i].w);
}
short nwt = (short)Tiles::NOWALL;
setTile(nwt,midx,roomy-1);
setTile(nwt,midx,roomy);
setTile(nwt,midx+roomx,roomy-1);
setTile(nwt,midx+roomx,roomy);
setTile(nwt,roomx-1,midy);
setTile(nwt,roomx,midy);
setTile(nwt,roomx-1,midy+roomy);
setTile(nwt,roomx,midy+roomy);
int halfsz = tile_map.size()/2;
for(int i=0;i<halfsz;i++){
cout<<i%(roomx*2)<<","<<i/(roomx*2)<<":"<<i%(roomx*2)<<","<<i/(roomx*2)<<"\n";
setTile(getTile(i%(roomx*2),i/(roomx*2)),i%(roomx*2),i/(roomx*2),1);
}
}
void Level::generateRoom(int xi,int yi, int tilex, int tiley){
//x=xi;y=yi;z=1;
if(xi<=1 || yi<=1){
cout << "Level::generateRoom bounds error\n";
exit(0);
}
cout << "\nGENERATING ROOM\n";
short tileR=1;
/*for(int j=tiley; j < yi+tiley; j++){
for(int i=tilex; i < xi+tilex; i++){
if(i==tilex){
if(j==tiley)
tileR=((short)Tiles::SW);
else if(j==(yi-1+tiley))
tileR=((short)Tiles::NW);
else
tileR=((short)Tiles::W);
}
else if(i==xi-1+tilex){
if(j==tiley)
tileR=((short)Tiles::SE);
else if(j==(tiley+yi-1))
tileR=((short)Tiles::NE);
else
tileR=((short)Tiles::E);
}
else if(j==tiley){
tileR=((short)Tiles::S);
}
else if(j==tiley+yi-1){
tileR=((short)Tiles::N);
}
else
tileR=((short)Tiles::NOWALL);
tile_map.push_back(tileR);
cout << tileR;
tileR=1;
}
}*/
for(int j=0; j < yi; j++){
for(int i=0; i < xi; i++){
if(i==0){
if(j==0)
tileR=((short)Tiles::SW);
else if(j==(yi-1))
tileR=((short)Tiles::NW);
else
tileR=((short)Tiles::W);
}
else if(i==xi-1){
if(j==0)
tileR=((short)Tiles::SE);
else if(j==(yi-1))
tileR=((short)Tiles::NE);
else
tileR=((short)Tiles::E);
}
else if(j==0){
tileR=((short)Tiles::S);
}
else if(j==yi-1){
tileR=((short)Tiles::N);
}
else
tileR=((short)Tiles::NOWALL);
cout << "before setTile\n";
cout << "["<<i+tilex<<","<<j+tiley<<"]=\n";
cout << getTile(i+tilex,j+tiley)<<"\n";
//setTile(,0,tileR);
setTile(tileR,i+tilex,j+tiley);
cout << tileR;
tileR=1;
}
}
}
//simple test platform
int Level::generateLevel(int xi, int yi, int zi)
{
x=xi;y=yi;z=zi;
cout << "\nGENERATING LEVEL\n";
for(int i=0; i < xi;i++){
for(int j=0; j < yi;j++){
for(int k=0; k < zi;k++){
short tile = rand()%13+1;
tile_map.push_back(tile);
} } }
cout << "\n\n LEVEL GENERATED \n\n";
}
Level::~Level(){
delete(tileset);
}
void Level::printLevel(){
for(int k=0; k<z;k++){
cout<<"\nLevel:"<<k<<"\n";
for(int i=0; i<y;i++){
for(int j=0; j<x;j++){
cout <<"["<<j<<i<<k<<"="<<getTile(j,i,k)<<"]";
}
cout <<"\n";
}
}
}