00001
#pragma warning(disable: 4786)
00002
00003
00004
#include <fstream>
00005
#include <string>
00006
#include <iostream>
00007
00008
#include "bsp.h"
00009
#include "level.h"
00010
#include "main.h"
00011
#include "errrors_display.h"
00012
00013
00014
using namespace std;
00015
00016
00017 typedef enum {
READING_AREA ,
READING_WALL ,
READING_UNDEF ,
READING_RED_TRAP ,
READING_BLUE_TRAP ,
READING_GREEN_TRAP ,
00018
READING_RED_BALL,
READING_BLUE_BALL,
READING_GREEN_BALL ,
READING_TIME ,
READING_PLAYER }
READING_STATE;
00019
00020
00021
00022
bool Get_Pair(
int &x,
int &y , ifstream& in);
00023
00024
00025
00026
00027
00028
00029
00030
bool Get_Pair(
int &x,
int &y , ifstream& in)
00031 {
00032
char c;
00033
00034 in >> c;
00035
if( c !=
'[' )
00036
return false;
00037 in >> x;
00038 in >> c;
00039
if( c !=
',' )
00040
return false;
00041 in >> y;
00042 in >> c;
00043
if( c !=
']' )
00044
return false;
00045
00046
return true;
00047 }
00048
00049
00050
00051
00052
00053
00054
00055 bool cLevel_Loader::Read_Area(ifstream& in,
cGame_Manager * game)
00056 {
00057
int x,y;
00058
if( !
Get_Pair( x, y , in))
00059
return false;
00060
00061
00062 cGame_World::Ecran =
cPositive_Rectangle(
cPoint2D(0,0) ,
cPoint2D(x,y));
00063
return true;
00064 }
00065
00066
00067 bool cLevel_Loader::Read_Player(ifstream& in,
cGame_Manager * game)
00068 {
00069
int x,y;
00070
if( !
Get_Pair( x, y , in))
00071
return false;
00072
00073
00074 game->
Player->
Px = x;
00075 game->
Player->
Py = y;
00076
return true;
00077 }
00078
00079
00080
00081 bool cLevel_Loader::Read_Time(ifstream& in,
cGame_Manager * game)
00082 {
00083
int time;
00084 in >> time ;
00085 game->
Nb_Seconds_For_Level = time;
00086
00087
00088
return true;
00089 }
00090
00091
00092 bool cLevel_Loader::Read_Wall(ifstream& in ,
cGame_Manager * game)
00093 {
00094
char c;
00095
int x0,y0,x1,y1;
00096
00097
if( !
Get_Pair( x0, y0 , in))
00098
return false;
00099
00100
00101 in >> c;
00102
if( c!=
'-' )
00103
return false;
00104 in >> c;
00105
if( c!=
'>' )
00106
return false;
00107
00108
if( !
Get_Pair( x1, y1 , in))
00109
return false;
00110
00111 game->
Game_World->
My_BSP.
Add_Divider(
cHV_Wall(
cPoint2D(x0,y0),
cPoint2D(x1,y1) ) );
00112
00113
return true;
00114 }
00115
00116
00117 bool cLevel_Loader::Read_Interactive_Item(Interactive_Item_Type type,ifstream& in ,
cGame_Manager * game)
00118 {
00119
int x,y;
00120
if( !
Get_Pair( x, y , in))
00121
return false;
00122
00123 game->
Add_Bonus(type,x,y);
00124
return true;
00125 }
00126
00127 bool cLevel_Loader::Read_Ennemy(Ennemy_Type type,ifstream& in ,
cGame_Manager * game)
00128 {
00129
int x,y;
00130
if( !
Get_Pair( x, y , in))
00131
return false;
00132
00133 game->
Add_Ennemy(type,x,y);
00134
return true;
00135 }
00136
00137
00138 bool cLevel_Loader::Load(
char *filename ,
cGame_Manager * game)
00139 {
00140 ifstream in;
00141
int n_line=0;
00142
char c;
00143
char line[100];
00144 string str_line;
00145
READING_STATE state =
READING_UNDEF;
00146 std::string err_message;
00147
00148 in.open( filename );
00149
if( ! in.is_open() )
00150 {
00151 cout <<
"error while opening file : " << filename <<
"\n";
00152 err_message +=
"fichier ";
00153 err_message +=filename;
00154 err_message +=
" introuvable ";
00155 Display_Fatal_Error(
hWnd , err_message.c_str() );
00156 PostQuitMessage(0);
00157
return false;
00158 }
00159
00160
while( ! in.eof() )
00161 {
00162
while( in.peek() ==
'\n' )
00163 {
00164 in.ignore(1);
00165 n_line++;
00166 }
00167
if( in.eof() )
00168
return true;
00169
00170 c= in.peek();
00171
if( c ==
'<' )
00172 {
00173 in.getline(line,100);
00174 n_line++;
00175 str_line = string(line);
00176
if( str_line ==
"<AREA>")
00177 state =
READING_AREA;
00178
else if( str_line ==
"<WALLS>")
00179 state =
READING_WALL;
00180
else if( str_line ==
"<RED_TRAP>")
00181 state =
READING_RED_TRAP;
00182
else if( str_line ==
"<BLUE_TRAP>")
00183 state =
READING_BLUE_TRAP;
00184
else if( str_line ==
"<GREEN_TRAP>")
00185 state =
READING_GREEN_TRAP;
00186
else if( str_line ==
"<RED_BALL>")
00187 state =
READING_RED_BALL;
00188
else if( str_line ==
"<BLUE_BALL>")
00189 state =
READING_BLUE_BALL;
00190
else if( str_line ==
"<GREEN_BALL>")
00191 state =
READING_GREEN_BALL;
00192
else if( str_line ==
"<TIME>")
00193 state =
READING_TIME;
00194
else if( str_line ==
"<PLAYER>")
00195 state =
READING_PLAYER;
00196
else
00197 {
00198 err_message.erase();
00199 err_message +=
"balise inconnue ";
00200 err_message += line;
00201 err_message +=
" dans le fichier ";
00202 err_message +=filename;
00203 err_message +=
"\n";
00204 Display_Fatal_Error(
hWnd , err_message.c_str());
00205 state=
READING_UNDEF;
00206 PostQuitMessage(0);
00207
return false;
00208 }
00209 }
00210
00211
switch( state)
00212 {
00213
case READING_AREA:
00214
if( !
Read_Area(in,game))
00215 {
00216
Display_Error( n_line , filename);
00217
return false;
00218 }
00219
break;
00220
case READING_WALL:
00221
if( !
Read_Wall(in,game) )
00222 {
00223
Display_Error( n_line , filename);
00224
return false;
00225 }
00226
break;
00227
case READING_RED_TRAP:
00228
if( !
Read_Interactive_Item(
RED_TRAP,in,game) )
00229 {
00230
Display_Error( n_line , filename);
00231
return false;
00232 }
00233
break;
00234
case READING_BLUE_TRAP:
00235
if( !
Read_Interactive_Item(
BLUE_TRAP,in,game) )
00236 {
00237
Display_Error( n_line , filename);
00238
return false;
00239 }
00240
break;
00241
case READING_GREEN_TRAP:
00242
if( !
Read_Interactive_Item(
GREEN_TRAP,in,game) )
00243 {
00244
Display_Error( n_line , filename);
00245
return false;
00246 }
00247
break;
00248
case READING_RED_BALL:
00249
if( !
Read_Ennemy(
RED_BALL,in,game) )
00250 {
00251
Display_Error( n_line , filename);
00252
return false;
00253 }
00254
break;
00255
case READING_BLUE_BALL:
00256
if( !
Read_Ennemy(
BLUE_BALL,in,game) )
00257 {
00258
Display_Error( n_line , filename);
00259
return false;
00260 }
00261
break;
00262
case READING_GREEN_BALL:
00263
if( !
Read_Ennemy(
GREEN_BALL,in,game) )
00264 {
00265
Display_Error( n_line , filename);
00266
return false;
00267 }
00268
break;
00269
case READING_TIME:
00270
if( !
Read_Time(in,game) )
00271 {
00272
Display_Error( n_line , filename);
00273
return false;
00274 }
00275
break;
00276
case READING_PLAYER:
00277
if( !
Read_Player(in,game) )
00278 {
00279
Display_Error( n_line , filename);
00280
return false;
00281 }
00282
break;
00283
default:
00284 cout <<
"case READING_UNDEF: \n";
00285 cout <<
"error while reading " << filename <<
"\n";
00286 err_message.erase();
00287 err_message +=
"balise inconnue dans le fichier ";
00288 err_message +=filename;
00289 err_message +=
"\n";
00290 Display_Fatal_Error(
hWnd , err_message.c_str());
00291 PostQuitMessage(0);
00292
return false;
00293 }
00294 }
00295
00296
return true;
00297 }
00298
00299
00300
00301 void cLevel_Loader::Display_Error(
int n_line ,
char * file)
00302 {
00303
00304
00305 cout <<
"ERROR while reading file : " << file <<
" at line " << n_line <<
"\n";
00306 }