ELEC2645 (2018/19) / Mbed 2 deprecated el17aio

Dependencies:   mbed

Committer:
ikenna1
Date:
Sun Apr 14 18:11:32 2019 +0000
Revision:
24:ab821bfeb383
Parent:
23:0301effce801
Child:
25:faba9eb44514
added imperion lazer - seeker collision

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 ship_width = 9;
ikenna1 9:241a1a7d8527 76 _weapons.init(ship_xpos, ship_ypos, ship_width);
ikenna1 13:e114d362186d 77 _ycursor = _menu.get_ycursor();
ikenna1 9:241a1a7d8527 78 }
ikenna1 9:241a1a7d8527 79 void RosenEngine::title(N5110 &lcd)
ikenna1 9:241a1a7d8527 80 {
ikenna1 14:88ca5b1a111a 81 _menu.title(lcd);
ikenna1 10:c33d7593a275 82 _menu.update(_d);
ikenna1 10:c33d7593a275 83 }
ikenna1 13:e114d362186d 84 int RosenEngine::get_ycursor()
ikenna1 10:c33d7593a275 85 {
ikenna1 13:e114d362186d 86 _ycursor = _menu.get_ycursor();
ikenna1 10:c33d7593a275 87 return _ycursor;
ikenna1 12:47578eb9ea73 88 }
ikenna1 21:628fb703188f 89 int RosenEngine::get_shipno()
ikenna1 13:e114d362186d 90 {
ikenna1 21:628fb703188f 91 _shipno = _menu.get_xcursor();
ikenna1 21:628fb703188f 92 return _shipno;
ikenna1 13:e114d362186d 93 }
ikenna1 12:47578eb9ea73 94 void RosenEngine::ship_select(N5110 &lcd)
ikenna1 12:47578eb9ea73 95 {
ikenna1 12:47578eb9ea73 96 _menu.update(_d);
ikenna1 12:47578eb9ea73 97 _menu.disp_ships(lcd);
ikenna1 17:e65a9f981834 98 }
ikenna1 18:2cc6898de6b2 99 void RosenEngine::check_enemy_ship_collision()
ikenna1 18:2cc6898de6b2 100 {
ikenna1 18:2cc6898de6b2 101 Vector2D ship_pos = _ship.get_pos();
ikenna1 19:3316dba9787e 102 Vector2D seeker_pos = _enemy.get_seekerpos();
ikenna1 19:3316dba9787e 103 int ship_x[9];
ikenna1 19:3316dba9787e 104 int ship_y[6];
ikenna1 19:3316dba9787e 105 int seeker_x[9];
ikenna1 19:3316dba9787e 106 int seeker_y[6];
ikenna1 19:3316dba9787e 107 bool xcol = false;
ikenna1 19:3316dba9787e 108 bool ycol = false;
ikenna1 19:3316dba9787e 109
ikenna1 19:3316dba9787e 110 // create an array of all x positions for the ship sprite i.e along its width (ship_x)
ikenna1 20:5b4b3bf5795c 111 for(int countx = 0; countx<=9; countx=countx+1) {
ikenna1 19:3316dba9787e 112 ship_x[countx] = ship_pos.x + countx;
ikenna1 19:3316dba9787e 113 seeker_x[countx] = seeker_pos.x + countx;
ikenna1 21:628fb703188f 114 // printf("ship_x = %d, seeker_x = %d\n", ship_x[countx], seeker_x[countx]);
ikenna1 19:3316dba9787e 115 }
ikenna1 19:3316dba9787e 116
ikenna1 19:3316dba9787e 117 // create an array of all y positions for the ship sprite i.e along its height (ship_y)
ikenna1 20:5b4b3bf5795c 118 for(int county = 0; county<=6; county=county+1) {
ikenna1 19:3316dba9787e 119 ship_y[county] = ship_pos.y + county;
ikenna1 19:3316dba9787e 120 seeker_y[county] = seeker_pos.y + county;
ikenna1 21:628fb703188f 121 //printf("ship_y = %d, seeker_y = %d\n", ship_y[county], seeker_y[county]);
ikenna1 19:3316dba9787e 122 }
ikenna1 19:3316dba9787e 123 // check all values of ship position against all values of seekers x position
ikenna1 20:5b4b3bf5795c 124 for(int nx = 0; nx<=9; nx=nx+1) {
ikenna1 20:5b4b3bf5795c 125 for(int mx = 0; mx<=9; mx=mx+1) {
ikenna1 19:3316dba9787e 126 if(ship_x[nx] == seeker_x[mx]) {
ikenna1 19:3316dba9787e 127 xcol = true;
ikenna1 19:3316dba9787e 128 }
ikenna1 19:3316dba9787e 129 }
ikenna1 19:3316dba9787e 130 }
ikenna1 20:5b4b3bf5795c 131 for(int ny = 0; ny<=6; ny=ny+1) {
ikenna1 20:5b4b3bf5795c 132 for(int my = 0; my<=6; my=my+1) {
ikenna1 19:3316dba9787e 133 if(ship_y[ny] == seeker_y[my]) {
ikenna1 19:3316dba9787e 134 ycol = true;
ikenna1 19:3316dba9787e 135 }
ikenna1 19:3316dba9787e 136 }
ikenna1 19:3316dba9787e 137 }
ikenna1 20:5b4b3bf5795c 138
ikenna1 21:628fb703188f 139 printf("xcol = %d, ycol = %d\n", xcol, ycol);
ikenna1 19:3316dba9787e 140 if(xcol == true && ycol == true) {
ikenna1 18:2cc6898de6b2 141 _health.update(1);
ikenna1 18:2cc6898de6b2 142 _enemy.reset_seeker();
ikenna1 18:2cc6898de6b2 143 }
ikenna1 23:0301effce801 144 }
ikenna1 23:0301effce801 145 void RosenEngine::check_enemy_projectile_collision()
ikenna1 23:0301effce801 146 {
ikenna1 24:ab821bfeb383 147 Vector2D ship_pos = _ship.get_pos();
ikenna1 24:ab821bfeb383 148 int lazer_x[3];
ikenna1 24:ab821bfeb383 149 int seeker_x[9];
ikenna1 24:ab821bfeb383 150 int seeker_y[38];
ikenna1 24:ab821bfeb383 151 bool xcol = false;
ikenna1 24:ab821bfeb383 152
ikenna1 24:ab821bfeb383 153 // get seekers x any y values and put into arrays
ikenna1 23:0301effce801 154 Vector2D seeker_pos = _enemy.get_seekerpos();
ikenna1 24:ab821bfeb383 155 for(int countx = 0; countx<=9; countx=countx+1) {
ikenna1 24:ab821bfeb383 156 seeker_x[countx] = seeker_pos.x + countx;
ikenna1 24:ab821bfeb383 157 }
ikenna1 24:ab821bfeb383 158 for(int county = 0; county<=6; county=county+1) {
ikenna1 24:ab821bfeb383 159 seeker_y[county] = seeker_pos.y + county;
ikenna1 24:ab821bfeb383 160 }
ikenna1 24:ab821bfeb383 161 // kestrel canon
ikenna1 24:ab821bfeb383 162 if(_shipno == 0) {
ikenna1 24:ab821bfeb383 163
ikenna1 24:ab821bfeb383 164 }
ikenna1 23:0301effce801 165
ikenna1 24:ab821bfeb383 166 // imperion lazer
ikenna1 24:ab821bfeb383 167 if(_shipno == 1) {
ikenna1 24:ab821bfeb383 168 for(int countx = 0; countx<=3; countx=countx+1) {
ikenna1 24:ab821bfeb383 169 lazer_x[countx] = (ship_pos.x + 2) + countx;
ikenna1 24:ab821bfeb383 170 }
ikenna1 24:ab821bfeb383 171 // ony register collision if seeker is above ship
ikenna1 24:ab821bfeb383 172 if(seeker_pos.y + 6 < ship_pos.y) {
ikenna1 24:ab821bfeb383 173 // check all values of ship position against all values of seekers x position
ikenna1 24:ab821bfeb383 174 for(int nx = 0; nx<=3; nx=nx+1) {
ikenna1 24:ab821bfeb383 175 for(int mx = 0; mx<=9; mx=mx+1) {
ikenna1 24:ab821bfeb383 176 if(lazer_x[nx] == seeker_x[mx]) {
ikenna1 24:ab821bfeb383 177 xcol = true;
ikenna1 24:ab821bfeb383 178 }
ikenna1 24:ab821bfeb383 179 }
ikenna1 24:ab821bfeb383 180 }
ikenna1 24:ab821bfeb383 181 }
ikenna1 24:ab821bfeb383 182 if(xcol == true && A == true) {
ikenna1 24:ab821bfeb383 183 _enemy.reset_seeker();
ikenna1 24:ab821bfeb383 184 }
ikenna1 24:ab821bfeb383 185
ikenna1 23:0301effce801 186 }
ikenna1 24:ab821bfeb383 187
ikenna1 18:2cc6898de6b2 188 }