ELEC2645 (2018/19) / Mbed 2 deprecated el17aio

Dependencies:   mbed

Committer:
ikenna1
Date:
Thu Apr 18 06:53:52 2019 +0000
Revision:
25:faba9eb44514
Parent:
24:ab821bfeb383
Child:
26:a53d41adf40b
* All current weapon systems working; * Started working on new ship orion

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