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:
- 40:90c7a893d513
- Parent:
- 39:7824f9080f59
- Child:
- 41:e1fa36c0492e
--- a/RosenEngine/RosenEngine.cpp Mon May 06 16:49:53 2019 +0000 +++ b/RosenEngine/RosenEngine.cpp Tue May 07 10:01:34 2019 +0000 @@ -18,7 +18,6 @@ { // initialise the game parameters _ship.init(ship_width,ship_height,ship_speed,ship_xpos,ship_ypos); - // place seeker above the ship _no_shooters = 0; _no_seekers = 0; _enemy.init(_no_shooters,_no_seekers); @@ -61,23 +60,26 @@ _enemy.draw_seeker(lcd); _enemy.draw_shooter(lcd); _enemy.draw_shw(lcd,pad); + Vector2D inde = find_closest1(); + int index = inde.x; + Vector2D closest = find_closest2(index); if(_shipno == 0) { _ship.set_dimensions(9,6); _ship.draw_ship(lcd,_shipno); - _weapons.draw(lcd,pad,_shipno); + _weapons.draw(lcd,pad,_shipno,closest); } if(_shipno == 1) { _ship.set_dimensions(7,10); _ship.draw_ship(lcd,_shipno); - _weapons.draw(lcd,pad,_shipno); + _weapons.draw(lcd,pad,_shipno,closest); } if(_shipno == 2) { _ship.set_dimensions(7,10); _ship.draw_ship(lcd,_shipno); - _weapons.draw(lcd,pad,_shipno); + _weapons.draw(lcd,pad,_shipno,closest); } - if(_dead == true){ - game_over(lcd); + if(_dead == true) { + game_over(lcd); } disp_points(lcd); } @@ -110,17 +112,18 @@ imperionw_seeker_collision(pad); kestrelw_shooter_collision(pad); imperionw_shooter_collision(pad); + orionw_collision(pad); check_health(); rand_no(); scaling(timer(12)); - + } void RosenEngine::get_pos() { Vector2D ship_pos = _ship.get_pos(); ship_xpos = ship_pos.x; ship_ypos = ship_pos.y; - + _seeker1_pos = _enemy.get_seekerpos(1); _seeker2_pos = _enemy.get_seekerpos(2); _seeker3_pos = _enemy.get_seekerpos(3); @@ -241,12 +244,12 @@ void RosenEngine::seeker_ship_collision(Gamepad &pad) { bool col1,col2,col3; - + col1 = check_collision(ship_xpos,ship_ypos,9,6,_seeker1_pos.x, _seeker1_pos.y,10,7); col2 = check_collision(ship_xpos,ship_ypos,9,6,_seeker2_pos.x, _seeker2_pos.y,10,7); col3 = check_collision(ship_xpos,ship_ypos,9,6,_seeker3_pos.x, _seeker3_pos.y,10,7); int sel = 0; - + if (col1 == true && _no_seekers >= 1) { sel = 1; } @@ -257,7 +260,7 @@ sel = 3; } // printf("col1 = %d, col2 = %d, col3 = %d, no_seekers = %d\n",col1,col2,col3,_no_seekers); - if(sel != 0){ + if(sel != 0) { _health.update(5,pad); _health.seekerh_update(sel,10); pad.tone(500,0.05); @@ -281,11 +284,11 @@ if(col3 == true && _no_shooters >= 3) { sel = 3; } - if(sel != 0){ + if(sel != 0) { _health.update(1,pad); _health.shooterh_update(sel,10); pad.tone(500,0.05); - wait(0.05); + wait(0.05); } } void RosenEngine::shooterw_ship_collision(Gamepad &pad) @@ -309,10 +312,10 @@ if(col3 == true && _no_shooters >= 3) { sel = 3; } - if(sel != 0){ + if(sel != 0) { _health.update(1,pad); pad.tone(500,0.05); - wait(0.05); + wait(0.05); } } void RosenEngine::kestrelw_seeker_collision(Gamepad &pad) @@ -332,7 +335,7 @@ if (col3 == true && _no_seekers >= 3) { sel = 3; } - if(sel != 0){ + if(sel != 0) { pad.tone(500,0.05); _health.seekerh_update(sel,5); wait(0.05); @@ -342,10 +345,11 @@ { bool col1,col2,col3; int sel = 0; + if(_shipno == 1){ if(ship_ypos > _seeker1_pos.y + 6) { col1 = check_collision1(_seeker1_pos.x,9,ship_xpos + 2,3); if (col1 == true && A == true && _no_seekers >= 1) { - sel = 1; + sel = 1; } } if(ship_ypos > _seeker2_pos.y + 6) { @@ -360,7 +364,8 @@ sel = 3; } } - if(sel != 0){ + } + if(sel != 0) { _health.seekerh_update(sel,10); pad.tone(500,0.05); wait(0.05); @@ -383,8 +388,8 @@ if (col3 == true && _no_shooters >= 3) { sel = 3; } - // printf("col1 = %d,col2 = %d,col3 = %d, no_shooters = %d\n",col1,col2,col3,_no_shooters); - if(sel != 0){ + // printf("col1 = %d,col2 = %d,col3 = %d, no_shooters = %d\n",col1,col2,col3,_no_shooters); + if(sel != 0) { pad.tone(500,0.05); _health.shooterh_update(sel,5); wait(0.05); @@ -394,40 +399,65 @@ { bool col1,col2,col3; int sel = 0; - if(ship_ypos > _shooter1_pos.y + 6) { - col1 = check_collision1(_shooter1_pos.x,9,ship_xpos + 2,3); - if (col1 == true && A == true && _no_shooters >= 1) { - sel = 1; + if(_shipno == 1) { + if(ship_ypos > _shooter1_pos.y + 6) { + col1 = check_collision1(_shooter1_pos.x,9,ship_xpos + 2,3); + if (col1 == true && A == true && _no_shooters >= 1) { + sel = 1; + } + } + if(ship_ypos > _shooter2_pos.y + 6) { + col2 = check_collision1(_shooter2_pos.x,9,ship_xpos + 2,3); + if (col2 == true && A == true && _no_shooters >= 2) { + sel = 2; + } + } + if(ship_ypos > _shooter3_pos.y + 6) { + col3 = check_collision1(_shooter3_pos.x,9,ship_xpos + 2,3); + if (col3 == true && A == true && _no_shooters >= 3) { + sel = 3; + } } } - if(ship_ypos > _shooter2_pos.y + 6) { - col2 = check_collision1(_shooter2_pos.x,9,ship_xpos + 2,3); - if (col2 == true && A == true && _no_shooters >= 2) { - sel = 2; - } - } - if(ship_ypos > _shooter3_pos.y + 6) { - col3 = check_collision1(_shooter3_pos.x,9,ship_xpos + 2,3); - if (col3 == true && A == true && _no_shooters >= 3) { - sel = 3; - } - } - if(sel != 0){ + if(sel != 0) { _health.shooterh_update(sel,10); pad.tone(500,0.05); wait(0.05); } } -void RosenEngine::check_health(){ +void RosenEngine::orionw_collision(Gamepad &pad) +{ + + Vector2D inde = find_closest1(); + int index1 = inde.x; + int index2 = inde.x - 3; + int distance = inde.y; + + // enemy 0,1 and 2 are shooters + if(_no_shooters >= index1 && A == true && distance < 15){ + _health.shooterh_update(index1,10); + pad.tone(500,0.05); + wait(0.05); + } + if(_no_seekers >= index2 && A == true && distance < 15){ + _health.seekerh_update(index2,10); + pad.tone(500,0.05); + wait(0.05); + } + + +} +void RosenEngine::check_health() +{ check_se_health(); check_sh_health(); - + Vector2D hp = _health.get_hp(); - if(hp.x <= 0){ + if(hp.x <= 0) { // printf("player deaad\n"); _dead = true; - } - + } + } void RosenEngine::check_se_health() { @@ -436,16 +466,16 @@ int seeker2_health = _health.get_seekerh(2); int seeker3_health = _health.get_seekerh(3); // printf("seeker1_h = %d, seeker2_h = %d, seeker3_h = %d\n",seeker1_health,seeker2_health,seeker3_health); - if(seeker1_health == 0){ + if(seeker1_health == 0) { sel = 1; } - if(seeker2_health == 0){ + if(seeker2_health == 0) { sel = 2; } - if(seeker3_health == 0){ + if(seeker3_health == 0) { sel = 3; } - if(sel != 0){ + if(sel != 0) { _enemy.reset_seeker(sel); _health.reset_seekerh(sel); _score = _score + 10; @@ -457,17 +487,17 @@ int shooter1_health = _health.get_shooterh(1); int shooter2_health = _health.get_shooterh(2); int shooter3_health = _health.get_shooterh(3); - - if(shooter1_health == 0){ + + if(shooter1_health == 0) { sel = 1; } - if(shooter2_health == 0){ + if(shooter2_health == 0) { sel = 2; } - if(shooter3_health == 0){ + if(shooter3_health == 0) { sel = 3; } - if(sel != 0){ + if(sel != 0) { _enemy.reset_shooter(sel); _health.reset_shooterh(sel); _score = _score + 20; @@ -484,7 +514,7 @@ { _times_run = _times_run + 1; float time_frame = 1.0f/fps; - float time_elapsed = _times_run * time_frame; + float time_elapsed = _times_run * time_frame; // printf("time elapsed = %f,time frame = %f, _times_run = %d\n",time_elapsed,time_frame,_times_run); return time_elapsed; } @@ -499,16 +529,16 @@ void RosenEngine::scaling(float time_elapsed) { - if(time_elapsed == _wait_time){ + if(time_elapsed == _wait_time) { _no_shooters = _no_shooters + 1; - // _no_seekers = _no_seekers + 1; + _no_seekers = _no_seekers + 1; _enemy.sh_scaling(time_elapsed); _wait_time = _wait_time + 10.00; } - if(_no_shooters > 3){ + if(_no_shooters > 3) { _no_shooters = 3; } - if(_no_seekers > 3){ + if(_no_seekers > 3) { _no_seekers = 3; } _enemy.set_noshooters(_no_shooters); @@ -530,27 +560,27 @@ } void RosenEngine::intro(N5110 &lcd) { - if(_intro == false){ - lcd.clear(); - lcd.refresh(); - lcd.printString("You are tasked",2,0); - lcd.printString("with holding",2,1); - lcd.printString(" the line ",2,2); - lcd.printString(" from an ",2,3); - lcd.printString("invading army ",2,4); - lcd.refresh(); - wait(2); - lcd.clear(); - lcd.refresh(); - lcd.printString(" as an ",2,0); - lcd.printString(" expendable ",2,1); - lcd.printString(" asset, ",2,2); - lcd.printString("you are not ",2,3); - lcd.printString("expected to",2,4); - lcd.printString(" survive ",2,5); - lcd.refresh(); - wait(2); - + if(_intro == false) { + lcd.clear(); + lcd.refresh(); + lcd.printString("You're tasked",2,0); + lcd.printString(" with holding",2,1); + lcd.printString(" the line ",2,2); + lcd.printString(" from an ",2,3); + lcd.printString("invading army ",2,4); + lcd.refresh(); + wait(2); + lcd.clear(); + lcd.refresh(); + lcd.printString(" as an ",2,0); + lcd.printString(" expendable ",2,1); + lcd.printString(" asset, ",2,2); + lcd.printString(" you are not ",2,3); + lcd.printString(" expected to",2,4); + lcd.printString(" survive ",2,5); + lcd.refresh(); + wait(2); + } _intro = true; } @@ -560,3 +590,62 @@ sprintf(buffer,"%d",_score); lcd.printString(buffer,2,0); } +Vector2D RosenEngine::get_enemynum() +{ + return{_no_shooters,_no_seekers}; + +} +int RosenEngine::range(int x1, int y1, float x2, float y2) +{ + float rangex = (abs(x1 - x2)); + float rangey = (abs(y1 - y2)); + int distance = floor((rangex+rangey)/2); + // printf("distance = %d\n",distance); + return distance; +} +Vector2D RosenEngine::find_closest1() +{ + // get the distance for all enemies + int sh1 = range(ship_xpos,ship_ypos,_shooter1_pos.x,_shooter1_pos.y); + int sh2 = range(ship_xpos,ship_ypos,_shooter2_pos.x,_shooter2_pos.y); + int sh3 = range(ship_xpos,ship_ypos,_shooter3_pos.x,_shooter3_pos.y); + int se1 = range(ship_xpos,ship_ypos,_seeker1_pos.x,_seeker1_pos.y); + int se2 = range(ship_xpos,ship_ypos,_seeker2_pos.x,_seeker2_pos.y); + int se3 = range(ship_xpos,ship_ypos,_seeker3_pos.x,_seeker3_pos.y); + + int close[6] = {sh1,sh2,sh3,se1,se2,se3}; + // find index of the smallest element + int index = 0; + int smallest = close[0]; + for(int i=0; i<6; i=i+1) { + if(smallest>close[i]) { + smallest=close[i]; + index = i; + } + } + return {(index + 1),smallest}; +} +Vector2D RosenEngine::find_closest2(int index) +{ + // printf("index = %d, _no_shooters = %d, _no_seekers = %d\n",index,_no_shooters,_no_seekers); + // return the position of the closest enemy + if(index == 1 && _no_shooters >= 1) { + return {_shooter1_pos.x,_shooter1_pos.y}; + } + if(index == 2 && _no_shooters >= 2) { + return {_shooter2_pos.x,_shooter2_pos.y}; + } + if(index == 3 && _no_shooters >= 3) { + return {_shooter3_pos.x,_shooter3_pos.y}; + } + if(index == 4 && _no_seekers >= 1 ) { + return {_seeker1_pos.x,_seeker1_pos.y}; + } + if(index == 5 && _no_seekers >= 2) { + return {_seeker2_pos.x,_seeker2_pos.y}; + } + if(index == 6 && _no_seekers >= 3) { + return {_seeker3_pos.x,_seeker3_pos.y}; + } + +}