00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "fgentitylist.h"
00030
00031 FGEntityList::FGEntityList(){
00032 node_count = 0;
00033 first = NULL;
00034 index = 0;
00035 }
00036
00037 FGEntityList::~FGEntityList(){
00038
00039 }
00040
00041 void FGEntityList::add_fgentity(FGEntity *fgentity){
00042 node *new_node = new node;
00043 new_node->fgentity = fgentity;
00044 new_node->next = NULL;
00045 new_node->index = fgentity->get_index();
00046 add_node(new_node);
00047 fgentity->set_fgentity_list(this);
00048 node_count++;
00049 index++;
00050 }
00051
00052 void FGEntityList::add_node(node *new_node){
00053 node *walk = first;
00054 if(new_node != NULL){
00055 if(first == NULL){
00056 first = new_node;
00057 }else{
00058 while(walk->next != NULL) walk = walk->next;
00059 walk->next = new_node;
00060 }
00061 }
00062 }
00063
00064 void FGEntityList::remove_node(node *bye_node){
00065 node *walk = first;
00066 cerr << "Removing node " << bye_node->fgentity->get_name() << endl;
00067 if(first == bye_node){
00068 first = walk->next;
00069 delete walk;
00070 }else{
00071 while(walk->next != bye_node) walk = walk->next;
00072 walk->next = bye_node->next;
00073 delete bye_node;
00074 }
00075 node_count--;
00076 }
00077
00078 void FGEntityList::set_bgentity(BGEntity *bge){
00079 bgentity = bge;
00080 }
00081
00082 void FGEntityList::set_background_draw(BGDraw &bgd){
00083 bgdraw = bgd;
00084 }
00085
00086 void FGEntityList::set_screen(Screen &s){
00087 screen = s;
00088 }
00089
00090 void FGEntityList::check_collisions(){
00091 node *walk = first;
00092 node *test_node;
00093 SDL_Rect box, testbox;
00094
00095 while(walk->next != NULL){
00096 box = walk->fgentity->get_box();
00097 test_node = walk->next;
00098 while(test_node->next != NULL){
00099 testbox = test_node->fgentity->get_box();
00100
00101 if((box.x < (testbox.x + testbox.w)) && ((box.x + box.w) > testbox.x))
00102 walk->fgentity->reverse();
00103 if((box.y < (testbox.y + testbox.h)) && ((box.y + box.h) > testbox.y))
00104 walk->fgentity->reverse();
00105 test_node = test_node->next;
00106 }
00107 walk = walk->next;
00108 }
00109
00110 }
00111
00112 void FGEntityList::test(int dir){
00113 node *walk = first;
00114 node *holdnode;
00115 int i, retcode;
00116 int count = 0;
00117 SDL_Rect fgr;
00118 SDL_Rect pr;
00119 int tsize = pinfo->tilesize;
00120 int xin, yin;
00121 int unmove = 0;
00122 player.update();
00123 if(dir != -1){
00124
00125 if(check_bg_collisions(dir, player.current_box) && check_fg_walkable(dir, pinfo->paul_x, pinfo->paul_y)){
00126 bgdraw.initialize();
00127 player.move(dir);
00128
00129 pr = makerect(pinfo->paul_x, pinfo->paul_y, pinfo->tilesize, pinfo->tilesize);
00130
00131 while(count < node_count){
00132
00133 fgr = walk->fgentity->current_box;
00134
00135 i = walk->fgentity->get_index();
00136
00137 xin = yin = 0;
00138
00139 if (pr.x >= fgr.x) {
00140 if(pr.x - fgr.x <= tsize){ xin = 1;}
00141 }else{
00142 if(fgr.x - pr.x <= tsize){ xin = 1;}
00143 }
00144 if (pr.y >= fgr.y) {
00145 if(pr.y - fgr.y <= tsize){ yin = 1;}
00146 }else{
00147 if(fgr.y - pr.y <= tsize){ yin = 1;}
00148 }
00149 if (xin && yin){
00150 cerr << "Collision with " << walk->fgentity->get_name() << endl;
00151 retcode = walk->fgentity->activate();
00152 if(retcode) {
00153 holdnode = walk->next;
00154 remove_node(walk);
00155 walk = holdnode;
00156 }else{
00157 walk = walk->next;
00158 }
00159 }else{
00160 walk = walk->next;
00161 }
00162 count++;
00163 }
00164 }else{
00165 bgdraw.initialize();
00166 player.static_redraw_cycle(dir);
00167 }
00168 }else{
00169 bgdraw.initialize();
00170 player.static_redraw(dir);
00171 }
00172
00173
00174 count = 0;
00175 walk = first;
00176 while(count < node_count){
00177 i = walk->fgentity->get_index();
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202 walk->fgentity->update();
00203 if (walk->fgentity->is_onscreen()){
00204
00205 if (walk->fgentity->animate){ walk->fgentity->activate(); }
00206 screen.blit(walk->fgentity->tiledata.tile[walk->fgentity->current_frame],
00207 walk->fgentity->current_box);
00208 }
00209 walk = walk->next;
00210 count++;
00211 }
00212 player.overlay();
00213 }
00214
00215 void FGEntityList::set_player(Player &p){
00216 player = p;
00217 }
00218
00219 void FGEntityList::set_paul_info(paul_info *p){
00220 pinfo = p;
00221 }
00222
00223
00224
00225
00226
00227
00228 int FGEntityList::check_bg_collisions(int dir, SDL_Rect char_box){
00229 int x, y, tile_x, tile_y;
00230 int move = 1;
00231 int function_x, function_y;
00232 x = y = -1;
00233 tile_x = char_box.x/pinfo->tilesize;
00234 tile_y = char_box.y/pinfo->tilesize;
00235
00236 switch(dir){
00237 case 0: y = char_box.y - MOVE_PER_FRAME;
00238 function_x = 1;
00239 break;
00240 case 1: y = char_box.y - MOVE_PER_FRAME;
00241 x = char_box.x + MOVE_PER_FRAME + pinfo->tilesize;
00242 function_x = 0; function_y = 0;
00243 break;
00244 case 2: x = char_box.x + MOVE_PER_FRAME + pinfo->tilesize;
00245 function_y = 0;
00246 break;
00247 case 3: y = char_box.y + MOVE_PER_FRAME + pinfo->tilesize;
00248 x = char_box.x + MOVE_PER_FRAME + pinfo->tilesize;
00249 function_x = 0; function_y = 0;
00250 break;
00251 case 4: y = char_box.y + MOVE_PER_FRAME + pinfo->tilesize;
00252 function_x = 1;
00253 break;
00254 case 5: y = char_box.y + MOVE_PER_FRAME + pinfo->tilesize;
00255 x = char_box.x - MOVE_PER_FRAME;
00256 function_x = 1; function_y = 1;
00257 break;
00258 case 6: x = char_box.x - MOVE_PER_FRAME;
00259 function_y = 1;
00260 break;
00261 case 7: y = char_box.y - MOVE_PER_FRAME;
00262 x = char_box.x - MOVE_PER_FRAME;
00263 function_x = 1; function_y = 1;
00264 break;
00265 }
00266 if(x != -1) tile_x = x/pinfo->tilesize;
00267 if(y != -1) tile_y = y/pinfo->tilesize;
00268
00269 if(y != -1){
00270
00271
00272 if(!bgentity[(tile_y * pinfo->map_width_in_tiles)+tile_x].is_walkable()){
00273 move = 0;
00274 }
00275
00276
00277
00278 if((x%pinfo->tilesize) > 0){
00279 if(function_x){tile_x++;}else{tile_x--;}
00280 if(!bgentity[(tile_y * pinfo->map_width_in_tiles)+tile_x].is_walkable()){
00281 move = 0;
00282 }
00283 }
00284 }
00285 if(x != -1){
00286
00287
00288 if(!bgentity[(tile_y * pinfo->map_width_in_tiles)+tile_x].is_walkable()){
00289 move = 0;
00290 }
00291
00292
00293
00294 if((y%pinfo->tilesize) > 0){
00295 if(function_y){tile_y++;}else{tile_y--;}
00296 if(!bgentity[(tile_y * pinfo->map_width_in_tiles)+tile_x].is_walkable()){
00297 move = 0;
00298 }
00299 }
00300 }
00301 return(move);
00302 }
00303
00304
00305 int FGEntityList::check_fg_walkable(int dir, int prx, int pry){
00306 int i;
00307 node *walk = first;
00308 int move = 1;
00309 int count = 0;
00310 SDL_Rect fgr;
00311 SDL_Rect pr;
00312 int tsize = pinfo->tilesize;
00313 int xin, yin;
00314
00315 switch(dir){
00316 case 0: pry = pry - MOVE_PER_FRAME;
00317 break;
00318 case 1: pry = pry - MOVE_PER_FRAME;
00319 prx = prx + MOVE_PER_FRAME;
00320 break;
00321 case 2: prx = prx + MOVE_PER_FRAME;
00322 break;
00323 case 3: pry = pry + MOVE_PER_FRAME;
00324 prx = prx + MOVE_PER_FRAME;
00325 break;
00326 case 4: pry = pry + MOVE_PER_FRAME;
00327 break;
00328 case 5: pry = pry + MOVE_PER_FRAME;
00329 prx = prx - MOVE_PER_FRAME;
00330 break;
00331 case 6: prx = prx - MOVE_PER_FRAME;
00332 break;
00333 case 7: pry = pry - MOVE_PER_FRAME;
00334 prx = prx - MOVE_PER_FRAME;
00335 break;
00336 }
00337
00338 pr = makerect(prx, pry, pinfo->tilesize, pinfo->tilesize);
00339 while(count < node_count){
00340
00341 fgr = walk->fgentity->current_box;
00342 i = walk->fgentity->get_index();
00343 xin = yin = 0;
00344
00345 if (pr.x >= fgr.x) {
00346 if(pr.x - fgr.x <= tsize){ xin = 1;}
00347 }else{
00348 if(fgr.x - pr.x <= tsize){ xin = 1;}
00349 }
00350 if (pr.y >= fgr.y) {
00351 if(pr.y - fgr.y <= tsize){ yin = 1;}
00352 }else{
00353 if(fgr.y - pr.y <= tsize){ yin = 1;}
00354 }
00355
00356 if (xin && yin && i >= 197 && i <= 200 && (!walk->fgentity->animate || walk->fgentity->current_frame > 0)){
00357 i = walk->fgentity->activate();
00358 }
00359 if (xin && yin && !walk->fgentity->walkable){
00360 move = 0;
00361 }
00362 walk = walk->next;
00363 count++;
00364 }
00365 return(move);
00366 }