Ikenna Adrian Ozoemena 201157039

Dependencies:   mbed

Committer:
ikenna1
Date:
Wed May 08 09:22:16 2019 +0000
Revision:
41:e1fa36c0492e
Parent:
40:90c7a893d513
Child:
42:ee13e1d103d8
Child:
45:fe5fc85a5c73
Increased lore and cleaned code in preparation for documentation

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