Ikenna Adrian Ozoemena 201157039

Dependencies:   mbed

Committer:
ikenna1
Date:
Tue May 07 10:01:34 2019 +0000
Revision:
40:90c7a893d513
Parent:
39:7824f9080f59
Child:
41:e1fa36c0492e
Add Orion ship weapons ; Gives a weapon that tracks the movements of enemy ship to the Orion

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 15:009ccc07bb57 12 const int seeker[7][9] = {
ikenna1 15:009ccc07bb57 13 {1,1,0,0,0,0,0,1,1},
ikenna1 15:009ccc07bb57 14 {1,0,1,0,0,0,1,0,1},
ikenna1 15:009ccc07bb57 15 {1,0,0,1,1,1,0,0,1},
ikenna1 15:009ccc07bb57 16 {0,1,0,0,0,0,0,1,0},
ikenna1 15:009ccc07bb57 17 {0,0,1,0,0,0,1,0,0},
ikenna1 15:009ccc07bb57 18 {0,0,1,1,0,1,1,0,0},
ikenna1 15:009ccc07bb57 19 {0,0,0,0,1,0,0,0,0},
ikenna1 15:009ccc07bb57 20 };
ikenna1 28:6319e928f0aa 21 const int shooter[10][11] = {
ikenna1 28:6319e928f0aa 22 {0,0,1,0,0,0,0,0,1,0,0},
ikenna1 28:6319e928f0aa 23 {0,1,0,1,0,0,0,1,0,1,0},
ikenna1 28:6319e928f0aa 24 {1,0,0,1,0,1,0,1,0,0,1},
ikenna1 28:6319e928f0aa 25 {1,0,0,1,1,0,1,1,0,0,1},
ikenna1 28:6319e928f0aa 26 {0,1,0,0,0,0,0,0,0,1,0},
ikenna1 28:6319e928f0aa 27 {0,1,0,0,1,1,1,0,0,1,0},
ikenna1 28:6319e928f0aa 28 {0,0,1,0,0,1,0,0,1,0,0},
ikenna1 28:6319e928f0aa 29 {0,0,0,1,0,0,0,1,0,0,0},
ikenna1 28:6319e928f0aa 30 {0,0,0,0,1,0,1,0,0,0,0},
ikenna1 28:6319e928f0aa 31 {0,0,0,0,0,1,0,0,0,0,0},
ikenna1 28:6319e928f0aa 32 };
ikenna1 39:7824f9080f59 33 void Enemy::init(int shno, int seno)
ikenna1 15:009ccc07bb57 34 {
ikenna1 30:711d722f3cef 35 // initialize seeker
ikenna1 39:7824f9080f59 36 _seeker1_xpos = rand_no(68);
ikenna1 39:7824f9080f59 37 _seeker1_ypos = 0;
ikenna1 39:7824f9080f59 38 _seeker2_xpos = rand_no(68);
ikenna1 39:7824f9080f59 39 _seeker2_ypos = 0;
ikenna1 39:7824f9080f59 40 _seeker3_xpos = rand_no(68);
ikenna1 39:7824f9080f59 41 _seeker3_ypos = 0;
ikenna1 39:7824f9080f59 42 _seno = seno;
ikenna1 34:6d0786582d81 43 _shno = shno;
ikenna1 31:c7bd3ed16840 44 // initialize shooter
ikenna1 39:7824f9080f59 45 _vx1 = 1;
ikenna1 39:7824f9080f59 46 _vx2 = 1;
ikenna1 39:7824f9080f59 47 _vx3 = 1;
ikenna1 31:c7bd3ed16840 48 _vy1 = 1;
ikenna1 39:7824f9080f59 49 _vy2 = 1;
ikenna1 31:c7bd3ed16840 50 _vy3 = 1;
ikenna1 30:711d722f3cef 51 // for now just starting them spaced apart add randomizer later
ikenna1 38:4571537238ed 52
ikenna1 39:7824f9080f59 53 _shooter_xpos1 = rand_no(68);
ikenna1 39:7824f9080f59 54 _shooter_ypos1 = 1;
ikenna1 39:7824f9080f59 55 _shooter_xpos2 = rand_no(68);
ikenna1 39:7824f9080f59 56 _shooter_ypos2 = 1;
ikenna1 39:7824f9080f59 57 _shooter_xpos3 = rand_no(68);
ikenna1 39:7824f9080f59 58 _shooter_ypos3 = 1;
ikenna1 30:711d722f3cef 59
ikenna1 15:009ccc07bb57 60 }
ikenna1 38:4571537238ed 61 void Enemy::set_noshooters(int no_shooters)
ikenna1 38:4571537238ed 62 {
ikenna1 38:4571537238ed 63 _shno = no_shooters;
ikenna1 38:4571537238ed 64 }
ikenna1 39:7824f9080f59 65 void Enemy::set_noseekers(int no_seekers)
ikenna1 39:7824f9080f59 66 {
ikenna1 39:7824f9080f59 67 _seno = no_seekers;
ikenna1 39:7824f9080f59 68 }
ikenna1 15:009ccc07bb57 69 // Draw the ship ***Note: figure out how to change ship type e.g from basic to devotion
ikenna1 15:009ccc07bb57 70 void Enemy::draw_seeker(N5110 &lcd)
ikenna1 15:009ccc07bb57 71 {
ikenna1 39:7824f9080f59 72 if(_seno == 1) {
ikenna1 39:7824f9080f59 73 lcd.drawSprite(_seeker1_xpos,_seeker1_ypos,7,9,(int *)seeker);
ikenna1 39:7824f9080f59 74 }
ikenna1 39:7824f9080f59 75 if(_seno == 2) {
ikenna1 39:7824f9080f59 76 lcd.drawSprite(_seeker1_xpos,_seeker1_ypos,7,9,(int *)seeker);
ikenna1 39:7824f9080f59 77 lcd.drawSprite(_seeker2_xpos,_seeker2_ypos,7,9,(int *)seeker);
ikenna1 39:7824f9080f59 78 }
ikenna1 39:7824f9080f59 79 if(_seno == 3) {
ikenna1 39:7824f9080f59 80 lcd.drawSprite(_seeker1_xpos,_seeker1_ypos,7,9,(int *)seeker);
ikenna1 39:7824f9080f59 81 lcd.drawSprite(_seeker2_xpos,_seeker2_ypos,7,9,(int *)seeker);
ikenna1 39:7824f9080f59 82 lcd.drawSprite(_seeker3_xpos,_seeker3_ypos,7,9,(int *)seeker);
ikenna1 39:7824f9080f59 83 }
ikenna1 15:009ccc07bb57 84 }
ikenna1 34:6d0786582d81 85 void Enemy::draw_shooter(N5110 &lcd)
ikenna1 30:711d722f3cef 86 {
ikenna1 34:6d0786582d81 87 if(_shno == 1) {
ikenna1 30:711d722f3cef 88 lcd.drawSprite(_shooter_xpos1,_shooter_ypos1,10,11,(int *)shooter);
ikenna1 30:711d722f3cef 89 }
ikenna1 34:6d0786582d81 90 if(_shno == 2) {
ikenna1 30:711d722f3cef 91 lcd.drawSprite(_shooter_xpos1,_shooter_ypos1,10,11,(int *)shooter);
ikenna1 30:711d722f3cef 92 lcd.drawSprite(_shooter_xpos2,_shooter_ypos2,10,11,(int *)shooter);
ikenna1 30:711d722f3cef 93 }
ikenna1 34:6d0786582d81 94 if(_shno == 3) {
ikenna1 30:711d722f3cef 95 lcd.drawSprite(_shooter_xpos1,_shooter_ypos1,10,11,(int *)shooter);
ikenna1 30:711d722f3cef 96 lcd.drawSprite(_shooter_xpos2,_shooter_ypos2,10,11,(int *)shooter);
ikenna1 30:711d722f3cef 97 lcd.drawSprite(_shooter_xpos3,_shooter_ypos3,10,11,(int *)shooter);
ikenna1 30:711d722f3cef 98 }
ikenna1 34:6d0786582d81 99 // printf("_shno = %d, shooter_xpos1 = %d, shooter_ypos1 = %d/n",_shno,_shooter_xpos1,_shooter_ypos1);
ikenna1 30:711d722f3cef 100 }
ikenna1 15:009ccc07bb57 101 void Enemy::update_seeker(int ship_xpos, int ship_ypos)
ikenna1 15:009ccc07bb57 102 {
ikenna1 39:7824f9080f59 103 if(_seno == 1) {
ikenna1 39:7824f9080f59 104 update_seeker1(ship_xpos,ship_ypos);
ikenna1 39:7824f9080f59 105 }
ikenna1 39:7824f9080f59 106 if(_seno == 2) {
ikenna1 39:7824f9080f59 107 update_seeker1(ship_xpos,ship_ypos);
ikenna1 39:7824f9080f59 108 update_seeker2(ship_xpos,ship_ypos);
ikenna1 39:7824f9080f59 109 }
ikenna1 39:7824f9080f59 110 if(_seno == 3) {
ikenna1 39:7824f9080f59 111 update_seeker1(ship_xpos,ship_ypos);
ikenna1 39:7824f9080f59 112 update_seeker2(ship_xpos,ship_ypos);
ikenna1 39:7824f9080f59 113 update_seeker3(ship_xpos,ship_ypos);
ikenna1 39:7824f9080f59 114 }
ikenna1 39:7824f9080f59 115 //printf("seno = %d\n",_seno);
ikenna1 39:7824f9080f59 116 }
ikenna1 39:7824f9080f59 117 void Enemy::update_seeker1(int ship_xpos, int ship_ypos)
ikenna1 39:7824f9080f59 118 {
ikenna1 39:7824f9080f59 119 int rangex = _seeker1_xpos - ship_xpos;
ikenna1 39:7824f9080f59 120 int rangey = _seeker1_ypos - ship_ypos;
ikenna1 15:009ccc07bb57 121
ikenna1 15:009ccc07bb57 122 // make seeker move towards ship
ikenna1 15:009ccc07bb57 123 if(rangey < 0) {
ikenna1 15:009ccc07bb57 124 if(rangex < 0) {
ikenna1 39:7824f9080f59 125 _seeker1_xpos = _seeker1_xpos + 2;
ikenna1 15:009ccc07bb57 126 } else if(rangex == 0) {
ikenna1 39:7824f9080f59 127 _seeker1_xpos = _seeker1_xpos;
ikenna1 15:009ccc07bb57 128 } else {
ikenna1 39:7824f9080f59 129 _seeker1_xpos = _seeker1_xpos - 2;
ikenna1 15:009ccc07bb57 130 }
ikenna1 15:009ccc07bb57 131 }
ikenna1 15:009ccc07bb57 132
ikenna1 15:009ccc07bb57 133 // reset seeker to top
ikenna1 39:7824f9080f59 134 if(_seeker1_ypos > 54) {
ikenna1 39:7824f9080f59 135 reset_seeker(1);
ikenna1 39:7824f9080f59 136 }
ikenna1 39:7824f9080f59 137 _seeker1_ypos = _seeker1_ypos + 2;
ikenna1 39:7824f9080f59 138 // printf("seeker_xpos , seeker_ypos = %d, %d\n", _seeker_xpos, _seeker_ypos);
ikenna1 39:7824f9080f59 139 }
ikenna1 39:7824f9080f59 140 void Enemy::update_seeker2(int ship_xpos, int ship_ypos)
ikenna1 39:7824f9080f59 141 {
ikenna1 39:7824f9080f59 142 int rangex = _seeker2_xpos - ship_xpos;
ikenna1 39:7824f9080f59 143 int rangey = _seeker2_ypos - ship_ypos;
ikenna1 39:7824f9080f59 144
ikenna1 39:7824f9080f59 145 // make seeker move towards ship
ikenna1 39:7824f9080f59 146 if(rangey < 0) {
ikenna1 39:7824f9080f59 147 if(rangex < 0) {
ikenna1 39:7824f9080f59 148 _seeker2_xpos = _seeker2_xpos + 2;
ikenna1 39:7824f9080f59 149 } else if(rangex == 0) {
ikenna1 39:7824f9080f59 150 _seeker2_xpos = _seeker2_xpos;
ikenna1 39:7824f9080f59 151 } else {
ikenna1 39:7824f9080f59 152 _seeker2_xpos = _seeker2_xpos - 2;
ikenna1 39:7824f9080f59 153 }
ikenna1 15:009ccc07bb57 154 }
ikenna1 39:7824f9080f59 155
ikenna1 39:7824f9080f59 156 // reset seeker to top
ikenna1 39:7824f9080f59 157 if(_seeker2_ypos > 54) {
ikenna1 39:7824f9080f59 158 reset_seeker(2);
ikenna1 39:7824f9080f59 159 }
ikenna1 39:7824f9080f59 160 _seeker2_ypos = _seeker2_ypos + 2;
ikenna1 39:7824f9080f59 161 // printf("seeker_xpos , seeker_ypos = %d, %d\n", _seeker_xpos, _seeker_ypos);
ikenna1 39:7824f9080f59 162 }
ikenna1 39:7824f9080f59 163 void Enemy::update_seeker3(int ship_xpos, int ship_ypos)
ikenna1 39:7824f9080f59 164 {
ikenna1 39:7824f9080f59 165 int rangex = _seeker3_xpos - ship_xpos;
ikenna1 39:7824f9080f59 166 int rangey = _seeker3_ypos - ship_ypos;
ikenna1 39:7824f9080f59 167
ikenna1 39:7824f9080f59 168 // make seeker move towards ship
ikenna1 39:7824f9080f59 169 if(rangey < 0) {
ikenna1 39:7824f9080f59 170 if(rangex < 0) {
ikenna1 39:7824f9080f59 171 _seeker3_xpos = _seeker3_xpos + 2;
ikenna1 39:7824f9080f59 172 } else if(rangex == 0) {
ikenna1 39:7824f9080f59 173 _seeker3_xpos = _seeker3_xpos;
ikenna1 39:7824f9080f59 174 } else {
ikenna1 39:7824f9080f59 175 _seeker3_xpos = _seeker3_xpos - 2;
ikenna1 39:7824f9080f59 176 }
ikenna1 39:7824f9080f59 177 }
ikenna1 39:7824f9080f59 178
ikenna1 39:7824f9080f59 179 // reset seeker to top
ikenna1 39:7824f9080f59 180 if(_seeker3_ypos > 54) {
ikenna1 39:7824f9080f59 181 reset_seeker(3);
ikenna1 39:7824f9080f59 182 }
ikenna1 39:7824f9080f59 183 _seeker3_ypos = _seeker3_ypos + 2;
ikenna1 20:5b4b3bf5795c 184 // printf("seeker_xpos , seeker_ypos = %d, %d\n", _seeker_xpos, _seeker_ypos);
ikenna1 15:009ccc07bb57 185 }
ikenna1 30:711d722f3cef 186 void Enemy::update_shooter(int ship_xpos, int ship_ypos)
ikenna1 30:711d722f3cef 187 {
ikenna1 39:7824f9080f59 188 Vector2D shooter1pos = shooter_motion(_shooter_xpos1,_shooter_ypos1,ship_xpos,ship_ypos,_shwx1,_shwy1,_vx1,_vy1);
ikenna1 39:7824f9080f59 189 Vector2D shooter2pos = shooter_motion(_shooter_xpos2,_shooter_ypos2,ship_xpos,ship_ypos,_shwx2,_shwy2,_vx2,_vy2);
ikenna1 39:7824f9080f59 190 Vector2D shooter3pos = shooter_motion(_shooter_xpos3,_shooter_ypos3,ship_xpos,ship_ypos,_shwx3,_shwy3,_vx3,_vy3);
ikenna1 39:7824f9080f59 191
ikenna1 39:7824f9080f59 192 _shooter_xpos1 = shooter1pos.x;
ikenna1 39:7824f9080f59 193 _shooter_ypos1 = shooter1pos.y;
ikenna1 39:7824f9080f59 194 _shooter_xpos2 = shooter2pos.x;
ikenna1 39:7824f9080f59 195 _shooter_ypos2 = shooter2pos.y;
ikenna1 39:7824f9080f59 196 _shooter_xpos3 = shooter3pos.x;
ikenna1 39:7824f9080f59 197 _shooter_ypos3 = shooter3pos.y;
ikenna1 39:7824f9080f59 198 //printf("_shooter_xpos1 = %d,_shooter_ypos1 = %d,projy = %d\n",_shooter_xpos1 ,_shooter_ypos1,_shwy1);
ikenna1 39:7824f9080f59 199 }
ikenna1 39:7824f9080f59 200 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 201 {
ikenna1 39:7824f9080f59 202 int high_bar =10;
ikenna1 39:7824f9080f59 203 int low_bar = 25;
ikenna1 39:7824f9080f59 204 int rangex = shooter_xpos - ship_xpos;
ikenna1 39:7824f9080f59 205 int rangey = shooter_ypos - ship_ypos;
ikenna1 39:7824f9080f59 206 if(high_bar <= projy && projy <= low_bar){
ikenna1 39:7824f9080f59 207 if(rangex > 0){
ikenna1 39:7824f9080f59 208 // avoid ship
ikenna1 39:7824f9080f59 209 shooter_xpos = shooter_xpos + vx;
ikenna1 39:7824f9080f59 210 }
ikenna1 39:7824f9080f59 211 if(rangex < 0){
ikenna1 39:7824f9080f59 212 shooter_xpos = shooter_xpos - vx;
ikenna1 39:7824f9080f59 213 }
ikenna1 34:6d0786582d81 214 }
ikenna1 39:7824f9080f59 215 else{
ikenna1 39:7824f9080f59 216 // track ship
ikenna1 39:7824f9080f59 217 if(rangex > 0){
ikenna1 39:7824f9080f59 218 shooter_xpos = shooter_xpos - vx;
ikenna1 39:7824f9080f59 219 }
ikenna1 39:7824f9080f59 220 if(rangex < 0){
ikenna1 39:7824f9080f59 221 shooter_xpos = shooter_xpos + vx;
ikenna1 39:7824f9080f59 222 }
ikenna1 31:c7bd3ed16840 223 }
ikenna1 39:7824f9080f59 224 if(shooter_xpos < 1) {
ikenna1 39:7824f9080f59 225 shooter_xpos = 1;
ikenna1 31:c7bd3ed16840 226 }
ikenna1 39:7824f9080f59 227 if(shooter_xpos > 84 - 8 - 11) {
ikenna1 39:7824f9080f59 228 shooter_xpos = 84 - 8 - 11;
ikenna1 31:c7bd3ed16840 229 }
ikenna1 39:7824f9080f59 230 //printf("_shooter_xpos1 = %d,_shooter_ypos1 = %d,projy = %d\n",_shooter_xpos1 ,_shooter_ypos1,_shwy1);
ikenna1 39:7824f9080f59 231 // shooter_ypos = shooter_ypos + 1;
ikenna1 39:7824f9080f59 232 return{shooter_xpos,shooter_ypos};
ikenna1 39:7824f9080f59 233
ikenna1 32:098fbc1222cd 234 }
ikenna1 32:098fbc1222cd 235
ikenna1 32:098fbc1222cd 236 void Enemy::draw_shw1(N5110 &lcd,Gamepad &pad)
ikenna1 32:098fbc1222cd 237 {
ikenna1 39:7824f9080f59 238 _vshw1.x = 0; //Projectile doesn't move sideways.
ikenna1 39:7824f9080f59 239 _vshw1.y = 2; //Projectile moves upwards on screen.
ikenna1 32:098fbc1222cd 240
ikenna1 32:098fbc1222cd 241 //resets once projectile reaches bottom of screen
ikenna1 39:7824f9080f59 242 if(_shwy1 >= 48) {
ikenna1 39:7824f9080f59 243 reset1 = 0;
ikenna1 39:7824f9080f59 244 }
ikenna1 32:098fbc1222cd 245
ikenna1 39:7824f9080f59 246 if(reset1 == 0) {
ikenna1 39:7824f9080f59 247 _shwx1 = _shooter_xpos1 + 5;
ikenna1 39:7824f9080f59 248 _shwy1 = _shooter_ypos1 + 11;
ikenna1 39:7824f9080f59 249 reset1 = reset1 + 1;
ikenna1 39:7824f9080f59 250 }
ikenna1 39:7824f9080f59 251 lcd.drawRect(_shwx1,_shwy1,1,1,FILL_BLACK);
ikenna1 32:098fbc1222cd 252 // printf("Ship x and y pos, reset = %d , %d ,%d \n", _ship_xpos, _ship_ypos, reset);
ikenna1 32:098fbc1222cd 253 }
ikenna1 32:098fbc1222cd 254 void Enemy::draw_shw2(N5110 &lcd,Gamepad &pad)
ikenna1 32:098fbc1222cd 255 {
ikenna1 32:098fbc1222cd 256 _vshw2.x = 0; //Projectile doesn't move sideways.
ikenna1 32:098fbc1222cd 257 _vshw2.y = 5; //Projectile moves upwards on screen.
ikenna1 32:098fbc1222cd 258
ikenna1 32:098fbc1222cd 259 //resets once projectile reaches bottom of screen
ikenna1 32:098fbc1222cd 260 if(_shwy2 >= 48) {
ikenna1 32:098fbc1222cd 261 reset2= 0;
ikenna1 32:098fbc1222cd 262 }
ikenna1 32:098fbc1222cd 263
ikenna1 32:098fbc1222cd 264 if(reset2 == 0) {
ikenna1 32:098fbc1222cd 265 _shwx2 = _shooter_xpos2 + 5;
ikenna1 32:098fbc1222cd 266 _shwy2 = _shooter_ypos2 + 11;
ikenna1 32:098fbc1222cd 267 reset2 = reset2 + 1;
ikenna1 32:098fbc1222cd 268 }
ikenna1 32:098fbc1222cd 269 lcd.drawRect(_shwx2,_shwy2,1,1,FILL_BLACK);
ikenna1 32:098fbc1222cd 270 // printf("Ship x and y pos, reset = %d , %d ,%d \n", _ship_xpos, _ship_ypos, reset);
ikenna1 30:711d722f3cef 271 }
ikenna1 32:098fbc1222cd 272 void Enemy::draw_shw3(N5110 &lcd,Gamepad &pad)
ikenna1 32:098fbc1222cd 273 {
ikenna1 32:098fbc1222cd 274 _vshw3.x = 0; //Projectile doesn't move sideways.
ikenna1 32:098fbc1222cd 275 _vshw3.y = 3; //Projectile moves upwards on screen.
ikenna1 32:098fbc1222cd 276
ikenna1 32:098fbc1222cd 277 //resets once projectile reaches bottom of screen
ikenna1 32:098fbc1222cd 278 if(_shwy3 >= 48) {
ikenna1 32:098fbc1222cd 279 reset3 = 0;
ikenna1 32:098fbc1222cd 280 }
ikenna1 32:098fbc1222cd 281
ikenna1 32:098fbc1222cd 282 if(reset3 == 0) {
ikenna1 32:098fbc1222cd 283 _shwx3 = _shooter_xpos3 + 5;
ikenna1 32:098fbc1222cd 284 _shwy3 = _shooter_ypos3 + 11;
ikenna1 32:098fbc1222cd 285 reset3 = reset3 + 1;
ikenna1 32:098fbc1222cd 286 }
ikenna1 32:098fbc1222cd 287 lcd.drawRect(_shwx3,_shwy3,1,1,FILL_BLACK);
ikenna1 32:098fbc1222cd 288 // printf("Ship x and y pos, reset = %d , %d ,%d \n", _ship_xpos, _ship_ypos, reset);
ikenna1 32:098fbc1222cd 289 }
ikenna1 34:6d0786582d81 290 void Enemy::draw_shw(N5110 &lcd,Gamepad &pad)
ikenna1 32:098fbc1222cd 291 {
ikenna1 34:6d0786582d81 292 if(_shno == 1) {
ikenna1 32:098fbc1222cd 293 draw_shw1(lcd,pad);
ikenna1 32:098fbc1222cd 294 }
ikenna1 34:6d0786582d81 295 if(_shno == 2) {
ikenna1 32:098fbc1222cd 296 draw_shw1(lcd,pad);
ikenna1 32:098fbc1222cd 297 draw_shw2(lcd,pad);
ikenna1 32:098fbc1222cd 298 }
ikenna1 34:6d0786582d81 299 if(_shno == 3) {
ikenna1 32:098fbc1222cd 300 draw_shw1(lcd,pad);
ikenna1 32:098fbc1222cd 301 draw_shw2(lcd,pad);
ikenna1 32:098fbc1222cd 302 draw_shw3(lcd,pad);
ikenna1 32:098fbc1222cd 303 }
ikenna1 32:098fbc1222cd 304 }
ikenna1 32:098fbc1222cd 305 void Enemy::update_shw()
ikenna1 32:098fbc1222cd 306 {
ikenna1 39:7824f9080f59 307 _shwx1 = _shwx1 + _vshw1.x;
ikenna1 39:7824f9080f59 308 _shwy1 = _shwy1 + _vshw1.y;
ikenna1 39:7824f9080f59 309 _shwx2 = _shwx2 + _vshw2.x;
ikenna1 39:7824f9080f59 310 _shwy2 = _shwy2 + _vshw2.y;
ikenna1 39:7824f9080f59 311 _shwx3 = _shwx3 + _vshw3.x;
ikenna1 39:7824f9080f59 312 _shwy3 = _shwy3 + _vshw3.y;
ikenna1 32:098fbc1222cd 313 }
ikenna1 32:098fbc1222cd 314
ikenna1 37:8d8c8cce0bc7 315 Vector2D Enemy::get_shwpos(int shno)
ikenna1 32:098fbc1222cd 316 {
ikenna1 39:7824f9080f59 317 if(shno == 1) {
ikenna1 39:7824f9080f59 318 Vector2D pos = {_shwx1,_shwy1};
ikenna1 39:7824f9080f59 319 return pos;
ikenna1 37:8d8c8cce0bc7 320 }
ikenna1 39:7824f9080f59 321 if(shno == 2) {
ikenna1 39:7824f9080f59 322 Vector2D pos = {_shwx2,_shwy2};
ikenna1 39:7824f9080f59 323 return pos;
ikenna1 37:8d8c8cce0bc7 324 }
ikenna1 39:7824f9080f59 325 if(shno == 3) {
ikenna1 39:7824f9080f59 326 Vector2D pos = {_shwx3,_shwy3};
ikenna1 39:7824f9080f59 327 return pos;
ikenna1 37:8d8c8cce0bc7 328 }
ikenna1 32:098fbc1222cd 329 }
ikenna1 32:098fbc1222cd 330 /*
ikenna1 32:098fbc1222cd 331 void Weapons::set_pos(int xpos, int ypos)
ikenna1 32:098fbc1222cd 332 {
ikenna1 32:098fbc1222cd 333 _x = xpos;
ikenna1 32:098fbc1222cd 334 _y = ypos;
ikenna1 32:098fbc1222cd 335 }*/
ikenna1 39:7824f9080f59 336 void Enemy::reset_seeker(int seekno)
ikenna1 18:2cc6898de6b2 337 {
ikenna1 39:7824f9080f59 338 if(seekno == 1) {
ikenna1 39:7824f9080f59 339 _seeker1_xpos = rand_no(68);
ikenna1 39:7824f9080f59 340 _seeker1_ypos = 0;
ikenna1 39:7824f9080f59 341 }
ikenna1 39:7824f9080f59 342 if(seekno == 2) {
ikenna1 39:7824f9080f59 343 _seeker2_xpos = rand_no(68);
ikenna1 39:7824f9080f59 344 _seeker2_ypos = 0;
ikenna1 39:7824f9080f59 345 }
ikenna1 39:7824f9080f59 346 if(seekno == 3) {
ikenna1 39:7824f9080f59 347 _seeker3_xpos = rand_no(68);
ikenna1 39:7824f9080f59 348 _seeker3_ypos = 0;
ikenna1 39:7824f9080f59 349 }
ikenna1 18:2cc6898de6b2 350 }
ikenna1 35:3341f2bd0408 351 void Enemy::reset_shooter(int shooter)
ikenna1 35:3341f2bd0408 352 {
ikenna1 39:7824f9080f59 353 if(shooter == 1) {
ikenna1 39:7824f9080f59 354 _shooter_xpos1 = rand_no(68);
ikenna1 39:7824f9080f59 355 _shooter_ypos1 = 5;
ikenna1 35:3341f2bd0408 356 }
ikenna1 39:7824f9080f59 357 if(shooter == 2) {
ikenna1 39:7824f9080f59 358 _shooter_xpos2 = rand_no(68);
ikenna1 39:7824f9080f59 359 _shooter_ypos2 = 5;
ikenna1 35:3341f2bd0408 360 }
ikenna1 39:7824f9080f59 361 if(shooter == 3) {
ikenna1 39:7824f9080f59 362 _shooter_xpos3 = rand_no(68);
ikenna1 39:7824f9080f59 363 _shooter_ypos3 = 5;
ikenna1 35:3341f2bd0408 364 }
ikenna1 35:3341f2bd0408 365 }
ikenna1 39:7824f9080f59 366 Vector2D Enemy::get_seekerpos(int seekno)
ikenna1 15:009ccc07bb57 367 {
ikenna1 39:7824f9080f59 368 if(seekno == 1) {
ikenna1 39:7824f9080f59 369 Vector2D seeker_pos = {_seeker1_xpos,_seeker1_ypos};
ikenna1 39:7824f9080f59 370 return seeker_pos;
ikenna1 39:7824f9080f59 371 }
ikenna1 39:7824f9080f59 372 if(seekno == 2) {
ikenna1 39:7824f9080f59 373 Vector2D seeker_pos = {_seeker2_xpos,_seeker2_ypos};
ikenna1 39:7824f9080f59 374 return seeker_pos;
ikenna1 39:7824f9080f59 375 }
ikenna1 39:7824f9080f59 376 if(seekno == 3) {
ikenna1 39:7824f9080f59 377 Vector2D seeker_pos = {_seeker3_xpos,_seeker3_ypos};
ikenna1 39:7824f9080f59 378 return seeker_pos;
ikenna1 39:7824f9080f59 379 }
ikenna1 35:3341f2bd0408 380 }
ikenna1 37:8d8c8cce0bc7 381 Vector2D Enemy::get_shooterpos(int shno)
ikenna1 35:3341f2bd0408 382 {
ikenna1 39:7824f9080f59 383 if(shno == 1) {
ikenna1 39:7824f9080f59 384 Vector2D shooter_pos = {_shooter_xpos1,_shooter_ypos1};
ikenna1 39:7824f9080f59 385 return shooter_pos;
ikenna1 37:8d8c8cce0bc7 386 }
ikenna1 39:7824f9080f59 387 if(shno == 2) {
ikenna1 39:7824f9080f59 388 Vector2D shooter_pos = {_shooter_xpos2,_shooter_ypos2};
ikenna1 39:7824f9080f59 389 return shooter_pos;
ikenna1 37:8d8c8cce0bc7 390 }
ikenna1 39:7824f9080f59 391 if(shno == 3) {
ikenna1 39:7824f9080f59 392 Vector2D shooter_pos = {_shooter_xpos3,_shooter_ypos3};
ikenna1 39:7824f9080f59 393 return shooter_pos;
ikenna1 37:8d8c8cce0bc7 394 }
ikenna1 35:3341f2bd0408 395 }
ikenna1 37:8d8c8cce0bc7 396 int Enemy::rand_no(int scale)
ikenna1 35:3341f2bd0408 397 {
ikenna1 37:8d8c8cce0bc7 398 srand(time(NULL));
ikenna1 37:8d8c8cce0bc7 399 int rand_no = (rand() %scale) + 1;
ikenna1 37:8d8c8cce0bc7 400 // printf("random no = %d\n",rand_no);
ikenna1 37:8d8c8cce0bc7 401 return rand_no;
ikenna1 38:4571537238ed 402 }
ikenna1 39:7824f9080f59 403 void Enemy::sh_scaling(float time_elapsed)
ikenna1 38:4571537238ed 404 {
ikenna1 38:4571537238ed 405 // add if functions for shno
ikenna1 38:4571537238ed 406 // correct movement
ikenna1 39:7824f9080f59 407 int vmax = 3;
ikenna1 39:7824f9080f59 408 int time = floor(time_elapsed);
ikenna1 40:90c7a893d513 409 if(time%10 == 0) {
ikenna1 39:7824f9080f59 410 if(_vx1 > -vmax && _vx1 < vmax) {
ikenna1 39:7824f9080f59 411 _vx1 = _vx1 * 1.1;
ikenna1 39:7824f9080f59 412 }
ikenna1 39:7824f9080f59 413 if(_vx2 > - vmax && _vx2 < vmax) {
ikenna1 39:7824f9080f59 414 _vx2 = _vx2 * 1.1;
ikenna1 39:7824f9080f59 415 }
ikenna1 39:7824f9080f59 416 if(_vx3 > -vmax && _vx3 < vmax) {
ikenna1 39:7824f9080f59 417 _vx3 = _vx3 * 1.1;
ikenna1 39:7824f9080f59 418 }
ikenna1 39:7824f9080f59 419 if(_vy1 > -vmax && _vy1 < vmax) {
ikenna1 39:7824f9080f59 420 _vy1 = _vy1 * 1.1;
ikenna1 39:7824f9080f59 421 }
ikenna1 39:7824f9080f59 422 if(_vy2 > -vmax && _vy2 < vmax) {
ikenna1 39:7824f9080f59 423 _vy2 = _vy2 * 1.1;
ikenna1 39:7824f9080f59 424 }
ikenna1 39:7824f9080f59 425 if(_vy3 > -vmax && _vy3 < vmax) {
ikenna1 39:7824f9080f59 426 _vy3 = _vy3 * 1.1;
ikenna1 39:7824f9080f59 427 }
ikenna1 38:4571537238ed 428 }
ikenna1 40:90c7a893d513 429 // printf("time = %d, _vx1 = %f\n",time,_vx1);
ikenna1 40:90c7a893d513 430 }
ikenna1 40:90c7a893d513 431 int Enemy::closest(int x1, int y1, int x2, int y2)
ikenna1 40:90c7a893d513 432 {
ikenna1 40:90c7a893d513 433 int rangex = abs(x1 - x2);
ikenna1 40:90c7a893d513 434 int rangey = abs(y1 - y2);
ikenna1 40:90c7a893d513 435 int distance = (rangex+rangey)/2;
ikenna1 40:90c7a893d513 436 return distance;
ikenna1 40:90c7a893d513 437 }
ikenna1 40:90c7a893d513 438
ikenna1 40:90c7a893d513 439 Vector2D Enemy::find_closest(int ship_xpos,int ship_ypos, int seno, int shno)
ikenna1 40:90c7a893d513 440 {
ikenna1 40:90c7a893d513 441 // get the distance for all enemies
ikenna1 40:90c7a893d513 442 int sh1 = closest(ship_xpos,ship_ypos,_shooter_xpos1,_shooter_ypos1);
ikenna1 40:90c7a893d513 443 int sh2 = closest(ship_xpos,ship_ypos,_shooter_xpos2,_shooter_ypos2);
ikenna1 40:90c7a893d513 444 int sh3 = closest(ship_xpos,ship_ypos,_shooter_xpos3,_shooter_ypos3);
ikenna1 40:90c7a893d513 445 int se1 = closest(ship_xpos,ship_ypos,_seeker1_xpos,_seeker1_ypos);
ikenna1 40:90c7a893d513 446 int se2 = closest(ship_xpos,ship_ypos,_seeker2_xpos,_seeker2_ypos);
ikenna1 40:90c7a893d513 447 int se3 = closest(ship_xpos,ship_ypos,_seeker3_xpos,_seeker3_ypos);
ikenna1 40:90c7a893d513 448 printf("sh1 = %d\n",sh1);
ikenna1 40:90c7a893d513 449 printf("sh2 = %d\n",sh2);
ikenna1 40:90c7a893d513 450 printf("sh3 = %d\n",sh3);
ikenna1 40:90c7a893d513 451 printf("se1 = %d\n",se1);
ikenna1 40:90c7a893d513 452 printf("se2 = %d\n",se2);
ikenna1 40:90c7a893d513 453 printf("se3 = %d\n",se3);
ikenna1 40:90c7a893d513 454
ikenna1 40:90c7a893d513 455 int close[6] = {sh1,sh2,sh3,se1,se2,se3};
ikenna1 40:90c7a893d513 456 // find index of the smallest element
ikenna1 40:90c7a893d513 457 int index;
ikenna1 40:90c7a893d513 458 int smallest = close[0];
ikenna1 40:90c7a893d513 459 for(int i=0; i<6; i=i+1) {
ikenna1 40:90c7a893d513 460 if(smallest>close[i]) {
ikenna1 40:90c7a893d513 461 smallest=close[i];
ikenna1 40:90c7a893d513 462 index = i;
ikenna1 40:90c7a893d513 463 }
ikenna1 40:90c7a893d513 464 }
ikenna1 40:90c7a893d513 465 printf("index = %d, _shno = %d, _seno = %d\n",index,shno,seno);
ikenna1 40:90c7a893d513 466 // return the position of the closest enemy
ikenna1 40:90c7a893d513 467 if(index == 0 && shno >= 1){
ikenna1 40:90c7a893d513 468 printf("shooter1\n");
ikenna1 40:90c7a893d513 469 return {_shooter_xpos1,_shooter_ypos1};
ikenna1 40:90c7a893d513 470 }
ikenna1 40:90c7a893d513 471 if(index == 1 && shno >= 2){
ikenna1 40:90c7a893d513 472 // printf("shooter_xpos2 = %d, shooter_ypos2 = %d\n",_shooter_xpos2,_shooter_ypos2);
ikenna1 40:90c7a893d513 473 return {_shooter_xpos2,_shooter_ypos2};
ikenna1 40:90c7a893d513 474 }
ikenna1 40:90c7a893d513 475 if(index == 2 && shno >= 3){
ikenna1 40:90c7a893d513 476 printf("shooter3\n");
ikenna1 40:90c7a893d513 477 return {_shooter_xpos3,_shooter_ypos3};
ikenna1 40:90c7a893d513 478 }
ikenna1 40:90c7a893d513 479 if(index == 3 && seno >= 1 ){
ikenna1 40:90c7a893d513 480 printf("seeker1\n");
ikenna1 40:90c7a893d513 481 return {_seeker1_xpos,_seeker1_ypos};
ikenna1 40:90c7a893d513 482 }
ikenna1 40:90c7a893d513 483 if(index == 4 && seno >= 2){
ikenna1 40:90c7a893d513 484 printf("seeker2\n");
ikenna1 40:90c7a893d513 485 return {_seeker2_xpos,_seeker2_ypos};
ikenna1 40:90c7a893d513 486 }
ikenna1 40:90c7a893d513 487 if(index == 5 && seno >= 3){
ikenna1 40:90c7a893d513 488 printf("seeker3\n");
ikenna1 40:90c7a893d513 489 return {_seeker3_xpos,_seeker3_ypos};
ikenna1 40:90c7a893d513 490 }
ikenna1 40:90c7a893d513 491
ikenna1 40:90c7a893d513 492 }