ELEC2645 (2018/19) / Mbed 2 deprecated el17aio

Dependencies:   mbed

Revision:
41:e1fa36c0492e
Parent:
40:90c7a893d513
Child:
42:ee13e1d103d8
Child:
43:500b8cff3715
--- a/RosenEngine/RosenEngine.cpp	Tue May 07 10:01:34 2019 +0000
+++ b/RosenEngine/RosenEngine.cpp	Wed May 08 09:22:16 2019 +0000
@@ -27,14 +27,14 @@
     _score = 0;
     _dead = false;
     _intro = false;
-    _wait_time = 10.25;
+    _wait_time = 2.25;
 
 }
 void RosenEngine::reset()
 {
     _enemy.init(_no_shooters,_no_seekers);
     _health.init(_shipno);
-    _wait_time = 10.25;
+    _wait_time = 2.25;
     _times_run = 0;
     _score = 0;
     _no_shooters = 0;
@@ -45,11 +45,11 @@
 void RosenEngine::read_input(Gamepad &pad)
 {
     Vector2D mapped_coord = pad.get_coord();
-    _xjoystick = mapped_coord.x;
-    _yjoystick = mapped_coord.y;
+    _joystick.x = mapped_coord.x;
+    _joystick.y = mapped_coord.y;
     _d = pad.get_direction();
     wait(0.1);
-    // printf("_xjoystick ,_yjoystick = %f , %f\n",_xjoystick, _yjoystick);
+    // printf("_joystick.x ,_joystick.y = %f , %f\n",_joystick.x, _joystick.y);
 }
 
 void RosenEngine::draw(N5110 &lcd, Gamepad &pad)
@@ -60,52 +60,44 @@
     _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,closest);
-    }
-    if(_shipno == 1) {
-        _ship.set_dimensions(7,10);
-        _ship.draw_ship(lcd,_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,closest);
-    }
+    draw_ship(lcd,pad);
     if(_dead == true) {
         game_over(lcd);
     }
     disp_points(lcd);
 }
+void RosenEngine::draw_ship(N5110 &lcd, Gamepad &pad)
+{
+        // find the closest enemy (used in orions firing)
+        Vector2D inde = find_closest1();
+        int index = inde.x;
+        Vector2D closest = find_closest2(index);
+        // Draw ships and weapons depending on the ship being used
+        switch (_shipno) {
+        case 0:
+            _ship.set_dimensions(9,6);
+            _ship.draw_ship(lcd,_shipno);
+            _weapons.draw(lcd,pad,_shipno,closest);
+            break;
+        case 1:
+            _ship.set_dimensions(7,10);
+            _ship.draw_ship(lcd,_shipno);
+            _weapons.draw(lcd,pad,_shipno,closest);
+            break;
+        case 2:
+            _ship.set_dimensions(7,10);
+            _ship.draw_ship(lcd,_shipno);
+            _weapons.draw(lcd,pad,_shipno,closest);
+            break;
+    }
+}
 
 void RosenEngine::update(Gamepad &pad)
 {
-    _enemy.update_seeker(ship_xpos, ship_ypos);
-    _enemy.update_shooter(ship_xpos, ship_ypos);
-    //printf("update_shooter\n");
+    _enemy.update_seeker(_shipPos.x, _shipPos.y);
+    _enemy.update_shooter(_shipPos.x, _shipPos.y);
     _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();
-    }
-    // test();
+    update_shooter_weapon(pad);
     shooter_ship_collision(pad);
     seeker_ship_collision(pad);
     shooterw_ship_collision(pad);
@@ -118,31 +110,58 @@
     scaling(timer(12));
 
 }
+void RosenEngine::update_shooter_weapon(Gamepad &pad)
+{
+    if(_shipno == 0) {
+        _ship.update_ship(_joystick.x,_joystick.y);
+        _weapons.update();
+        kestrelw_seeker_collision(pad);
+    }
+    //printf("if shipno == 0\n");
+    if(_shipno == 1 && A == false) {
+        _ship.update_ship(_joystick.x,_joystick.y);
+        _weapons.update();
+    }
+    //printf("if shipno == 1\n");
+    if(_shipno == 2) {
+        _ship.update_ship(_joystick.x,_joystick.y);
+        _weapons.update();
+    }
+}
 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);
