123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- #include "box.hpp"
- void Box::refresh(b_list& bullets, b_list& bombs, e_list& enemies, player& player1, int &score, boss& boss1,w_vec &walls, b_list& powerups,r_list& rockets)
- {
- clear(); //Empty matrix
- upload(bullets, bombs, enemies, player1, boss1, walls, powerups, rockets); //writes id values on matrix
- kill(bullets, bombs, enemies, score, boss1, walls, powerups, player1, rockets); //evaluates interactions and removes what should be removed
- clear(); //empty again
- upload(bullets, bombs, enemies, player1, boss1, walls, powerups, rockets); //rewrites survived game objects' id
- }
- void Box::print() //this method is used for debugging purposes, not in game
- {
- for(int k=0; k<R; k++)
- {
- for(int l=0; l<C; l++)
- cout << matrix[k][l] << " ";
- cout << endl;
- }
- cout << endl;
- }
- void Box::clear()
- {
- for(int i=0; i<R; i++)
- for(int j=0; j<C; j++)
- matrix[i][j]=0;
- }
- void Box::upload(b_list & bullets, b_list& bombs, e_list& enemies, player& player1, boss& boss1, w_vec &walls, b_list & powerups,r_list& rockets) //writes on the box the IDs of all game objects (even if superimposed). Also uploads curses's window
- {
- e_list::const_iterator e_end = enemies.end();
- for(e_list::iterator it=enemies.begin(); it!=e_end; ++it)
- if(it->alive){
- attron(COLOR_PAIR(2));
- mvaddch(it->y,it->x,ENEMY_SPRITE);
- matrix[it->y][it->x]+=it->id;
- }
-
- if(!bullets.empty())
- for(b_list::iterator it=bullets.begin(); it!=bullets.end(); ++it){
- attron(COLOR_PAIR(3));
- mvaddch(it->y,it->x,BULLET_SPRITE);
- matrix[it->y][it->x]+=it->id;
- }
-
- if(!rockets.empty())
- for(r_list::iterator it=rockets.begin(); it!=rockets.end(); ++it){
- attron(COLOR_PAIR(4));
- mvaddch(it->y,it->x,ROCKET_SPRITE);
- matrix[it->y][it->x]+=it->id;
- }
- if(!bombs.empty())
- for(b_list::iterator it=bombs.begin(); it!=bombs.end(); ++it){
- attron(COLOR_PAIR(6));
- mvaddch(it->y,it->x,BOMB_SPRITE);
- matrix[it->y][it->x]+=it->id;
- }
-
- if(!powerups.empty())
- for(b_list::iterator it=powerups.begin(); it!=powerups.end(); ++it){
- if(it->id==64){
- attron(COLOR_PAIR(3));
- mvaddch(it->y,it->x,POWERUP1_SPRITE);
- }
- else if(it->id==128){
- attron(COLOR_PAIR(1));
- mvaddch(it->y,it->x,POWERUP2_SPRITE);
- }
- else{
- attron(COLOR_PAIR(4));
- mvaddch(it->y,it->x,POWERUP3_SPRITE);
- }
- matrix[it->y][it->x]+=it->id;
- }
-
- for(int i=player1.x; i<player1.x+player1.length; ++i){
- attron(COLOR_PAIR(1));
- mvaddch(player1.y,i,PLAYER_SPRITE);
- matrix[player1.y][i]+=player1.id;
- }
-
- for(unsigned int i=0;i<walls.size();i++)
- for(int k=0;k<walls[i].height;k++)
- for(int j=0;j<walls[i].width;j++)
- if(walls[i].health[k][j]>0){
- attron(COLOR_PAIR(4));
- if(walls[i].health[k][j]==1)
- attron(COLOR_PAIR(2));
- mvaddch(walls[i].y+k,walls[i].x+j,WALL_SPRITE);
- matrix[walls[i].y+k][walls[i].x+j]+=walls[i].id;
- }
-
- if(boss1.alive)
- for(int i=boss1.y; i<boss1.y+boss1.height; i++)
- for(int j=boss1.x; j<boss1.x+boss1.width; j++){
- if(boss1.health>.5*boss1.healthmax)
- attron(COLOR_PAIR(5));
- else if(boss1.health>.25*boss1.healthmax)
- attron(COLOR_PAIR(3));
- else
- attron(COLOR_PAIR(2));
- mvaddch(i,j,boss1.picture[i-boss1.y][j-boss1.x]);
- matrix[i][j]+=boss1.id;
- }
-
- }
- void Box::kill(b_list & bullets, b_list& bombs, e_list& enemies, int &score, boss& boss1, w_vec &walls, b_list & powerups, player& player1,r_list& rockets) //controls what changes the box's elements must undergo
- {
-
- for(int i=0; i<R; i++)
- for(int j=0; j<C; j++)
- {
- if(i==0 || i==R-1)
- switch(matrix[i][j]) //destroys bullets and bombs at box's limits
- { //DESTRUCTION SHOULD HAPPEN AFTER THEY'VE BEEN PRINTED ONCE!!
- case 2:
- case 8:
- case 64:
- case 128:
- case 256:
- case 512:
- destroy(i,j,bullets,bombs,enemies,powerups,rockets);
- break;
- default:
- break;
- }
- if(!boss1.alive && matrix[i][j]==16) destroy(i,j,bullets,bombs,enemies,powerups,rockets);
-
- switch (matrix[i][j]) //id player=1 bullet=2 enemy=4 bomb=8 boss=16 wall=32 powerup+=64, powerup-=128, rocket=256, powerupX=512
- {
- case 2: //destruction cases: 6=enemy+bullet; 10=bomb+bullet; 14=bomb+enemy+bullet; and so on...
- if(i!=0)
- if(matrix[i-1][j]==8) //to make the bullets less 'transparent'...
- {
- destroy(i, j, bullets,bombs, enemies,powerups,rockets);
- destroy(i-1, j, bullets,bombs,enemies,powerups,rockets);
- score+=50;
- }
- break;
- case 6: //enemy+bullet
- score+=100;
- destroy(i,j,bullets,bombs,enemies,powerups,rockets);
- if((double)rand()/RAND_MAX<(double)AVERAGE_DROP/ENEMY_NUM) //a powerup is dropped (AVERAGE_DROP is the mean number of powerups dropped per play in Normal mode)
- {
- if((double)rand()/RAND_MAX<1./POWERUP_RATIO){ //1 powerup out of POWERUP_RATIO is either a "-" (with ID 128 instead of 64) or a "X"
- if((double)rand()/RAND_MAX<1./ROCKET_RATIO){ //1 powerup out of ROCKET_RATIO*POWERUP_RATIO is a "X" (ID 512)
- powerup newpowerup(j,i+1,512);
- powerups.push_back(newpowerup);
- }
- else{
- powerup newpowerup(j,i+1,128);
- powerups.push_back(newpowerup);
- }
- }
- else{
- powerup newpowerup(j,i+1); //otherwise, it is a "+"
- powerups.push_back(newpowerup);
- }
-
- }
- break;
- case 10: //bomb+bullet
- score+=50;
- destroy(i,j,bullets,bombs,enemies,powerups,rockets);
- break;
- case 14: //bomb+enemy+bullet
- score+=150;
- destroy(i,j,bullets,bombs,enemies,powerups,rockets);
- break;
- case 18: //boss+bullet
- case 272:
- if(j>=boss1.x+boss1.width/2-2 && j<=boss1.x+boss1.width/2+2)
- boss1.health--;
- break;
- case 34: //wall+bullet
- case 40: //wall+bomb
- case 288: //wall+rocket
- destroy(i,j,bullets,bombs,enemies,powerups,rockets);
- for(unsigned int k=0;k<walls.size();k++)
- if(j>=walls[k].x && j<=walls[k].x+walls[k].width && i>=walls[k].y && i<=walls[k].y+walls[k].height) //finds out what wall has been hit
- walls[k].health[i-walls[k].y][j-walls[k].x]--; //subtracts 1 HP to the hit wall's correct health matrix's element
- break;
- case 65: //"+" powerup+player
- if(player1.weaponclass<MAX_WEAPONCLASS)
- player1.weaponclass++;
- destroy(i,j,bullets,bombs,enemies,powerups,rockets);
- break;
- case 68: //"+" powerup+enemy
- case 72: //"+" powerup+bomb
- case 80: //"+" powerup+boss
- case 132: //"-" powerup+enemy [etc.]
- case 136:
- case 144:
- case 516: //"X" powerup+enemy [etc.]
- case 520:
- case 528:
- destroy(i,j,bullets,bombs,enemies,powerups,rockets);
- break;
- case 129: //"-" powerup+player
- if(player1.length>2){
- mvaddch(player1.y,player1.x+player1.length-1,' ');
- player1.length--;
- }
- destroy(i,j,bullets,bombs,enemies,powerups,rockets);
- break;
- case 260: //enemy+rocket
- case 264: //bomb+rocket
- destroy(i,j,bullets,bombs,enemies,powerups,rockets);
- break;
- case 513: //player+"X" powerup
- player1.rocketlauncher=TRUE;
- destroy(i,j,bullets,bombs,enemies,powerups,rockets);
- break;
- }
- }
- }
- void Box::destroy(int Y, int X, b_list & bullets, b_list& bombs, e_list& enemies, b_list & powerups,r_list& rockets) //destroys any bomb/enemy/bullet/powerup/rocket found in matrix[Y][X]
- {
- b_list::iterator it;
- r_list::iterator itr;
-
- if(!bullets.empty()){
- it=bullets.begin(); //we could use remove_if() if i could just think of a way...
- while(it!=bullets.end())
- {
- if(it->x==X && it->y==Y)
- {
- mvaddch(it->y,it->x,' ');
- it = bullets.erase(it);
- break;
- } else ++it;
- }
- }
- if(!rockets.empty()){
- itr=rockets.begin();
- while(itr!=rockets.end())
- {
- if(itr->x==X && itr->y==Y)
- {
- mvaddch(itr->y,itr->x,' ');
- itr = rockets.erase(itr);
- break;
- } else ++itr;
- }
- }
- if(!bombs.empty()){
- it=bombs.begin();
- while(it!=bombs.end())
- {
- if(it->x==X && it->y==Y)
- {
- mvaddch(it->y,it->x,' ');
- it = bombs.erase(it);
- break;
- } else ++it;
- }
- }
-
- if(!powerups.empty()){
- it=powerups.begin();
- while(it!=powerups.end())
- {
- if(it->x==X && it->y==Y)
- {
- mvaddch(it->y,it->x,' ');
- it = powerups.erase(it);
- break;
- }else ++it;
- }
- }
- e_list::const_iterator e_end = enemies.end();
-
- for(e_list::iterator it=enemies.begin(); it!=e_end; ++it) //this cycle destroys an enemy in X,Y (quite unefficiently?)
- if(it->x==X && it->y==Y)
- {
- it->alive=false;
- break;
- }
- }
|