void Asteroid::onDestroy() { sf::Vector2f pos = getPosition(); switch (m_size) { case BIG: // Create 3 medium asteroids for (int i = 0; i < 3; ++i) { Asteroid* asteroid = new Asteroid(MEDIUM, math::rand(0, 360)); asteroid->setPosition(pos); EntityManager::getInstance().addEntity(asteroid); } SoundSystem::playSound("asteroid-break.ogg", 0.5f); break; case MEDIUM: // Create 3 small asteroids for (int i = 0; i < 3; ++i) { Asteroid* asteroid = new Asteroid(SMALL, math::rand(0, 360)); asteroid->setPosition(pos); EntityManager::getInstance().addEntity(asteroid); } SoundSystem::playSound("asteroid-break.ogg", 0.75f); break; default: SoundSystem::playSound("asteroid-break.ogg", 1.f); break; } EntityManager::getInstance().createImpactParticles(getPosition(), 10); }
bool Game::createAsteroids() { // asteroid Asteroid * A; // random values std::mt19937 rng; rng.seed(std::random_device()()); std::uniform_int_distribution<std::mt19937::result_type> xrand(-ceil(limitWidth / 2),ceil(limitWidth / 2)); std::uniform_int_distribution<std::mt19937::result_type> yrand(-ceil(limitHeight / 2),ceil(limitHeight / 2)); int x,y; // create asteroids with random positions for(int i = 0; i < numAsteroids; ++i) { A = new Asteroid(); A->setMass(1); vector<int> p; x = xrand(rng); y = yrand(rng); p.push_back(x); p.push_back(y); A->setPosition(p); A->setVolume(1); theAsteroids.push_back(A); } // Load sprite sheet texture for asteroid if( !( loadAsteroidSpriteSheet() ) ) { cout << "Sprite sheet load fail!" << endl; return false; } else { // default sprite for asteroids int x,y,w,h; x = 0; y = 0; w = asteroidSpriteSheetWidth; h = asteroidSpriteSheetHeight; defaultAsteroidSprite.x = x; defaultAsteroidSprite.y = y; defaultAsteroidSprite.w = w; defaultAsteroidSprite.h = h; } return true; }
void AsteroidsGenerator::generate(Asteroid& asteroid, const Point* const position) const { Point pointFrom, pointTo; getPoints(pointFrom, pointTo); if (position != 0) { asteroid.generate(edgeCountMin_, edgeCountMax_, radiusMin_ / 2.0f, radiusMax_ / 2.0f); pointFrom = *position; } else { asteroid.generate(edgeCountMin_, edgeCountMax_, radiusMin_, radiusMax_); } asteroid.setPosition(pointFrom); pointTo.x -= pointFrom.x; pointTo.y -= pointFrom.y; const float velocity = velocityMin_ + rand() % int(velocityMax_ - velocityMin_); const float angularVelocity = velocityMin_ + rand() % int(velocityMax_ - velocityMin_); if (position != 0) { asteroid.setAngularVelocity(2.0f * angularVelocity); asteroid.setGeneration(2); PointFunctions::normalize(pointTo, 2.0f * velocity); } else { asteroid.setAngularVelocity(angularVelocity); asteroid.setGeneration(1); PointFunctions::normalize(pointTo, velocity); } asteroid.setVelocity(pointTo.x, pointTo.y); }
//--------------------------------------------------------------------------- void GameScreen::updateClient(const Ogre::FrameEvent &evt, float * positions) { ship->setPosition(positions[0], positions[1], positions[2]); ship->getNode()->setOrientation(Ogre::Quaternion(positions[3],positions[4],positions[5],positions[6])); alien->setPosition(positions[7], positions[8], positions[9]); alien->getNode()->setOrientation(Ogre::Quaternion(positions[10],positions[11],positions[12],positions[13])); alien->setCam(positions[7], positions[8] + 25, positions[9] + 40, positions[7], positions[8], positions[9] - 25); alien ->setLight(positions[7], positions[8] + 500, positions[9] + 250); paddle->setPosition(positions[14], positions[15], positions[16]); paddle->getNode()->setOrientation(Ogre::Quaternion(positions[17],positions[18],positions[19],positions[20])); std::deque<GameObject*> oList = *(sim->getObjList()); int astIndex = 3; for(int i = 21; i < 21+7*NUM_ASTEROIDS; i+=7, astIndex++){ Asteroid* ast = (Asteroid*)oList[astIndex]; ast->setPosition(positions[i], positions[i+1], positions[i+2]); ast->getNode()->setOrientation(Ogre::Quaternion(positions[i+3],positions[i+4],positions[i+5],positions[i+6])); } }
void FirstLevel::update(float dt) { /*parallax */ CCPoint backgroundScrollVert = ccp(-1000,0) ; _background->setPosition(ccpAdd(_background->getPosition(),ccpMult(backgroundScrollVert,dt))); CCArray *spaceDusts = CCArray::createWithCapacity(2); spaceDusts->addObject(_spacedust1) ; spaceDusts->addObject(_spacedust2) ; for (int i = 0; i < spaceDusts->count(); i++ ) { CCSprite * spaceDust = (CCSprite *)(spaceDusts->objectAtIndex(i)) ; float xPosition = _background->convertToWorldSpace(spaceDust->getPosition()).x; float size = spaceDust->getContentSize().width ; if (xPosition < -size) _background->incrementOffset(ccp(spaceDust->getContentSize().width*2,0),spaceDust); } /*end parallax */ /*accelerometer*/ float maxY = winSize.height - _ship->getContentSize().height/2; float minY = _ship->getContentSize().height/2; float diff = _shipPositionY * dt; float newY = _ship->getPosition().y + diff; newY = MIN(MAX(newY, minY), maxY); _ship->setPosition(ccp(_ship->getPosition().x, newY)); /*end accelerometer*/ /*first we move sprites and then bodies*/ /*sprites animation*/ float curTime = getTimeTick(); if (curTime > _nextAsteroidSpawn){ // if time to show new asteroid? float randY = randomBetween(0, winSize.height); // new asteroid position float randms = randomBetween(0.2, 1.0)*1000; // how often show new asteroid _nextAsteroidSpawn = randms + curTime; // when show next asteroid float duration = randomBetween(1.0, 7.0); // android's "speed" Asteroid* asteroid = _asteroids->at(_nextAsteroid++); if(_nextAsteroid >= _asteroids->size()) _nextAsteroid = 0; /*take asteroid from cage and set next params; *when asteroid end moving - hide */ asteroid->stopAllActions(); asteroid->setPosition(ccp(winSize.width + asteroid->getContentSize().width/2, randY)); asteroid->setLives(3); asteroid->setVisible(true); asteroid->runAction(CCSequence::create(CCMoveBy::create(duration,ccp( -winSize.width - asteroid->getContentSize().width, 0)), CCCallFuncN::create(this, callfuncN_selector(FirstLevel::setInvisible)),NULL)); } /*end sprites animation*/ /*bodies animation*/ _world->Step(dt, 10, 10); for(b2Body *b = _world->GetBodyList(); b; b = b->GetNext()){ if (b->GetUserData() != NULL){ CCSprite* sprite = (CCSprite*) b->GetUserData(); if (sprite->isVisible()) { b2Vec2 position = b2Vec2(sprite->getPosition().x/PTM_RATIO, sprite->getPosition().y/PTM_RATIO); float angle = -1 * CC_DEGREES_TO_RADIANS(sprite->getRotation()); b->SetTransform(position, angle); } } } /*end bodies animation*/ /*body's collisions*/ std::deque<CurrentContact>::iterator i; for(i = _collisionListener->contacts.begin(); i != _collisionListener->contacts.end(); ++i){ CurrentContact contact = *i; b2Body *a = contact.a->GetBody(); b2Body *b = contact.b->GetBody(); if (a->GetUserData() != NULL && b->GetUserData() != NULL){ CCSprite *sA = (CCSprite*)a->GetUserData(); CCSprite *sB = (CCSprite*)b->GetUserData(); Asteroid *ast; CCSprite *bullet; if (sA->isVisible() && sB->isVisible()) { /*asteroid and bullet (laser)*/ //if (sA->getTag() != 0 || sB->getTag() != 0) if ((sA->getTag() == 1 && sB->getTag() == 2) || (sA->getTag() == 2 && sB->getTag() == 1)) { b2Body *temp; // bullet body if (sA->getTag() == 1) { ast = (Asteroid*) a->GetUserData(); bullet = (CCSprite*) b->GetUserData(); temp = b; } else { ast = (Asteroid*) b->GetUserData(); bullet = (CCSprite*) a->GetUserData(); temp = a; } if (ast->getLives() > 1){ // hide only bullet ast->subLife(); temp->SetTransform(b2Vec2(-100.0, -100.0), 0.0f); // bullet body } else { // hide asteroid and bullet a->SetTransform(b2Vec2(-100.0, -100.0), 0.0f); b->SetTransform(b2Vec2(-100.0, -100.0), 0.0f); ast->stopAllActions(); ast->setVisible(false); _ship->addScore(score); updateScore(_ship); } bullet->stopAllActions(); bullet->setVisible(false); continue; } /*asteroid and ship*/ if ((sA->getTag() == 0 && sB->getTag() == 1) || (sA->getTag() == 1 && sB->getTag() == 0)) { if (_ship->getLives() > 0){ _ship->subLife(); updateLives(_ship); _ship->runAction(CCBlink::create(1.0, 3)); if (sA->getTag() == 1){ sA->setVisible(false); sA->stopAllActions(); a->SetTransform(b2Vec2(-100.0, -100.0), 0.0f); } else { sB->setVisible(false); sB->stopAllActions(); b->SetTransform(b2Vec2(-100.0, -100.0), 0.0f); } } else { CCLog("GAME OVER"); // TODO: show dialog } continue; } } } } /*end body's collisions*/ }