ELEC2645 (2018/19) / Mbed 2 deprecated el17aio

Dependencies:   mbed

Committer:
ikenna1
Date:
Fri Apr 19 02:40:08 2019 +0000
Revision:
29:4c7b16b5b6df
Parent:
28:6319e928f0aa
Child:
30:711d722f3cef
fixed alarm issue

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 29:4c7b16b5b6df 97 if(_shipno == 0 || _shipno == 1) {
ikenna1 28:6319e928f0aa 98 if(health <= 2 ) {
ikenna1 29:4c7b16b5b6df 99 pad.tone(1300,0.05);
ikenna1 29:4c7b16b5b6df 100 wait(0.05);
ikenna1 26:a53d41adf40b 101 }
ikenna1 28:6319e928f0aa 102 }
ikenna1 29:4c7b16b5b6df 103 else if(_shipno == 2) {
ikenna1 28:6319e928f0aa 104 if(health <= 1 ) {
ikenna1 29:4c7b16b5b6df 105 pad.tone(1300,0.05);
ikenna1 29:4c7b16b5b6df 106 wait(0.05);
ikenna1 28:6319e928f0aa 107 }
ikenna1 28:6319e928f0aa 108 }
ikenna1 29:4c7b16b5b6df 109 printf("health = %d||shipno = %d",health,_shipno);
ikenna1 7:ed5870cfb3e0 110 }
ikenna1 9:241a1a7d8527 111 void RosenEngine::get_pos()
ikenna1 9:241a1a7d8527 112 {
ikenna1 9:241a1a7d8527 113 Vector2D ship_pos = _ship.get_pos();
ikenna1 9:241a1a7d8527 114 ship_xpos = ship_pos.x;
ikenna1 9:241a1a7d8527 115 ship_ypos = ship_pos.y;
ikenna1 9:241a1a7d8527 116 _weapons.init(ship_xpos, ship_ypos, ship_width);
ikenna1 13:e114d362186d 117 _ycursor = _menu.get_ycursor();
ikenna1 25:faba9eb44514 118
ikenna1 25:faba9eb44514 119 if(_shipno == 0) {
ikenna1 25:faba9eb44514 120 ship_width = 9;
ikenna1 25:faba9eb44514 121 ship_height = 6;
ikenna1 25:faba9eb44514 122 }
ikenna1 25:faba9eb44514 123 if(_shipno == 1) {
ikenna1 25:faba9eb44514 124 ship_width = 7;
ikenna1 25:faba9eb44514 125 ship_height = 10;
ikenna1 25:faba9eb44514 126 }
ikenna1 9:241a1a7d8527 127 }
ikenna1 9:241a1a7d8527 128 void RosenEngine::title(N5110 &lcd)
ikenna1 9:241a1a7d8527 129 {
ikenna1 14:88ca5b1a111a 130 _menu.title(lcd);
ikenna1 10:c33d7593a275 131 _menu.update(_d);
ikenna1 10:c33d7593a275 132 }
ikenna1 13:e114d362186d 133 int RosenEngine::get_ycursor()
ikenna1 10:c33d7593a275 134 {
ikenna1 13:e114d362186d 135 _ycursor = _menu.get_ycursor();
ikenna1 10:c33d7593a275 136 return _ycursor;
ikenna1 12:47578eb9ea73 137 }
ikenna1 21:628fb703188f 138 int RosenEngine::get_shipno()
ikenna1 13:e114d362186d 139 {
ikenna1 21:628fb703188f 140 _shipno = _menu.get_xcursor();
ikenna1 21:628fb703188f 141 return _shipno;
ikenna1 13:e114d362186d 142 }
ikenna1 12:47578eb9ea73 143 void RosenEngine::ship_select(N5110 &lcd)
ikenna1 12:47578eb9ea73 144 {
ikenna1 12:47578eb9ea73 145 _menu.update(_d);
ikenna1 12:47578eb9ea73 146 _menu.disp_ships(lcd);
ikenna1 17:e65a9f981834 147 }
ikenna1 26:a53d41adf40b 148 bool RosenEngine::enemy_ship_collision()
ikenna1 18:2cc6898de6b2 149 {
ikenna1 25:faba9eb44514 150 // Vector2D ship_pos = _ship.get_pos();
ikenna1 19:3316dba9787e 151 Vector2D seeker_pos = _enemy.get_seekerpos();
ikenna1 25:faba9eb44514 152 int seeker_xpos = seeker_pos.x, seeker_ypos = seeker_pos.y;
ikenna1 25:faba9eb44514 153 int sxcol = 0;
ikenna1 25:faba9eb44514 154 int sycol = 0;
ikenna1 25:faba9eb44514 155 int ship_x[9],ship_y[6],seeker_x[9],seeker_y[6];
ikenna1 25:faba9eb44514 156 // printf("shipx = %d, shipy = %d\n",ship_xpos,ship_ypos);
ikenna1 19:3316dba9787e 157 // create an array of all x positions for the ship sprite i.e along its width (ship_x)
ikenna1 25:faba9eb44514 158 for(int cx = 0; cx<=9; cx=cx+1) {
ikenna1 25:faba9eb44514 159 ship_x[cx] = ship_xpos + cx;
ikenna1 25:faba9eb44514 160 seeker_x[cx] = seeker_xpos + cx;
ikenna1 25:faba9eb44514 161 // printf("ship_x = %d, seeker_x = %d\n", ship_x[cx], seeker_x[cx]);
ikenna1 19:3316dba9787e 162 }
ikenna1 19:3316dba9787e 163
ikenna1 19:3316dba9787e 164 // create an array of all y positions for the ship sprite i.e along its height (ship_y)
ikenna1 25:faba9eb44514 165 for(int cy = 0; cy<=6; cy=cy+1) {
ikenna1 25:faba9eb44514 166 ship_y[cy] = (ship_ypos ) + cy ;
ikenna1 25:faba9eb44514 167 seeker_y[cy] = seeker_ypos + cy;
ikenna1 25:faba9eb44514 168 // printf("ship_y = %d, seeker_y = %d\n", ship_y[cy], seeker_y[cy]);
ikenna1 19:3316dba9787e 169 }
ikenna1 19:3316dba9787e 170 // check all values of ship position against all values of seekers x position
ikenna1 20:5b4b3bf5795c 171 for(int nx = 0; nx<=9; nx=nx+1) {
ikenna1 20:5b4b3bf5795c 172 for(int mx = 0; mx<=9; mx=mx+1) {
ikenna1 19:3316dba9787e 173 if(ship_x[nx] == seeker_x[mx]) {
ikenna1 25:faba9eb44514 174 sxcol = 1;
ikenna1 19:3316dba9787e 175 }
ikenna1 19:3316dba9787e 176 }
ikenna1 19:3316dba9787e 177 }
ikenna1 20:5b4b3bf5795c 178 for(int ny = 0; ny<=6; ny=ny+1) {
ikenna1 20:5b4b3bf5795c 179 for(int my = 0; my<=6; my=my+1) {
ikenna1 19:3316dba9787e 180 if(ship_y[ny] == seeker_y[my]) {
ikenna1 25:faba9eb44514 181 sycol = 1;
ikenna1 19:3316dba9787e 182 }
ikenna1 19:3316dba9787e 183 }
ikenna1 19:3316dba9787e 184 }
ikenna1 20:5b4b3bf5795c 185
ikenna1 25:faba9eb44514 186 printf("sxcol = %d, sycol = %d\n", sxcol, sycol);
ikenna1 25:faba9eb44514 187 if(sxcol == 1 && sycol == 1) {
ikenna1 26:a53d41adf40b 188 return true;
ikenna1 26:a53d41adf40b 189 } else {
ikenna1 26:a53d41adf40b 190 return false;
ikenna1 18:2cc6898de6b2 191 }
ikenna1 23:0301effce801 192 }
ikenna1 26:a53d41adf40b 193 bool RosenEngine::enemy_projectile_collision()
ikenna1 23:0301effce801 194 {
ikenna1 25:faba9eb44514 195 Vector2D seeker_pos = _enemy.get_seekerpos();
ikenna1 25:faba9eb44514 196 int lazer_x[3],seeker_x[9],seeker_y[38];
ikenna1 25:faba9eb44514 197 int wxcol = 0;
ikenna1 25:faba9eb44514 198 int wycol = 0;
ikenna1 24:ab821bfeb383 199
ikenna1 24:ab821bfeb383 200 // get seekers x any y values and put into arrays
ikenna1 25:faba9eb44514 201 for(int cx = 0; cx<=9; cx=cx+1) {
ikenna1 25:faba9eb44514 202 seeker_x[cx] = seeker_pos.x + cx;
ikenna1 24:ab821bfeb383 203 }
ikenna1 25:faba9eb44514 204 for(int cy = 0; cy<=6; cy=cy+1) {
ikenna1 25:faba9eb44514 205 seeker_y[cy] = seeker_pos.y + cy;
ikenna1 24:ab821bfeb383 206 }
ikenna1 25:faba9eb44514 207
ikenna1 25:faba9eb44514 208 // kestrel artemis missle
ikenna1 24:ab821bfeb383 209 if(_shipno == 0) {
ikenna1 25:faba9eb44514 210 Vector2D missle_pos = _weapons.get_pos(_shipno);
ikenna1 24:ab821bfeb383 211
ikenna1 25:faba9eb44514 212 for(int cx = 0; cx<=9; cx=cx+1) {
ikenna1 25:faba9eb44514 213 seeker_x[cx] = seeker_pos.x + cx;
ikenna1 25:faba9eb44514 214 if(seeker_x[cx] == missle_pos.x) {
ikenna1 26:a53d41adf40b 215 printf("seeker_xpos = %f, missle_xpos = %f\n",seeker_pos.x,missle_pos.x);
ikenna1 25:faba9eb44514 216 wxcol = 1;
ikenna1 25:faba9eb44514 217 }
ikenna1 25:faba9eb44514 218 }
ikenna1 25:faba9eb44514 219 for(int cy = 0; cy<=7; cy=cy+1) {
ikenna1 25:faba9eb44514 220 seeker_y[cy] = seeker_pos.y + cy;
ikenna1 26:a53d41adf40b 221 if(seeker_y[cy] == missle_pos.y + 4) {
ikenna1 26:a53d41adf40b 222 printf("seeker_ypos = %f, missle_ypos = %f\n",seeker_pos.y,missle_pos.y);
ikenna1 25:faba9eb44514 223 wycol = 1;
ikenna1 25:faba9eb44514 224 }
ikenna1 25:faba9eb44514 225 }
ikenna1 25:faba9eb44514 226 if(wxcol == 1 && wycol == 1) {
ikenna1 27:f99249e727fd 227 _weapons.set_pos(ship_xpos,ship_ypos); // reset missle after contact
ikenna1 26:a53d41adf40b 228 return true;
ikenna1 25:faba9eb44514 229 }
ikenna1 24:ab821bfeb383 230 }
ikenna1 25:faba9eb44514 231
ikenna1 25:faba9eb44514 232
ikenna1 25:faba9eb44514 233 // imperion lazer
ikenna1 24:ab821bfeb383 234 if(_shipno == 1) {
ikenna1 25:faba9eb44514 235 for(int cx = 0; cx<=3; cx=cx+1) {
ikenna1 25:faba9eb44514 236 lazer_x[cx] = (ship_xpos + 2) + cx;
ikenna1 24:ab821bfeb383 237 }
ikenna1 24:ab821bfeb383 238 // ony register collision if seeker is above ship
ikenna1 25:faba9eb44514 239 if(seeker_pos.y + 6 < ship_ypos) {
ikenna1 24:ab821bfeb383 240 // check all values of ship position against all values of seekers x position
ikenna1 24:ab821bfeb383 241 for(int nx = 0; nx<=3; nx=nx+1) {
ikenna1 24:ab821bfeb383 242 for(int mx = 0; mx<=9; mx=mx+1) {
ikenna1 24:ab821bfeb383 243 if(lazer_x[nx] == seeker_x[mx]) {
ikenna1 25:faba9eb44514 244 wxcol = 1;
ikenna1 26:a53d41adf40b 245 printf("lazer_x = %d,seeker_x = %f\n");
ikenna1 24:ab821bfeb383 246 }
ikenna1 24:ab821bfeb383 247 }
ikenna1 24:ab821bfeb383 248 }
ikenna1 24:ab821bfeb383 249 }
ikenna1 25:faba9eb44514 250 if(wxcol == 1 && A == true) {
ikenna1 26:a53d41adf40b 251 return true;
ikenna1 28:6319e928f0aa 252 } else {
ikenna1 26:a53d41adf40b 253 return false;
ikenna1 24:ab821bfeb383 254 }
ikenna1 23:0301effce801 255 }
ikenna1 27:f99249e727fd 256 if(_shipno == 2) {
ikenna1 27:f99249e727fd 257 return false;
ikenna1 28:6319e928f0aa 258 }
ikenna1 29:4c7b16b5b6df 259 }
ikenna1 29:4c7b16b5b6df 260 void RosenEngine::score(){
ikenna1 18:2cc6898de6b2 261 }