void Game::updateMeteor(double dt, vector<Node*>::iterator nodeIt) { Meteor* meteor = (Meteor*) (*nodeIt); // Move meteor meteor->translate(meteor->getXFallSpeed(), dt * meteor->getYFallSpeed()); // Make it spin meteor->rotate(meteor->getRotateSpeed()); // Mark it for deletion if it's out if (meteor->isOut()) { deleted_.push_back(nodeIt - scene_.begin()); } // If meteor hit shuttle than the game is over if (shuttle_->isIntersect(meteor)) { isOver_ = true; } }
void Game::work(double dt) { dt = fmin(dt, 1.0f); // Clear some buffers glClearColor(0.2353f, 0.2471f, 0.2549f, 1.0f); checkGlError("glClearColor"); glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); checkGlError("glClear"); // Use some programs glUseProgram(gProgram_); checkGlError("glUseProgram"); glLineWidth(2.0f); // Randomly generate meteors at approximate rate one per second if ( ((float)rand() / RAND_MAX) < dt ) { Meteor* meteor = new Meteor(); float sky = (float) width_ / (float)height_; float x = ((float)rand() / RAND_MAX) * sky - sky / 2; meteor->translate(x, 1.0f); meteor->updateXSpeed(); scene_.push_back(meteor); } // Render scene loop for (vector<Node*>::iterator node = scene_.begin(); node < scene_.end(); ++node) { // Let's draw it (*node)->draw(dt, gaPositionHandle_, gaColorHandle_, guVeiwProjHandle_, mProj_); enum NodeType type = (*node)->getType(); // If it is a meteor than update it's position and stuff if (type == METEOR || type == SMALL_METEOR) { updateMeteor(dt, node); } // Do the same for bullet if (type == BULLET) { updateBullet(dt, node); } } if (!deleted_.empty()) { // Sort "deleted" array to be able remove elements in right order sort(deleted_.begin(), deleted_.end()); // Remove duplicates deleted_.erase( unique( deleted_.begin(), deleted_.end() ), deleted_.end() ); } // Remove elements in backwards order for (int i = deleted_.size() - 1; i >= 0; --i) { int index = deleted_[i]; delete scene_[index]; scene_.erase(scene_.begin() + index); } // All useless elements are deleted so clear the "deleted array" deleted_.clear(); // If flag is set than it's time to spawn small ones // And if we hit meteor at (0, 0), well.. than it's a lucky shot if (smallMeteorX_ || smallMeteorY_) { for (int i = 0; i < smallMeteors; ++i) { SmallMeteor* smallMeteor = new SmallMeteor(smallMeteorX_, smallMeteorY_); scene_.push_back(smallMeteor); } // Clear the spawn flag smallMeteorX_ = smallMeteorY_ = 0.0f; } }