Idk what I did really

main
Braydon Kains 6 years ago
parent 3623b4c016
commit 08699ac83b

@ -21,11 +21,6 @@ Bullet::Bullet(Behavior _behavior) {
void Bullet::reset_pos(float x, float y) { void Bullet::reset_pos(float x, float y) {
x_pos = x; x_pos = x;
y_pos = y; y_pos = y;
hitbox.x = x_pos;
hitbox.y = y_pos;
hitbox.width = width;
hitbox.height = height;
} }
void Bullet::draw() { void Bullet::draw() {
@ -47,3 +42,13 @@ void Bullet::move(Direction dir) {
oob = true; oob = true;
} }
} }
Hitbox Bullet::get_hitbox()
{
Hitbox hitbox;
hitbox.x = x_pos;
hitbox.y = y_pos;
hitbox.width = width;
hitbox.height = height;
return hitbox;
}

@ -11,4 +11,5 @@ public:
void reset_pos(float x, float y); void reset_pos(float x, float y);
void draw(); void draw();
void move(Direction dir); void move(Direction dir);
Hitbox get_hitbox();
}; };

@ -15,9 +15,9 @@ public:
float width; float width;
bool oob; bool oob;
Behavior behavior; Behavior behavior;
Hitbox hitbox;
virtual void reset_pos(float x, float y) = 0; virtual void reset_pos(float x, float y) = 0;
virtual void move(Direction dir) = 0; virtual void move(Direction dir) = 0;
virtual void draw() = 0; virtual void draw() = 0;
virtual Hitbox get_hitbox() = 0;
}; };

@ -180,9 +180,9 @@ void GameScreen::run(ALLEGRO_FONT* font) {
} }
//Global refresh //Global refresh
/*if (objects.chk_player_col()) { if (objects.chk_player_col()) {
exit_screen = true; exit_screen = true;
}*/ }
//score += objects.chk_bullet_col(); //score += objects.chk_bullet_col();
objects.move_enemies(); objects.move_enemies();
al_clear_to_color(al_map_rgb(0, 0, 0)); al_clear_to_color(al_map_rgb(0, 0, 0));

