diff --git a/Assignment3Project/Assignment3Project/bullet.cpp b/Assignment3Project/Assignment3Project/bullet.cpp index 05cc535..f1693de 100644 --- a/Assignment3Project/Assignment3Project/bullet.cpp +++ b/Assignment3Project/Assignment3Project/bullet.cpp @@ -21,11 +21,6 @@ Bullet::Bullet(Behavior _behavior) { void Bullet::reset_pos(float x, float y) { x_pos = x; y_pos = y; - - hitbox.x = x_pos; - hitbox.y = y_pos; - hitbox.width = width; - hitbox.height = height; } void Bullet::draw() { @@ -46,4 +41,14 @@ void Bullet::move(Direction dir) { if (y_pos < 0 - height) { oob = true; } -} \ No newline at end of file +} + +Hitbox Bullet::get_hitbox() +{ + Hitbox hitbox; + hitbox.x = x_pos; + hitbox.y = y_pos; + hitbox.width = width; + hitbox.height = height; + return hitbox; +} diff --git a/Assignment3Project/Assignment3Project/bullet.h b/Assignment3Project/Assignment3Project/bullet.h index 48c0d3c..c616fc8 100644 --- a/Assignment3Project/Assignment3Project/bullet.h +++ b/Assignment3Project/Assignment3Project/bullet.h @@ -11,4 +11,5 @@ public: void reset_pos(float x, float y); void draw(); void move(Direction dir); + Hitbox get_hitbox(); }; \ No newline at end of file diff --git a/Assignment3Project/Assignment3Project/game_element.h b/Assignment3Project/Assignment3Project/game_element.h index 715dc03..d7baa01 100644 --- a/Assignment3Project/Assignment3Project/game_element.h +++ b/Assignment3Project/Assignment3Project/game_element.h @@ -15,9 +15,9 @@ public: float width; bool oob; Behavior behavior; - Hitbox hitbox; virtual void reset_pos(float x, float y) = 0; virtual void move(Direction dir) = 0; virtual void draw() = 0; + virtual Hitbox get_hitbox() = 0; }; \ No newline at end of file diff --git a/Assignment3Project/Assignment3Project/game_screen.cpp b/Assignment3Project/Assignment3Project/game_screen.cpp index 3686d1a..7b44475 100644 --- a/Assignment3Project/Assignment3Project/game_screen.cpp +++ b/Assignment3Project/Assignment3Project/game_screen.cpp @@ -180,9 +180,9 @@ void GameScreen::run(ALLEGRO_FONT* font) { } //Global refresh - /*if (objects.chk_player_col()) { + if (objects.chk_player_col()) { exit_screen = true; - }*/ + } //score += objects.chk_bullet_col(); objects.move_enemies(); al_clear_to_color(al_map_rgb(0, 0, 0)); diff --git a/Assignment3Project/Assignment3Project/object_manager.cpp b/Assignment3Project/Assignment3Project/object_manager.cpp index 462b8cf..9322972 100644 --- a/Assignment3Project/Assignment3Project/object_manager.cpp +++ b/Assignment3Project/Assignment3Project/object_manager.cpp @@ -12,33 +12,39 @@ void ObjectManager::initiate(Ship * _player) { } bool ObjectManager::chk_player_col() { - for (vector::iterator it = enemies.begin(); it != enemies.end(); it++) { - if (col_eval(player->hitbox, (*it)->hitbox)) { - return true; + if (enemies.size() > 0) { + for (vector::iterator it = enemies.begin(); it != enemies.end(); it++) { + if (col_eval(player->get_hitbox(), (*it)->get_hitbox())) { + return true; + } } } + return false; } int ObjectManager::chk_bullet_col() { int points = 0; - int i = 0; - vector dead; - for (vector::iterator it = enemies.begin(); it != enemies.end(); it++) { - for (vector::iterator it_b = player_bullets.begin(); it_b != player_bullets.end(); it++) { - if (col_eval((*it_b)->hitbox, (*it)->hitbox)) { - switch ((*it)->behavior) { - default: - points += 10; + if (enemies.size() > 0 && player_bullets.size() > 0) { + int i = 0; + vector dead; + for (vector::iterator it = enemies.begin(); it != enemies.end(); it++) { + for (vector::iterator it_b = player_bullets.begin(); it_b != player_bullets.end(); it_b++) { + if (col_eval((*it_b)->get_hitbox(), (*it)->get_hitbox())) { + switch ((*it)->behavior) { + default: + points += 10; + } + delete *it; + dead.push_back(i); } - delete *it; - dead.push_back(i); } + i++; + } + //Removed backwards to avoid indexing errors + for (vector::iterator it = dead.end(); it != dead.begin(); it--) { + delete enemies.at(*it); + enemies.erase(enemies.begin() + *it); } - i++; - } - //Removed backwards to avoid indexing errors - for (vector::iterator it = dead.end(); it != dead.begin(); it--) { - enemies.erase(enemies.begin() + *it); } return points; } @@ -47,20 +53,22 @@ void ObjectManager::draw_objects() { player->draw(); - int i = 0; - vector dead; - for (vector::iterator it = enemies.begin(); it != enemies.end(); it++) { - if (!(*it)->oob) { - (*it)->draw(); + if (enemies.size() > 0) { + int i = 0; + vector oob_ships; + for (vector::iterator it = enemies.begin(); it != enemies.end(); it++) { + if (!(*it)->oob) { + (*it)->draw(); + } + else { + oob_ships.push_back(i); + } + i++; } - else { - dead.push_back(i); + //Removed backwards to avoid indexing errors + for (vector::iterator it = oob_ships.end(); it != oob_ships.begin(); it--) { + enemies.erase(enemies.begin() + *it); } - i++; - } - //Removed backwards to avoid indexing errors - for (vector::iterator it = dead.end(); it != dead.begin(); it--) { - enemies.erase(enemies.begin() + *it); } bool unload = false; @@ -81,30 +89,60 @@ void ObjectManager::draw_objects() void ObjectManager::destroy_objects() { delete player; - for (vector::iterator it = enemies.begin(); it != enemies.end(); it++) { - delete *it; + if (enemies.size() > 0) { + for (vector::iterator it = enemies.begin(); it != enemies.end(); it++) { + delete *it; + } + enemies.clear(); } - enemies.clear(); - for (vector::iterator it = player_bullets.begin(); it != player_bullets.end(); it++) { - delete *it; + if (player_bullets.size() > 0) { + for (vector::iterator it = player_bullets.begin(); it != player_bullets.end(); it++) { + delete *it; + } + player_bullets.clear(); } - player_bullets.clear(); - for (vector::iterator it = enemy_bullets.begin(); it != enemy_bullets.end(); it++) { - delete *it; + if (enemy_bullets.size() > 0) { + for (vector::iterator it = enemy_bullets.begin(); it != enemy_bullets.end(); it++) { + delete *it; + } + enemy_bullets.clear(); } - enemy_bullets.clear(); } void ObjectManager::move_enemies() { - for (vector::iterator it = enemies.begin(); it != enemies.end(); it++) { - (*it)->move(D); + if (enemies.size() > 0) { + for (vector::iterator it = enemies.begin(); it != enemies.end(); it++) { + (*it)->move(D); + } } } bool ObjectManager::col_eval(Hitbox h1, Hitbox h2) { - if ((h1.x > h2.x && h1.x < (h2.x + h2.width)) && (h1.y > h2.y && h1.y < (h2.y + h2.height))) { - return true; - } + int l1x = h1.x; + int l1y = h1.y; + int r1x = h1.x + h1.width; + int r1y = h1.y + h1.height; + + int l2x = h2.x; + int l2y = h2.y; + int r2x = h2.x + h2.width; + int r2y = h2.y + h2.height; + + /*if (h1.x > (h2.x + h2.width) || h2.x > (h1.x + h1.width)) + return false; + + if (h1.y < (h2.y + h2.height) || h2.y < (h1.y + h1.height)) + return false;*/ + + // If one rectangle is on left side of other + if (l1x > r2x || l2x > r1x) + return false; + + // If one rectangle is above other + if (l1y < r2y || l2y < r1y) + return false; + + return true; } \ No newline at end of file diff --git a/Assignment3Project/Assignment3Project/ship.cpp b/Assignment3Project/Assignment3Project/ship.cpp index 80861b0..6345be0 100644 --- a/Assignment3Project/Assignment3Project/ship.cpp +++ b/Assignment3Project/Assignment3Project/ship.cpp @@ -28,11 +28,6 @@ Ship::Ship(Behavior _behavior) { void Ship::reset_pos(float x, float y) { x_pos = x; y_pos = y; - - hitbox.x = x_pos; - hitbox.y = y_pos; - hitbox.width = width; - hitbox.height = height; } void Ship::set_sprite(ALLEGRO_BITMAP* _sprite) { @@ -102,6 +97,16 @@ void Ship::move(Direction dir) { } } +Hitbox Ship::get_hitbox() +{ + Hitbox hitbox; + hitbox.x = x_pos; + hitbox.y = y_pos; + hitbox.width = width; + hitbox.height = height; + return hitbox; +} + Bullet Ship::fire() { Bullet new_bullet(behavior); float x = (x_pos) + width / 2.0; diff --git a/Assignment3Project/Assignment3Project/ship.h b/Assignment3Project/Assignment3Project/ship.h index 0cdcaf9..17eae73 100644 --- a/Assignment3Project/Assignment3Project/ship.h +++ b/Assignment3Project/Assignment3Project/ship.h @@ -16,6 +16,7 @@ public: void reset_pos(float x, float y); void draw(); void move(Direction dir); + Hitbox get_hitbox(); //unique to object void set_sprite(ALLEGRO_BITMAP* _sprite);