You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
187 lines
4.3 KiB
C++
187 lines
4.3 KiB
C++
6 years ago
|
#include "object_manager.h"
|
||
6 years ago
|
#include <ctime>
|
||
6 years ago
|
|
||
6 years ago
|
using std::vector;
|
||
6 years ago
|
using std::rand;
|
||
6 years ago
|
|
||
6 years ago
|
ObjectManager::ObjectManager() {
|
||
6 years ago
|
srand(time(NULL));
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
void ObjectManager::init_player(Ship _player) {
|
||
6 years ago
|
player = _player;
|
||
|
}
|
||
|
|
||
|
bool ObjectManager::chk_player_col() {
|
||
6 years ago
|
if (enemies.size() > 0) {
|
||
6 years ago
|
for (vector<Ship>::iterator it = enemies.begin(); it != enemies.end(); it++) {
|
||
|
if (col_eval(player.get_hitbox(), (*it).get_hitbox())) {
|
||
6 years ago
|
return true;
|
||
|
}
|
||
6 years ago
|
}
|
||
|
}
|
||
6 years ago
|
return false;
|
||
6 years ago
|
}
|
||
|
|
||
|
int ObjectManager::chk_bullet_col() {
|
||
6 years ago
|
int points = 0;
|
||
6 years ago
|
if (enemies.size() > 0 && player_bullets.size() > 0) {
|
||
6 years ago
|
int d = 0;
|
||
6 years ago
|
vector<int> dead;
|
||
6 years ago
|
vector<int> kill_bullets;
|
||
|
for (vector<Ship>::iterator it = enemies.begin(); it != enemies.end(); it++) {
|
||
|
int b = 0;
|
||
|
for (vector<Bullet>::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) {
|
||
6 years ago
|
default:
|
||
|
points += 10;
|
||
|
}
|
||
6 years ago
|
dead.push_back(d);
|
||
|
kill_bullets.push_back(b);
|
||
6 years ago
|
}
|
||
6 years ago
|
b++;
|
||
6 years ago
|
}
|
||
6 years ago
|
d++;
|
||
6 years ago
|
}
|
||
|
//Removed backwards to avoid indexing errors
|
||
6 years ago
|
for (unsigned int x = dead.size(); x > 0; x--) {
|
||
|
enemies.erase(enemies.begin() + dead.at(x - 1));
|
||
|
}
|
||
|
for (unsigned int x = kill_bullets.size(); x > 0; x--) {
|
||
|
player_bullets.erase(player_bullets.begin() + kill_bullets.at(x - 1));
|
||
6 years ago
|
}
|
||
|
}
|
||
|
return points;
|
||
6 years ago
|
}
|
||
6 years ago
|
|
||
|
void ObjectManager::draw_objects()
|
||
|
{
|
||
6 years ago
|
vector<int> unload;
|
||
|
move_background();
|
||
|
for (unsigned int k = 0; k < background.size(); k++) {
|
||
|
if (background.at(k).oob) {
|
||
|
unload.push_back(k);
|
||
|
}
|
||
|
else {
|
||
|
background.at(k).draw();
|
||
|
}
|
||
|
}
|
||
|
for (unsigned int j = unload.size(); j > 0; j--) {
|
||
|
background.erase(background.begin() + unload.at(j - 1));
|
||
|
}
|
||
|
|
||
|
player.draw();
|
||
6 years ago
|
|
||
6 years ago
|
if (enemies.size() > 0) {
|
||
|
int i = 0;
|
||
|
vector<int> oob_ships;
|
||
6 years ago
|
for (unsigned int k = 0; k < enemies.size(); k++) {
|
||
|
if (!enemies.at(k).oob) {
|
||
|
enemies.at(k).draw();
|
||
6 years ago
|
}
|
||
|
else {
|
||
|
oob_ships.push_back(i);
|
||
|
}
|
||
|
i++;
|
||
6 years ago
|
}
|
||
6 years ago
|
//Removed backwards to avoid indexing errors
|
||
6 years ago
|
for (unsigned int x = oob_ships.size(); x > 0; x--) {
|
||
|
enemies.erase(enemies.begin() + oob_ships.at(x - 1));
|
||
6 years ago
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
unload.clear();
|
||
|
for (unsigned int k = 0; k < player_bullets.size(); k++) {
|
||
|
if (player_bullets.at(k).oob) {
|
||
|
unload.push_back(k);
|
||
6 years ago
|
}
|
||
|
else {
|
||
6 years ago
|
player_bullets.at(k).move(U);
|
||
|
player_bullets.at(k).draw();
|
||
6 years ago
|
}
|
||
|
}
|
||
6 years ago
|
for (unsigned int j = unload.size(); j > 0; j--) {
|
||
|
player_bullets.erase(player_bullets.begin() + unload.at(j - 1));
|
||
|
}
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
void ObjectManager::destroy_objects() {
|
||
6 years ago
|
enemies.clear();
|
||
|
player_bullets.clear();
|
||
|
enemy_bullets.clear();
|
||
|
}
|
||
6 years ago
|
|
||
6 years ago
|
void ObjectManager::move_enemies() {
|
||
6 years ago
|
if (enemies.size() > 0) {
|
||
6 years ago
|
for (vector<Ship>::iterator it = enemies.begin(); it != enemies.end(); it++) {
|
||
|
(*it).move(D);
|
||
6 years ago
|
}
|
||
6 years ago
|
}
|
||
6 years ago
|
}
|
||
6 years ago
|
|
||
6 years ago
|
//Initialize background stars
|
||
6 years ago
|
void ObjectManager::set_background() {
|
||
|
for (int i = 0; i < 20; i++) {
|
||
6 years ago
|
//Pick random layer and position
|
||
|
Star new_star;
|
||
6 years ago
|
int l = rand() % 2;
|
||
|
BgLayer layer;
|
||
|
switch (l) {
|
||
|
case 0:
|
||
|
layer = Front;
|
||
|
break;
|
||
|
case 1:
|
||
|
layer = Middle;
|
||
|
break;
|
||
|
case 2:
|
||
|
layer = Back;
|
||
|
break;
|
||
6 years ago
|
}
|
||
6 years ago
|
int y = rand() % SCREEN_H;
|
||
|
int x = rand() % (SCREEN_R_B - SCREEN_L_B + 1) + SCREEN_L_B;
|
||
|
new_star.reset_pos(x, y);
|
||
|
new_star.set_layer(layer);
|
||
|
background.push_back(new_star);
|
||
6 years ago
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
void ObjectManager::move_background() {
|
||
|
for (unsigned int i = 0; i < background.size(); i++) {
|
||
|
background.at(i).move(D);
|
||
|
}
|
||
|
int generate_chance = rand() % 2;
|
||
|
if (generate_chance) {
|
||
|
Star new_star;
|
||
|
int l = rand() % 2;
|
||
|
BgLayer layer;
|
||
|
switch (l) {
|
||
|
case 0:
|
||
|
layer = Front;
|
||
|
break;
|
||
|
case 1:
|
||
|
layer = Middle;
|
||
|
break;
|
||
|
case 2:
|
||
|
layer = Back;
|
||
|
break;
|
||
6 years ago
|
}
|
||
6 years ago
|
int x = rand() % (SCREEN_R_B - SCREEN_L_B + 1) + SCREEN_L_B;
|
||
|
new_star.set_layer(layer);
|
||
|
new_star.reset_pos(x, new_star.h_t_bound);
|
||
|
background.push_back(new_star);
|
||
6 years ago
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
//Check if the hitboxes are overlapping
|
||
6 years ago
|
bool ObjectManager::col_eval(Hitbox h1, Hitbox h2) {
|
||
6 years ago
|
//Check if one hitbox is within the other's width
|
||
6 years ago
|
if (h1.x > (h2.x + h2.width) || h2.x > (h1.x + h1.width))
|
||
6 years ago
|
return false;
|
||
|
|
||
6 years ago
|
//Check if one hitbox is within the other's height
|
||
6 years ago
|
if (h1.y > (h2.y + h2.height) || h2.y > (h1.y + h1.height))
|
||
6 years ago
|
return false;
|
||
|
|
||
|
return true;
|
||
6 years ago
|
}
|