Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
RosenEngine/RosenEngine.cpp
- Committer:
- ikenna1
- Date:
- 2019-05-03
- Revision:
- 36:c25417f0d150
- Parent:
- 35:3341f2bd0408
- Child:
- 37:8d8c8cce0bc7
File content as of revision 36:c25417f0d150:
#include "RosenEngine.h" DigitalIn A(PTB9); // Constructor RosenEngine::RosenEngine() { } // Destructor RosenEngine::~RosenEngine() { } void RosenEngine::init(int ship_width,int ship_height,int ship_speed,int ship_xpos,int ship_ypos) { // initialise the game parameters _ship.init(ship_width,ship_height,ship_speed,ship_xpos,ship_ypos); // place seeker above the ship _enemy.init(48,0,3); _menu.init(16); _health.init(_shipno); } void RosenEngine::reset() { _health.init(_shipno); } void RosenEngine::read_input(Gamepad &pad) { Vector2D mapped_coord = pad.get_coord(); _xjoystick = mapped_coord.x; _yjoystick = mapped_coord.y; _d = pad.get_direction(); wait(0.1); // printf("_xjoystick ,_yjoystick = %f , %f\n",_xjoystick, _yjoystick); } void RosenEngine::draw(N5110 &lcd, Gamepad &pad) { lcd.drawRect(0,0,78,48,FILL_TRANSPARENT); _health.draw_health(lcd,_shipno); _health.draw_shields(lcd); _enemy.draw_seeker(lcd); _enemy.draw_shooter(lcd); _enemy.draw_shw(lcd,pad); if(_shipno == 0) { _ship.set_dimensions(9,6); _ship.draw_ship(lcd,_shipno); _weapons.draw(lcd,pad,_shipno); } if(_shipno == 1) { _ship.set_dimensions(7,10); _ship.draw_ship(lcd,_shipno); _weapons.draw(lcd,pad,_shipno); } if(_shipno == 2) { _ship.set_dimensions(7,10); _ship.draw_ship(lcd,_shipno); _weapons.draw(lcd,pad,_shipno); } } void RosenEngine::update(Gamepad &pad) { _enemy.update_seeker(ship_xpos, ship_ypos); _enemy.update_shooter(ship_xpos, ship_ypos); //printf("update_shooter\n"); _enemy.update_shw(); if(_shipno == 0) { _ship.update_ship(_xjoystick,_yjoystick); _weapons.update(); kestrelw_seeker_collision(pad); } //printf("if shipno == 0\n"); if(_shipno == 1 && A == false) { _ship.update_ship(_xjoystick,_yjoystick); _weapons.update(); } //printf("if shipno == 1\n"); if(_shipno == 2) { _ship.update_ship(_xjoystick,_yjoystick); _weapons.update(); } // test(); shooter_ship_collision(pad); seeker_ship_collision(pad); shooterw_ship_collision(pad); imperionw_seeker_collision(pad); kestrelw_shooter_collision(pad); imperionw_shooter_collision(pad); check_health(); } void RosenEngine::get_pos() { Vector2D ship_pos = _ship.get_pos(); ship_xpos = ship_pos.x; ship_ypos = ship_pos.y; _shooter1_pos = _enemy.get_shooter1pos(); _shooter2_pos = _enemy.get_shooter2pos(); _shooter3_pos = _enemy.get_shooter3pos(); _weapons.init(ship_xpos, ship_ypos, ship_width); _ycursor = _menu.get_ycursor(); if(_shipno == 0) { ship_width = 9; ship_height = 6; } if(_shipno == 1) { ship_width = 7; ship_height = 10; } } void RosenEngine::title(N5110 &lcd) { _menu.title(lcd,_shipno); _menu.update(_d); } int RosenEngine::get_ycursor() { _ycursor = _menu.get_ycursor(); return _ycursor; } int RosenEngine::get_shipno() { _shipno = _menu.get_xcursor(); return _shipno; } void RosenEngine::ship_select(N5110 &lcd) { _menu.update(_d); _menu.disp_ships(lcd); } bool RosenEngine::check_collision(int xpos1, int ypos1,int width1,int height1,int xpos2, int ypos2,int width2,int height2) { // Create arrays of all positions with appropriate height and length int xpos1_array[width1]; int ypos1_array[height1]; int xpos2_array[width2]; int ypos2_array[height2]; bool xcol = false; bool ycol = false; bool col = false; //printf("variables declared\n"); // Loop through both height and width to get a 2D aray of the sprites position for(int cx = 0; cx<width1; cx=cx+1) { xpos1_array[cx]= xpos1 + cx; for(int nx = 0; nx<width2; nx=nx+1) { xpos2_array[nx]= xpos2 + nx; if(xpos2_array[nx] == xpos1_array[cx]) { xcol = true; } // printf("xarray = %d,x2array = %d,xcol = %d\n",xpos2_array[nx],xpos1_array[cx],xcol); } } //printf("first loop done\n"); for(int cy = 0; cy<height1; cy=cy+1) { ypos1_array[cy]= ypos1 + cy; for(int ny = 0; ny<height2; ny=ny+1) { ypos2_array[ny]= ypos2 + ny; if(ypos2_array[ny] == ypos1_array[cy]) { ycol = true; } // printf("yarray = %d,y2array = %d,ycol = %d\n",ypos2_array[ny],ypos1_array[cy],ycol); } } //printf("second loop done\n"); // if both the hight and width position values are equal a collision has occured col = (xcol & ycol); //printf("col gotten, col = %d\n",col); return col; } bool RosenEngine::check_collision1(int xpos1,int width1,int xpos2,int width2) { // Create arrays of all positions with appropriate height and length int xpos1_array[width1]; int xpos2_array[width2]; bool xcol = false; for(int cx = 0; cx<width1; cx=cx+1) { xpos1_array[cx]= xpos1 + cx; for(int nx = 0; nx<width2; nx=nx+1) { xpos2_array[nx]= xpos2 + nx; if(xpos2_array[nx] == xpos1_array[cx]) { xcol = true; } // printf("xarray = %d,x2array = %d,xcol = %d\n",xpos2_array[nx],xpos1_array[cx],xcol); } } return xcol; } void RosenEngine::test() { bool test; test = check_collision(5,5,5,5,5,5,5,5); if (test == false) { printf("fail\n"); } else { printf("pass\n"); } test = check_collision(0,0,1,1,24,24,1,1); if (test == true) { printf("fail\n"); } else { printf("pass\n"); } } // make function resemble test void RosenEngine::seeker_ship_collision(Gamepad &pad) { Vector2D seeker_pos = _enemy.get_seekerpos(); bool collision; collision = check_collision(seeker_pos.x,seeker_pos.y,5,5,ship_xpos,ship_ypos,5,5); if (collision == true) { _health.update(1,pad); _enemy.reset_seeker(); pad.tone(500,0.05); wait(0.05); } } void RosenEngine::shooter_ship_collision(Gamepad &pad) { bool collision1; bool collision2; bool collision3; collision1 = check_collision(ship_xpos,ship_ypos,9,6,_shooter1_pos.x, _shooter1_pos.y,10,7); collision2 = check_collision(ship_xpos,ship_ypos,9,6,_shooter2_pos.x, _shooter2_pos.y,10,7); collision3 = check_collision(ship_xpos,ship_ypos,9,6,_shooter3_pos.x, _shooter3_pos.y,10,7); if(collision1 == true) { _health.update(1,pad); pad.tone(500,0.05); wait(0.05); } if(collision2 == true) { _health.update(1,pad); pad.tone(500,0.05); wait(0.05); } if(collision3 == true) { _health.update(1,pad); pad.tone(500,0.05); wait(0.05); } } void RosenEngine::shooterw_ship_collision(Gamepad &pad) { Vector2D _shooterw1_pos = _enemy.get_sh1wpos(); Vector2D _shooterw2_pos = _enemy.get_sh2wpos(); Vector2D _shooterw3_pos = _enemy.get_sh3wpos(); bool collision1; bool collision2; bool collision3; collision1 = check_collision(ship_xpos,ship_ypos,9,6,_shooterw1_pos.x, _shooterw1_pos.y,2,2); collision2 = check_collision(ship_xpos,ship_ypos,9,6,_shooterw2_pos.x, _shooterw2_pos.y,2,2); collision3 = check_collision(ship_xpos,ship_ypos,9,6,_shooterw3_pos.x, _shooterw3_pos.y,2,2); if(collision1 == true) { _health.update(1,pad); pad.tone(500,0.05); wait(0.05); } if(collision2 == true) { _health.update(1,pad); pad.tone(500,0.05); wait(0.05); } if(collision3 == true) { _health.update(1,pad); pad.tone(500,0.05); wait(0.05); } } void RosenEngine::kestrelw_seeker_collision(Gamepad &pad) { Vector2D seeker_pos = _enemy.get_seekerpos(); Vector2D missle_pos = _weapons.get_pos(_shipno); bool collision; collision = check_collision(seeker_pos.x,seeker_pos.y,9,6,missle_pos.x,missle_pos.y,1,1); if (collision == true) { _health.seekerh_update(5); pad.tone(500,0.05); wait(0.05); } } void RosenEngine::imperionw_seeker_collision(Gamepad &pad) { Vector2D seeker_pos = _enemy.get_seekerpos(); bool collision; if(ship_ypos > seeker_pos.y + 6) { collision = check_collision1(seeker_pos.x,9,ship_xpos + 2,3); if (collision == true && A == true) { _health.seekerh_update(5); pad.tone(500,0.05); wait(0.05); } } } void RosenEngine::kestrelw_shooter_collision(Gamepad &pad) { Vector2D missle_pos = _weapons.get_pos(_shipno); bool col1, col2, col3; col1 = check_collision(_shooter1_pos.x,_shooter1_pos.y,9,6,missle_pos.x,missle_pos.y,1,1); col2 = check_collision(_shooter2_pos.x,_shooter2_pos.y,9,6,missle_pos.x,missle_pos.y,1,1); col3 = check_collision(_shooter3_pos.x,_shooter3_pos.y,9,6,missle_pos.x,missle_pos.y,1,1); if (col1 == true) { pad.tone(500,0.05); _health.shooterh_update(1,5); wait(0.05); } if (col2 == true) { _health.shooterh_update(2,5); pad.tone(500,0.05); wait(0.05); } if (col3 == true) { _health.shooterh_update(3,5); pad.tone(500,0.05); wait(0.05); } } void RosenEngine::imperionw_shooter_collision(Gamepad &pad) { bool col1; bool col2; bool col3; if(ship_ypos > _shooter1_pos.y + 6) { col1 = check_collision1(_shooter1_pos.x,9,ship_xpos + 2,3); if (col1 == true && A == true) { _health.shooterh_update(1,5); pad.tone(500,0.05); wait(0.05); } } if(ship_ypos > _shooter2_pos.y + 6) { col2 = check_collision1(_shooter2_pos.x,9,ship_xpos + 2,3); if (col2 == true && A == true) { _health.shooterh_update(2,5); pad.tone(500,0.05); wait(0.05); } } if(ship_ypos > _shooter3_pos.y + 6) { col3 = check_collision1(_shooter3_pos.x,9,ship_xpos + 2,3); if (col3 == true && A == true) { _health.shooterh_update(3,5); pad.tone(500,0.05); wait(0.05); } } } void RosenEngine::score(int points) { _score = _score + points; } void RosenEngine::check_health() { int seeker_health = _health.get_seekerh(); int shooter1_health = _health.get_shooterh(1); int shooter2_health = _health.get_shooterh(2); int shooter3_health = _health.get_shooterh(3); if(seeker_health == 0){ _enemy.reset_seeker(); _health.reset_seekerh(); } if(shooter1_health == 0){ _enemy.reset_shooter(1); _health.reset_shooterh(1); } if(shooter2_health == 0){ _enemy.reset_shooter(2); _health.reset_shooterh(2); } if(shooter3_health == 0){ _enemy.reset_shooter(3); _health.reset_shooterh(3); } Vector2D hp = _health.get_hp(); if(hp.x <= 0){ printf("player deaad\n"); _dead = true; } }