Ikenna Adrian Ozoemena 201157039

Dependencies:   mbed

Committer:
ikenna1
Date:
Thu May 09 08:42:25 2019 +0000
Revision:
51:2231e2e141b9
Parent:
45:fe5fc85a5c73
Documentation test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ikenna1 15:009ccc07bb57 1 #include "Enemy.h"
ikenna1 15:009ccc07bb57 2
ikenna1 15:009ccc07bb57 3 Enemy::Enemy()
ikenna1 15:009ccc07bb57 4 {
ikenna1 15:009ccc07bb57 5
ikenna1 15:009ccc07bb57 6 }
ikenna1 15:009ccc07bb57 7
ikenna1 15:009ccc07bb57 8 Enemy::~Enemy()
ikenna1 15:009ccc07bb57 9 {
ikenna1 15:009ccc07bb57 10
ikenna1 15:009ccc07bb57 11 }
ikenna1 51:2231e2e141b9 12 // enemy sprites
ikenna1 15:009ccc07bb57 13 const int seeker[7][9] = {
ikenna1 15:009ccc07bb57 14 {1,1,0,0,0,0,0,1,1},
ikenna1 15:009ccc07bb57 15 {1,0,1,0,0,0,1,0,1},
ikenna1 15:009ccc07bb57 16 {1,0,0,1,1,1,0,0,1},
ikenna1 15:009ccc07bb57 17 {0,1,0,0,0,0,0,1,0},
ikenna1 15:009ccc07bb57 18 {0,0,1,0,0,0,1,0,0},
ikenna1 15:009ccc07bb57 19 {0,0,1,1,0,1,1,0,0},
ikenna1 15:009ccc07bb57 20 {0,0,0,0,1,0,0,0,0},
ikenna1 15:009ccc07bb57 21 };
ikenna1 28:6319e928f0aa 22 const int shooter[10][11] = {
ikenna1 28:6319e928f0aa 23 {0,0,1,0,0,0,0,0,1,0,0},
ikenna1 28:6319e928f0aa 24 {0,1,0,1,0,0,0,1,0,1,0},
ikenna1 28:6319e928f0aa 25 {1,0,0,1,0,1,0,1,0,0,1},
ikenna1 28:6319e928f0aa 26 {1,0,0,1,1,0,1,1,0,0,1},
ikenna1 28:6319e928f0aa 27 {0,1,0,0,0,0,0,0,0,1,0},
ikenna1 28:6319e928f0aa 28 {0,1,0,0,1,1,1,0,0,1,0},
ikenna1 28:6319e928f0aa 29 {0,0,1,0,0,1,0,0,1,0,0},
ikenna1 28:6319e928f0aa 30 {0,0,0,1,0,0,0,1,0,0,0},
ikenna1 28:6319e928f0aa 31 {0,0,0,0,1,0,1,0,0,0,0},
ikenna1 28:6319e928f0aa 32 {0,0,0,0,0,1,0,0,0,0,0},
ikenna1 28:6319e928f0aa 33 };
ikenna1 45:fe5fc85a5c73 34 void Enemy::init(int noShooters, int noSeekers)
ikenna1 15:009ccc07bb57 35 {
ikenna1 51:2231e2e141b9 36 // initialize the number of seekers and shooters
ikenna1 45:fe5fc85a5c73 37 _noSeekers = noSeekers;
ikenna1 45:fe5fc85a5c73 38 _noShooters = noShooters;
ikenna1 51:2231e2e141b9 39 // initialize seeker at top of screen whith a random x position
ikenna1 51:2231e2e141b9 40 _seekerPos[0].x = rand_no(68);
ikenna1 51:2231e2e141b9 41 _seekerPos[0].y = 0;
ikenna1 51:2231e2e141b9 42 _seekerPos[1].x = rand_no(68);
ikenna1 51:2231e2e141b9 43 _seekerPos[1].y = 0;
ikenna1 51:2231e2e141b9 44 _seekerPos[2].x = rand_no(68);
ikenna1 51:2231e2e141b9 45 _seekerPos[2].y = 0;
ikenna1 51:2231e2e141b9 46
ikenna1 51:2231e2e141b9 47 // initialize shooter at top of screen whith a random x position
ikenna1 51:2231e2e141b9 48 _shooterPos[0].x = rand_no(68);
ikenna1 51:2231e2e141b9 49 _shooterPos[0].y = 1;
ikenna1 51:2231e2e141b9 50 _shooterPos[1].x = rand_no(68);
ikenna1 51:2231e2e141b9 51 _shooterPos[1].y = 1;
ikenna1 51:2231e2e141b9 52 _shooterPos[2].x = rand_no(68);
ikenna1 51:2231e2e141b9 53 _shooterPos[2].y = 1;
ikenna1 51:2231e2e141b9 54 // initialize shooter speed
ikenna1 41:e1fa36c0492e 55 _shooterSpeed[0].x = 1;
ikenna1 41:e1fa36c0492e 56 _shooterSpeed[1].x = 1;
ikenna1 41:e1fa36c0492e 57 _shooterSpeed[2].x = 1;
ikenna1 41:e1fa36c0492e 58 _shooterSpeed[0].y = 1;
ikenna1 41:e1fa36c0492e 59 _shooterSpeed[1].y = 1;
ikenna1 41:e1fa36c0492e 60 _shooterSpeed[2].y = 1;
ikenna1 30:711d722f3cef 61
ikenna1 15:009ccc07bb57 62 }
ikenna1 45:fe5fc85a5c73 63 void Enemy::set_noshooters(int noShooters)
ikenna1 38:4571537238ed 64 {
ikenna1 51:2231e2e141b9 65 // set the number of enemy shooters
ikenna1 45:fe5fc85a5c73 66 _noShooters = noShooters;
ikenna1 38:4571537238ed 67 }
ikenna1 45:fe5fc85a5c73 68 void Enemy::set_noseekers(int noSeekers)
ikenna1 39:7824f9080f59 69 {
ikenna1 51:2231e2e141b9 70 // set the number of enemy seekers
ikenna1 45:fe5fc85a5c73 71 _noSeekers = noSeekers;
ikenna1 39:7824f9080f59 72 }
ikenna1 51:2231e2e141b9 73 void Enemy::draw_seeker(N5110 &lcd)
ikenna1 51:2231e2e141b9 74 {
ikenna1 51:2231e2e141b9 75 // Draw seekers based on _noSeekers with a maximum of three seekers
ikenna1 45:fe5fc85a5c73 76 switch (_noSeekers) {
ikenna1 41:e1fa36c0492e 77 case 1:
ikenna1 41:e1fa36c0492e 78 lcd.drawSprite( _seekerPos[0].x, _seekerPos[0].y,7,9,(int *)seeker);
ikenna1 41:e1fa36c0492e 79 break;
ikenna1 41:e1fa36c0492e 80 case 2:
ikenna1 41:e1fa36c0492e 81 lcd.drawSprite( _seekerPos[0].x, _seekerPos[0].y,7,9,(int *)seeker);
ikenna1 41:e1fa36c0492e 82 lcd.drawSprite( _seekerPos[1].x, _seekerPos[1].y,7,9,(int *)seeker);
ikenna1 41:e1fa36c0492e 83 break;
ikenna1 41:e1fa36c0492e 84 case 3:
ikenna1 41:e1fa36c0492e 85 lcd.drawSprite( _seekerPos[0].x, _seekerPos[0].y,7,9,(int *)seeker);
ikenna1 41:e1fa36c0492e 86 lcd.drawSprite( _seekerPos[1].x, _seekerPos[1].y,7,9,(int *)seeker);
ikenna1 41:e1fa36c0492e 87 lcd.drawSprite( _seekerPos[2].x, _seekerPos[2].y,7,9,(int *)seeker);
ikenna1 41:e1fa36c0492e 88 break;
ikenna1 39:7824f9080f59 89 }
ikenna1 15:009ccc07bb57 90 }
ikenna1 34:6d0786582d81 91 void Enemy::draw_shooter(N5110 &lcd)
ikenna1 30:711d722f3cef 92 {
ikenna1 51:2231e2e141b9 93 // Draw shooters based on _noShooters with a maximum of three shooters
ikenna1 45:fe5fc85a5c73 94 switch (_noShooters) {
ikenna1 41:e1fa36c0492e 95 case 1:
ikenna1 41:e1fa36c0492e 96 lcd.drawSprite(_shooterPos[0].x,_shooterPos[0].y,10,11,(int *)shooter);
ikenna1 41:e1fa36c0492e 97 break;
ikenna1 41:e1fa36c0492e 98 case 2:
ikenna1 41:e1fa36c0492e 99 lcd.drawSprite(_shooterPos[0].x,_shooterPos[0].y,10,11,(int *)shooter);
ikenna1 41:e1fa36c0492e 100 lcd.drawSprite(_shooterPos[1].x,_shooterPos[1].y,10,11,(int *)shooter);
ikenna1 41:e1fa36c0492e 101 break;
ikenna1 41:e1fa36c0492e 102 case 3:
ikenna1 41:e1fa36c0492e 103 lcd.drawSprite(_shooterPos[0].x,_shooterPos[0].y,10,11,(int *)shooter);
ikenna1 41:e1fa36c0492e 104 lcd.drawSprite(_shooterPos[1].x,_shooterPos[1].y,10,11,(int *)shooter);
ikenna1 41:e1fa36c0492e 105 lcd.drawSprite(_shooterPos[2].x,_shooterPos[2].y,10,11,(int *)shooter);
ikenna1 41:e1fa36c0492e 106 break;
ikenna1 30:711d722f3cef 107 }
ikenna1 30:711d722f3cef 108 }
ikenna1 15:009ccc07bb57 109 void Enemy::update_seeker(int ship_xpos, int ship_ypos)
ikenna1 15:009ccc07bb57 110 {
ikenna1 51:2231e2e141b9 111 // Update seekers based on _noSeekers with a maximum of three seekers
ikenna1 45:fe5fc85a5c73 112 switch (_noSeekers) {
ikenna1 41:e1fa36c0492e 113 case 1:
ikenna1 51:2231e2e141b9 114 _seekerPos[0] = seeker_motion(_seekerPos[0].x,_seekerPos[0].y,ship_xpos,ship_ypos,2,2);
ikenna1 41:e1fa36c0492e 115 break;
ikenna1 41:e1fa36c0492e 116 case 2:
ikenna1 51:2231e2e141b9 117 _seekerPos[0] = seeker_motion(_seekerPos[0].x,_seekerPos[0].y,ship_xpos,ship_ypos,2,2);
ikenna1 51:2231e2e141b9 118 _seekerPos[1] = seeker_motion(_seekerPos[1].x,_seekerPos[1].y,ship_xpos,ship_ypos,2,2);
ikenna1 41:e1fa36c0492e 119 break;
ikenna1 41:e1fa36c0492e 120 case 3:
ikenna1 51:2231e2e141b9 121 _seekerPos[0] = seeker_motion(_seekerPos[0].x,_seekerPos[0].y,ship_xpos,ship_ypos,2,2);
ikenna1 51:2231e2e141b9 122 _seekerPos[1] = seeker_motion(_seekerPos[1].x,_seekerPos[1].y,ship_xpos,ship_ypos,2,2);
ikenna1 51:2231e2e141b9 123 _seekerPos[2] = seeker_motion(_seekerPos[2].x,_seekerPos[2].y,ship_xpos,ship_ypos,2,2);
ikenna1 41:e1fa36c0492e 124 break;
ikenna1 39:7824f9080f59 125 }
ikenna1 39:7824f9080f59 126 }
ikenna1 51:2231e2e141b9 127 void Enemy::update_shooter(int ship_xpos, int ship_ypos)
ikenna1 39:7824f9080f59 128 {
ikenna1 51:2231e2e141b9 129 // Update shooters based on _noShooters with a maximum of three shooters
ikenna1 51:2231e2e141b9 130 switch (_noShooters) {
ikenna1 51:2231e2e141b9 131 case 1:
ikenna1 51:2231e2e141b9 132 _shooterPos[0] = shooter_motion(_shooterPos[0].x,_shooterPos[0].y,ship_xpos,ship_ypos,_shooterWPos[0].x,_shooterWPos[0].y,_shooterSpeed[0].x,_shooterSpeed[0].y);
ikenna1 51:2231e2e141b9 133 break;
ikenna1 51:2231e2e141b9 134 case 2:
ikenna1 51:2231e2e141b9 135 _shooterPos[0] = shooter_motion(_shooterPos[0].x,_shooterPos[0].y,ship_xpos,ship_ypos,_shooterWPos[0].x,_shooterWPos[0].y,_shooterSpeed[0].x,_shooterSpeed[0].y);
ikenna1 51:2231e2e141b9 136 _shooterPos[1] = shooter_motion(_shooterPos[1].x,_shooterPos[1].y,ship_xpos,ship_ypos,_shooterWPos[1].x,_shooterWPos[1].y,_shooterSpeed[1].x,_shooterSpeed[1].y);
ikenna1 51:2231e2e141b9 137 break;
ikenna1 51:2231e2e141b9 138 case 3:
ikenna1 51:2231e2e141b9 139 _shooterPos[0] = shooter_motion(_shooterPos[0].x,_shooterPos[0].y,ship_xpos,ship_ypos,_shooterWPos[0].x,_shooterWPos[0].y,_shooterSpeed[0].x,_shooterSpeed[0].y);
ikenna1 51:2231e2e141b9 140 _shooterPos[1] = shooter_motion(_shooterPos[1].x,_shooterPos[1].y,ship_xpos,ship_ypos,_shooterWPos[1].x,_shooterWPos[1].y,_shooterSpeed[1].x,_shooterSpeed[1].y);
ikenna1 51:2231e2e141b9 141 _shooterPos[2] = shooter_motion(_shooterPos[2].x,_shooterPos[2].y,ship_xpos,ship_ypos,_shooterWPos[2].x,_shooterWPos[2].y,_shooterSpeed[2].x,_shooterSpeed[2].y);
ikenna1 51:2231e2e141b9 142 break;
ikenna1 15:009ccc07bb57 143 }
ikenna1 39:7824f9080f59 144 }
ikenna1 51:2231e2e141b9 145 Vector2D Enemy::seeker_motion(int seeker_xpos,int seeker_ypos,int ship_xpos,int ship_ypos,int vx,int vy)
ikenna1 39:7824f9080f59 146 {
ikenna1 51:2231e2e141b9 147 int rangex = seeker_xpos - ship_xpos;
ikenna1 51:2231e2e141b9 148 int rangey = seeker_ypos - ship_ypos;
ikenna1 39:7824f9080f59 149
ikenna1 39:7824f9080f59 150 // make seeker move towards ship
ikenna1 39:7824f9080f59 151 if(rangey < 0) {
ikenna1 39:7824f9080f59 152 if(rangex < 0) {
ikenna1 51:2231e2e141b9 153 seeker_xpos = seeker_xpos + vx;
ikenna1 39:7824f9080f59 154 } else if(rangex == 0) {
ikenna1 51:2231e2e141b9 155 seeker_xpos = seeker_xpos + vx;
ikenna1 39:7824f9080f59 156 } else {
ikenna1 51:2231e2e141b9 157 seeker_xpos = seeker_xpos - vx;
ikenna1 39:7824f9080f59 158 }
ikenna1 15:009ccc07bb57 159 }
ikenna1 39:7824f9080f59 160
ikenna1 51:2231e2e141b9 161 // reset seeker to top once it has left the screen
ikenna1 51:2231e2e141b9 162 if( seeker_ypos > 54) {
ikenna1 51:2231e2e141b9 163 seeker_xpos = rand_no(68);
ikenna1 51:2231e2e141b9 164 seeker_ypos = 0;
ikenna1 39:7824f9080f59 165 }
ikenna1 51:2231e2e141b9 166 seeker_ypos = seeker_ypos + vy;
ikenna1 51:2231e2e141b9 167 return{seeker_xpos,seeker_ypos};
ikenna1 39:7824f9080f59 168 }
ikenna1 39:7824f9080f59 169 Vector2D Enemy::shooter_motion(int shooter_xpos,int shooter_ypos,int ship_xpos, int ship_ypos, int projx, int projy,int vx, int vy)
ikenna1 39:7824f9080f59 170 {
ikenna1 51:2231e2e141b9 171 // The shooter tracks the ship based on where the bullet it has fired is
ikenna1 39:7824f9080f59 172 int high_bar =10;
ikenna1 39:7824f9080f59 173 int low_bar = 25;
ikenna1 39:7824f9080f59 174 int rangex = shooter_xpos - ship_xpos;
ikenna1 39:7824f9080f59 175 int rangey = shooter_ypos - ship_ypos;
ikenna1 51:2231e2e141b9 176 // is the bullet has left the shooter and is around the middle of the screen it will avoid the ship
ikenna1 51:2231e2e141b9 177 if(high_bar <= projy && projy <= low_bar) {
ikenna1 51:2231e2e141b9 178 if(rangex > 0) {
ikenna1 51:2231e2e141b9 179 // avoid ship
ikenna1 39:7824f9080f59 180 shooter_xpos = shooter_xpos + vx;
ikenna1 39:7824f9080f59 181 }
ikenna1 51:2231e2e141b9 182 if(rangex < 0) {
ikenna1 39:7824f9080f59 183 shooter_xpos = shooter_xpos - vx;
ikenna1 39:7824f9080f59 184 }
ikenna1 51:2231e2e141b9 185 } else {
ikenna1 51:2231e2e141b9 186 // if it has past a certain point the shooter will try to line up the next shot
ikenna1 51:2231e2e141b9 187 // track ship
ikenna1 51:2231e2e141b9 188 if(rangex > 0) {
ikenna1 51:2231e2e141b9 189 shooter_xpos = shooter_xpos - vx;
ikenna1 51:2231e2e141b9 190 }
ikenna1 51:2231e2e141b9 191 if(rangex < 0) {
ikenna1 39:7824f9080f59 192 shooter_xpos = shooter_xpos + vx;
ikenna1 39:7824f9080f59 193 }
ikenna1 31:c7bd3ed16840 194 }
ikenna1 51:2231e2e141b9 195 // dont let the shooter leave the screen
ikenna1 39:7824f9080f59 196 if(shooter_xpos < 1) {
ikenna1 39:7824f9080f59 197 shooter_xpos = 1;
ikenna1 31:c7bd3ed16840 198 }
ikenna1 39:7824f9080f59 199 if(shooter_xpos > 84 - 8 - 11) {
ikenna1 39:7824f9080f59 200 shooter_xpos = 84 - 8 - 11;
ikenna1 31:c7bd3ed16840 201 }
ikenna1 39:7824f9080f59 202 return{shooter_xpos,shooter_ypos};
ikenna1 32:098fbc1222cd 203 }
ikenna1 32:098fbc1222cd 204
ikenna1 32:098fbc1222cd 205 void Enemy::draw_shw1(N5110 &lcd,Gamepad &pad)
ikenna1 32:098fbc1222cd 206 {
ikenna1 41:e1fa36c0492e 207 _shooterWSpeed[0].x = 0; //Projectile doesn't move sideways.
ikenna1 51:2231e2e141b9 208 _shooterWSpeed[0].y = 2; //Projectile donwards
ikenna1 32:098fbc1222cd 209
ikenna1 32:098fbc1222cd 210 //resets once projectile reaches bottom of screen
ikenna1 41:e1fa36c0492e 211 if(_shooterWPos[0].y >= 48) {
ikenna1 41:e1fa36c0492e 212 _reset[0] = 0;
ikenna1 39:7824f9080f59 213 }
ikenna1 32:098fbc1222cd 214
ikenna1 41:e1fa36c0492e 215 if(_reset[0] == 0) {
ikenna1 41:e1fa36c0492e 216 _shooterWPos[0].x = _shooterPos[0].x + 5;
ikenna1 41:e1fa36c0492e 217 _shooterWPos[0].y = _shooterPos[0].y + 11;
ikenna1 41:e1fa36c0492e 218 _reset[0] = _reset[0] + 1;
ikenna1 39:7824f9080f59 219 }
ikenna1 41:e1fa36c0492e 220 lcd.drawRect(_shooterWPos[0].x,_shooterWPos[0].y,1,1,FILL_BLACK);
ikenna1 32:098fbc1222cd 221 // printf("Ship x and y pos, reset = %d , %d ,%d \n", _ship_xpos, _ship_ypos, reset);
ikenna1 32:098fbc1222cd 222 }
ikenna1 32:098fbc1222cd 223 void Enemy::draw_shw2(N5110 &lcd,Gamepad &pad)
ikenna1 32:098fbc1222cd 224 {
ikenna1 41:e1fa36c0492e 225 _shooterWSpeed[1].x = 0; //Projectile doesn't move sideways.
ikenna1 51:2231e2e141b9 226 _shooterWSpeed[1].y = 5; //Projectile moves downwards
ikenna1 32:098fbc1222cd 227
ikenna1 32:098fbc1222cd 228 //resets once projectile reaches bottom of screen
ikenna1 41:e1fa36c0492e 229 if(_shooterWPos[1].y >= 48) {
ikenna1 41:e1fa36c0492e 230 _reset[1]= 0;
ikenna1 32:098fbc1222cd 231 }
ikenna1 32:098fbc1222cd 232
ikenna1 41:e1fa36c0492e 233 if(_reset[1] == 0) {
ikenna1 41:e1fa36c0492e 234 _shooterWPos[1].x = _shooterPos[1].x + 5;
ikenna1 41:e1fa36c0492e 235 _shooterWPos[1].y = _shooterPos[1].y + 11;
ikenna1 41:e1fa36c0492e 236 _reset[1] = _reset[1] + 1;
ikenna1 32:098fbc1222cd 237 }
ikenna1 41:e1fa36c0492e 238 lcd.drawRect(_shooterWPos[1].x,_shooterWPos[1].y,1,1,FILL_BLACK);
ikenna1 32:098fbc1222cd 239 // printf("Ship x and y pos, reset = %d , %d ,%d \n", _ship_xpos, _ship_ypos, reset);
ikenna1 30:711d722f3cef 240 }
ikenna1 32:098fbc1222cd 241 void Enemy::draw_shw3(N5110 &lcd,Gamepad &pad)
ikenna1 32:098fbc1222cd 242 {
ikenna1 41:e1fa36c0492e 243 _shooterWSpeed[2].x = 0; //Projectile doesn't move sideways.
ikenna1 41:e1fa36c0492e 244 _shooterWSpeed[2].y = 3; //Projectile moves upwards on screen.
ikenna1 32:098fbc1222cd 245
ikenna1 32:098fbc1222cd 246 //resets once projectile reaches bottom of screen
ikenna1 41:e1fa36c0492e 247 if(_shooterWPos[2].y >= 48) {
ikenna1 41:e1fa36c0492e 248 _reset[2] = 0;
ikenna1 32:098fbc1222cd 249 }
ikenna1 32:098fbc1222cd 250
ikenna1 41:e1fa36c0492e 251 if(_reset[2] == 0) {
ikenna1 41:e1fa36c0492e 252 _shooterWPos[2].x = _shooterPos[2].x + 5;
ikenna1 41:e1fa36c0492e 253 _shooterWPos[2].y = _shooterPos[2].y + 11;
ikenna1 41:e1fa36c0492e 254 _reset[2] = _reset[2] + 1;
ikenna1 32:098fbc1222cd 255 }
ikenna1 41:e1fa36c0492e 256 lcd.drawRect(_shooterWPos[2].x,_shooterWPos[2].y,1,1,FILL_BLACK);
ikenna1 32:098fbc1222cd 257 }
ikenna1 34:6d0786582d81 258 void Enemy::draw_shw(N5110 &lcd,Gamepad &pad)
ikenna1 32:098fbc1222cd 259 {
ikenna1 51:2231e2e141b9 260 // Draw projectiles depending on the number of shooters
ikenna1 45:fe5fc85a5c73 261 switch (_noShooters) {
ikenna1 41:e1fa36c0492e 262 case 1:
ikenna1 41:e1fa36c0492e 263 draw_shw1(lcd,pad);
ikenna1 41:e1fa36c0492e 264 break;
ikenna1 41:e1fa36c0492e 265 case 2:
ikenna1 41:e1fa36c0492e 266 draw_shw1(lcd,pad);
ikenna1 41:e1fa36c0492e 267 draw_shw2(lcd,pad);
ikenna1 41:e1fa36c0492e 268 break;
ikenna1 41:e1fa36c0492e 269 case 3:
ikenna1 41:e1fa36c0492e 270 draw_shw1(lcd,pad);
ikenna1 41:e1fa36c0492e 271 draw_shw2(lcd,pad);
ikenna1 41:e1fa36c0492e 272 draw_shw3(lcd,pad);
ikenna1 41:e1fa36c0492e 273 break;
ikenna1 32:098fbc1222cd 274 }
ikenna1 32:098fbc1222cd 275 }
ikenna1 32:098fbc1222cd 276 void Enemy::update_shw()
ikenna1 32:098fbc1222cd 277 {
ikenna1 51:2231e2e141b9 278 // update the shooters position based on the shooter projectile speed
ikenna1 41:e1fa36c0492e 279 _shooterWPos[0].x = _shooterWPos[0].x + _shooterWSpeed[0].x;
ikenna1 41:e1fa36c0492e 280 _shooterWPos[0].y = _shooterWPos[0].y + _shooterWSpeed[0].y;
ikenna1 41:e1fa36c0492e 281 _shooterWPos[1].x = _shooterWPos[1].x + _shooterWSpeed[1].x;
ikenna1 41:e1fa36c0492e 282 _shooterWPos[1].y = _shooterWPos[1].y + _shooterWSpeed[1].y;
ikenna1 41:e1fa36c0492e 283 _shooterWPos[2].x = _shooterWPos[2].x + _shooterWSpeed[2].x;
ikenna1 41:e1fa36c0492e 284 _shooterWPos[2].y = _shooterWPos[2].y + _shooterWSpeed[2].y;
ikenna1 32:098fbc1222cd 285 }
ikenna1 32:098fbc1222cd 286
ikenna1 37:8d8c8cce0bc7 287 Vector2D Enemy::get_shwpos(int shno)
ikenna1 32:098fbc1222cd 288 {
ikenna1 51:2231e2e141b9 289 // get appropriate shooter projectile position
ikenna1 39:7824f9080f59 290 if(shno == 1) {
ikenna1 41:e1fa36c0492e 291 Vector2D pos = {_shooterWPos[0].x,_shooterWPos[0].y};
ikenna1 39:7824f9080f59 292 return pos;
ikenna1 37:8d8c8cce0bc7 293 }
ikenna1 39:7824f9080f59 294 if(shno == 2) {
ikenna1 41:e1fa36c0492e 295 Vector2D pos = {_shooterWPos[1].x,_shooterWPos[1].y};
ikenna1 39:7824f9080f59 296 return pos;
ikenna1 37:8d8c8cce0bc7 297 }
ikenna1 39:7824f9080f59 298 if(shno == 3) {
ikenna1 41:e1fa36c0492e 299 Vector2D pos = {_shooterWPos[2].x,_shooterWPos[2].y};
ikenna1 39:7824f9080f59 300 return pos;
ikenna1 37:8d8c8cce0bc7 301 }
ikenna1 32:098fbc1222cd 302 }
ikenna1 41:e1fa36c0492e 303
ikenna1 41:e1fa36c0492e 304 void Enemy::reset_seeker(int seno)
ikenna1 18:2cc6898de6b2 305 {
ikenna1 51:2231e2e141b9 306 // reset seeker to top of screen
ikenna1 41:e1fa36c0492e 307 switch (seno) {
ikenna1 41:e1fa36c0492e 308 case 1:
ikenna1 41:e1fa36c0492e 309 _seekerPos[0].x = rand_no(68);
ikenna1 41:e1fa36c0492e 310 _seekerPos[0].y = 0;
ikenna1 41:e1fa36c0492e 311 break;
ikenna1 41:e1fa36c0492e 312 case 2:
ikenna1 41:e1fa36c0492e 313 _seekerPos[1].x = rand_no(68);
ikenna1 41:e1fa36c0492e 314 _seekerPos[1].y = 0;
ikenna1 41:e1fa36c0492e 315 break;
ikenna1 41:e1fa36c0492e 316 case 3:
ikenna1 41:e1fa36c0492e 317 _seekerPos[2].x = rand_no(68);
ikenna1 41:e1fa36c0492e 318 _seekerPos[2].y = 0;
ikenna1 41:e1fa36c0492e 319 break;
ikenna1 39:7824f9080f59 320 }
ikenna1 41:e1fa36c0492e 321
ikenna1 18:2cc6898de6b2 322 }
ikenna1 41:e1fa36c0492e 323 void Enemy::reset_shooter(int shno)
ikenna1 35:3341f2bd0408 324 {
ikenna1 51:2231e2e141b9 325 // reset shooter to top of screen
ikenna1 41:e1fa36c0492e 326 switch (shno) {
ikenna1 41:e1fa36c0492e 327 case 1:
ikenna1 41:e1fa36c0492e 328 _shooterPos[0].x = rand_no(68);
ikenna1 41:e1fa36c0492e 329 _shooterPos[0].y = 5;
ikenna1 41:e1fa36c0492e 330 break;
ikenna1 41:e1fa36c0492e 331 case 2:
ikenna1 41:e1fa36c0492e 332 _shooterPos[1].x = rand_no(68);
ikenna1 41:e1fa36c0492e 333 _shooterPos[1].y = 5;
ikenna1 41:e1fa36c0492e 334 break;
ikenna1 41:e1fa36c0492e 335 case 3:
ikenna1 41:e1fa36c0492e 336 _shooterPos[2].x = rand_no(68);
ikenna1 41:e1fa36c0492e 337 _shooterPos[2].y = 5;
ikenna1 41:e1fa36c0492e 338 break;
ikenna1 35:3341f2bd0408 339 }
ikenna1 35:3341f2bd0408 340 }
ikenna1 45:fe5fc85a5c73 341 Vector2D Enemy::get_seekerpos(int seno)
ikenna1 15:009ccc07bb57 342 {
ikenna1 51:2231e2e141b9 343 // get appropriate seeker position
ikenna1 45:fe5fc85a5c73 344 if(seno == 1) {
ikenna1 41:e1fa36c0492e 345 Vector2D seeker_pos = { _seekerPos[0].x, _seekerPos[0].y};
ikenna1 39:7824f9080f59 346 return seeker_pos;
ikenna1 39:7824f9080f59 347 }
ikenna1 45:fe5fc85a5c73 348 if(seno == 2) {
ikenna1 41:e1fa36c0492e 349 Vector2D seeker_pos = { _seekerPos[1].x, _seekerPos[1].y};
ikenna1 39:7824f9080f59 350 return seeker_pos;
ikenna1 39:7824f9080f59 351 }
ikenna1 45:fe5fc85a5c73 352 if(seno == 3) {
ikenna1 41:e1fa36c0492e 353 Vector2D seeker_pos = { _seekerPos[2].x, _seekerPos[2].y};
ikenna1 39:7824f9080f59 354 return seeker_pos;
ikenna1 39:7824f9080f59 355 }
ikenna1 35:3341f2bd0408 356 }
ikenna1 37:8d8c8cce0bc7 357 Vector2D Enemy::get_shooterpos(int shno)
ikenna1 35:3341f2bd0408 358 {
ikenna1 51:2231e2e141b9 359 // get appropriate shooter position
ikenna1 39:7824f9080f59 360 if(shno == 1) {
ikenna1 41:e1fa36c0492e 361 Vector2D shooter_pos = {_shooterPos[0].x,_shooterPos[0].y};
ikenna1 39:7824f9080f59 362 return shooter_pos;
ikenna1 37:8d8c8cce0bc7 363 }
ikenna1 39:7824f9080f59 364 if(shno == 2) {
ikenna1 41:e1fa36c0492e 365 Vector2D shooter_pos = {_shooterPos[1].x,_shooterPos[1].y};
ikenna1 39:7824f9080f59 366 return shooter_pos;
ikenna1 37:8d8c8cce0bc7 367 }
ikenna1 39:7824f9080f59 368 if(shno == 3) {
ikenna1 41:e1fa36c0492e 369 Vector2D shooter_pos = {_shooterPos[2].x,_shooterPos[2].y};
ikenna1 39:7824f9080f59 370 return shooter_pos;
ikenna1 37:8d8c8cce0bc7 371 }
ikenna1 35:3341f2bd0408 372 }
ikenna1 37:8d8c8cce0bc7 373 int Enemy::rand_no(int scale)
ikenna1 35:3341f2bd0408 374 {
ikenna1 51:2231e2e141b9 375 // seed the srand function using time from the <ctime> library
ikenna1 37:8d8c8cce0bc7 376 srand(time(NULL));
ikenna1 51:2231e2e141b9 377 int rand_no = (rand() %scale) + 1; // use scale to limit the output of the random number
ikenna1 37:8d8c8cce0bc7 378 // printf("random no = %d\n",rand_no);
ikenna1 37:8d8c8cce0bc7 379 return rand_no;
ikenna1 38:4571537238ed 380 }
ikenna1 39:7824f9080f59 381 void Enemy::sh_scaling(float time_elapsed)
ikenna1 38:4571537238ed 382 {
ikenna1 39:7824f9080f59 383 int vmax = 3;
ikenna1 39:7824f9080f59 384 int time = floor(time_elapsed);
ikenna1 51:2231e2e141b9 385 // Increase the shooters speed every 20 seconds to a maximum of 3
ikenna1 51:2231e2e141b9 386 if(time%20 == 0) {
ikenna1 41:e1fa36c0492e 387 if(_shooterSpeed[0].x > -vmax && _shooterSpeed[0].x < vmax) {
ikenna1 41:e1fa36c0492e 388 _shooterSpeed[0].x = _shooterSpeed[0].x * 1.1;
ikenna1 39:7824f9080f59 389 }
ikenna1 41:e1fa36c0492e 390 if(_shooterSpeed[1].x > - vmax && _shooterSpeed[1].x < vmax) {
ikenna1 41:e1fa36c0492e 391 _shooterSpeed[1].x = _shooterSpeed[1].x * 1.1;
ikenna1 39:7824f9080f59 392 }
ikenna1 41:e1fa36c0492e 393 if(_shooterSpeed[2].x > -vmax && _shooterSpeed[2].x < vmax) {
ikenna1 41:e1fa36c0492e 394 _shooterSpeed[2].x = _shooterSpeed[2].x * 1.1;
ikenna1 39:7824f9080f59 395 }
ikenna1 41:e1fa36c0492e 396 if(_shooterSpeed[0].y > -vmax && _shooterSpeed[0].y < vmax) {
ikenna1 41:e1fa36c0492e 397 _shooterSpeed[0].y = _shooterSpeed[0].y * 1.1;
ikenna1 39:7824f9080f59 398 }
ikenna1 41:e1fa36c0492e 399 if(_shooterSpeed[1].y > -vmax && _shooterSpeed[1].y < vmax) {
ikenna1 41:e1fa36c0492e 400 _shooterSpeed[1].y = _shooterSpeed[1].y * 1.1;
ikenna1 39:7824f9080f59 401 }
ikenna1 41:e1fa36c0492e 402 if(_shooterSpeed[2].y > -vmax && _shooterSpeed[2].y < vmax) {
ikenna1 41:e1fa36c0492e 403 _shooterSpeed[2].y = _shooterSpeed[2].y * 1.1;
ikenna1 39:7824f9080f59 404 }
ikenna1 38:4571537238ed 405 }
ikenna1 41:e1fa36c0492e 406 // printf("time = %d, _shooterSpeed[0].x = %f\n",time,_shooterSpeed[0].x);
ikenna1 40:90c7a893d513 407 }
ikenna1 45:fe5fc85a5c73 408 int Enemy::distance(int x1, int y1, int x2, int y2)
ikenna1 40:90c7a893d513 409 {
ikenna1 51:2231e2e141b9 410 // get the absolute difference between x1 and x2, and y1 and y2
ikenna1 51:2231e2e141b9 411 // then average that
ikenna1 40:90c7a893d513 412 int rangex = abs(x1 - x2);
ikenna1 40:90c7a893d513 413 int rangey = abs(y1 - y2);
ikenna1 45:fe5fc85a5c73 414 int dis = (rangex+rangey)/2;
ikenna1 45:fe5fc85a5c73 415 return dis;
ikenna1 40:90c7a893d513 416 }
ikenna1 40:90c7a893d513 417
ikenna1 45:fe5fc85a5c73 418 Vector2D Enemy::find_closest(int ship_xpos,int ship_ypos, int noSeekers, int noShooters)
ikenna1 40:90c7a893d513 419 {
ikenna1 40:90c7a893d513 420 // get the distance for all enemies
ikenna1 45:fe5fc85a5c73 421 int sh1 = distance(ship_xpos,ship_ypos,_shooterPos[0].x,_shooterPos[0].y);
ikenna1 45:fe5fc85a5c73 422 int sh2 = distance(ship_xpos,ship_ypos,_shooterPos[1].x,_shooterPos[1].y);
ikenna1 45:fe5fc85a5c73 423 int sh3 = distance(ship_xpos,ship_ypos,_shooterPos[2].x,_shooterPos[2].y);
ikenna1 45:fe5fc85a5c73 424 int se1 = distance(ship_xpos,ship_ypos, _seekerPos[0].x, _seekerPos[0].y);
ikenna1 45:fe5fc85a5c73 425 int se2 = distance(ship_xpos,ship_ypos, _seekerPos[1].x, _seekerPos[1].y);
ikenna1 45:fe5fc85a5c73 426 int se3 = distance(ship_xpos,ship_ypos, _seekerPos[2].x, _seekerPos[2].y);
ikenna1 51:2231e2e141b9 427
ikenna1 40:90c7a893d513 428 int close[6] = {sh1,sh2,sh3,se1,se2,se3};
ikenna1 40:90c7a893d513 429 // find index of the smallest element
ikenna1 40:90c7a893d513 430 int index;
ikenna1 40:90c7a893d513 431 int smallest = close[0];
ikenna1 40:90c7a893d513 432 for(int i=0; i<6; i=i+1) {
ikenna1 51:2231e2e141b9 433 if(smallest>close[i]) {
ikenna1 51:2231e2e141b9 434 smallest=close[i];
ikenna1 51:2231e2e141b9 435 index = i;
ikenna1 51:2231e2e141b9 436 }
ikenna1 40:90c7a893d513 437 }
ikenna1 40:90c7a893d513 438 // return the position of the closest enemy
ikenna1 51:2231e2e141b9 439 if(index == 0 && noShooters >= 1) {
ikenna1 51:2231e2e141b9 440 return {_shooterPos[0].x,_shooterPos[0].y};
ikenna1 40:90c7a893d513 441 }
ikenna1 51:2231e2e141b9 442 if(index == 1 && noShooters >= 2) {
ikenna1 51:2231e2e141b9 443 return {_shooterPos[1].x,_shooterPos[1].y};
ikenna1 40:90c7a893d513 444 }
ikenna1 51:2231e2e141b9 445 if(index == 2 && noShooters >= 3) {
ikenna1 51:2231e2e141b9 446 return {_shooterPos[2].x,_shooterPos[2].y};
ikenna1 40:90c7a893d513 447 }
ikenna1 51:2231e2e141b9 448 if(index == 3 && noSeekers >= 1 ) {
ikenna1 51:2231e2e141b9 449 return { _seekerPos[0].x, _seekerPos[0].y};
ikenna1 40:90c7a893d513 450 }
ikenna1 51:2231e2e141b9 451 if(index == 4 && noSeekers >= 2) {
ikenna1 51:2231e2e141b9 452 return { _seekerPos[1].x, _seekerPos[1].y};
ikenna1 40:90c7a893d513 453 }
ikenna1 51:2231e2e141b9 454 if(index == 5 && noSeekers >= 3) {
ikenna1 51:2231e2e141b9 455 return { _seekerPos[2].x, _seekerPos[2].y};
ikenna1 40:90c7a893d513 456 }
ikenna1 51:2231e2e141b9 457
ikenna1 51:2231e2e141b9 458 }