@ -12,33 +12,39 @@ void ObjectManager::initiate(Ship * _player) {
} }
bool ObjectManager::chk_player_col() { bool ObjectManager::chk_player_col() {
for (vector<Ship*>::iterator it = enemies.begin(); it != enemies.end(); it++) { if (enemies.size() > 0) {
if (col_eval(player->hitbox, (*it)->hitbox)) { for (vector<Ship*>::iterator it = enemies.begin(); it != enemies.end(); it++) {
return true; if (col_eval(player->get_hitbox(), (*it)->get_hitbox())) {
return true;
}
} }
} }
return false;
} }
int ObjectManager::chk_bullet_col() { int ObjectManager::chk_bullet_col() {
int points = 0; int points = 0;
int i = 0; if (enemies.size() > 0 && player_bullets.size() > 0) {
vector<int> dead; int i = 0;
for (vector<Ship*>::iterator it = enemies.begin(); it != enemies.end(); it++) { vector<int> dead;
for (vector<Bullet*>::iterator it_b = player_bullets.begin(); it_b != player_bullets.end(); it++) { for (vector<Ship*>::iterator it = enemies.begin(); it != enemies.end(); it++) {
if (col_eval((*it_b)->hitbox, (*it)->hitbox)) { for (vector<Bullet*>::iterator it_b = player_bullets.begin(); it_b != player_bullets.end(); it_b++) {
switch ((*it)->behavior) { if (col_eval((*it_b)->get_hitbox(), (*it)->get_hitbox())) {
default: switch ((*it)->behavior) {
points += 10; default:
points += 10;
}
delete *it;
dead.push_back(i);
} }
delete *it;
dead.push_back(i);
} }
i++;
}
//Removed backwards to avoid indexing errors
for (vector<int>::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<int>::iterator it = dead.end(); it != dead.begin(); it--) {
enemies.erase(enemies.begin() + *it);
} }
return points; return points;
} }
@ -47,20 +53,22 @@ void ObjectManager::draw_objects()
{ {
player->draw(); player->draw();
int i = 0; if (enemies.size() > 0) {
vector<int> dead; int i = 0;
for (vector<Ship*>::iterator it = enemies.begin(); it != enemies.end(); it++) { vector<int> oob_ships;
if (!(*it)->oob) { for (vector<Ship*>::iterator it = enemies.begin(); it != enemies.end(); it++) {
(*it)->draw(); if (!(*it)->oob) {
(*it)->draw();
}
else {
oob_ships.push_back(i);
}
i++;
} }
else { //Removed backwards to avoid indexing errors
dead.push_back(i); for (vector<int>::iterator it = oob_ships.end(); it != oob_ships.begin(); it--) {
enemies.erase(enemies.begin() + *it);
} }
i++;
}
//Removed backwards to avoid indexing errors
for (vector<int>::iterator it = dead.end(); it != dead.begin(); it--) {
enemies.erase(enemies.begin() + *it);
} }
bool unload = false; bool unload = false;
@ -81,30 +89,60 @@ void ObjectManager::draw_objects()
void ObjectManager::destroy_objects() { void ObjectManager::destroy_objects() {
delete player; delete player;
for (vector<Ship*>::iterator it = enemies.begin(); it != enemies.end(); it++) { if (enemies.size() > 0) {
delete *it; for (vector<Ship*>::iterator it = enemies.begin(); it != enemies.end(); it++) {
delete *it;
}
enemies.clear();
} }
enemies.clear();
for (vector<Bullet*>::iterator it = player_bullets.begin(); it != player_bullets.end(); it++) { if (player_bullets.size() > 0) {
delete *it; for (vector<Bullet*>::iterator it = player_bullets.begin(); it != player_bullets.end(); it++) {
delete *it;
}
player_bullets.clear();
} }
player_bullets.clear();
for (vector<Bullet*>::iterator it = enemy_bullets.begin(); it != enemy_bullets.end(); it++) { if (enemy_bullets.size() > 0) {
delete *it; for (vector<Bullet*>::iterator it = enemy_bullets.begin(); it != enemy_bullets.end(); it++) {
delete *it;
}
enemy_bullets.clear();
} }
enemy_bullets.clear();
} }
void ObjectManager::move_enemies() { void ObjectManager::move_enemies() {
for (vector<Ship*>::iterator it = enemies.begin(); it != enemies.end(); it++) { if (enemies.size() > 0) {
(*it)->move(D); for (vector<Ship*>::iterator it = enemies.begin(); it != enemies.end(); it++) {
(*it)->move(D);
}
} }
} }
bool ObjectManager::col_eval(Hitbox h1, Hitbox h2) { 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))) { int l1x = h1.x;
return true; 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;
} }

@ -28,11 +28,6 @@ Ship::Ship(Behavior _behavior) {
void Ship::reset_pos(float x, float y) { void Ship::reset_pos(float x, float y) {
x_pos = x; x_pos = x;
y_pos = y; y_pos = y;
hitbox.x = x_pos;
hitbox.y = y_pos;
hitbox.width = width;
hitbox.height = height;
} }
void Ship::set_sprite(ALLEGRO_BITMAP* _sprite) { 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 Ship::fire() {
Bullet new_bullet(behavior); Bullet new_bullet(behavior);
float x = (x_pos) + width / 2.0; float x = (x_pos) + width / 2.0;

@ -16,6 +16,7 @@ public:
void reset_pos(float x, float y); void reset_pos(float x, float y);
void draw(); void draw();
void move(Direction dir); void move(Direction dir);
Hitbox get_hitbox();
//unique to object //unique to object
void set_sprite(ALLEGRO_BITMAP* _sprite); void set_sprite(ALLEGRO_BITMAP* _sprite);

Loading…
Cancel
Save