ELEC2645 (2018/19) / Mbed 2 deprecated el17aio

Dependencies:   mbed

Committer:
ikenna1
Date:
Thu Apr 18 20:09:29 2019 +0000
Revision:
28:6319e928f0aa
Parent:
27:f99249e727fd
Child:
29:4c7b16b5b6df
* added warning sirens when health falls below threshold; * added new enemy shooter

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ikenna1 3:f9cd1a38d5c6 1 #include "RosenEngine.h"
ikenna1 23:0301effce801 2 DigitalIn A(PTB9);
ikenna1 3:f9cd1a38d5c6 3
ikenna1 2:66a4e5d7a7cd 4 // Constructor
ikenna1 2:66a4e5d7a7cd 5 RosenEngine::RosenEngine()
ikenna1 2:66a4e5d7a7cd 6 {
ikenna1 2:66a4e5d7a7cd 7
ikenna1 2:66a4e5d7a7cd 8 }
ikenna1 2:66a4e5d7a7cd 9 // Destructor
ikenna1 2:66a4e5d7a7cd 10 RosenEngine::~RosenEngine()
ikenna1 2:66a4e5d7a7cd 11 {
ikenna1 2:66a4e5d7a7cd 12
ikenna1 2:66a4e5d7a7cd 13 }
ikenna1 3:f9cd1a38d5c6 14
ikenna1 4:740e14ebbc97 15
ikenna1 9:241a1a7d8527 16 void RosenEngine::init(int ship_width,int ship_height,int ship_speed,int ship_xpos,int ship_ypos)
ikenna1 2:66a4e5d7a7cd 17 {
ikenna1 4:740e14ebbc97 18 // initialise the game parameters
ikenna1 9:241a1a7d8527 19 _ship.init(ship_width,ship_height,ship_speed,ship_xpos,ship_ypos);
ikenna1 15:009ccc07bb57 20 // place seeker above the ship
ikenna1 21:628fb703188f 21 _enemy.init(48,0);
ikenna1 9:241a1a7d8527 22 _menu.init(16);
ikenna1 21:628fb703188f 23 _health.init(_shipno);
ikenna1 13:e114d362186d 24
ikenna1 3:f9cd1a38d5c6 25 }
ikenna1 27:f99249e727fd 26 void RosenEngine::reset()
ikenna1 27:f99249e727fd 27 {
ikenna1 27:f99249e727fd 28 _health.init(_shipno);
ikenna1 27:f99249e727fd 29 }
ikenna1 4:740e14ebbc97 30
ikenna1 5:bb6edc5b5be3 31 void RosenEngine::read_input(Gamepad &pad)
ikenna1 4:740e14ebbc97 32 {
ikenna1 4:740e14ebbc97 33 Vector2D mapped_coord = pad.get_coord();
ikenna1 8:87a845b8575e 34 _xjoystick = mapped_coord.x;
ikenna1 8:87a845b8575e 35 _yjoystick = mapped_coord.y;
ikenna1 9:241a1a7d8527 36 _d = pad.get_direction();
ikenna1 14:88ca5b1a111a 37 // printf("_xjoystick ,_yjoystick = %f , %f\n",_xjoystick, _yjoystick);
ikenna1 4:740e14ebbc97 38 }
ikenna1 4:740e14ebbc97 39
ikenna1 14:88ca5b1a111a 40 void RosenEngine::draw(N5110 &lcd, Gamepad &pad)
ikenna1 7:ed5870cfb3e0 41 {
ikenna1 27:f99249e727fd 42 _health.draw_health(lcd,_shipno);
ikenna1 18:2cc6898de6b2 43 _health.draw_shields(lcd);
ikenna1 15:009ccc07bb57 44 _enemy.draw_seeker(lcd);
ikenna1 21:628fb703188f 45 if(_shipno == 0) {
ikenna1 14:88ca5b1a111a 46 _ship.set_dimensions(9,6);
ikenna1 26:a53d41adf40b 47 _ship.draw_ship(lcd,_shipno);
ikenna1 23:0301effce801 48 _weapons.draw(lcd,pad,_shipno);
ikenna1 13:e114d362186d 49 }
ikenna1 21:628fb703188f 50 if(_shipno == 1) {
ikenna1 14:88ca5b1a111a 51 _ship.set_dimensions(7,10);
ikenna1 26:a53d41adf40b 52 _ship.draw_ship(lcd,_shipno);
ikenna1 26:a53d41adf40b 53 _weapons.draw(lcd,pad,_shipno);
ikenna1 26:a53d41adf40b 54 }
ikenna1 26:a53d41adf40b 55 if(_shipno == 2) {
ikenna1 26:a53d41adf40b 56 _ship.set_dimensions(7,10);
ikenna1 26:a53d41adf40b 57 _ship.draw_ship(lcd,_shipno);
ikenna1 23:0301effce801 58 _weapons.draw(lcd,pad,_shipno);
ikenna1 13:e114d362186d 59 }
ikenna1 11:73cd744ffa80 60 }
ikenna1 7:ed5870cfb3e0 61
ikenna1 7:ed5870cfb3e0 62 void RosenEngine::update(Gamepad &pad)
ikenna1 7:ed5870cfb3e0 63 {
ikenna1 15:009ccc07bb57 64 _enemy.update_seeker(ship_xpos, ship_ypos);
ikenna1 21:628fb703188f 65 if(_shipno == 0) {
ikenna1 14:88ca5b1a111a 66 _ship.update_ship(_xjoystick,_yjoystick);
ikenna1 14:88ca5b1a111a 67 _weapons.update();
ikenna1 14:88ca5b1a111a 68 }
ikenna1 23:0301effce801 69 if(_shipno == 1 && A == false) {
ikenna1 14:88ca5b1a111a 70 _ship.update_ship(_xjoystick,_yjoystick);
ikenna1 14:88ca5b1a111a 71 _weapons.update();
ikenna1 14:88ca5b1a111a 72 }
ikenna1 27:f99249e727fd 73 if(_shipno == 2) {
ikenna1 27:f99249e727fd 74 _ship.update_ship(_xjoystick,_yjoystick);
ikenna1 27:f99249e727fd 75 _weapons.update();
ikenna1 27:f99249e727fd 76 }
ikenna1 14:88ca5b1a111a 77 // _menu.update(_d);
ikenna1 20:5b4b3bf5795c 78
ikenna1 28:6319e928f0aa 79 if(enemy_ship_collision() == true) {
ikenna1 28:6319e928f0aa 80 _health.update(1,pad);
ikenna1 26:a53d41adf40b 81 _enemy.reset_seeker();
ikenna1 28:6319e928f0aa 82 pad.tone(500,0.05);
ikenna1 28:6319e928f0aa 83 wait(0.05);
ikenna1 28:6319e928f0aa 84 }
ikenna1 28:6319e928f0aa 85 if(enemy_projectile_collision() == true) {
ikenna1 26:a53d41adf40b 86 _enemy.reset_seeker();
ikenna1 28:6319e928f0aa 87 pad.tone(1000,0.05);
ikenna1 28:6319e928f0aa 88 wait(0.05);
ikenna1 28:6319e928f0aa 89 pad.tone(1500,0.05);
ikenna1 28:6319e928f0aa 90 wait(0.05);
ikenna1 28:6319e928f0aa 91 }
ikenna1 28:6319e928f0aa 92 // printf("collision check complete");
ikenna1 28:6319e928f0aa 93
ikenna1 28:6319e928f0aa 94 // warning sound
ikenna1 28:6319e928f0aa 95 Vector2D hp = _health.get_hp();
ikenna1 28:6319e928f0aa 96 int health = hp.x;
ikenna1 28:6319e928f0aa 97 if(_shipno == 0 || 1) {
ikenna1 28:6319e928f0aa 98 if(health <= 2 ) {
ikenna1 28:6319e928f0aa 99 pad.tone(1300,0.1);
ikenna1 28:6319e928f0aa 100 wait(0.1);
ikenna1 26:a53d41adf40b 101 }
ikenna1 28:6319e928f0aa 102 }
ikenna1 28:6319e928f0aa 103 if(_shipno == 2) {
ikenna1 28:6319e928f0aa 104 if(health <= 1 ) {
ikenna1 28:6319e928f0aa 105 pad.tone(1300,0.1);
ikenna1 28:6319e928f0aa 106 wait(0.1);
ikenna1 28:6319e928f0aa 107 }
ikenna1 28:6319e928f0aa 108 }
ikenna1 7:ed5870cfb3e0 109 }
ikenna1 9:241a1a7d8527 110 void RosenEngine::get_pos()
ikenna1 9:241a1a7d8527 111 {
ikenna1 9:241a1a7d8527 112 Vector2D ship_pos = _ship.get_pos();
ikenna1 9:241a1a7d8527 113 ship_xpos = ship_pos.x;
ikenna1 9:241a1a7d8527 114 ship_ypos = ship_pos.y;
ikenna1 9:241a1a7d8527 115 _weapons.init(ship_xpos, ship_ypos, ship_width);
ikenna1 13:e114d362186d 116 _ycursor = _menu.get_ycursor();
ikenna1 25:faba9eb44514 117
ikenna1 25:faba9eb44514 118 if(_shipno == 0) {
ikenna1 25:faba9eb44514 119 ship_width = 9;
ikenna1 25:faba9eb44514 120 ship_height = 6;
ikenna1 25:faba9eb44514 121 }
ikenna1 25:faba9eb44514 122 if(_shipno == 1) {
ikenna1 25:faba9eb44514 123 ship_width = 7;
ikenna1 25:faba9eb44514 124 ship_height = 10;
ikenna1 25:faba9eb44514 125 }
ikenna1 9:241a1a7d8527 126 }
ikenna1 9:241a1a7d8527 127 void RosenEngine::title(N5110 &lcd)
ikenna1 9:241a1a7d8527 128 {
ikenna1 14:88ca5b1a111a 129 _menu.title(lcd);
ikenna1 10:c33d7593a275 130 _menu.update(_d);
ikenna1 10:c33d7593a275 131 }
ikenna1 13:e114d362186d 132 int RosenEngine::get_ycursor()
ikenna1 10:c33d7593a275 133 {
ikenna1 13:e114d362186d 134 _ycursor = _menu.get_ycursor();
ikenna1 10:c33d7593a275 135 return _ycursor;
ikenna1 12:47578eb9ea73 136 }
ikenna1 21:628fb703188f 137 int RosenEngine::get_shipno()
ikenna1 13:e114d362186d 138 {
ikenna1 21:628fb703188f 139 _shipno = _menu.get_xcursor();
ikenna1 21:628fb703188f 140 return _shipno;
ikenna1 13:e114d362186d 141 }
ikenna1 12:47578eb9ea73 142 void RosenEngine::ship_select(N5110 &lcd)
ikenna1 12:47578eb9ea73 143 {
ikenna1 12:47578eb9ea73 144 _menu.update(_d);
ikenna1 12:47578eb9ea73 145 _menu.disp_ships(lcd);
ikenna1 17:e65a9f981834 146 }
ikenna1 26:a53d41adf40b 147 bool RosenEngine::enemy_ship_collision()
ikenna1 18:2cc6898de6b2 148 {
ikenna1 25:faba9eb44514 149 // Vector2D ship_pos = _ship.get_pos();
ikenna1 19:3316dba9787e 150 Vector2D seeker_pos = _enemy.get_seekerpos();
ikenna1 25:faba9eb44514 151 int seeker_xpos = seeker_pos.x, seeker_ypos = seeker_pos.y;
ikenna1 25:faba9eb44514 152 int sxcol = 0;
ikenna1 25:faba9eb44514 153 int sycol = 0;
ikenna1 25:faba9eb44514 154 int ship_x[9],ship_y[6],seeker_x[9],seeker_y[6];
ikenna1 25:faba9eb44514 155 // printf("shipx = %d, shipy = %d\n",ship_xpos,ship_ypos);
ikenna1 19:3316dba9787e 156 // create an array of all x positions for the ship sprite i.e along its width (ship_x)
ikenna1 25:faba9eb44514 157 for(int cx = 0; cx<=9; cx=cx+1) {
ikenna1 25:faba9eb44514 158 ship_x[cx] = ship_xpos + cx;
ikenna1 25:faba9eb44514 159 seeker_x[cx] = seeker_xpos + cx;
ikenna1 25:faba9eb44514 160 // printf("ship_x = %d, seeker_x = %d\n", ship_x[cx], seeker_x[cx]);
ikenna1 19:3316dba9787e 161 }
ikenna1 19:3316dba9787e 162
ikenna1 19:3316dba9787e 163 // create an array of all y positions for the ship sprite i.e along its height (ship_y)
ikenna1 25:faba9eb44514 164 for(int cy = 0; cy<=6; cy=cy+1) {
ikenna1 25:faba9eb44514 165 ship_y[cy] = (ship_ypos ) + cy ;
ikenna1 25:faba9eb44514 166 seeker_y[cy] = seeker_ypos + cy;
ikenna1 25:faba9eb44514 167 // printf("ship_y = %d, seeker_y = %d\n", ship_y[cy], seeker_y[cy]);
ikenna1 19:3316dba9787e 168 }
ikenna1 19:3316dba9787e 169 // check all values of ship position against all values of seekers x position
ikenna1 20:5b4b3bf5795c 170 for(int nx = 0; nx<=9; nx=nx+1) {
ikenna1 20:5b4b3bf5795c 171 for(int mx = 0; mx<=9; mx=mx+1) {
ikenna1 19:3316dba9787e 172 if(ship_x[nx] == seeker_x[mx]) {
ikenna1 25:faba9eb44514 173 sxcol = 1;
ikenna1 19:3316dba9787e 174 }
ikenna1 19:3316dba9787e 175 }
ikenna1 19:3316dba9787e 176 }
ikenna1 20:5b4b3bf5795c 177 for(int ny = 0; ny<=6; ny=ny+1) {
ikenna1 20:5b4b3bf5795c 178 for(int my = 0; my<=6; my=my+1) {
ikenna1 19:3316dba9787e 179 if(ship_y[ny] == seeker_y[my]) {
ikenna1 25:faba9eb44514 180 sycol = 1;
ikenna1 19:3316dba9787e 181 }
ikenna1 19:3316dba9787e 182 }
ikenna1 19:3316dba9787e 183 }
ikenna1 20:5b4b3bf5795c 184
ikenna1 25:faba9eb44514 185 printf("sxcol = %d, sycol = %d\n", sxcol, sycol);
ikenna1 25:faba9eb44514 186 if(sxcol == 1 && sycol == 1) {
ikenna1 26:a53d41adf40b 187 return true;
ikenna1 26:a53d41adf40b 188 } else {
ikenna1 26:a53d41adf40b 189 return false;
ikenna1 18:2cc6898de6b2 190 }
ikenna1 23:0301effce801 191 }
ikenna1 26:a53d41adf40b 192 bool RosenEngine::enemy_projectile_collision()
ikenna1 23:0301effce801 193 {
ikenna1 25:faba9eb44514 194 Vector2D seeker_pos = _enemy.get_seekerpos();
ikenna1 25:faba9eb44514 195 int lazer_x[3],seeker_x[9],seeker_y[38];
ikenna1 25:faba9eb44514 196 int wxcol = 0;
ikenna1 25:faba9eb44514 197 int wycol = 0;
ikenna1 24:ab821bfeb383 198
ikenna1 24:ab821bfeb383 199 // get seekers x any y values and put into arrays
ikenna1 25:faba9eb44514 200 for(int cx = 0; cx<=9; cx=cx+1) {
ikenna1 25:faba9eb44514 201 seeker_x[cx] = seeker_pos.x + cx;
ikenna1 24:ab821bfeb383 202 }
ikenna1 25:faba9eb44514 203 for(int cy = 0; cy<=6; cy=cy+1) {
ikenna1 25:faba9eb44514 204 seeker_y[cy] = seeker_pos.y + cy;
ikenna1 24:ab821bfeb383 205 }
ikenna1 25:faba9eb44514 206
ikenna1 25:faba9eb44514 207 // kestrel artemis missle
ikenna1 24:ab821bfeb383 208 if(_shipno == 0) {
ikenna1 25:faba9eb44514 209 Vector2D missle_pos = _weapons.get_pos(_shipno);
ikenna1 24:ab821bfeb383 210
ikenna1 25:faba9eb44514 211 for(int cx = 0; cx<=9; cx=cx+1) {
ikenna1 25:faba9eb44514 212 seeker_x[cx] = seeker_pos.x + cx;
ikenna1 25:faba9eb44514 213 if(seeker_x[cx] == missle_pos.x) {
ikenna1 26:a53d41adf40b 214 printf("seeker_xpos = %f, missle_xpos = %f\n",seeker_pos.x,missle_pos.x);
ikenna1 25:faba9eb44514 215 wxcol = 1;
ikenna1 25:faba9eb44514 216 }
ikenna1 25:faba9eb44514 217 }
ikenna1 25:faba9eb44514 218 for(int cy = 0; cy<=7; cy=cy+1) {
ikenna1 25:faba9eb44514 219 seeker_y[cy] = seeker_pos.y + cy;
ikenna1 26:a53d41adf40b 220 if(seeker_y[cy] == missle_pos.y + 4) {
ikenna1 26:a53d41adf40b 221 printf("seeker_ypos = %f, missle_ypos = %f\n",seeker_pos.y,missle_pos.y);
ikenna1 25:faba9eb44514 222 wycol = 1;
ikenna1 25:faba9eb44514 223 }
ikenna1 25:faba9eb44514 224 }
ikenna1 25:faba9eb44514 225 if(wxcol == 1 && wycol == 1) {
ikenna1 27:f99249e727fd 226 _weapons.set_pos(ship_xpos,ship_ypos); // reset missle after contact
ikenna1 26:a53d41adf40b 227 return true;
ikenna1 25:faba9eb44514 228 }
ikenna1 24:ab821bfeb383 229 }
ikenna1 25:faba9eb44514 230
ikenna1 25:faba9eb44514 231
ikenna1 25:faba9eb44514 232 // imperion lazer
ikenna1 24:ab821bfeb383 233 if(_shipno == 1) {
ikenna1 25:faba9eb44514 234 for(int cx = 0; cx<=3; cx=cx+1) {
ikenna1 25:faba9eb44514 235 lazer_x[cx] = (ship_xpos + 2) + cx;
ikenna1 24:ab821bfeb383 236 }
ikenna1 24:ab821bfeb383 237 // ony register collision if seeker is above ship
ikenna1 25:faba9eb44514 238 if(seeker_pos.y + 6 < ship_ypos) {
ikenna1 24:ab821bfeb383 239 // check all values of ship position against all values of seekers x position
ikenna1 24:ab821bfeb383 240 for(int nx = 0; nx<=3; nx=nx+1) {
ikenna1 24:ab821bfeb383 241 for(int mx = 0; mx<=9; mx=mx+1) {
ikenna1 24:ab821bfeb383 242 if(lazer_x[nx] == seeker_x[mx]) {
ikenna1 25:faba9eb44514 243 wxcol = 1;
ikenna1 26:a53d41adf40b 244 printf("lazer_x = %d,seeker_x = %f\n");
ikenna1 24:ab821bfeb383 245 }
ikenna1 24:ab821bfeb383 246 }
ikenna1 24:ab821bfeb383 247 }
ikenna1 24:ab821bfeb383 248 }
ikenna1 25:faba9eb44514 249 if(wxcol == 1 && A == true) {
ikenna1 26:a53d41adf40b 250 return true;
ikenna1 28:6319e928f0aa 251 } else {
ikenna1 26:a53d41adf40b 252 return false;
ikenna1 24:ab821bfeb383 253 }
ikenna1 23:0301effce801 254 }
ikenna1 27:f99249e727fd 255 if(_shipno == 2) {
ikenna1 27:f99249e727fd 256 return false;
ikenna1 28:6319e928f0aa 257 }
ikenna1 18:2cc6898de6b2 258 }