ELEC2645 (2018/19) / Mbed 2 deprecated el17aio

Dependencies:   mbed

Committer:
ikenna1
Date:
Thu May 09 05:59:31 2019 +0000
Revision:
49:aa204bf7ee2e
Parent:
45:fe5fc85a5c73
Child:
52:29772e31a620
Documentation test

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