ELEC2645 (2018/19) / Mbed 2 deprecated el17aio

Dependencies:   mbed

Committer:
ikenna1
Date:
Thu Apr 18 11:40:47 2019 +0000
Revision:
26:a53d41adf40b
Parent:
25:faba9eb44514
Child:
27:f99249e727fd
Changed the way collision functions work to now return a true or false value depending on if the collision occurred.;

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