ELEC2645 (2018/19) / Mbed 2 deprecated el17aio

Dependencies:   mbed

Committer:
ikenna1
Date:
Thu Apr 18 13:57:41 2019 +0000
Revision:
27:f99249e727fd
Parent:
26:a53d41adf40b
Child:
28:6319e928f0aa
change the way shields go down

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