-
-    _shooter1_pos = _enemy.get_shooterpos(1);
-    _shooter2_pos = _enemy.get_shooterpos(2);
-    _shooter3_pos = _enemy.get_shooterpos(3);
+    _shipPos = _ship.get_pos();
+    _seekerPos[0] = _enemy.get_seekerpos(1);
+    _seekerPos[1] = _enemy.get_seekerpos(2);
+    _seekerPos[2] = _enemy.get_seekerpos(3);
+    _shooterPos[0] = _enemy.get_shooterpos(1);
+    _shooterPos[1] = _enemy.get_shooterpos(2);
+    _shooterPos[2] = _enemy.get_shooterpos(3);
+    _shooterWPos[0] = _enemy.get_shwpos(1);
+    _shooterWPos[1] = _enemy.get_shwpos(2);
+    _shooterWPos[2] = _enemy.get_shwpos(3);
 
-    _weapons.init(ship_xpos, ship_ypos, ship_width);
+    _weapons.init(_shipPos.x, _shipPos.y, _shipWidth);
     _ycursor = _menu.get_ycursor();
+    set_ship_size();
+}
+void RosenEngine::set_ship_size()
+{
+    switch (_shipno) {
+        case 0:
+            _shipWidth = 9;
+            _shipHeight = 6;
+            break;
+        case 1:
+            _shipWidth = 7;
+            _shipHeight = 10;
+            break;
+        case 2:
+            _shipWidth = 7;
+            _shipHeight = 10;
+            break;
+    }
 
-    if(_shipno == 0) {
-        ship_width = 9;
-        ship_height = 6;
-    }
-    if(_shipno == 1) {
-        ship_width = 7;
-        ship_height = 10;
-    }
 }
 void RosenEngine::title(N5110 &lcd)
 {
@@ -197,7 +216,6 @@
             // 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);
@@ -245,9 +263,9 @@
 {
     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);
+    col1 = check_collision(_shipPos.x,_shipPos.y,9,6,_seekerPos[0].x, _seekerPos[0].y,10,7);
+    col2 = check_collision(_shipPos.x,_shipPos.y,9,6,_seekerPos[1].x, _seekerPos[1].y,10,7);
+    col3 = check_collision(_shipPos.x,_shipPos.y,9,6,_seekerPos[2].x, _seekerPos[2].y,10,7);
     int sel = 0;
 
     if (col1 == true && _no_seekers >= 1) {
@@ -271,9 +289,9 @@
 {
     bool col1,col2,col3;
     int sel = 0;
-    col1 = check_collision(ship_xpos,ship_ypos,9,6,_shooter1_pos.x, _shooter1_pos.y,10,7);
-    col2 = check_collision(ship_xpos,ship_ypos,9,6,_shooter2_pos.x, _shooter2_pos.y,10,7);
-    col3 = check_collision(ship_xpos,ship_ypos,9,6,_shooter3_pos.x, _shooter3_pos.y,10,7);
+    col1 = check_collision(_shipPos.x,_shipPos.y,9,6,_shooterPos[0].x, _shooterPos[1].y,10,7);
+    col2 = check_collision(_shipPos.x,_shipPos.y,9,6,_shooterPos[1].x, _shooterPos[0].y,10,7);
+    col3 = check_collision(_shipPos.x,_shipPos.y,9,6,_shooterPos[2].x, _shooterPos[0].y,10,7);
 
     if(col1 == true && _no_shooters >= 1) {
         sel = 1;
@@ -293,15 +311,11 @@
 }
 void RosenEngine::shooterw_ship_collision(Gamepad &pad)
 {
-    Vector2D _shooterw1_pos = _enemy.get_shwpos(1);
-    Vector2D _shooterw2_pos = _enemy.get_shwpos(2);
-    Vector2D _shooterw3_pos = _enemy.get_shwpos(3);
-
     bool col1,col2,col3;
     int sel = 0;
-    col1 = check_collision(ship_xpos,ship_ypos,9,6,_shooterw1_pos.x, _shooterw1_pos.y,2,2);
-    col2 = check_collision(ship_xpos,ship_ypos,9,6,_shooterw2_pos.x, _shooterw2_pos.y,2,2);
-    col3 = check_collision(ship_xpos,ship_ypos,9,6,_shooterw3_pos.x, _shooterw3_pos.y,2,2);
+    col1 = check_collision(_shipPos.x,_shipPos.y,9,6,_shooterWPos[0].x, _shooterWPos[1].y,2,2);
+    col2 = check_collision(_shipPos.x,_shipPos.y,9,6,_shooterWPos[1].x, _shooterWPos[2].y,2,2);
+    col3 = check_collision(_shipPos.x,_shipPos.y,9,6,_shooterWPos[2].x, _shooterWPos[2].y,2,2);
 
     if(col1 == true && _no_shooters >= 1) {
         sel = 1;
@@ -323,9 +337,9 @@
     bool col1, col2, col3;
     int sel = 0;
     Vector2D missle_pos = _weapons.get_pos(_shipno);
-    col1 = check_collision(_seeker1_pos.x,_seeker1_pos.y,9,6,missle_pos.x,missle_pos.y,1,1);
-    col2 = check_collision(_seeker2_pos.x,_seeker2_pos.y,9,6,missle_pos.x,missle_pos.y,1,1);
-    col3 = check_collision(_seeker3_pos.x,_seeker3_pos.y,9,6,missle_pos.x,missle_pos.y,1,1);
+    col1 = check_collision(_seekerPos[0].x,_seekerPos[0].y,9,6,missle_pos.x,missle_pos.y,1,1);
+    col2 = check_collision(_seekerPos[1].x,_seekerPos[1].y,9,6,missle_pos.x,missle_pos.y,1,1);
+    col3 = check_collision(_seekerPos[2].x,_seekerPos[2].y,9,6,missle_pos.x,missle_pos.y,1,1);
     if (col1 == true && _no_seekers >= 1) {
         sel = 1;
     }
@@ -345,26 +359,26 @@
 {
     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;
+    if(_shipno == 1) {
+        if(_shipPos.y > _seekerPos[0].y + 6) {
+            col1 = check_collision1(_seekerPos[0].x,9,_shipPos.x  + 2,3);
+            if (col1 == true && A == true && _no_seekers >= 1) {
+                sel = 1;
+            }
         }
-    }
-    if(ship_ypos > _seeker2_pos.y + 6) {
-        col2 = check_collision1(_seeker2_pos.x,9,ship_xpos + 2,3);
-        if (col2 == true && A == true && _no_seekers >= 2) {
-            sel = 2;
+        if(_shipPos.y > _seekerPos[1].y + 6) {
+            col2 = check_collision1(_seekerPos[1].x,9,_shipPos.x  + 2,3);
+            if (col2 == true && A == true && _no_seekers >= 2) {
+                sel = 2;
+            }
         }
-    }
-    if(ship_ypos > _seeker1_pos.y + 6) {
-        col3 = check_collision1(_seeker3_pos.x,9,ship_xpos + 2,3);
-        if (col3 == true && A == true && _no_seekers >= 3) {
-            sel = 3;
+        if(_shipPos.y > _seekerPos[0].y + 6) {
+            col3 = check_collision1(_seekerPos[2].x,9,_shipPos.x  + 2,3);
+            if (col3 == true && A == true && _no_seekers >= 3) {
+                sel = 3;
+            }
         }
     }
-    }
     if(sel != 0) {
         _health.seekerh_update(sel,10);
         pad.tone(500,0.05);
@@ -376,9 +390,9 @@
     Vector2D missle_pos = _weapons.get_pos(_shipno);
     bool col1, col2, col3;
     int sel = 0;
-    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);
+    col1 = check_collision(_shooterPos[0].x,_shooterPos[0].y,9,6,missle_pos.x,missle_pos.y,1,1);
+    col2 = check_collision(_shooterPos[1].x,_shooterPos[1].y,9,6,missle_pos.x,missle_pos.y,1,1);
+    col3 = check_collision(_shooterPos[2].x,_shooterPos[2].y,9,6,missle_pos.x,missle_pos.y,1,1);
     if (col1 == true && _no_shooters >= 1) {
         sel = 1;
     }
@@ -400,20 +414,20 @@
     bool col1,col2,col3;
     int sel = 0;
     if(_shipno == 1) {
-        if(ship_ypos > _shooter1_pos.y + 6) {
-            col1 = check_collision1(_shooter1_pos.x,9,ship_xpos + 2,3);
+        if(_shipPos.y > _shooterPos[0].y + 6) {
+            col1 = check_collision1(_shooterPos[0].x,9,_shipPos.x  + 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(_shipPos.y > _shooterPos[1].y + 6) {
+            col2 = check_collision1(_shooterPos[1].x,9,_shipPos.x  + 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(_shipPos.y > _shooterPos[2].y + 6) {
+            col3 = check_collision1(_shooterPos[2].x,9,_shipPos.x  + 2,3);
             if (col3 == true && A == true && _no_shooters >= 3) {
                 sel = 3;
             }
@@ -427,31 +441,31 @@
 }
 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){
+
+    // enemy 1,2 and 3 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){
+    // enemy 4,5 and 6 are seekers so 3 is subtracted from inde.x to compensate
+    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 player and enemy health
     check_se_health();
     check_sh_health();
-
     Vector2D hp = _health.get_hp();
     if(hp.x <= 0) {
         // printf("player deaad\n");
@@ -475,6 +489,7 @@
     if(seeker3_health == 0) {
         sel = 3;
     }
+    // reset seeker and update score when shooter health goes to zero
     if(sel != 0) {
         _enemy.reset_seeker(sel);
         _health.reset_seekerh(sel);
@@ -497,6 +512,7 @@
     if(shooter3_health == 0) {
         sel = 3;
     }
+    // reset shooter and update score when shooter health goes to zero
     if(sel != 0) {
         _enemy.reset_shooter(sel);
         _health.reset_shooterh(sel);
@@ -505,9 +521,9 @@
 }
 int RosenEngine::rand_no()
 {
+    // seeds and returns a random number using the ctime library
     srand(time(NULL));
     int rand_no = (rand() %45) + 1;
-    // printf("random no = %d\n",rand_no);
     return rand_no;
 }
 float RosenEngine::timer(int fps)
@@ -529,6 +545,7 @@
 
 void RosenEngine::scaling(float time_elapsed)
 {
+    // increases difficulty as game progresses
     if(time_elapsed == _wait_time) {
         _no_shooters = _no_shooters + 1;
         _no_seekers = _no_seekers + 1;
@@ -547,41 +564,12 @@
 }
 void RosenEngine::game_over(N5110 &lcd)
 {
-    /*
-    lcd.clear();
-    lcd.refresh();
-    lcd.printString("Game Over ",2,2);
-    lcd.printString("Try dodging ",2,3);
-    lcd.printString("next time",2,4);
-    lcd.refresh();
-    wait(2);
-    */
-    _lore.display(lcd);
+    // Display random tips after every loss
+    _lore.display(lcd,rand_no());
 }
 void RosenEngine::intro(N5110 &lcd)
 {
-    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);
-
-    }
+    _lore.intro(lcd);
     _intro = true;
 }
 void RosenEngine::disp_points(N5110 &lcd)
@@ -592,26 +580,26 @@
 }
 Vector2D RosenEngine::get_enemynum()
 {
+    // return the number of enemy shooters and seekers currently on play
     return{_no_shooters,_no_seekers};
-
 }
 int RosenEngine::range(int x1, int y1, float x2, float y2)
 {
+    // calculates average distance between two points
     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 sh1 = range(_shipPos.x,_shipPos.y,_shooterPos[0].x,_shooterPos[0].y);
+    int sh2 = range(_shipPos.x,_shipPos.y,_shooterPos[1].x,_shooterPos[1].y);
+    int sh3 = range(_shipPos.x,_shipPos.y,_shooterPos[2].x,_shooterPos[2].y);
+    int se1 = range(_shipPos.x,_shipPos.y,_seekerPos[0].x,_seekerPos[0].y);
+    int se2 = range(_shipPos.x,_shipPos.y,_seekerPos[1].x,_seekerPos[1].y);
+    int se3 = range(_shipPos.x,_shipPos.y,_seekerPos[2].x,_seekerPos[2].y);
 
     int close[6] = {sh1,sh2,sh3,se1,se2,se3};
     // find index of the smallest element
@@ -623,29 +611,29 @@
             index = i;
         }
     }
+    // return the index, so we know what the closest enemy, and the distance to check its in range
     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};
+        return {_shooterPos[0].x,_shooterPos[0].y};
     }
     if(index == 2 && _no_shooters >= 2) {
-        return {_shooter2_pos.x,_shooter2_pos.y};
+        return {_shooterPos[1].x,_shooterPos[1].y};
     }
     if(index == 3 && _no_shooters >= 3) {
-        return {_shooter3_pos.x,_shooter3_pos.y};
+        return {_shooterPos[2].x,_shooterPos[2].y};
     }
     if(index == 4 && _no_seekers >= 1 ) {
-        return {_seeker1_pos.x,_seeker1_pos.y};
+        return {_seekerPos[0].x,_seekerPos[0].y};
     }
     if(index == 5 && _no_seekers >= 2) {
-        return {_seeker2_pos.x,_seeker2_pos.y};
+        return {_seekerPos[1].x,_seekerPos[1].y};
     }
     if(index == 6 && _no_seekers >= 3) {
-        return {_seeker3_pos.x,_seeker3_pos.y};
+        return {_seekerPos[2].x,_seekerPos[2].y};
     }
 
 }