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/Engine.cpp
- Revision:
- 8:d1c04f0e4890
- Parent:
- 7:08f78909dda7
- Child:
- 9:3a0194c87afe
--- a/Engine/Engine.cpp Thu May 09 01:30:57 2019 +0000 +++ b/Engine/Engine.cpp Sat May 11 08:23:54 2019 +0000 @@ -2,28 +2,38 @@ /////////////// objects /////////////// Map map; -Hero hero; -Bullets bhero; -Enemy e1; // my 10 possible enemies -Bullets b1; // my 11 bullet classes for each character +Hero hero; // Our Hero +Missiles missiles; // Our Heros ammunition +Enemy e1; // Our 10 possible enemies +Bullets b1; // Our 10 bullet classes for each enemy +Coin c1; // Our 10 coins for each enemy Enemy e2; Bullets b2; +Coin c2; Enemy e3; Bullets b3; +Coin c3; Enemy e4; Bullets b4; +Coin c4; Enemy e5; Bullets b5; +Coin c5; Enemy e6; Bullets b6; +Coin c6; Enemy e7; Bullets b7; +Coin c7; Enemy e8; Bullets b8; +Coin c8; Enemy e9; Bullets b9; +Coin c9; Enemy e10; Bullets b10; +Coin c10; Engine::Engine() { @@ -35,6 +45,7 @@ hero.init(swidth, sheight); init_enemies(); init_bullets(); + missiles.init(); } void Engine::init_integers(int map_width, int map_height, int swidth, int sheight) { @@ -46,6 +57,7 @@ enemies = 1; max_enemies = 10; spawn_rate = 50; + score = 0; } void Engine::init_enemies() { @@ -87,25 +99,29 @@ void Engine::read(Gamepad &pad) { _mag = pad.get_mag(); _d = pad.get_direction(); + Apressed = pad.check_event(Gamepad::A_PRESSED); + Bpressed = pad.check_event(Gamepad::B_PRESSED); } -bool Engine::write(int max_speed, Gamepad &pad) { +void Engine::write(int max_speed, Gamepad &pad, N5110 &lcd) { frame_counter ++; collision = false; change_in_position(max_speed); change_origin(); map.write(_x1, _y1); + missiles.write(pix_x, pix_y, _d, Apressed, Bpressed, _x1, _y1, m1_hit, m2_hit, m3_hit, pad, lcd); + get_missile_pos(); check_enemies(); write_enemies(); get_enemy_pos(); write_bullets(); check_bullet_collision(); check_enemy_collision(); - if( (hero.health(collision, pad)) ) { - return true; - } else { - return false; - } + hits(); + write_coins(); + scores(); + hero.health(collision, pad); + health = hero.get_health(); } void Engine::change_in_position(int max_speed) { @@ -155,6 +171,18 @@ } } +void Engine::get_missile_pos() { + Vector2D m1_pos = missiles.get_m1_pos(); + Vector2D m2_pos = missiles.get_m2_pos(); + Vector2D m3_pos = missiles.get_m3_pos(); + m1x = m1_pos.x; + m1y = m1_pos.y; + m2x = m2_pos.x; + m2y = m2_pos.y; + m3x = m3_pos.x; + m3y = m3_pos.y; +} + void Engine::check_enemies() { // should an enemy be created? if (frame_counter >= spawn_rate) { frame_counter = 0; @@ -203,67 +231,67 @@ } void Engine::get_enemy_pos() { - e1_xpos = e1.get_x(); - e1_ypos = e1.get_y(); - e2_xpos = e2.get_x(); - e2_ypos = e2.get_y(); - e3_xpos = e3.get_x(); - e3_ypos = e3.get_y(); - e4_xpos = e4.get_x(); - e4_ypos = e4.get_y(); - e5_xpos = e5.get_x(); - e5_ypos = e5.get_y(); - e6_xpos = e6.get_x(); - e6_ypos = e6.get_y(); - e7_xpos = e7.get_x(); - e7_ypos = e7.get_y(); - e8_xpos = e8.get_x(); - e8_ypos = e8.get_y(); - e9_xpos = e9.get_x(); - e9_ypos = e9.get_y(); - e10_xpos = e10.get_x(); - e10_ypos = e10.get_y(); + e1x = e1.get_x(); + e1y = e1.get_y(); + e2x = e2.get_x(); + e2y = e2.get_y(); + e3x = e3.get_x(); + e3y = e3.get_y(); + e4x = e4.get_x(); + e4y = e4.get_y(); + e5x = e5.get_x(); + e5y = e5.get_y(); + e6x = e6.get_x(); + e6y = e6.get_y(); + e7x = e7.get_x(); + e7y = e7.get_y(); + e8x = e8.get_x(); + e8y = e8.get_y(); + e9x = e9.get_x(); + e9y = e9.get_y(); + e10x = e10.get_x(); + e10y = e10.get_y(); } void Engine::write_bullets() { if (e1_true == 1) { - b1.write(e1_xpos, e1_ypos, pix_x, pix_y, _x1, _y1); + b1.write(e1x, e1y, pix_x, pix_y, _x1, _y1); } else { b1.init(); } if (e2_true == 1) { - b2.write(e2_xpos, e2_ypos, pix_x, pix_y, _x1, _y1); + b2.write(e2x, e2y, pix_x, pix_y, _x1, _y1); } else { b2.init(); } if (e3_true == 1) { - b3.write(e3_xpos, e3_ypos, pix_x, pix_y, _x1, _y1); + b3.write(e3x, e3y, pix_x, pix_y, _x1, _y1); } else { b3.init(); } if (e4_true == 1) { - b4.write(e4_xpos, e4_ypos, pix_x, pix_y, _x1, _y1); + b4.write(e4x, e4y, pix_x, pix_y, _x1, _y1); } else { b4.init(); } if (e5_true == 1) { - b5.write(e5_xpos, e5_ypos, pix_x, pix_y, _x1, _y1); + b5.write(e5x, e5y, pix_x, pix_y, _x1, _y1); } else { b5.init(); } if (e6_true == 1) { - b6.write(e6_xpos, e6_ypos, pix_x, pix_y, _x1, _y1); + b6.write(e6x, e6y, pix_x, pix_y, _x1, _y1); } else { b6.init(); } if (e7_true == 1) { - b7.write(e7_xpos, e7_ypos, pix_x, pix_y, _x1, _y1); + b7.write(e7x, e7y, pix_x, pix_y, _x1, _y1); } else { b7.init(); } if (e8_true == 1) { - b8.write(e8_xpos, e8_ypos, pix_x, pix_y, _x1, _y1); + b8.write(e8x, e8y, pix_x, pix_y, _x1, _y1); } else { b8.init(); } if (e9_true == 1) { - b9.write(e9_xpos, e9_ypos, pix_x, pix_y, _x1, _y1); + b9.write(e9x, e9y, pix_x, pix_y, _x1, _y1); } else { b9.init(); } if (e10_true == 1) { - b10.write(e10_xpos, e10_ypos, pix_x, pix_y, _x1, _y1); + b10.write(e10y, e10y, pix_x, pix_y, _x1, _y1); } else { b10.init(); } @@ -337,6 +365,118 @@ } } +void Engine::hits() { + m1_hit = false; + m2_hit = false; + m3_hit = false; + if (e1_true == 1) { + e1_true = check_hit(e1x, e1y); + if (e1_true == -1) { + c1.set(e1x, e1y); + } + } if (e2_true == 1) { + e2_true = check_hit(e2x, e2y); + if (e2_true == -1) { + c2.set(e2x, e2y); + } + } if (e3_true == 1) { + e3_true = check_hit(e3x, e3y); + if (e3_true == -1) { + c3.set(e3x, e3y); + } + } if (e4_true == 1) { + e4_true = check_hit(e4x, e4y); + if (e4_true == -1) { + c4.set(e4x, e4y); + } + } if (e5_true == 1) { + e5_true = check_hit(e5x, e5y); + if (e5_true == -1) { + c5.set(e5x, e5y); + } + } if (e6_true == 1) { + e6_true = check_hit(e6x, e6y); + if (e6_true == -1) { + c6.set(e6x, e6y); + } + } if (e7_true == 1) { + e7_true = check_hit(e7x, e7y); + if (e7_true == -1) { + c7.set(e7x, e7y); + } + } if (e8_true == 1) { + e8_true = check_hit(e8x, e8y); + if (e8_true == -1) { + c8.set(e8x, e8y); + } + } if (e9_true == 1) { + e9_true = check_hit(e9x, e9y); + if (e9_true == -1) { + c9.set(e9x, e9y); + } + } if (e10_true == 1) { + e10_true = check_hit(e10x, e10y); + if (e10_true == -1) { + c10.set(e10x, e10y); + } + } +} + +int Engine::check_hit(int ex, int ey) { + e_alive = 1; + if ( ( (ex + 9) >= m1x) && + ( (ex - 9) <= (m1x + 2) ) && + ( (ey + 9) >= m1y) && + ( (ey - 9) <= (m1y + 2) ) ) { + m1_hit = true; + e_alive = -1; + enemies--; + } else if ( ( (ex + 9) >= m2x) && + ( (ex - 9) <= (m2x + 2) ) && + ( (ey + 9) >= m2y) && + ( (ey - 9) <= (m2y + 2) ) ) { + m2_hit = true; + e_alive = -1; + enemies--; + } else if ( ( (ex + 9) >= m3x) && + ( (ex - 9) <= (m3x + 2) ) && + ( (ey + 9) >= m3y) && + ( (ey - 9) <= (m3y + 2) ) ) { + m3_hit = true; + e_alive = -1; + enemies--; + } + return e_alive; +} + +void Engine::write_coins() { + c1.write(-pix_x, pix_y); + c2.write(-pix_x, pix_y); + c3.write(-pix_x, pix_y); + c4.write(-pix_x, pix_y); + c5.write(-pix_x, pix_y); + c6.write(-pix_x, pix_y); + c7.write(-pix_x, pix_y); + c8.write(-pix_x, pix_y); + c9.write(-pix_x, pix_y); + c10.write(-pix_x, pix_y); +} + +void Engine::scores() { + c1s = c1.get_score(); + c2s = c2.get_score(); + c3s = c3.get_score(); + c4s = c4.get_score(); + c5s = c5.get_score(); + c6s = c6.get_score(); + c7s = c7.get_score(); + c8s = c8.get_score(); + c9s = c9.get_score(); + c10s = c10.get_score(); + + score += c1s + c2s + c3s + c4s + c5s + c6s + c7s + c8s + c9s + c10s; +} + void Engine::render(N5110 &lcd) { lcd.clear(); @@ -347,7 +487,9 @@ void Engine::draw(N5110 &lcd) { map.draw(lcd); hero.draw(lcd); + missiles.draw(lcd); draw_enemy(lcd); + draw_coin(lcd); } void Engine::draw_enemy(N5110 &lcd) { @@ -392,3 +534,24 @@ b10.draw(lcd); } } + +void Engine::draw_coin(N5110 &lcd) { + c1.draw(lcd); + c2.draw(lcd); + c3.draw(lcd); + c4.draw(lcd); + c5.draw(lcd); + c6.draw(lcd); + c7.draw(lcd); + c8.draw(lcd); + c9.draw(lcd); + c10.draw(lcd); +} + +int Engine::get_health() { + return health; +} + +int Engine::get_score() { + return score; +}