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.
Diff: Engine.cpp
- Revision:
- 11:10c01766f774
- Parent:
- 10:b2dd5f484f98
- Child:
- 12:d68c757d829a
--- a/Engine.cpp Tue Apr 25 10:13:53 2017 +0000 +++ b/Engine.cpp Mon May 01 13:01:57 2017 +0000 @@ -1,31 +1,36 @@ #include "Engine.h" +#define LEVEL_ONE 2 +#define LEVEL_TWO 3 +#define LEVEL_THREE 4 +#define LEVEL_FOUR 5 +#define LEVEL_FIVE 6 + Engine::Engine() { collisions = 0; wave_counter = 0; + ammo = 3; } Engine::~Engine() { -} +} + -void Engine::init(int friendly_width,int friendly_height,int ship_size,int speed,N5110 &lcd, Gamepad &pad) +// Initialization function: + +void Engine::init(int speed, N5110 &lcd, Gamepad &pad) { - _friendly_width = friendly_width; - _friendly_height = friendly_height; - _ship_size = ship_size; _speed = speed; - _friendlyx = GAP; - _friendly.init(_friendlyx,_friendly_height,_friendly_width); - _ship1.init(_ship_size,_speed); - _ship2.init(_ship_size,_speed); - _ship3.init(_ship_size,_speed); - _ship4.init(_ship_size,_speed); - _ship5.init(_ship_size,_speed); - _ship6.init(_ship_size,_speed); - _bullet.init(_speed); + _friendly.init(); // friendly ship initialization + _ship1.init(_speed); // enemy ships initialization + _ship2.init(_speed); + _ship3.init(_speed); + _ship4.init(_speed); + _ship5.init(_speed); + _ship6.init(_speed); } void Engine::read_input(Gamepad &pad) @@ -34,33 +39,22 @@ _mag = pad.get_mag(); } -// DRAWING FUNCTIONS + +// ::: DRAWING FUNCTIONS ::: void Engine::draw(N5110 &lcd) { - grid_draw(lcd); - ships_draw(lcd); - _friendly.draw(lcd); - _stats.draw_health(lcd); - _stats.check_health(lcd, collisions); - wave_draw(lcd); + _stats.grid_draw(lcd); // initial grid and border generation + _stats.draw_health(lcd); // health counter generation (initial state) + _stats.check_health(lcd, collisions); // health counter generation (later states) + _stats.check_rocket(lcd, ammo); // rocket and ammo generation + _stats.wave_draw(lcd, wave_counter); // wave counter generation + _friendly.draw(lcd); // friendly ship generation + ships_draw(lcd); // enemy ships generation } -void Engine::grid_draw(N5110 &lcd) -{ - lcd.drawRect(0, 0, WIDTH, HEIGHT-8, FILL_TRANSPARENT); - lcd.drawLine(WIDTH-1, HEIGHT-8, WIDTH-1, HEIGHT, 1); - lcd.drawLine(WIDTH-11, HEIGHT-8, WIDTH-11, HEIGHT, 1); - lcd.drawLine(12, HEIGHT-1, WIDTH, HEIGHT-1, 1); - lcd.drawLine(12, HEIGHT-8, 12, HEIGHT, 1); -} -void Engine::wave_draw(N5110 &lcd) -{ - char buffer[14]; - int length = sprintf(buffer,"%2d",wave_counter); - lcd.printString(buffer,0,5); -} +// Enemy ships generation: void Engine::ships_draw(N5110 &lcd) { @@ -72,12 +66,22 @@ _ship6.draw(lcd); } + +// State change checker: + void Engine::checker(N5110 &lcd, Gamepad &pad) { - check_pass(pad); - check_death(lcd, pad); + check_pass(pad); // checks if enemy passed screen border and generates another + check_death_all(lcd, pad); // checks for collisions and eventual friendly death + level_two(lcd, pad); // checks if level two has been reached, same for below + level_three(lcd, pad); + level_four(lcd, pad); + level_five(lcd, pad); } + +// Dynamic object position/velocity updater: + void Engine::update(N5110 &lcd, Gamepad &pad) { _friendly.update(_d,_mag); @@ -87,195 +91,266 @@ _ship4.update(); _ship5.update(); _ship6.update(); - //_bullet.update(); } -void Engine::shoot(N5110 &lcd, Gamepad &pad, int speed) + +// Function to enable shooting and eliminating enemy ships: + +void Engine::shoot(N5110 &lcd, Gamepad &pad) { - _speed = speed; + Vector2D friendly_pos = _friendly.get_pos(); + _x = friendly_pos.x+6; + _y = friendly_pos.y+3; + _bullet.init(_x, _y); - if (pad.check_event(Gamepad::A_PRESSED) == true) + if ((pad.check_event(Gamepad::B_PRESSED) == true) && + (ammo > 0)) { - //trigger = true; + trigger = true; } - //if (trigger == true) - //{ - //pad.tone(1200.0,0.1); - //_bullet.init(_speed); - //_bullet.draw(lcd); - //_bullet.update(); - //trigger = false; - //} + if (trigger == true) + { + _bullet.draw(lcd); + check_enemy_death(pad); + pad.tone(1500.0,0.1); + ammo = ammo - 1; + trigger = false; + } } + +// Checks if (each) enemy ship passed off screen and re-initializes ones that have: (Wave counter associated with ship1) + void Engine::check_pass(Gamepad &pad) { Vector2D ship1_pos = _ship1.get_pos(); - if (ship1_pos.x + _ship_size < 0) + if (ship1_pos.x < 0) { - _ship1.init(_ship_size,_speed); + _ship1.init(_speed); wave_counter = wave_counter + 1; } Vector2D ship2_pos = _ship2.get_pos(); - if (ship2_pos.x + _ship_size < 0) + if (ship2_pos.x < 0) { - _ship2.init(_ship_size,_speed); + _ship2.init(_speed); } Vector2D ship3_pos = _ship3.get_pos(); - if (ship3_pos.x + _ship_size < 0) + if (ship3_pos.x < 0) { - _ship3.init(_ship_size,_speed); + _ship3.init(_speed); } Vector2D ship4_pos = _ship4.get_pos(); - if (ship4_pos.x + _ship_size < 0) + if (ship4_pos.x < 0) { - _ship4.init(_ship_size,_speed); + _ship4.init(_speed); } Vector2D ship5_pos = _ship5.get_pos(); - if (ship5_pos.x + _ship_size < 0) + if (ship5_pos.x < 0) { - _ship5.init(_ship_size,_speed); + _ship5.init(_speed); } Vector2D ship6_pos = _ship6.get_pos(); - if (ship6_pos.x + _ship_size < 0) + if (ship6_pos.x < 0) { - _ship6.init(_ship_size,_speed); + _ship6.init(_speed); } } -void Engine::check_death1(N5110 &lcd, Gamepad &pad) + +// Checks if enemy ship has been destroyed: + +void Engine::check_enemy_death(Gamepad &pad) { Vector2D friendly_pos = _friendly.get_pos(); - Vector2D ship1_pos = _ship1.get_pos(); - if ((friendly_pos.y >= ship1_pos.y-5) && // change 5 to friendly size and 6 to ship size - (friendly_pos.y <= ship1_pos.y+5) && - (friendly_pos.x+6 >= ship1_pos.x) && - (friendly_pos.x+6 <= ship1_pos.x+5)) + if (friendly_pos.y < 5) + { + _ship1.init(_speed); + } + else if ((friendly_pos.y < 11) && + (friendly_pos.y > 4)) + { + _ship2.init(_speed); + } + else if ((friendly_pos.y < 17) && + (friendly_pos.y > 10)) + { + _ship3.init(_speed); + } + else if ((friendly_pos.y < 25) && + (friendly_pos.y > 16)) + { + _ship4.init(_speed); + } + else if ((friendly_pos.y < 31) && + (friendly_pos.y > 24)) + { + _ship5.init(_speed); + } + else + { + _ship6.init(_speed); + } +} + + +// Checks for collisions between friendly and enemy ship: (Associates collisions counter) + +void Engine::check_death(Gamepad &pad, Vector2D ship_pos) +{ + Vector2D friendly_pos = _friendly.get_pos(); + + if ((friendly_pos.y >= ship_pos.y-5) && // change 5 to friendly size and 6 to ship size + (friendly_pos.y <= ship_pos.y+5) && + (friendly_pos.x+6 >= ship_pos.x) && + (friendly_pos.x+6 <= ship_pos.x+5)) { pad.tone(800.0,0.1); collisions = collisions + 1; } } -void Engine::check_death2(N5110 &lcd, Gamepad &pad) + +// Checks for collisions between friendly and each enemy ship: + +void Engine::check_death_all(N5110 &lcd, Gamepad &pad) { - Vector2D friendly_pos = _friendly.get_pos(); + Vector2D ship1_pos = _ship1.get_pos(); Vector2D ship2_pos = _ship2.get_pos(); + Vector2D ship3_pos = _ship3.get_pos(); + Vector2D ship4_pos = _ship4.get_pos(); + Vector2D ship5_pos = _ship5.get_pos(); + Vector2D ship6_pos = _ship6.get_pos(); - if ((friendly_pos.y >= ship2_pos.y-5) && // change 5 to friendly size and 6 to ship size - (friendly_pos.y <= ship2_pos.y+5) && - (friendly_pos.x+6 >= ship2_pos.x) && - (friendly_pos.x+6 <= ship2_pos.x+5)) - { - pad.tone(800.0,0.1); - collisions = collisions + 1; - } + check_death(pad, ship1_pos); + check_death(pad, ship2_pos); + check_death(pad, ship3_pos); + check_death(pad, ship4_pos); + check_death(pad, ship5_pos); + check_death(pad, ship6_pos); + game_over(lcd, pad); // game over sequence, if health subzero } -void Engine::check_death3(N5110 &lcd, Gamepad &pad) + +// Level two sequence, occurs if wave 5 is reached: + +void Engine::level_two(N5110 &lcd, Gamepad &pad) { - Vector2D friendly_pos = _friendly.get_pos(); - Vector2D ship3_pos = _ship3.get_pos(); - - if ((friendly_pos.y >= ship3_pos.y-5) && // change 5 to friendly size and 6 to ship size - (friendly_pos.y <= ship3_pos.y+5) && - (friendly_pos.x+6 >= ship3_pos.x) && - (friendly_pos.x+6 <= ship3_pos.x+5)) + if (wave_counter == 5) { - pad.tone(800.0,0.1); - collisions = collisions + 1; + lcd.drawRect(0,0,84,48,FILL_WHITE); + lcd.printString("Nice! Level 2",0,1); + lcd.printString(" Press Back! ",0,4); + lcd.refresh(); + wait(1); + + while (pad.check_event(Gamepad::BACK_PRESSED) == false) { + wait(0.1); + } + + init(LEVEL_TWO, lcd, pad); + wave_counter = 6; } } -void Engine::check_death4(N5110 &lcd, Gamepad &pad) + +// Level three sequence, occurs if wave 25 is reached: + +void Engine::level_three(N5110 &lcd, Gamepad &pad) { - Vector2D friendly_pos = _friendly.get_pos(); - Vector2D ship4_pos = _ship4.get_pos(); - - if ((friendly_pos.y >= ship4_pos.y-5) && // change 5 to friendly size and 6 to ship size - (friendly_pos.y <= ship4_pos.y+5) && - (friendly_pos.x+6 >= ship4_pos.x) && - (friendly_pos.x+6 <= ship4_pos.x+5)) + if (wave_counter == 25) { - pad.tone(800.0,0.1); - collisions = collisions + 1; + lcd.drawRect(0,0,84,48,FILL_WHITE); + lcd.printString("Nice! Level 3",0,1); + lcd.printString(" Press Back! ",0,4); + lcd.refresh(); + wait(1); + + while (pad.check_event(Gamepad::BACK_PRESSED) == false) { + wait(0.1); + } + + init(LEVEL_THREE, lcd, pad); + wave_counter = 26; } } -void Engine::check_death5(N5110 &lcd, Gamepad &pad) + +// Level three sequence, occurs if wave 50 is reached: + +void Engine::level_four(N5110 &lcd, Gamepad &pad) { - Vector2D friendly_pos = _friendly.get_pos(); - Vector2D ship5_pos = _ship5.get_pos(); - - if ((friendly_pos.y >= ship5_pos.y-5) && // change 5 to friendly size and 6 to ship size - (friendly_pos.y <= ship5_pos.y+5) && - (friendly_pos.x+6 >= ship5_pos.x) && - (friendly_pos.x+6 <= ship5_pos.x+5)) + if (wave_counter == 50) { - pad.tone(800.0,0.1); - collisions = collisions + 1; + lcd.drawRect(0,0,84,48,FILL_WHITE); + lcd.printString("Nice! Level 4",0,1); + lcd.printString(" Press Back! ",0,4); + lcd.refresh(); + wait(1); + + while (pad.check_event(Gamepad::BACK_PRESSED) == false) { + wait(0.1); + } + + init(LEVEL_FOUR, lcd, pad); + wave_counter = 51; } } -void Engine::check_death6(N5110 &lcd, Gamepad &pad) + +// Level five sequence, occurs if wave 100 is reached: + +void Engine::level_five(N5110 &lcd, Gamepad &pad) { - Vector2D friendly_pos = _friendly.get_pos(); - Vector2D ship6_pos = _ship6.get_pos(); - - if ((friendly_pos.y >= ship6_pos.y-5) && // change 5 to friendly size and 6 to ship size - (friendly_pos.y <= ship6_pos.y+5) && - (friendly_pos.x+6 >= ship6_pos.x) && - (friendly_pos.x+6 <= ship6_pos.x+5)) + if (wave_counter == 100) { - pad.tone(800.0,0.1); - collisions = collisions + 1; + lcd.drawRect(0,0,84,48,FILL_WHITE); + lcd.printString("5. Good Luck!",0,1); + lcd.printString(" Press Back! ",0,4); + lcd.refresh(); + wait(1); + + while (pad.check_event(Gamepad::BACK_PRESSED) == false) { + wait(0.1); + } + + init(LEVEL_FIVE, lcd, pad); + wave_counter = 101; } } -void Engine::check_death(N5110 &lcd, Gamepad &pad) -{ - check_death1(lcd, pad); - check_death2(lcd, pad); - check_death3(lcd, pad); - check_death4(lcd, pad); - check_death5(lcd, pad); - check_death6(lcd, pad); - game_over(lcd, pad); -} + +// Game over sequence, ends game if health is below zero: void Engine::game_over(N5110 &lcd, Gamepad &pad) { if (collisions >= 6) { lcd.drawRect(0,0,84,48,FILL_WHITE); - lcd.printString(" You Lose! ",0,0); - lcd.printString(" Press Right ",0,2); - lcd.printString(" & Left! ",0,4); + lcd.printString(" You Lose! ",0,1); + lcd.printString(" Press Back! ",0,4); lcd.refresh(); wait(1); - //printf("Hello\n"); - while ((pad.check_event(Gamepad::R_PRESSED) == false) || - (pad.check_event(Gamepad::L_PRESSED) == false)) { + while (pad.check_event(Gamepad::BACK_PRESSED) == false) { wait(0.1); - //printf("3333\n"); } - init(_friendly_width, _friendly_height, _ship_size, _speed, lcd, pad); + init(LEVEL_ONE, lcd, pad); collisions = 0; wave_counter = 0; + ammo = 3; } } \ No newline at end of file