ELEC2645 (2018/19) / Mbed 2 deprecated el17aio

Dependencies:   mbed

Committer:
ikenna1
Date:
Thu May 09 13:22:45 2019 +0000
Revision:
52:29772e31a620
Parent:
49:aa204bf7ee2e
Child:
53:3fdc4486f672
Add testing and Menu information

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ikenna1 3:f9cd1a38d5c6 1 #include "RosenEngine.h"
ikenna1 39:7824f9080f59 2
ikenna1 23:0301effce801 3 DigitalIn A(PTB9);
ikenna1 3:f9cd1a38d5c6 4
ikenna1 2:66a4e5d7a7cd 5 // Constructor
ikenna1 2:66a4e5d7a7cd 6 RosenEngine::RosenEngine()
ikenna1 2:66a4e5d7a7cd 7 {
ikenna1 2:66a4e5d7a7cd 8
ikenna1 2:66a4e5d7a7cd 9 }
ikenna1 2:66a4e5d7a7cd 10 // Destructor
ikenna1 2:66a4e5d7a7cd 11 RosenEngine::~RosenEngine()
ikenna1 2:66a4e5d7a7cd 12 {
ikenna1 2:66a4e5d7a7cd 13
ikenna1 2:66a4e5d7a7cd 14 }
ikenna1 3:f9cd1a38d5c6 15
ikenna1 4:740e14ebbc97 16
ikenna1 9:241a1a7d8527 17 void RosenEngine::init(int ship_width,int ship_height,int ship_speed,int ship_xpos,int ship_ypos)
ikenna1 2:66a4e5d7a7cd 18 {
ikenna1 4:740e14ebbc97 19 // initialise the game parameters
ikenna1 9:241a1a7d8527 20 _ship.init(ship_width,ship_height,ship_speed,ship_xpos,ship_ypos);
ikenna1 39:7824f9080f59 21 _no_shooters = 0;
ikenna1 39:7824f9080f59 22 _no_seekers = 0;
ikenna1 39:7824f9080f59 23 _enemy.init(_no_shooters,_no_seekers);
ikenna1 9:241a1a7d8527 24 _menu.init(16);
ikenna1 44:a6a361bea806 25 _health.init(_shipUsed);
ikenna1 38:4571537238ed 26 _times_run = 0;
ikenna1 38:4571537238ed 27 _score = 0;
ikenna1 38:4571537238ed 28 _dead = false;
ikenna1 39:7824f9080f59 29 _intro = false;
ikenna1 41:e1fa36c0492e 30 _wait_time = 2.25;
ikenna1 13:e114d362186d 31
ikenna1 3:f9cd1a38d5c6 32 }
ikenna1 27:f99249e727fd 33 void RosenEngine::reset()
ikenna1 27:f99249e727fd 34 {
ikenna1 52:29772e31a620 35 // reinitialize parameters once back is pressed and game is exited
ikenna1 39:7824f9080f59 36 _enemy.init(_no_shooters,_no_seekers);
ikenna1 44:a6a361bea806 37 _health.init(_shipUsed);
ikenna1 41:e1fa36c0492e 38 _wait_time = 2.25;
ikenna1 38:4571537238ed 39 _times_run = 0;
ikenna1 38:4571537238ed 40 _score = 0;
ikenna1 39:7824f9080f59 41 _no_shooters = 0;
ikenna1 38:4571537238ed 42 _dead = false;
ikenna1 39:7824f9080f59 43 _no_seekers = 0;
ikenna1 27:f99249e727fd 44 }
ikenna1 4:740e14ebbc97 45
ikenna1 5:bb6edc5b5be3 46 void RosenEngine::read_input(Gamepad &pad)
ikenna1 4:740e14ebbc97 47 {
ikenna1 52:29772e31a620 48 // read all inputs from the gamepad
ikenna1 4:740e14ebbc97 49 Vector2D mapped_coord = pad.get_coord();
ikenna1 41:e1fa36c0492e 50 _joystick.x = mapped_coord.x;
ikenna1 41:e1fa36c0492e 51 _joystick.y = mapped_coord.y;
ikenna1 9:241a1a7d8527 52 _d = pad.get_direction();
ikenna1 35:3341f2bd0408 53 wait(0.1);
ikenna1 41:e1fa36c0492e 54 // printf("_joystick.x ,_joystick.y = %f , %f\n",_joystick.x, _joystick.y);
ikenna1 4:740e14ebbc97 55 }
ikenna1 4:740e14ebbc97 56
ikenna1 14:88ca5b1a111a 57 void RosenEngine::draw(N5110 &lcd, Gamepad &pad)
ikenna1 7:ed5870cfb3e0 58 {
ikenna1 52:29772e31a620 59 // Draw border
ikenna1 31:c7bd3ed16840 60 lcd.drawRect(0,0,78,48,FILL_TRANSPARENT);
ikenna1 52:29772e31a620 61 // Draw all other game assets
ikenna1 44:a6a361bea806 62 _health.draw_health(lcd,_shipUsed);
ikenna1 18:2cc6898de6b2 63 _health.draw_shields(lcd);
ikenna1 15:009ccc07bb57 64 _enemy.draw_seeker(lcd);
ikenna1 34:6d0786582d81 65 _enemy.draw_shooter(lcd);
ikenna1 34:6d0786582d81 66 _enemy.draw_shw(lcd,pad);
ikenna1 41:e1fa36c0492e 67 draw_ship(lcd,pad);
ikenna1 52:29772e31a620 68 disp_points(lcd);
ikenna1 52:29772e31a620 69 // if player dies display death scene
ikenna1 40:90c7a893d513 70 if(_dead == true) {
ikenna1 40:90c7a893d513 71 game_over(lcd);
ikenna1 38:4571537238ed 72 }
ikenna1 11:73cd744ffa80 73 }
ikenna1 41:e1fa36c0492e 74 void RosenEngine::draw_ship(N5110 &lcd, Gamepad &pad)
ikenna1 41:e1fa36c0492e 75 {
ikenna1 41:e1fa36c0492e 76 // find the closest enemy (used in orions firing)
ikenna1 41:e1fa36c0492e 77 Vector2D inde = find_closest1();
ikenna1 41:e1fa36c0492e 78 int index = inde.x;
ikenna1 41:e1fa36c0492e 79 Vector2D closest = find_closest2(index);
ikenna1 52:29772e31a620 80 // Draw ships and set ship parameters depending on the ship being used
ikenna1 43:500b8cff3715 81 switch (_shipUsed) {
ikenna1 43:500b8cff3715 82 case kestrel:
ikenna1 45:fe5fc85a5c73 83 _ship.set_parameters(9,6,4);
ikenna1 43:500b8cff3715 84 _ship.draw_ship(lcd,_shipUsed);
ikenna1 43:500b8cff3715 85 _weapons.draw(lcd,pad,_shipUsed,closest);
ikenna1 41:e1fa36c0492e 86 break;
ikenna1 43:500b8cff3715 87 case imperion:
ikenna1 45:fe5fc85a5c73 88 _ship.set_parameters(7,10,2);
ikenna1 43:500b8cff3715 89 _ship.draw_ship(lcd,_shipUsed);
ikenna1 43:500b8cff3715 90 _weapons.draw(lcd,pad,_shipUsed,closest);
ikenna1 41:e1fa36c0492e 91 break;
ikenna1 43:500b8cff3715 92 case orion:
ikenna1 45:fe5fc85a5c73 93 _ship.set_parameters(7,10,3);
ikenna1 43:500b8cff3715 94 _ship.draw_ship(lcd,_shipUsed);
ikenna1 43:500b8cff3715 95 _weapons.draw(lcd,pad,_shipUsed,closest);
ikenna1 41:e1fa36c0492e 96 break;
ikenna1 41:e1fa36c0492e 97 }
ikenna1 45:fe5fc85a5c73 98 //printf("_shipUsed = %d\n",_shipUsed);
ikenna1 41:e1fa36c0492e 99 }
ikenna1 7:ed5870cfb3e0 100
ikenna1 7:ed5870cfb3e0 101 void RosenEngine::update(Gamepad &pad)
ikenna1 7:ed5870cfb3e0 102 {
ikenna1 52:29772e31a620 103 // set the ship in use (kestrel imperion or orion)
ikenna1 52:29772e31a620 104 set_shipUsed();
ikenna1 52:29772e31a620 105 // update enemies
ikenna1 41:e1fa36c0492e 106 _enemy.update_seeker(_shipPos.x, _shipPos.y);
ikenna1 41:e1fa36c0492e 107 _enemy.update_shooter(_shipPos.x, _shipPos.y);
ikenna1 32:098fbc1222cd 108 _enemy.update_shw();
ikenna1 52:29772e31a620 109 // update ship weapons
ikenna1 52:29772e31a620 110 update_ship_weapon(pad);
ikenna1 52:29772e31a620 111 // check collisions
ikenna1 35:3341f2bd0408 112 shooter_ship_collision(pad);
ikenna1 35:3341f2bd0408 113 seeker_ship_collision(pad);
ikenna1 35:3341f2bd0408 114 shooterw_ship_collision(pad);
ikenna1 35:3341f2bd0408 115 imperionw_seeker_collision(pad);
ikenna1 35:3341f2bd0408 116 kestrelw_shooter_collision(pad);
ikenna1 35:3341f2bd0408 117 imperionw_shooter_collision(pad);
ikenna1 40:90c7a893d513 118 orionw_collision(pad);
ikenna1 52:29772e31a620 119 // check health
ikenna1 36:c25417f0d150 120 check_health();
ikenna1 52:29772e31a620 121 // increase difficulty over time
ikenna1 38:4571537238ed 122 scaling(timer(12));
ikenna1 40:90c7a893d513 123
ikenna1 7:ed5870cfb3e0 124 }
ikenna1 52:29772e31a620 125 void RosenEngine::update_ship_weapon(Gamepad &pad)
ikenna1 41:e1fa36c0492e 126 {
ikenna1 52:29772e31a620 127 // update ship weapons based on ship being used
ikenna1 43:500b8cff3715 128 if(_shipUsed == kestrel) {
ikenna1 41:e1fa36c0492e 129 _ship.update_ship(_joystick.x,_joystick.y);
ikenna1 41:e1fa36c0492e 130 _weapons.update();
ikenna1 41:e1fa36c0492e 131 kestrelw_seeker_collision(pad);
ikenna1 41:e1fa36c0492e 132 }
ikenna1 43:500b8cff3715 133 if(_shipUsed == imperion && A == false) {
ikenna1 41:e1fa36c0492e 134 _ship.update_ship(_joystick.x,_joystick.y);
ikenna1 41:e1fa36c0492e 135 _weapons.update();
ikenna1 41:e1fa36c0492e 136 }
ikenna1 43:500b8cff3715 137 if(_shipUsed == orion) {
ikenna1 41:e1fa36c0492e 138 _ship.update_ship(_joystick.x,_joystick.y);
ikenna1 41:e1fa36c0492e 139 _weapons.update();
ikenna1 41:e1fa36c0492e 140 }
ikenna1 41:e1fa36c0492e 141 }
ikenna1 9:241a1a7d8527 142 void RosenEngine::get_pos()
ikenna1 9:241a1a7d8527 143 {
ikenna1 52:29772e31a620 144 // get ship position
ikenna1 41:e1fa36c0492e 145 _shipPos = _ship.get_pos();
ikenna1 52:29772e31a620 146 // get enemy position
ikenna1 41:e1fa36c0492e 147 _seekerPos[0] = _enemy.get_seekerpos(1);
ikenna1 41:e1fa36c0492e 148 _seekerPos[1] = _enemy.get_seekerpos(2);
ikenna1 41:e1fa36c0492e 149 _seekerPos[2] = _enemy.get_seekerpos(3);
ikenna1 41:e1fa36c0492e 150 _shooterPos[0] = _enemy.get_shooterpos(1);
ikenna1 41:e1fa36c0492e 151 _shooterPos[1] = _enemy.get_shooterpos(2);
ikenna1 41:e1fa36c0492e 152 _shooterPos[2] = _enemy.get_shooterpos(3);
ikenna1 41:e1fa36c0492e 153 _shooterWPos[0] = _enemy.get_shwpos(1);
ikenna1 41:e1fa36c0492e 154 _shooterWPos[1] = _enemy.get_shwpos(2);
ikenna1 41:e1fa36c0492e 155 _shooterWPos[2] = _enemy.get_shwpos(3);
ikenna1 52:29772e31a620 156 // set ship size
ikenna1 52:29772e31a620 157 set_ship_size();
ikenna1 52:29772e31a620 158 // weapons are initialized here to pass accurate size values to the weapon class
ikenna1 41:e1fa36c0492e 159 _weapons.init(_shipPos.x, _shipPos.y, _shipWidth);
ikenna1 13:e114d362186d 160 _ycursor = _menu.get_ycursor();
ikenna1 41:e1fa36c0492e 161 }
ikenna1 41:e1fa36c0492e 162 void RosenEngine::set_ship_size()
ikenna1 41:e1fa36c0492e 163 {
ikenna1 52:29772e31a620 164 // change ship size depending on ship being used
ikenna1 43:500b8cff3715 165 switch (_shipUsed) {
ikenna1 43:500b8cff3715 166 case kestrel:
ikenna1 41:e1fa36c0492e 167 _shipWidth = 9;
ikenna1 41:e1fa36c0492e 168 _shipHeight = 6;
ikenna1 41:e1fa36c0492e 169 break;
ikenna1 43:500b8cff3715 170 case imperion:
ikenna1 41:e1fa36c0492e 171 _shipWidth = 7;
ikenna1 41:e1fa36c0492e 172 _shipHeight = 10;
ikenna1 41:e1fa36c0492e 173 break;
ikenna1 43:500b8cff3715 174 case orion:
ikenna1 41:e1fa36c0492e 175 _shipWidth = 7;
ikenna1 41:e1fa36c0492e 176 _shipHeight = 10;
ikenna1 41:e1fa36c0492e 177 break;
ikenna1 41:e1fa36c0492e 178 }
ikenna1 9:241a1a7d8527 179 }
ikenna1 9:241a1a7d8527 180 void RosenEngine::title(N5110 &lcd)
ikenna1 9:241a1a7d8527 181 {
ikenna1 52:29772e31a620 182 // display main menu screen
ikenna1 44:a6a361bea806 183 _menu.title(lcd,_shipUsed);
ikenna1 45:fe5fc85a5c73 184 _menu.update(_d,_joystick);
ikenna1 10:c33d7593a275 185 }
ikenna1 13:e114d362186d 186 int RosenEngine::get_ycursor()
ikenna1 10:c33d7593a275 187 {
ikenna1 13:e114d362186d 188 _ycursor = _menu.get_ycursor();
ikenna1 10:c33d7593a275 189 return _ycursor;
ikenna1 12:47578eb9ea73 190 }
ikenna1 52:29772e31a620 191 void RosenEngine::set_shipUsed()
ikenna1 43:500b8cff3715 192 {
ikenna1 52:29772e31a620 193 // assign the ship being used based on xcursor which is the cursor in
ikenna1 52:29772e31a620 194 // the ship select part of the menu
ikenna1 43:500b8cff3715 195 int shipno = _menu.get_xcursor();
ikenna1 43:500b8cff3715 196 _shipUsed = (SHIP)shipno;
ikenna1 43:500b8cff3715 197 }
ikenna1 12:47578eb9ea73 198 void RosenEngine::ship_select(N5110 &lcd)
ikenna1 12:47578eb9ea73 199 {
ikenna1 45:fe5fc85a5c73 200 _menu.update(_d,_joystick);
ikenna1 12:47578eb9ea73 201 _menu.disp_ships(lcd);
ikenna1 17:e65a9f981834 202 }
ikenna1 35:3341f2bd0408 203 bool RosenEngine::check_collision(int xpos1, int ypos1,int width1,int height1,int xpos2, int ypos2,int width2,int height2)
ikenna1 35:3341f2bd0408 204 {
ikenna1 35:3341f2bd0408 205 // Create arrays of all positions with appropriate height and length
ikenna1 35:3341f2bd0408 206 int xpos1_array[width1];
ikenna1 35:3341f2bd0408 207 int ypos1_array[height1];
ikenna1 35:3341f2bd0408 208 int xpos2_array[width2];
ikenna1 35:3341f2bd0408 209 int ypos2_array[height2];
ikenna1 35:3341f2bd0408 210 bool xcol = false;
ikenna1 35:3341f2bd0408 211 bool ycol = false;
ikenna1 35:3341f2bd0408 212 bool col = false;
ikenna1 35:3341f2bd0408 213 //printf("variables declared\n");
ikenna1 35:3341f2bd0408 214 // Loop through both height and width to get a 2D aray of the sprites position
ikenna1 52:29772e31a620 215 // and compare all positionr returning true is any match
ikenna1 35:3341f2bd0408 216 for(int cx = 0; cx<width1; cx=cx+1) {
ikenna1 35:3341f2bd0408 217 xpos1_array[cx]= xpos1 + cx;
ikenna1 35:3341f2bd0408 218 for(int nx = 0; nx<width2; nx=nx+1) {
ikenna1 35:3341f2bd0408 219 xpos2_array[nx]= xpos2 + nx;
ikenna1 35:3341f2bd0408 220 if(xpos2_array[nx] == xpos1_array[cx]) {
ikenna1 35:3341f2bd0408 221 xcol = true;
ikenna1 35:3341f2bd0408 222 }
ikenna1 35:3341f2bd0408 223 }
ikenna1 35:3341f2bd0408 224 }
ikenna1 35:3341f2bd0408 225 //printf("first loop done\n");
ikenna1 35:3341f2bd0408 226 for(int cy = 0; cy<height1; cy=cy+1) {
ikenna1 35:3341f2bd0408 227 ypos1_array[cy]= ypos1 + cy;
ikenna1 35:3341f2bd0408 228 for(int ny = 0; ny<height2; ny=ny+1) {
ikenna1 35:3341f2bd0408 229 ypos2_array[ny]= ypos2 + ny;
ikenna1 35:3341f2bd0408 230 if(ypos2_array[ny] == ypos1_array[cy]) {
ikenna1 35:3341f2bd0408 231 ycol = true;
ikenna1 35:3341f2bd0408 232 }
ikenna1 35:3341f2bd0408 233 }
ikenna1 35:3341f2bd0408 234 }
ikenna1 35:3341f2bd0408 235 // if both the hight and width position values are equal a collision has occured
ikenna1 35:3341f2bd0408 236 col = (xcol & ycol);
ikenna1 35:3341f2bd0408 237 return col;
ikenna1 35:3341f2bd0408 238 }
ikenna1 35:3341f2bd0408 239 bool RosenEngine::check_collision1(int xpos1,int width1,int xpos2,int width2)
ikenna1 35:3341f2bd0408 240 {
ikenna1 52:29772e31a620 241 // Create arrays of all positions with appropriate width values
ikenna1 35:3341f2bd0408 242 int xpos1_array[width1];
ikenna1 35:3341f2bd0408 243 int xpos2_array[width2];
ikenna1 35:3341f2bd0408 244 bool xcol = false;
ikenna1 52:29772e31a620 245 // loop through both arrays comparing their values and return true if any are equal
ikenna1 35:3341f2bd0408 246 for(int cx = 0; cx<width1; cx=cx+1) {
ikenna1 35:3341f2bd0408 247 xpos1_array[cx]= xpos1 + cx;
ikenna1 35:3341f2bd0408 248 for(int nx = 0; nx<width2; nx=nx+1) {
ikenna1 35:3341f2bd0408 249 xpos2_array[nx]= xpos2 + nx;
ikenna1 35:3341f2bd0408 250 if(xpos2_array[nx] == xpos1_array[cx]) {
ikenna1 35:3341f2bd0408 251 xcol = true;
ikenna1 35:3341f2bd0408 252 }
ikenna1 35:3341f2bd0408 253 }
ikenna1 35:3341f2bd0408 254 }
ikenna1 35:3341f2bd0408 255 return xcol;
ikenna1 35:3341f2bd0408 256 }
ikenna1 35:3341f2bd0408 257
ikenna1 35:3341f2bd0408 258 void RosenEngine::seeker_ship_collision(Gamepad &pad)
ikenna1 35:3341f2bd0408 259 {
ikenna1 39:7824f9080f59 260 bool col1,col2,col3;
ikenna1 52:29772e31a620 261 // Check for any collisions using the check_collision function
ikenna1 41:e1fa36c0492e 262 col1 = check_collision(_shipPos.x,_shipPos.y,9,6,_seekerPos[0].x, _seekerPos[0].y,10,7);
ikenna1 41:e1fa36c0492e 263 col2 = check_collision(_shipPos.x,_shipPos.y,9,6,_seekerPos[1].x, _seekerPos[1].y,10,7);
ikenna1 41:e1fa36c0492e 264 col3 = check_collision(_shipPos.x,_shipPos.y,9,6,_seekerPos[2].x, _seekerPos[2].y,10,7);
ikenna1 39:7824f9080f59 265 int sel = 0;
ikenna1 39:7824f9080f59 266 if (col1 == true && _no_seekers >= 1) {
ikenna1 39:7824f9080f59 267 sel = 1;
ikenna1 39:7824f9080f59 268 }
ikenna1 39:7824f9080f59 269 if (col2 == true && _no_seekers >= 2) {
ikenna1 39:7824f9080f59 270 sel = 2;
ikenna1 39:7824f9080f59 271 }
ikenna1 39:7824f9080f59 272 if (col3 == true && _no_seekers >= 3) {
ikenna1 39:7824f9080f59 273 sel = 3;
ikenna1 39:7824f9080f59 274 }
ikenna1 39:7824f9080f59 275 // printf("col1 = %d, col2 = %d, col3 = %d, no_seekers = %d\n",col1,col2,col3,_no_seekers);
ikenna1 52:29772e31a620 276 // if there is a collision between any seeker and the ship update both the seeker and the ships health
ikenna1 40:90c7a893d513 277 if(sel != 0) {
ikenna1 39:7824f9080f59 278 _health.update(5,pad);
ikenna1 39:7824f9080f59 279 _health.seekerh_update(sel,10);
ikenna1 35:3341f2bd0408 280 pad.tone(500,0.05);
ikenna1 35:3341f2bd0408 281 wait(0.05);
ikenna1 35:3341f2bd0408 282 }
ikenna1 35:3341f2bd0408 283 }
ikenna1 35:3341f2bd0408 284 void RosenEngine::shooter_ship_collision(Gamepad &pad)
ikenna1 35:3341f2bd0408 285 {
ikenna1 39:7824f9080f59 286 bool col1,col2,col3;
ikenna1 39:7824f9080f59 287 int sel = 0;
ikenna1 52:29772e31a620 288 // Check for any collisions using the check_collision function
ikenna1 41:e1fa36c0492e 289 col1 = check_collision(_shipPos.x,_shipPos.y,9,6,_shooterPos[0].x, _shooterPos[1].y,10,7);
ikenna1 41:e1fa36c0492e 290 col2 = check_collision(_shipPos.x,_shipPos.y,9,6,_shooterPos[1].x, _shooterPos[0].y,10,7);
ikenna1 41:e1fa36c0492e 291 col3 = check_collision(_shipPos.x,_shipPos.y,9,6,_shooterPos[2].x, _shooterPos[0].y,10,7);
ikenna1 35:3341f2bd0408 292
ikenna1 39:7824f9080f59 293 if(col1 == true && _no_shooters >= 1) {
ikenna1 39:7824f9080f59 294 sel = 1;
ikenna1 39:7824f9080f59 295 }
ikenna1 39:7824f9080f59 296 if(col2 == true && _no_shooters >= 2) {
ikenna1 39:7824f9080f59 297 sel = 2;
ikenna1 39:7824f9080f59 298 }
ikenna1 39:7824f9080f59 299 if(col3 == true && _no_shooters >= 3) {
ikenna1 39:7824f9080f59 300 sel = 3;
ikenna1 39:7824f9080f59 301 }
ikenna1 52:29772e31a620 302 // if there is a collision between any shooter and the ship update both the shooter and the ships health
ikenna1 52:29772e31a620 303 if(sel != 0) {// sel is default 0 and will only change if a collision occurs
ikenna1 35:3341f2bd0408 304 _health.update(1,pad);
ikenna1 39:7824f9080f59 305 _health.shooterh_update(sel,10);
ikenna1 35:3341f2bd0408 306 pad.tone(500,0.05);
ikenna1 40:90c7a893d513 307 wait(0.05);
ikenna1 35:3341f2bd0408 308 }
ikenna1 35:3341f2bd0408 309 }
ikenna1 35:3341f2bd0408 310 void RosenEngine::shooterw_ship_collision(Gamepad &pad)
ikenna1 35:3341f2bd0408 311 {
ikenna1 39:7824f9080f59 312 bool col1,col2,col3;
ikenna1 39:7824f9080f59 313 int sel = 0;
ikenna1 52:29772e31a620 314 // Check for any collisions using the check_collision function
ikenna1 41:e1fa36c0492e 315 col1 = check_collision(_shipPos.x,_shipPos.y,9,6,_shooterWPos[0].x, _shooterWPos[1].y,2,2);
ikenna1 41:e1fa36c0492e 316 col2 = check_collision(_shipPos.x,_shipPos.y,9,6,_shooterWPos[1].x, _shooterWPos[2].y,2,2);
ikenna1 41:e1fa36c0492e 317 col3 = check_collision(_shipPos.x,_shipPos.y,9,6,_shooterWPos[2].x, _shooterWPos[2].y,2,2);
ikenna1 35:3341f2bd0408 318
ikenna1 39:7824f9080f59 319 if(col1 == true && _no_shooters >= 1) {
ikenna1 39:7824f9080f59 320 sel = 1;
ikenna1 39:7824f9080f59 321 }
ikenna1 39:7824f9080f59 322 if(col2 == true && _no_shooters >= 2) {
ikenna1 39:7824f9080f59 323 sel = 2;
ikenna1 39:7824f9080f59 324 }
ikenna1 39:7824f9080f59 325 if(col3 == true && _no_shooters >= 3) {
ikenna1 39:7824f9080f59 326 sel = 3;
ikenna1 39:7824f9080f59 327 }
ikenna1 52:29772e31a620 328 // if there is a collision and the ship was hit update the ships damage
ikenna1 52:29772e31a620 329 if(sel != 0) {// sel is default 0 and will only change if a collision occurs
ikenna1 35:3341f2bd0408 330 _health.update(1,pad);
ikenna1 35:3341f2bd0408 331 pad.tone(500,0.05);
ikenna1 40:90c7a893d513 332 wait(0.05);
ikenna1 35:3341f2bd0408 333 }
ikenna1 35:3341f2bd0408 334 }
ikenna1 35:3341f2bd0408 335 void RosenEngine::kestrelw_seeker_collision(Gamepad &pad)
ikenna1 52:29772e31a620 336 { // the kestrel fires a projectile whose x and y values must be checked with the
ikenna1 52:29772e31a620 337 // x, and y, values of every enemy to check for a collision
ikenna1 39:7824f9080f59 338 bool col1, col2, col3;
ikenna1 39:7824f9080f59 339 int sel = 0;
ikenna1 52:29772e31a620 340 Vector2D missle_pos = _weapons.get_pos(_shipUsed); // get the kestrel's projectile position
ikenna1 52:29772e31a620 341 // Check for any collisions using the check_collision function
ikenna1 41:e1fa36c0492e 342 col1 = check_collision(_seekerPos[0].x,_seekerPos[0].y,9,6,missle_pos.x,missle_pos.y,1,1);
ikenna1 41:e1fa36c0492e 343 col2 = check_collision(_seekerPos[1].x,_seekerPos[1].y,9,6,missle_pos.x,missle_pos.y,1,1);
ikenna1 41:e1fa36c0492e 344 col3 = check_collision(_seekerPos[2].x,_seekerPos[2].y,9,6,missle_pos.x,missle_pos.y,1,1);
ikenna1 39:7824f9080f59 345 if (col1 == true && _no_seekers >= 1) {
ikenna1 39:7824f9080f59 346 sel = 1;
ikenna1 39:7824f9080f59 347 }
ikenna1 39:7824f9080f59 348 if (col2 == true && _no_seekers >= 2) {
ikenna1 39:7824f9080f59 349 sel = 2;
ikenna1 39:7824f9080f59 350 }
ikenna1 39:7824f9080f59 351 if (col3 == true && _no_seekers >= 3) {
ikenna1 39:7824f9080f59 352 sel = 3;
ikenna1 39:7824f9080f59 353 }
ikenna1 52:29772e31a620 354 // if there are collisions update the seekers health
ikenna1 52:29772e31a620 355 if(sel != 0) {// sel is default 0 and will only change if a collision occurs
ikenna1 35:3341f2bd0408 356 pad.tone(500,0.05);
ikenna1 39:7824f9080f59 357 _health.seekerh_update(sel,5);
ikenna1 35:3341f2bd0408 358 wait(0.05);
ikenna1 35:3341f2bd0408 359 }
ikenna1 35:3341f2bd0408 360 }
ikenna1 35:3341f2bd0408 361 void RosenEngine::imperionw_seeker_collision(Gamepad &pad)
ikenna1 52:29772e31a620 362 { // the imperion fires a lazer to the top of screen as such we don't need to check all the
ikenna1 52:29772e31a620 363 // x, y values for the sprite to be sure of collisions just the x value. as long as the ship is above
ikenna1 52:29772e31a620 364 // the targetted enemy a collision will register
ikenna1 39:7824f9080f59 365 bool col1,col2,col3;
ikenna1 39:7824f9080f59 366 int sel = 0;
ikenna1 43:500b8cff3715 367 if(_shipUsed == imperion) {
ikenna1 52:29772e31a620 368 if(_shipPos.y > _seekerPos[0].y + 6) { // only regiser if seeker is above ship
ikenna1 52:29772e31a620 369 col1 = check_collision1(_seekerPos[0].x,9,_shipPos.x + 2,3); // check for collision using the check_collision1 function
ikenna1 52:29772e31a620 370 if (col1 == true && A == true && _no_seekers >= 1) {
ikenna1 52:29772e31a620 371 sel = 1; // if there is a collision set an appropriate select value
ikenna1 41:e1fa36c0492e 372 }
ikenna1 35:3341f2bd0408 373 }
ikenna1 41:e1fa36c0492e 374 if(_shipPos.y > _seekerPos[1].y + 6) {
ikenna1 41:e1fa36c0492e 375 col2 = check_collision1(_seekerPos[1].x,9,_shipPos.x + 2,3);
ikenna1 41:e1fa36c0492e 376 if (col2 == true && A == true && _no_seekers >= 2) {
ikenna1 41:e1fa36c0492e 377 sel = 2;
ikenna1 41:e1fa36c0492e 378 }
ikenna1 39:7824f9080f59 379 }
ikenna1 41:e1fa36c0492e 380 if(_shipPos.y > _seekerPos[0].y + 6) {
ikenna1 41:e1fa36c0492e 381 col3 = check_collision1(_seekerPos[2].x,9,_shipPos.x + 2,3);
ikenna1 41:e1fa36c0492e 382 if (col3 == true && A == true && _no_seekers >= 3) {
ikenna1 41:e1fa36c0492e 383 sel = 3;
ikenna1 41:e1fa36c0492e 384 }
ikenna1 39:7824f9080f59 385 }
ikenna1 39:7824f9080f59 386 }
ikenna1 52:29772e31a620 387 // if a collision occured update the seekers health
ikenna1 52:29772e31a620 388 if(sel != 0) {// sel is default 0 and will only change if a collision occurs
ikenna1 39:7824f9080f59 389 _health.seekerh_update(sel,10);
ikenna1 39:7824f9080f59 390 pad.tone(500,0.05);
ikenna1 39:7824f9080f59 391 wait(0.05);
ikenna1 39:7824f9080f59 392 }
ikenna1 35:3341f2bd0408 393 }
ikenna1 35:3341f2bd0408 394 void RosenEngine::kestrelw_shooter_collision(Gamepad &pad)
ikenna1 35:3341f2bd0408 395 {
ikenna1 52:29772e31a620 396 Vector2D missle_pos = _weapons.get_pos(_shipUsed); // get the kestrel's projectile position
ikenna1 35:3341f2bd0408 397 bool col1, col2, col3;
ikenna1 39:7824f9080f59 398 int sel = 0;
ikenna1 52:29772e31a620 399 // Check for any collisions using the check_collision function
ikenna1 41:e1fa36c0492e 400 col1 = check_collision(_shooterPos[0].x,_shooterPos[0].y,9,6,missle_pos.x,missle_pos.y,1,1);
ikenna1 41:e1fa36c0492e 401 col2 = check_collision(_shooterPos[1].x,_shooterPos[1].y,9,6,missle_pos.x,missle_pos.y,1,1);
ikenna1 41:e1fa36c0492e 402 col3 = check_collision(_shooterPos[2].x,_shooterPos[2].y,9,6,missle_pos.x,missle_pos.y,1,1);
ikenna1 39:7824f9080f59 403 if (col1 == true && _no_shooters >= 1) {
ikenna1 39:7824f9080f59 404 sel = 1;
ikenna1 39:7824f9080f59 405 }
ikenna1 39:7824f9080f59 406 if (col2 == true && _no_shooters >= 2) {
ikenna1 39:7824f9080f59 407 sel = 2;
ikenna1 35:3341f2bd0408 408 }
ikenna1 39:7824f9080f59 409 if (col3 == true && _no_shooters >= 3) {
ikenna1 39:7824f9080f59 410 sel = 3;
ikenna1 39:7824f9080f59 411 }
ikenna1 52:29772e31a620 412 // if a collision did occur update the right shooter using the select value
ikenna1 52:29772e31a620 413 if(sel != 0) { // sel is default 0 and will only change if a collision occurs
ikenna1 35:3341f2bd0408 414 pad.tone(500,0.05);
ikenna1 39:7824f9080f59 415 _health.shooterh_update(sel,5);
ikenna1 35:3341f2bd0408 416 wait(0.05);
ikenna1 35:3341f2bd0408 417 }
ikenna1 35:3341f2bd0408 418 }
ikenna1 35:3341f2bd0408 419 void RosenEngine::imperionw_shooter_collision(Gamepad &pad)
ikenna1 35:3341f2bd0408 420 {
ikenna1 39:7824f9080f59 421 bool col1,col2,col3;
ikenna1 39:7824f9080f59 422 int sel = 0;
ikenna1 52:29772e31a620 423 // check collision using check_collision1 function
ikenna1 44:a6a361bea806 424 if(_shipUsed == imperion) {
ikenna1 41:e1fa36c0492e 425 if(_shipPos.y > _shooterPos[0].y + 6) {
ikenna1 41:e1fa36c0492e 426 col1 = check_collision1(_shooterPos[0].x,9,_shipPos.x + 2,3);
ikenna1 40:90c7a893d513 427 if (col1 == true && A == true && _no_shooters >= 1) {
ikenna1 40:90c7a893d513 428 sel = 1;
ikenna1 40:90c7a893d513 429 }
ikenna1 40:90c7a893d513 430 }
ikenna1 41:e1fa36c0492e 431 if(_shipPos.y > _shooterPos[1].y + 6) {
ikenna1 41:e1fa36c0492e 432 col2 = check_collision1(_shooterPos[1].x,9,_shipPos.x + 2,3);
ikenna1 40:90c7a893d513 433 if (col2 == true && A == true && _no_shooters >= 2) {
ikenna1 40:90c7a893d513 434 sel = 2;
ikenna1 40:90c7a893d513 435 }
ikenna1 40:90c7a893d513 436 }
ikenna1 41:e1fa36c0492e 437 if(_shipPos.y > _shooterPos[2].y + 6) {
ikenna1 41:e1fa36c0492e 438 col3 = check_collision1(_shooterPos[2].x,9,_shipPos.x + 2,3);
ikenna1 40:90c7a893d513 439 if (col3 == true && A == true && _no_shooters >= 3) {
ikenna1 40:90c7a893d513 440 sel = 3;
ikenna1 40:90c7a893d513 441 }
ikenna1 35:3341f2bd0408 442 }
ikenna1 35:3341f2bd0408 443 }
ikenna1 52:29772e31a620 444 // if a collision did occur update the shooters health
ikenna1 52:29772e31a620 445 if(sel != 0) {// sel is default 0 and will only change if a collision occurs
ikenna1 39:7824f9080f59 446 _health.shooterh_update(sel,10);
ikenna1 39:7824f9080f59 447 pad.tone(500,0.05);
ikenna1 39:7824f9080f59 448 wait(0.05);
ikenna1 39:7824f9080f59 449 }
ikenna1 36:c25417f0d150 450 }
ikenna1 40:90c7a893d513 451 void RosenEngine::orionw_collision(Gamepad &pad)
ikenna1 52:29772e31a620 452 { // the orions weapon is an arc weapon that latches on to the nearest enemy.
ikenna1 52:29772e31a620 453 // as the nearest enemy position has already been found in dind_closest()
ikenna1 52:29772e31a620 454 // all we need to do now is ensure that the enemy is within range of the orions weapon
ikenna1 40:90c7a893d513 455 Vector2D inde = find_closest1();
ikenna1 40:90c7a893d513 456 int index1 = inde.x;
ikenna1 40:90c7a893d513 457 int index2 = inde.x - 3;
ikenna1 40:90c7a893d513 458 int distance = inde.y;
ikenna1 41:e1fa36c0492e 459
ikenna1 41:e1fa36c0492e 460 // enemy 1,2 and 3 are shooters
ikenna1 52:29772e31a620 461 if(_no_shooters >= index1 && A == true && distance < 15) {// the wepon fires when a is pressed and the enemy ship is within the distaance
ikenna1 40:90c7a893d513 462 _health.shooterh_update(index1,10);
ikenna1 40:90c7a893d513 463 pad.tone(500,0.05);
ikenna1 40:90c7a893d513 464 wait(0.05);
ikenna1 40:90c7a893d513 465 }
ikenna1 41:e1fa36c0492e 466 // enemy 4,5 and 6 are seekers so 3 is subtracted from inde.x to compensate
ikenna1 41:e1fa36c0492e 467 if(_no_seekers >= index2 && A == true && distance < 15) {
ikenna1 40:90c7a893d513 468 _health.seekerh_update(index2,10);
ikenna1 40:90c7a893d513 469 pad.tone(500,0.05);
ikenna1 40:90c7a893d513 470 wait(0.05);
ikenna1 40:90c7a893d513 471 }
ikenna1 41:e1fa36c0492e 472
ikenna1 41:e1fa36c0492e 473
ikenna1 40:90c7a893d513 474 }
ikenna1 40:90c7a893d513 475 void RosenEngine::check_health()
ikenna1 40:90c7a893d513 476 {
ikenna1 41:e1fa36c0492e 477 // check player and enemy health
ikenna1 39:7824f9080f59 478 check_se_health();
ikenna1 39:7824f9080f59 479 check_sh_health();
ikenna1 36:c25417f0d150 480 Vector2D hp = _health.get_hp();
ikenna1 40:90c7a893d513 481 if(hp.x <= 0) {
ikenna1 38:4571537238ed 482 // printf("player deaad\n");
ikenna1 36:c25417f0d150 483 _dead = true;
ikenna1 40:90c7a893d513 484 }
ikenna1 40:90c7a893d513 485
ikenna1 39:7824f9080f59 486 }
ikenna1 39:7824f9080f59 487 void RosenEngine::check_se_health()
ikenna1 39:7824f9080f59 488 {
ikenna1 52:29772e31a620 489 // set sel (select) as default 0
ikenna1 39:7824f9080f59 490 int sel = 0;
ikenna1 52:29772e31a620 491 // get the seekers health
ikenna1 39:7824f9080f59 492 int seeker1_health = _health.get_seekerh(1);
ikenna1 39:7824f9080f59 493 int seeker2_health = _health.get_seekerh(2);
ikenna1 39:7824f9080f59 494 int seeker3_health = _health.get_seekerh(3);
ikenna1 39:7824f9080f59 495 // printf("seeker1_h = %d, seeker2_h = %d, seeker3_h = %d\n",seeker1_health,seeker2_health,seeker3_health);
ikenna1 52:29772e31a620 496 // set sel (select) depending on if any seekers health falls below zero
ikenna1 40:90c7a893d513 497 if(seeker1_health == 0) {
ikenna1 39:7824f9080f59 498 sel = 1;
ikenna1 39:7824f9080f59 499 }
ikenna1 40:90c7a893d513 500 if(seeker2_health == 0) {
ikenna1 39:7824f9080f59 501 sel = 2;
ikenna1 39:7824f9080f59 502 }
ikenna1 40:90c7a893d513 503 if(seeker3_health == 0) {
ikenna1 39:7824f9080f59 504 sel = 3;
ikenna1 39:7824f9080f59 505 }
ikenna1 52:29772e31a620 506 // reset seeker and update score when seekers health goes to zero
ikenna1 52:29772e31a620 507 if(sel != 0) {
ikenna1 39:7824f9080f59 508 _enemy.reset_seeker(sel);
ikenna1 39:7824f9080f59 509 _health.reset_seekerh(sel);
ikenna1 39:7824f9080f59 510 _score = _score + 10;
ikenna1 39:7824f9080f59 511 }
ikenna1 39:7824f9080f59 512 }
ikenna1 39:7824f9080f59 513 void RosenEngine::check_sh_health()
ikenna1 52:29772e31a620 514 { // set sel (select) as default 0
ikenna1 39:7824f9080f59 515 int sel = 0;
ikenna1 52:29772e31a620 516 // get the shooters health
ikenna1 39:7824f9080f59 517 int shooter1_health = _health.get_shooterh(1);
ikenna1 39:7824f9080f59 518 int shooter2_health = _health.get_shooterh(2);
ikenna1 39:7824f9080f59 519 int shooter3_health = _health.get_shooterh(3);
ikenna1 52:29772e31a620 520 // set sel depending on if any shooters health falls below zero
ikenna1 40:90c7a893d513 521 if(shooter1_health == 0) {
ikenna1 39:7824f9080f59 522 sel = 1;
ikenna1 39:7824f9080f59 523 }
ikenna1 40:90c7a893d513 524 if(shooter2_health == 0) {
ikenna1 39:7824f9080f59 525 sel = 2;
ikenna1 39:7824f9080f59 526 }
ikenna1 40:90c7a893d513 527 if(shooter3_health == 0) {
ikenna1 39:7824f9080f59 528 sel = 3;
ikenna1 39:7824f9080f59 529 }
ikenna1 41:e1fa36c0492e 530 // reset shooter and update score when shooter health goes to zero
ikenna1 40:90c7a893d513 531 if(sel != 0) {
ikenna1 39:7824f9080f59 532 _enemy.reset_shooter(sel);
ikenna1 39:7824f9080f59 533 _health.reset_shooterh(sel);
ikenna1 39:7824f9080f59 534 _score = _score + 20;
ikenna1 39:7824f9080f59 535 }
ikenna1 37:8d8c8cce0bc7 536 }
ikenna1 37:8d8c8cce0bc7 537 int RosenEngine::rand_no()
ikenna1 37:8d8c8cce0bc7 538 {
ikenna1 41:e1fa36c0492e 539 // seeds and returns a random number using the ctime library
ikenna1 37:8d8c8cce0bc7 540 srand(time(NULL));
ikenna1 37:8d8c8cce0bc7 541 int rand_no = (rand() %45) + 1;
ikenna1 37:8d8c8cce0bc7 542 return rand_no;
ikenna1 38:4571537238ed 543 }
ikenna1 38:4571537238ed 544 float RosenEngine::timer(int fps)
ikenna1 38:4571537238ed 545 {
ikenna1 52:29772e31a620 546 // using the amount of times run and the frames per second set the amount of
ikenna1 52:29772e31a620 547 // time past is calculated
ikenna1 38:4571537238ed 548 _times_run = _times_run + 1;
ikenna1 38:4571537238ed 549 float time_frame = 1.0f/fps;
ikenna1 40:90c7a893d513 550 float time_elapsed = _times_run * time_frame;
ikenna1 38:4571537238ed 551 // printf("time elapsed = %f,time frame = %f, _times_run = %d\n",time_elapsed,time_frame,_times_run);
ikenna1 38:4571537238ed 552 return time_elapsed;
ikenna1 38:4571537238ed 553 }
ikenna1 38:4571537238ed 554 void RosenEngine::score(int points)
ikenna1 38:4571537238ed 555 {
ikenna1 38:4571537238ed 556 _score = _score + points;
ikenna1 38:4571537238ed 557 }
ikenna1 38:4571537238ed 558 bool RosenEngine::dead()
ikenna1 38:4571537238ed 559 {
ikenna1 38:4571537238ed 560 return _dead;
ikenna1 38:4571537238ed 561 }
ikenna1 38:4571537238ed 562
ikenna1 38:4571537238ed 563 void RosenEngine::scaling(float time_elapsed)
ikenna1 38:4571537238ed 564 {
ikenna1 41:e1fa36c0492e 565 // increases difficulty as game progresses
ikenna1 52:29772e31a620 566 if(time_elapsed == _wait_time) { // check if time elapsed is equal to the wit time
ikenna1 52:29772e31a620 567 _no_shooters = _no_shooters + 1; // update number of shooters and seekers after wait time
ikenna1 52:29772e31a620 568 _no_seekers = _no_seekers + 1; //increase wait time
ikenna1 38:4571537238ed 569 _enemy.sh_scaling(time_elapsed);
ikenna1 39:7824f9080f59 570 _wait_time = _wait_time + 10.00;
ikenna1 38:4571537238ed 571 }
ikenna1 52:29772e31a620 572 // limit the number of seekers and number of shooters to 3
ikenna1 40:90c7a893d513 573 if(_no_shooters > 3) {
ikenna1 38:4571537238ed 574 _no_shooters = 3;
ikenna1 39:7824f9080f59 575 }
ikenna1 40:90c7a893d513 576 if(_no_seekers > 3) {
ikenna1 39:7824f9080f59 577 _no_seekers = 3;
ikenna1 39:7824f9080f59 578 }
ikenna1 52:29772e31a620 579 // set approriate enemy numbers
ikenna1 38:4571537238ed 580 _enemy.set_noshooters(_no_shooters);
ikenna1 39:7824f9080f59 581 _enemy.set_noseekers(_no_seekers);
ikenna1 39:7824f9080f59 582 // printf("time_elapsed = %f, no_shooters = %d, wait_time = %f\n",time_elapsed,_no_shooters,_wait_time);
ikenna1 38:4571537238ed 583 }
ikenna1 38:4571537238ed 584 void RosenEngine::game_over(N5110 &lcd)
ikenna1 38:4571537238ed 585 {
ikenna1 41:e1fa36c0492e 586 // Display random tips after every loss
ikenna1 41:e1fa36c0492e 587 _lore.display(lcd,rand_no());
ikenna1 39:7824f9080f59 588 }
ikenna1 39:7824f9080f59 589 void RosenEngine::intro(N5110 &lcd)
ikenna1 39:7824f9080f59 590 {
ikenna1 52:29772e31a620 591 // display intro
ikenna1 41:e1fa36c0492e 592 _lore.intro(lcd);
ikenna1 39:7824f9080f59 593 _intro = true;
ikenna1 39:7824f9080f59 594 }
ikenna1 39:7824f9080f59 595 void RosenEngine::disp_points(N5110 &lcd)
ikenna1 39:7824f9080f59 596 {
ikenna1 52:29772e31a620 597 // display players points on screen
ikenna1 39:7824f9080f59 598 char buffer[10];
ikenna1 39:7824f9080f59 599 sprintf(buffer,"%d",_score);
ikenna1 39:7824f9080f59 600 lcd.printString(buffer,2,0);
ikenna1 39:7824f9080f59 601 }
ikenna1 40:90c7a893d513 602 Vector2D RosenEngine::get_enemynum()
ikenna1 40:90c7a893d513 603 {
ikenna1 41:e1fa36c0492e 604 // return the number of enemy shooters and seekers currently on play
ikenna1 40:90c7a893d513 605 return{_no_shooters,_no_seekers};
ikenna1 40:90c7a893d513 606 }
ikenna1 40:90c7a893d513 607 int RosenEngine::range(int x1, int y1, float x2, float y2)
ikenna1 40:90c7a893d513 608 {
ikenna1 41:e1fa36c0492e 609 // calculates average distance between two points
ikenna1 40:90c7a893d513 610 float rangex = (abs(x1 - x2));
ikenna1 40:90c7a893d513 611 float rangey = (abs(y1 - y2));
ikenna1 40:90c7a893d513 612 int distance = floor((rangex+rangey)/2);
ikenna1 40:90c7a893d513 613 return distance;
ikenna1 40:90c7a893d513 614 }
ikenna1 40:90c7a893d513 615 Vector2D RosenEngine::find_closest1()
ikenna1 40:90c7a893d513 616 {
ikenna1 40:90c7a893d513 617 // get the distance for all enemies
ikenna1 41:e1fa36c0492e 618 int sh1 = range(_shipPos.x,_shipPos.y,_shooterPos[0].x,_shooterPos[0].y);
ikenna1 41:e1fa36c0492e 619 int sh2 = range(_shipPos.x,_shipPos.y,_shooterPos[1].x,_shooterPos[1].y);
ikenna1 41:e1fa36c0492e 620 int sh3 = range(_shipPos.x,_shipPos.y,_shooterPos[2].x,_shooterPos[2].y);
ikenna1 41:e1fa36c0492e 621 int se1 = range(_shipPos.x,_shipPos.y,_seekerPos[0].x,_seekerPos[0].y);
ikenna1 41:e1fa36c0492e 622 int se2 = range(_shipPos.x,_shipPos.y,_seekerPos[1].x,_seekerPos[1].y);
ikenna1 41:e1fa36c0492e 623 int se3 = range(_shipPos.x,_shipPos.y,_seekerPos[2].x,_seekerPos[2].y);
ikenna1 40:90c7a893d513 624
ikenna1 40:90c7a893d513 625 int close[6] = {sh1,sh2,sh3,se1,se2,se3};
ikenna1 40:90c7a893d513 626 // find index of the smallest element
ikenna1 40:90c7a893d513 627 int index = 0;
ikenna1 40:90c7a893d513 628 int smallest = close[0];
ikenna1 40:90c7a893d513 629 for(int i=0; i<6; i=i+1) {
ikenna1 40:90c7a893d513 630 if(smallest>close[i]) {
ikenna1 40:90c7a893d513 631 smallest=close[i];
ikenna1 40:90c7a893d513 632 index = i;
ikenna1 40:90c7a893d513 633 }
ikenna1 40:90c7a893d513 634 }
ikenna1 41:e1fa36c0492e 635 // return the index, so we know what the closest enemy, and the distance to check its in range
ikenna1 40:90c7a893d513 636 return {(index + 1),smallest};
ikenna1 40:90c7a893d513 637 }
ikenna1 40:90c7a893d513 638 Vector2D RosenEngine::find_closest2(int index)
ikenna1 40:90c7a893d513 639 {
ikenna1 40:90c7a893d513 640 // return the position of the closest enemy
ikenna1 40:90c7a893d513 641 if(index == 1 && _no_shooters >= 1) {
ikenna1 41:e1fa36c0492e 642 return {_shooterPos[0].x,_shooterPos[0].y};
ikenna1 40:90c7a893d513 643 }
ikenna1 40:90c7a893d513 644 if(index == 2 && _no_shooters >= 2) {
ikenna1 41:e1fa36c0492e 645 return {_shooterPos[1].x,_shooterPos[1].y};
ikenna1 40:90c7a893d513 646 }
ikenna1 40:90c7a893d513 647 if(index == 3 && _no_shooters >= 3) {
ikenna1 41:e1fa36c0492e 648 return {_shooterPos[2].x,_shooterPos[2].y};
ikenna1 40:90c7a893d513 649 }
ikenna1 40:90c7a893d513 650 if(index == 4 && _no_seekers >= 1 ) {
ikenna1 41:e1fa36c0492e 651 return {_seekerPos[0].x,_seekerPos[0].y};
ikenna1 40:90c7a893d513 652 }
ikenna1 40:90c7a893d513 653 if(index == 5 && _no_seekers >= 2) {
ikenna1 41:e1fa36c0492e 654 return {_seekerPos[1].x,_seekerPos[1].y};
ikenna1 40:90c7a893d513 655 }
ikenna1 40:90c7a893d513 656 if(index == 6 && _no_seekers >= 3) {
ikenna1 41:e1fa36c0492e 657 return {_seekerPos[2].x,_seekerPos[2].y};
ikenna1 40:90c7a893d513 658 }
ikenna1 40:90c7a893d513 659
ikenna1 40:90c7a893d513 660 }
ikenna1 52:29772e31a620 661 Vector2D RosenEngine::get_shipPos()
ikenna1 52:29772e31a620 662 {
ikenna1 52:29772e31a620 663 return{_shipPos};
ikenna1 52:29772e31a620 664 }