コード例 #1
0
void CollisionManager::perform_collision_bullet(vector<WorldObject*> victims,
        vector<Projectile*> bullets) {
    vector<WorldObject*>::iterator victims_it;
    vector<Projectile*>::iterator bullets_it;

    if (bullets.size() < 1) {
        return;
    }

    if (victims.size() < 1) {
        return;
    }

    for (victims_it = victims.begin(); victims_it != victims.end(); victims_it++) {
        WorldObject* victim = (*victims_it);

        if (!victim->getcollisionable()) {
            continue;
        }

        // check collision for each bullet
        for (bullets_it = bullets.begin(); bullets_it != bullets.end(); bullets_it++) {
            Projectile* bullet = (*bullets_it);

            if ((bullet->getx() >= (victim->getx() - MAX_DISTANCE_FOR_COLLISION))
                    && (bullet->gety() >= (victim->gety() - MAX_DISTANCE_FOR_COLLISION))) {

                int bullet_min_x = bullet->getx();
                int bullet_min_y = bullet->gety();
                int bullet_max_x = bullet->getx() + bullet->getwidth();
                int bullet_max_y = bullet->gety() + bullet->getheight();

                int victim_min_x = victim->getx();
                int victim_min_y = victim->gety();
                int victim_max_x = victim->getx() + victim->getsurface()->w;
                int victim_max_y = victim->gety() + victim->getsurface()->h;

                if (((bullet_max_x > victim_min_x) &&
                        (bullet_max_y > victim_min_y) &&
                        (bullet_min_x < victim_max_x) &&
                        (bullet_min_y < victim_max_y)) ||
                        (bullet_min_x < 0 ||
                         bullet_max_x > DrawEngine::get_screen_width() ||
                         bullet_min_y < 0 ||
                         bullet_max_y > DrawEngine::get_screen_height())) {

                    // remove the bullet
                    GarbageCollector::add_deletion_queue(bullet);
                }
            }

            if ((bullet->getx() > (bullet->getorgx() + 300)) ||
                    (bullet->gety() > (bullet->getorgy() + 300))) {
                // if the bullet has been fired over 300 px, lets delete
                GarbageCollector::add_deletion_queue(bullet);
            }
        }
    }
}
コード例 #2
0
void CollisionManager::perform_bullet_hit(vector<Zombie*> zombies, vector<Projectile*> bullets) {
    vector<Zombie*>::iterator zombie_it;
    vector<Projectile*>::iterator bullets_it;

    bool hit = false;

    for (zombie_it = zombies.begin(); zombie_it != zombies.end(); zombie_it++) {
        Zombie* zombie = (*zombie_it);

        if (!zombie->getcollisionable()) {
            continue;
        }

        if (!zombie->getrevealed() || zombie->gethidden()) {
            continue;
        }

        // check collision for each bullet
        for (bullets_it = bullets.begin(); bullets_it != bullets.end(); bullets_it++) {
            Projectile* bullet = (*bullets_it);

            if ((bullet->getx() >= (zombie->getx() - MAX_DISTANCE_FOR_COLLISION))
                    && (bullet->gety() >= (zombie->gety() - MAX_DISTANCE_FOR_COLLISION))
                    && (bullet->gety() <= (zombie->gety() + zombie->getsurface()->h))
                    && (bullet->getx() <= (zombie->getx() + zombie->getsurface()->w))) {

                int bullet_min_x = bullet->getx();
                int bullet_min_y = bullet->gety();
                int bullet_max_x = bullet->getx() + bullet->getwidth();
                int bullet_max_y = bullet->gety() + bullet->getheight();

                int zombie_min_x = zombie->getx();
                int zombie_min_y = zombie->gety();
                int zombie_max_x = zombie->getx() + zombie->getsurface()->w;
                int zombie_max_y = zombie->gety() + zombie->getsurface()->h;

                if ((bullet_min_x > zombie_min_x) &&
                        (bullet_max_x < zombie_max_x) &&
                        (bullet_min_y > zombie_min_y) &&
                        (bullet_max_y < zombie_max_y)) {
                    hit = true;
                }
            }

            if (hit) {
                // remove the bullet
                if (zombie->getrevealed() && !zombie->gethidden()) {
                    zombie->setdamage(bullet->getdamage());

                    if (zombie->gethealth() <= 0) {
                        GarbageCollector::add_deletion_queue(zombie);
                    }

                    GarbageCollector::add_deletion_queue(bullet);
                }

                hit = false;
            }
        }

        zombie = 0;
    }
}