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