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