Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Diff: RosenEngine/RosenEngine.cpp
- Revision:
- 35:3341f2bd0408
- Parent:
- 34:6d0786582d81
- Child:
- 36:c25417f0d150
diff -r 6d0786582d81 -r 3341f2bd0408 RosenEngine/RosenEngine.cpp --- a/RosenEngine/RosenEngine.cpp Tue Apr 30 05:03:15 2019 +0000 +++ b/RosenEngine/RosenEngine.cpp Fri May 03 09:41:53 2019 +0000 @@ -34,6 +34,7 @@ _xjoystick = mapped_coord.x; _yjoystick = mapped_coord.y; _d = pad.get_direction(); + wait(0.1); // printf("_xjoystick ,_yjoystick = %f , %f\n",_xjoystick, _yjoystick); } @@ -64,72 +65,43 @@ void RosenEngine::update(Gamepad &pad) { - /// _enemy.update_seeker(ship_xpos, ship_ypos); + _enemy.update_seeker(ship_xpos, ship_ypos); _enemy.update_shooter(ship_xpos, ship_ypos); + //printf("update_shooter\n"); _enemy.update_shw(); if(_shipno == 0) { _ship.update_ship(_xjoystick,_yjoystick); _weapons.update(); + kestrelw_seeker_collision(pad); } + //printf("if shipno == 0\n"); if(_shipno == 1 && A == false) { _ship.update_ship(_xjoystick,_yjoystick); _weapons.update(); } + //printf("if shipno == 1\n"); if(_shipno == 2) { _ship.update_ship(_xjoystick,_yjoystick); _weapons.update(); } - if(shooter_weapon_collision(3) == true) { - _health.update(1,pad); - pad.tone(500,0.05); - wait(0.05); - } - /* - if(seeker_ship_collision() == true) { - _health.update(1,pad); - _enemy.reset_seeker(); - pad.tone(500,0.05); - wait(0.05); - }*/ - if(enemy_projectile_collision() == true) { - _enemy.reset_seeker(); - pad.tone(1000,0.05); - wait(0.05); - pad.tone(1500,0.05); - wait(0.05); - } - Vector2D sh_col = shooter_projectile_collision(); // first vlaue is 1 if col occured and 0 otherwise. second is the shooter that actually collided - if(sh_col.x == 1){ - _health.update(2,pad); - pad.tone(500,0.05); - } - - // printf("collision check complete"); - - // warning sound - Vector2D hp = _health.get_hp(); - int health = hp.x; - if(_shipno == 0 || _shipno == 1) { - if(health <= 2 ) { - pad.tone(1300,0.05); - wait(0.05); - } - } else if(_shipno == 2) { - if(health <= 1 ) { - pad.tone(2000,0.05); - wait(0.05); - } - } - // printf("health = %d||shipno = %d",health,_shipno); + // test(); + shooter_ship_collision(pad); + seeker_ship_collision(pad); + shooterw_ship_collision(pad); + imperionw_seeker_collision(pad); + kestrelw_shooter_collision(pad); + imperionw_shooter_collision(pad); } void RosenEngine::get_pos() { Vector2D ship_pos = _ship.get_pos(); ship_xpos = ship_pos.x; ship_ypos = ship_pos.y; - _shooter1_pos = _enemy.get_sh1pos(); - _shooter2_pos = _enemy.get_sh2pos(); - _shooter3_pos = _enemy.get_sh3pos(); + + _shooter1_pos = _enemy.get_shooter1pos(); + _shooter2_pos = _enemy.get_shooter2pos(); + _shooter3_pos = _enemy.get_shooter3pos(); + _weapons.init(ship_xpos, ship_ypos, ship_width); _ycursor = _menu.get_ycursor(); @@ -162,319 +134,230 @@ _menu.update(_d); _menu.disp_ships(lcd); } -bool RosenEngine::seeker_ship_collision() -{ - // Vector2D ship_pos = _ship.get_pos(); - Vector2D seeker_pos = _enemy.get_seekerpos(); - int seeker_xpos = seeker_pos.x, seeker_ypos = seeker_pos.y; - int sxcol = 0; - int sycol = 0; - int ship_x[9],ship_y[6],seeker_x[9],seeker_y[6]; - // printf("shipx = %d, shipy = %d\n",ship_xpos,ship_ypos); - // create an array of all x positions for the ship sprite i.e along its width (ship_x) - for(int cx = 0; cx<=9; cx=cx+1) { - ship_x[cx] = ship_xpos + cx; - seeker_x[cx] = seeker_xpos + cx; - // printf("ship_x = %d, seeker_x = %d\n", ship_x[cx], seeker_x[cx]); - } - - // create an array of all y positions for the ship sprite i.e along its height (ship_y) - for(int cy = 0; cy<=6; cy=cy+1) { - ship_y[cy] = (ship_ypos ) + cy ; - seeker_y[cy] = seeker_ypos + cy; - // printf("ship_y = %d, seeker_y = %d\n", ship_y[cy], seeker_y[cy]); - } - // check all values of ship position against all values of seekers x position - for(int nx = 0; nx<=9; nx=nx+1) { - for(int mx = 0; mx<=9; mx=mx+1) { - if(ship_x[nx] == seeker_x[mx]) { - sxcol = 1; - } - } - } - for(int ny = 0; ny<=6; ny=ny+1) { - for(int my = 0; my<=6; my=my+1) { - if(ship_y[ny] == seeker_y[my]) { - sycol = 1; - } - } - } - - printf("sxcol = %d, sycol = %d\n", sxcol, sycol); //************brocken, function stops without it - if(sxcol == 1 && sycol == 1) { - return true; - } else { - return false; - } -} -Vector2D RosenEngine::shooter_projectile_collision() -{ - int shooter1_x[11],shooter1_y[10],shooter2_x[11],shooter2_y[10],shooter3_x[11],shooter3_y[10], lazer_x[3]; - int wx1col = 0; - int wx2col = 0; - int wx3col = 0; - int wy1col = 0; - int wy2col = 0; - int wy3col = 0; - float shnum; // the shooter that was hit - float coloc; // if collision coocured 1, else 0 - - // get shooters x any y values and put into arrays - for(int cx = 0; cx<=11; cx=cx+1) { - shooter1_x[cx]= _shooter1_pos.x + cx; - } - for(int cy = 0; cy<=10; cy=cy+1) { - shooter1_y[cy]= _shooter1_pos.y + cy; - } - for(int cx = 0; cx<=11; cx=cx+1) { - shooter2_x[cx]= _shooter2_pos.x + cx; - } - for(int cy = 0; cy<=10; cy=cy+1) { - shooter2_y[cy]= _shooter2_pos.y + cy; - } - for(int cx = 0; cx<=11; cx=cx+1) { - shooter3_x[cx]= _shooter3_pos.x + cx; - } - for(int cy = 0; cy<=10; cy=cy+1) { - shooter3_y[cy]= _shooter3_pos.y + cy; - } - - if(_shipno == 0) { - Vector2D missle_pos = _weapons.get_pos(_shipno); - - for(int cx = 0; cx<=11; cx=cx+1) { - if(shooter1_x[cx] == missle_pos.x) { - wx1col = 1; - } - if(shooter2_x[cx] == missle_pos.x) { - wx2col = 1; - } - if(shooter3_x[cx] == missle_pos.x) { - wx3col = 1; - } - // printf("shooter1_x = %d,shooter2_x = %d,shooter3_x = %d,missle_pos.x = %f\n",shooter1_x[cx],shooter2_x[cx],shooter3_x[cx],missle_pos.x); - // printf("wx1col = %d,wx2col = %d,wx3col = %d\n",wx1col,wx2col,wx3col); - } - - for(int cy = 0; cy<=10; cy=cy+1) { - if(shooter1_y[cy] == missle_pos.y - 5) { - wy1col = 1; - } - if(shooter2_y[cy] == missle_pos.y - 5) { - wy2col = 1; - } - if(shooter3_y[cy] == missle_pos.y - 5) { - wy3col = 1; - } - //printf("shooter1_y = %d,shooter2_y = %d,shooter3_y = %d,missle_pos.y = %f\n",shooter1_y[cy],shooter2_y[cy],shooter3_y[cy],missle_pos.y); - //printf("wy1col = %d,wy2col = %d,wy3col = %d\n",wy1col,wy2col,wy3col); - // it seems to be iffy move the ship to the right to fire at stationary seekers to test uncomment draw seeker - } - if(wx1col == 1 && wy1col == 1) { - _weapons.set_pos(ship_xpos,ship_ypos); // reset missle after contact - shnum = 1; - coloc = 1; - } else if(wx2col == 1 && wy2col == 1) { - _weapons.set_pos(ship_xpos,ship_ypos); // reset missle after contact - shnum = 2; - coloc = 1; - } else if(wx3col == 1 && wy3col == 1) { - _weapons.set_pos(ship_xpos,ship_ypos); // reset missle after contact - shnum = 3; - coloc = 1; - } else { - shnum = 0; - coloc = 0; - } - - printf("shnum = %f, coloc = %f\n",shnum,coloc); - } - - - // imperion lazer - if(_shipno == 1) { - for(int cx = 0; cx<=3; cx=cx+1) { - lazer_x[cx] = (ship_xpos + 2) + cx; - } - // ony register collision if seeker is above ship - if(_shooter1_pos.y + 6 < ship_ypos) { - // check all values of ship position against all values of seekers x position - for(int nx = 0; nx<=3; nx=nx+1) { - for(int mx = 0; mx<=11; mx=mx+1) { - if(lazer_x[nx] == shooter1_x[mx]) { - wx1col = 1; - // printf("lazer_x = %d,shooter1_x = %d\n",lazer_x[nx],shooter1_x[mx]); - } - } - } - } - if(_shooter2_pos.y + 6 < ship_ypos) { - // check all values of ship position against all values of seekers x position - for(int nx = 0; nx<=3; nx=nx+1) { - for(int mx = 0; mx<=11; mx=mx+1) { - if(lazer_x[nx] == shooter2_x[mx]) { - wx2col = 1; - // printf("lazer_x = %d,shooter2_x = %d\n",lazer_x[nx],shooter2_x[mx]); - } - } - } - } - if(_shooter3_pos.y + 6 < ship_ypos) { - // check all values of ship position against all values of seekers x position - for(int nx = 0; nx<=3; nx=nx+1) { - for(int mx = 0; mx<=11; mx=mx+1) { - if(lazer_x[nx] == shooter3_x[mx]) { - wx3col = 1; - // printf("lazer_x = %d,shooter3x = %d\n",lazer_x[nx],shooter3_x[mx]); - } - } - } - } - if(wx1col == 1 && A == true) { - shnum = 1; - coloc = 1; - } else if(wx2col == 1 && A == true) { - shnum = 2; - coloc = 1; - } else if(wx3col == 1 && A == true) { - shnum = 3; - coloc = 1; - } else { - shnum = 0; - coloc = 0; - } - } - if(_shipno == 2) { - shnum = 0; - coloc = 0; - } - return {coloc,shnum}; -} -bool RosenEngine::enemy_projectile_collision() -{ - Vector2D seeker_pos = _enemy.get_seekerpos(); - int lazer_x[3],seeker_x[9],seeker_y[38]; - int wxcol = 0; - int wycol = 0; - - // get seekers x any y values and put into arrays - for(int cx = 0; cx<=9; cx=cx+1) { - seeker_x[cx] = seeker_pos.x + cx; - } - for(int cy = 0; cy<=6; cy=cy+1) { - seeker_y[cy] = seeker_pos.y + cy; - } - - // kestrel artemis missle - if(_shipno == 0) { - Vector2D missle_pos = _weapons.get_pos(_shipno); - - for(int cx = 0; cx<=9; cx=cx+1) { - seeker_x[cx] = seeker_pos.x + cx; - if(seeker_x[cx] == missle_pos.x) { - // printf("seeker_xpos = %f, missle_xpos = %f\n",seeker_pos.x,missle_pos.x); - wxcol = 1; - } - } - for(int cy = 0; cy<=7; cy=cy+1) { - seeker_y[cy] = seeker_pos.y + cy; - if(seeker_y[cy] == missle_pos.y + 4) { - // printf("seeker_ypos = %f, missle_ypos = %f\n",seeker_pos.y,missle_pos.y); - wycol = 1; - } - } - if(wxcol == 1 && wycol == 1) { - _weapons.set_pos(ship_xpos,ship_ypos); // reset missle after contact - return true; - } - } - - -// imperion lazer - if(_shipno == 1) { - for(int cx = 0; cx<=3; cx=cx+1) { - lazer_x[cx] = (ship_xpos + 2) + cx; - } - // ony register collision if seeker is above ship - if(seeker_pos.y + 6 < ship_ypos) { - // check all values of ship position against all values of seekers x position - for(int nx = 0; nx<=3; nx=nx+1) { - for(int mx = 0; mx<=9; mx=mx+1) { - if(lazer_x[nx] == seeker_x[mx]) { - wxcol = 1; - // printf("lazer_x = %d,seeker_x = %f\n",lazer_x[nx],seeker_x[mx]); - } - } - } - } - if(wxcol == 1 && A == true) { - return true; - } else { - return false; - } - } - if(_shipno == 2) { - return false; - } -} -bool RosenEngine::shooter_weapon_collision(int sh_no) -{ - Vector2D shw1_pos = _enemy.get_sh1pos(); - Vector2D shw2_pos = _enemy.get_sh2pos(); - Vector2D shw3_pos = _enemy.get_sh3pos(); - - bool shw1xcol = false; - bool shw1ycol = false; - bool shw2xcol = false; - bool shw2ycol = false; - bool shw3xcol = false; - bool shw3ycol = false; - - int ship_x[9]; - int ship_y[6]; - - for(int cx = 0; cx<=9; cx=cx+1) { - ship_x[cx] = ship_xpos + cx; - if(ship_x[cx] == shw1_pos.x) { - shw1xcol = true; - } - if(ship_x[cx] == shw2_pos.x) { - shw2xcol = true; - } - if(ship_x[cx] == shw3_pos.x) { - shw3xcol = true; - } - } - for(int cy = 0; cy<=6; cy=cy+1) { - ship_y[cy] = (ship_ypos ) + cy ; - if(ship_y[cy] == shw1_pos.y) { - shw1ycol = true; - } - if(ship_y[cy] == shw2_pos.y) { - shw2ycol = true; - } - if(ship_y[cy] == shw3_pos.y) { - shw3ycol = true; - } - } - if(sh_no == 1) { - shw2xcol = false; - shw2ycol = false; - shw3xcol = false; - shw3ycol = false; - } - if(sh_no == 2) { - shw3xcol = false; - shw3ycol = false; - } - - if(shw1xcol && shw1ycol == true || shw2xcol && shw2ycol == true || shw3xcol && shw3ycol == true) { - return true; - } else { - return false; - } -} void RosenEngine::score(int points) { _score = _score + points; +} +bool RosenEngine::check_collision(int xpos1, int ypos1,int width1,int height1,int xpos2, int ypos2,int width2,int height2) +{ + // Create arrays of all positions with appropriate height and length + int xpos1_array[width1]; + int ypos1_array[height1]; + int xpos2_array[width2]; + int ypos2_array[height2]; + bool xcol = false; + bool ycol = false; + bool col = false; + //printf("variables declared\n"); + // Loop through both height and width to get a 2D aray of the sprites position + for(int cx = 0; cx<width1; cx=cx+1) { + xpos1_array[cx]= xpos1 + cx; + for(int nx = 0; nx<width2; nx=nx+1) { + xpos2_array[nx]= xpos2 + nx; + if(xpos2_array[nx] == xpos1_array[cx]) { + xcol = true; + } + // printf("xarray = %d,x2array = %d,xcol = %d\n",xpos2_array[nx],xpos1_array[cx],xcol); + } + } + //printf("first loop done\n"); + for(int cy = 0; cy<height1; cy=cy+1) { + ypos1_array[cy]= ypos1 + cy; + for(int ny = 0; ny<height2; ny=ny+1) { + ypos2_array[ny]= ypos2 + ny; + if(ypos2_array[ny] == ypos1_array[cy]) { + ycol = true; + } + // printf("yarray = %d,y2array = %d,ycol = %d\n",ypos2_array[ny],ypos1_array[cy],ycol); + } + } + //printf("second loop done\n"); + // if both the hight and width position values are equal a collision has occured + col = (xcol & ycol); + //printf("col gotten, col = %d\n",col); + return col; +} +bool RosenEngine::check_collision1(int xpos1,int width1,int xpos2,int width2) +{ + // Create arrays of all positions with appropriate height and length + int xpos1_array[width1]; + int xpos2_array[width2]; + bool xcol = false; + + for(int cx = 0; cx<width1; cx=cx+1) { + xpos1_array[cx]= xpos1 + cx; + for(int nx = 0; nx<width2; nx=nx+1) { + xpos2_array[nx]= xpos2 + nx; + if(xpos2_array[nx] == xpos1_array[cx]) { + xcol = true; + } + // printf("xarray = %d,x2array = %d,xcol = %d\n",xpos2_array[nx],xpos1_array[cx],xcol); + } + } + return xcol; +} + +void RosenEngine::test() +{ + bool test; + test = check_collision(5,5,5,5,5,5,5,5); + if (test == false) { + printf("fail\n"); + } else { + printf("pass\n"); + } + test = check_collision(0,0,1,1,24,24,1,1); + if (test == true) { + printf("fail\n"); + } else { + printf("pass\n"); + } +} +// make function resemble test + +void RosenEngine::seeker_ship_collision(Gamepad &pad) +{ + Vector2D seeker_pos = _enemy.get_seekerpos(); + bool collision; + collision = check_collision(seeker_pos.x,seeker_pos.y,5,5,ship_xpos,ship_ypos,5,5); + if (collision == true) { + _health.update(1,pad); + _enemy.reset_seeker(); + pad.tone(500,0.05); + wait(0.05); + } +} +void RosenEngine::shooter_ship_collision(Gamepad &pad) +{ + bool collision1; + bool collision2; + bool collision3; + + collision1 = check_collision(ship_xpos,ship_ypos,9,6,_shooter1_pos.x, _shooter1_pos.y,10,7); + collision2 = check_collision(ship_xpos,ship_ypos,9,6,_shooter2_pos.x, _shooter2_pos.y,10,7); + collision3 = check_collision(ship_xpos,ship_ypos,9,6,_shooter3_pos.x, _shooter3_pos.y,10,7); + + if(collision1 == true) { + _health.update(1,pad); + pad.tone(500,0.05); + wait(0.05); + } + if(collision2 == true) { + _health.update(1,pad); + pad.tone(500,0.05); + wait(0.05); + } + if(collision3 == true) { + _health.update(1,pad); + pad.tone(500,0.05); + wait(0.05); + } +} +void RosenEngine::shooterw_ship_collision(Gamepad &pad) +{ + Vector2D _shooterw1_pos = _enemy.get_sh1wpos(); + Vector2D _shooterw2_pos = _enemy.get_sh2wpos(); + Vector2D _shooterw3_pos = _enemy.get_sh3wpos(); + + bool collision1; + bool collision2; + bool collision3; + + collision1 = check_collision(ship_xpos,ship_ypos,9,6,_shooterw1_pos.x, _shooterw1_pos.y,2,2); + collision2 = check_collision(ship_xpos,ship_ypos,9,6,_shooterw2_pos.x, _shooterw2_pos.y,2,2); + collision3 = check_collision(ship_xpos,ship_ypos,9,6,_shooterw3_pos.x, _shooterw3_pos.y,2,2); + + if(collision1 == true) { + _health.update(1,pad); + pad.tone(500,0.05); + wait(0.05); + } + if(collision2 == true) { + _health.update(1,pad); + pad.tone(500,0.05); + wait(0.05); + } + if(collision3 == true) { + _health.update(1,pad); + pad.tone(500,0.05); + wait(0.05); + } +} +void RosenEngine::kestrelw_seeker_collision(Gamepad &pad) +{ + Vector2D seeker_pos = _enemy.get_seekerpos(); + Vector2D missle_pos = _weapons.get_pos(_shipno); + bool collision; + collision = check_collision(seeker_pos.x,seeker_pos.y,9,6,missle_pos.x,missle_pos.y,1,1); + if (collision == true) { + _enemy.reset_seeker(); + pad.tone(500,0.05); + wait(0.05); + } +} +void RosenEngine::imperionw_seeker_collision(Gamepad &pad) +{ + Vector2D seeker_pos = _enemy.get_seekerpos(); + bool collision; + if(ship_ypos > seeker_pos.y + 6) { + collision = check_collision1(seeker_pos.x,9,ship_xpos + 2,3); + if (collision == true && A == true) { + _enemy.reset_seeker(); + pad.tone(500,0.05); + wait(0.05); + } + } +} +void RosenEngine::kestrelw_shooter_collision(Gamepad &pad) +{ + Vector2D missle_pos = _weapons.get_pos(_shipno); + bool col1, col2, col3; + col1 = check_collision(_shooter1_pos.x,_shooter1_pos.y,9,6,missle_pos.x,missle_pos.y,1,1); + col2 = check_collision(_shooter2_pos.x,_shooter2_pos.y,9,6,missle_pos.x,missle_pos.y,1,1); + col3 = check_collision(_shooter3_pos.x,_shooter3_pos.y,9,6,missle_pos.x,missle_pos.y,1,1); + if (col1 == true) { + _enemy.reset_shooter(1); + pad.tone(500,0.05); + wait(0.05); + } + if (col2 == true) { + _enemy.reset_shooter(2); + pad.tone(500,0.05); + wait(0.05); + } + if (col3 == true) { + _enemy.reset_shooter(3); + pad.tone(500,0.05); + wait(0.05); + } +} +void RosenEngine::imperionw_shooter_collision(Gamepad &pad) +{ + bool col1; + bool col2; + bool col3; + if(ship_ypos > _shooter1_pos.y + 6) { + col1 = check_collision1(_shooter1_pos.x,9,ship_xpos + 2,3); + if (col1 == true && A == true) { + _enemy.reset_shooter(1); + pad.tone(500,0.05); + wait(0.05); + } + } + if(ship_ypos > _shooter2_pos.y + 6) { + col2 = check_collision1(_shooter2_pos.x,9,ship_xpos + 2,3); + if (col2 == true && A == true) { + _enemy.reset_shooter(2); + pad.tone(500,0.05); + wait(0.05); + } + } + if(ship_ypos > _shooter3_pos.y + 6) { + col3 = check_collision1(_shooter3_pos.x,9,ship_xpos + 2,3); + if (col3 == true && A == true) { + _enemy.reset_shooter(3); + pad.tone(500,0.05); + wait(0.05); + } + } } \ No newline at end of file