ELEC2645 (2018/19) / Mbed 2 deprecated el17aio

Dependencies:   mbed

Committer:
ikenna1
Date:
Sun Apr 14 10:11:43 2019 +0000
Revision:
21:628fb703188f
Parent:
20:5b4b3bf5795c
Child:
22:8cad70085883
collision fully working between seeker and ship. changed _xcursor to shipno for clarity

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ikenna1 3:f9cd1a38d5c6 1 #include "RosenEngine.h"
ikenna1 3:f9cd1a38d5c6 2
ikenna1 14:88ca5b1a111a 3 DigitalIn A_button(PTB9);
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 13:e114d362186d 44 _weapons.draw(lcd);
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 14:88ca5b1a111a 49
ikenna1 14:88ca5b1a111a 50 if((A_button) == true) {
ikenna1 14:88ca5b1a111a 51 pad.tone(500,0.5);
ikenna1 14:88ca5b1a111a 52 wait(0.5);
ikenna1 14:88ca5b1a111a 53 pad.tone(1000,0.3);
ikenna1 14:88ca5b1a111a 54 wait(0.3);
ikenna1 14:88ca5b1a111a 55 pad.tone(1500,0.2);
ikenna1 14:88ca5b1a111a 56 wait(0.2);
ikenna1 14:88ca5b1a111a 57 pad.tone(1700,0.15);
ikenna1 14:88ca5b1a111a 58 wait(0.15);
ikenna1 14:88ca5b1a111a 59 while((A_button) == true) {
ikenna1 14:88ca5b1a111a 60 _weapons.draw_i(lcd);
ikenna1 14:88ca5b1a111a 61 pad.tone(2000,0.1);
ikenna1 14:88ca5b1a111a 62 wait(0.1);
ikenna1 14:88ca5b1a111a 63 }
ikenna1 14:88ca5b1a111a 64 }
ikenna1 13:e114d362186d 65 }
ikenna1 11:73cd744ffa80 66 }
ikenna1 7:ed5870cfb3e0 67
ikenna1 7:ed5870cfb3e0 68 void RosenEngine::update(Gamepad &pad)
ikenna1 7:ed5870cfb3e0 69 {
ikenna1 15:009ccc07bb57 70 _enemy.update_seeker(ship_xpos, ship_ypos);
ikenna1 21:628fb703188f 71 if(_shipno == 0) {
ikenna1 14:88ca5b1a111a 72 _ship.update_ship(_xjoystick,_yjoystick);
ikenna1 14:88ca5b1a111a 73 _weapons.update();
ikenna1 14:88ca5b1a111a 74 }
ikenna1 21:628fb703188f 75 if(_shipno == 1 && A_button == false) {
ikenna1 14:88ca5b1a111a 76 _ship.update_ship(_xjoystick,_yjoystick);
ikenna1 14:88ca5b1a111a 77 _weapons.update();
ikenna1 14:88ca5b1a111a 78 }
ikenna1 14:88ca5b1a111a 79 // _menu.update(_d);
ikenna1 20:5b4b3bf5795c 80
ikenna1 18:2cc6898de6b2 81 check_enemy_ship_collision();
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 ship_width = 9;
ikenna1 9:241a1a7d8527 90 _weapons.init(ship_xpos, ship_ypos, ship_width);
ikenna1 13:e114d362186d 91 _ycursor = _menu.get_ycursor();
ikenna1 9:241a1a7d8527 92 }
ikenna1 9:241a1a7d8527 93 void RosenEngine::title(N5110 &lcd)
ikenna1 9:241a1a7d8527 94 {
ikenna1 14:88ca5b1a111a 95 _menu.title(lcd);
ikenna1 10:c33d7593a275 96 _menu.update(_d);
ikenna1 10:c33d7593a275 97 }
ikenna1 13:e114d362186d 98 int RosenEngine::get_ycursor()
ikenna1 10:c33d7593a275 99 {
ikenna1 13:e114d362186d 100 _ycursor = _menu.get_ycursor();
ikenna1 10:c33d7593a275 101 return _ycursor;
ikenna1 12:47578eb9ea73 102 }
ikenna1 21:628fb703188f 103 int RosenEngine::get_shipno()
ikenna1 13:e114d362186d 104 {
ikenna1 21:628fb703188f 105 _shipno = _menu.get_xcursor();
ikenna1 21:628fb703188f 106 return _shipno;
ikenna1 13:e114d362186d 107 }
ikenna1 12:47578eb9ea73 108 void RosenEngine::ship_select(N5110 &lcd)
ikenna1 12:47578eb9ea73 109 {
ikenna1 12:47578eb9ea73 110 _menu.update(_d);
ikenna1 12:47578eb9ea73 111 _menu.disp_ships(lcd);
ikenna1 17:e65a9f981834 112 }
ikenna1 18:2cc6898de6b2 113 void RosenEngine::check_enemy_ship_collision()
ikenna1 18:2cc6898de6b2 114 {
ikenna1 18:2cc6898de6b2 115 Vector2D ship_pos = _ship.get_pos();
ikenna1 19:3316dba9787e 116 Vector2D seeker_pos = _enemy.get_seekerpos();
ikenna1 19:3316dba9787e 117 int ship_x[9];
ikenna1 19:3316dba9787e 118 int ship_y[6];
ikenna1 19:3316dba9787e 119 int seeker_x[9];
ikenna1 19:3316dba9787e 120 int seeker_y[6];
ikenna1 19:3316dba9787e 121 bool xcol = false;
ikenna1 19:3316dba9787e 122 bool ycol = false;
ikenna1 19:3316dba9787e 123
ikenna1 19:3316dba9787e 124 // create an array of all x positions for the ship sprite i.e along its width (ship_x)
ikenna1 20:5b4b3bf5795c 125 for(int countx = 0; countx<=9; countx=countx+1) {
ikenna1 19:3316dba9787e 126 ship_x[countx] = ship_pos.x + countx;
ikenna1 19:3316dba9787e 127 seeker_x[countx] = seeker_pos.x + countx;
ikenna1 21:628fb703188f 128 // printf("ship_x = %d, seeker_x = %d\n", ship_x[countx], seeker_x[countx]);
ikenna1 19:3316dba9787e 129 }
ikenna1 19:3316dba9787e 130
ikenna1 19:3316dba9787e 131 // create an array of all y positions for the ship sprite i.e along its height (ship_y)
ikenna1 20:5b4b3bf5795c 132 for(int county = 0; county<=6; county=county+1) {
ikenna1 19:3316dba9787e 133 ship_y[county] = ship_pos.y + county;
ikenna1 19:3316dba9787e 134 seeker_y[county] = seeker_pos.y + county;
ikenna1 21:628fb703188f 135 //printf("ship_y = %d, seeker_y = %d\n", ship_y[county], seeker_y[county]);
ikenna1 19:3316dba9787e 136 }
ikenna1 19:3316dba9787e 137 // check all values of ship position against all values of seekers x position
ikenna1 20:5b4b3bf5795c 138 for(int nx = 0; nx<=9; nx=nx+1) {
ikenna1 20:5b4b3bf5795c 139 for(int mx = 0; mx<=9; mx=mx+1) {
ikenna1 19:3316dba9787e 140 if(ship_x[nx] == seeker_x[mx]) {
ikenna1 19:3316dba9787e 141 xcol = true;
ikenna1 19:3316dba9787e 142 }
ikenna1 19:3316dba9787e 143 }
ikenna1 19:3316dba9787e 144 }
ikenna1 20:5b4b3bf5795c 145 for(int ny = 0; ny<=6; ny=ny+1) {
ikenna1 20:5b4b3bf5795c 146 for(int my = 0; my<=6; my=my+1) {
ikenna1 19:3316dba9787e 147 if(ship_y[ny] == seeker_y[my]) {
ikenna1 19:3316dba9787e 148 ycol = true;
ikenna1 19:3316dba9787e 149 }
ikenna1 19:3316dba9787e 150 }
ikenna1 19:3316dba9787e 151 }
ikenna1 20:5b4b3bf5795c 152
ikenna1 21:628fb703188f 153 printf("xcol = %d, ycol = %d\n", xcol, ycol);
ikenna1 19:3316dba9787e 154 if(xcol == true && ycol == true) {
ikenna1 18:2cc6898de6b2 155 _health.update(1);
ikenna1 18:2cc6898de6b2 156 _enemy.reset_seeker();
ikenna1 18:2cc6898de6b2 157 }
ikenna1 18:2cc6898de6b2 158 }