void World::update(sf::Time dt) { // Scroll the world, reset player velocity mWorldView.move(0.f, mScrollSpeed * dt.asSeconds() * mScrollSpeedCompensation); FOREACH(Aircraft* a, mPlayerAircrafts) a->setVelocity(0.f, 0.f); // Setup commands to destroy entities, and guide missiles destroyEntitiesOutsideView(); guideMissiles(); // Forward commands to scene graph, adapt velocity (scrolling, diagonal correction) while (!mCommandQueue.isEmpty()) mSceneGraph.onCommand(mCommandQueue.pop(), dt); adaptPlayerVelocity(); // Collision detection and response (may destroy entities) handleCollisions(); // Remove aircrafts that were destroyed (World::removeWrecks() only destroys the entities, not the pointers in mPlayerAircraft) auto firstToRemove = std::remove_if(mPlayerAircrafts.begin(), mPlayerAircrafts.end(), std::mem_fn(&Aircraft::isMarkedForRemoval)); mPlayerAircrafts.erase(firstToRemove, mPlayerAircrafts.end()); // Remove all destroyed entities, create new ones mSceneGraph.removeWrecks(); spawnEnemies(); // Regular update step, adapt position (correct if outside view) mSceneGraph.update(dt, mCommandQueue); adaptPlayerPosition(); updateSounds(); }
void World::update(sf::Time dt) { // Scroll the world, reset player velocity mWorldView.move(0.f, mScrollSpeed * dt.asSeconds()); mPlayerAircraft->setVelocity(0.f, 0.f); // Setup commands to destroy entities, and guide missiles destroyEntitiesOutsideView(); guideMissiles(); // Forward commands to scene graph, adapt velocity (scrolling, diagonal correction) while (!mCommandQueue.isEmpty()) mSceneGraph.onCommand(mCommandQueue.pop(), dt); adaptPlayerVelocity(); // Collision detection and response (may destroy entities) handleCollisions(); // Remove all destroyed entities, create new ones mSceneGraph.removeWrecks(); spawnEnemies(); // Regular update step, adapt position (correct if outside view) mSceneGraph.update(dt, mCommandQueue); adaptPlayerPosition(); updateSounds(); }
///*** Run this such that there are 30 runs in a second, emulating frames ***/// void Game::timerEvent(QTimerEvent *event) { frameTimer++; score = player->getXP()*10; emit scoreChanged(score); if(!player->updatePlayer()) { gameOver = true; std::cout << "SNATHOEUSANOTH.\n"; repaint(); } for(int i=0;i<drones.size();i++) { drones.at(i)->updateEnemy(player->getLocation()); } for(int i=0;i<archers.size();i++) { archers.at(i)->updateEnemy(player->getLocation()); } checkCollision(); checkEnemyDeath(); spawnEnemies(); repaint(); }
/** * Runs the game! WHOO! */ int Game::start() { game_running = true; while(window.isOpen()) { while(game_running) { sf::Event event; while(window.pollEvent(event)) { if(event.type == sf::Event::Closed) { window.close(); game_running = false; game_over = true; } } bullets = p.getBullets(); spawnEnemies(); checkInput(); checkBorderCollision(p); checkEntityCollision(); checkDeath(); updateAnimations(); updateScore(); updateInterface(); updateGameClocks(); for(std::vector<Enemy*>::iterator iter = enemies.begin(); iter != enemies.end(); ++iter) { Enemy *eni = *iter; eni->stalkPlayer(p); } window.clear(sf::Color(0,230,0)); Sword * s = p.getSword(); if(s->isSwung()) { window.draw(s->shape); } window.draw(p.shape); for(std::vector<Enemy*>::iterator iter = enemies.begin(); iter != enemies.end(); ++iter) { Enemy *eni = *iter; window.draw(eni->shape); } for(std::vector<Bullet*>::iterator iter = bullets.begin(); iter != bullets.end(); ++iter) { Bullet *b = *iter; window.draw(b->shape); } for(std::vector<sf::Drawable*>::iterator iter = interface.begin(); iter != interface.end(); ++iter) { sf::Drawable *d = *iter; window.draw(*d); } window.display(); } setupEntities(); game_running = true; } return 0; }
///*** If a player chooses to be an Assn, spawn it ***/// void Game::startAssassinGame() { player = new Assassin(); if (!gameStarted) { gameStarted = true; emit scoreChanged(score); emit levelChanged(level); gameOver = false; setMouseTracking(true); timerId = startTimer(33); spawnEnemies(); } }
void gameClass::update(float deltaTime) { if (newLevel) { currentLevel++; enemies.clear(); projectiles.clear(); player->tile.resetPos(mapLevel[currentLevel].getEntrancePos()); renderer->resetCam(player->tile.getPos(), player->tile.getSize()); newLevel = false; } renderer->getCam(renderingPort.x, renderingPort.y); renderingPort *= -1; spawnTimer += deltaTime; projectileTimer += deltaTime; moveEnemies(deltaTime); if (spawnTimer > Constants::spawnRate) { spawnEnemies(); spawnTimer = 0; } moveProjectiles(deltaTime); if(player->getHealth() <= 0) { if(!isPlayerDead) { animationTime = 0.0f; player->resetIndex(); isPlayerDead = true; player->setCurrentAction("tipping over"); SoundEngine->play2D("audio/Sound Effect (31).wav", GL_FALSE); } animationTime += deltaTime; if (animationTime >= Constants::animationFrameTime) { animationTime = 0.0f; player->increaseIndex(); } } }
void World::update( sf::Time dt ) { mWorldView.move( 0.0f, mScrollSpeed * dt.asSeconds() * mScrollSpeedCompensation ); for ( Aircraft* a : mPlayerAircrafts ) { a->SetVelocity( 0.0f, 0.0f ); } destroyEntitiesOutsideView(); guideMissiles(); while ( !mCommandQueue.isEmpty() ) { mSceneGraph.OnCommand( mCommandQueue.pop(), dt ); } adaptPlayerVelocity(); handleCollisions(); // Remove aircrafts that were destroyed // (World::removeWrecks() only destroys the entities, // not the pointers in mPlayerAircrafts) auto firstToRemove = std::remove_if( mPlayerAircrafts.begin(), mPlayerAircrafts.end(), std::mem_fn( &Aircraft::IsMarkedForRemoval ) ); mPlayerAircrafts.erase( firstToRemove, mPlayerAircrafts.end() ); mSceneGraph.RemoveWrecks(); spawnEnemies(); mSceneGraph.Update( dt, mCommandQueue ); adaptPlayerPosition(); updateSounds(); }
void GameScene::update(float dt) { updateHero(dt); adaptPlayerPosition(); updateAnimationState(); m_flamesParticle->setPosition(ccpAdd(m_hero->getPosition(), ccp(-m_hero->getContentSize().width * 0.25f, -m_hero->getContentSize().height * 0.25f))); spawnEnemies(dt); updateEnemies(dt); updateProjectiles(dt); if (m_hp <= 0) { gameOver(); } m_background->update(); // Update Box2D world //world->Step(dt, 6, 3); // BOX2D TIP // Update objects from box2d coordinates here }
void World::update(sf::Time dt) { // Scroll the world, reset player velocity mWorldView.move(0.f, mScrollSpeed * dt.asSeconds()); mPlayerSpaceship->setVelocity(0.f, 0.f); // Check for delete enemies destroyEntitiesOutsideView(); // Forward commands to scene graph, adapt velocity (scrolling, diagonal correction) while (!mCommandQueue.isEmpty()) mSceneGraph.onCommand(mCommandQueue.pop(), dt); adaptPlayerVelocity(); // Handle our collision. handleCollisions(); // Remove all destroyed entities, and create new. mSceneGraph.removeWrecks(); // Templates for add new Enemies. addNewEnemies(dt); spawnEnemies(); // Check, for edding new world texture. updateWorldBounds(); // Regular update step, adapt position (correct if outside view) mSceneGraph.update(dt, mCommandQueue); adaptPlayerPosition(); // Update Points and Health. updateText(dt); mBattlifieldBounds.setPosition(getBattlefieldBounds().left, getBattlefieldBounds().top); // Update ScrollSpeed mScrollSpeed -= ScrollSpeedAccelerate * dt.asSeconds(); }
void World::update(sf::Time dt) { mWorldView.move(0.f, dt.asSeconds() * mScrollSpeed); mPlayerAircraft->setVelocity(0.f, 0.f); destoryEntitiesOutOfView(); guideMissiles(); updateScore(); // onCommand while (!mCommandQueue.isEmpty()) mSceneGraph.onCommand(mCommandQueue.pop(), dt); // handles the diagonal velocity sf::Vector2f velocity = mPlayerAircraft->getVelocity(); if (velocity.x != 0.f && velocity.y != 0.f) mPlayerAircraft->setVelocity(velocity / std::sqrt(2.f)); mPlayerAircraft->accelerate(0.f, mScrollSpeed); // handles the cases when aircraft reaches boundaries sf::FloatRect viewBounds(mWorldView.getCenter() - mWorldView.getSize() / 2.f, mWorldView.getSize()); const float borderDistance = 40.f; sf::Vector2f position = mPlayerAircraft->getPosition(); position.x = std::max(position.x, viewBounds.left + borderDistance); position.x = std::min(position.x, viewBounds.left + viewBounds.width - borderDistance); position.y = std::min(position.y, viewBounds.top + viewBounds.height - borderDistance -20.f); position.y = std::max(position.y, viewBounds.top +borderDistance); mPlayerAircraft->setPosition(position); /////////////////// handleCollision(); mSceneGraph.removeWrecks(); spawnEnemies(); mSceneGraph.update(dt, mCommandQueue); }
void loadLevel(int level, bool ascending) { std::wstring filename; int numWalls = 0; wall *walls = NULL; switch (level) { // transition case -1: { player.setScore(player.getScore() + 50); filename = Gosu::resourcePrefix() + L"media/ui/transitionScreen.bmp"; despawnItems(); despawnEnemies(); items.push_back(Item(kittensAnim, KITTENS, 135, 105)); break; } // start screen case 0: { onStartScreen = true; filename = Gosu::resourcePrefix() + L"media/ui/startScreen.bmp"; despawnItems(); despawnEnemies(); // Set entrances and exits down = Stair(15, 45, false); up = Stair(105, 195, true); // Set player location if (ascending) { player.warp(down.x(), down.y()); } else { player.warp(up.x(), up.y()); } break; } case 1: { // Get map graphic filename = Gosu::resourcePrefix() + L"media/maps/map1.bmp"; // Get map walls numWalls = 25; wall someWalls[] = { wall(0, 0, 240, 0), wall(0, 0, 0, 210), wall(0, 210, 240, 210), wall(240, 210, 240, 0), wall(0, 180, 30, 180), wall(30, 30, 30, 120), wall(30, 150, 30, 180), wall(30, 30, 90, 30), wall(90, 30, 90, 60), wall(90, 60, 60, 60), wall(30, 120, 60, 120), wall(60, 120, 60, 150), wall(30, 90, 120, 90), wall(120, 90, 120, 0), wall(90, 210, 90, 180), wall(60, 180, 210, 180), wall(210, 180, 210, 30), wall(210, 60, 180, 60), wall(210, 120, 180, 120), wall(210, 150, 90, 150), wall(90, 150, 90, 120), wall(90, 120, 150, 120), wall(150, 120, 150, 30), wall(150, 30, 180, 30), wall(150, 90, 180, 90) }; walls = (wall *)realloc(walls, numWalls * sizeof(wall)); memcpy(walls, someWalls, (numWalls * sizeof(wall))); // Set entrances and exits down = Stair(15, 45, false); up = Stair(105, 195, true); // Set player location if (ascending) { player.warp(down.x(), down.y()); } else { player.warp(up.x(), up.y()); } // Set items despawnItems(); items.push_back(Item(ammoAnim, AMMO, 195, 75)); items.push_back(Item(healthAnim, HEALTH, 195, 165)); // Spawn enemies spawnEnemies(); break; } case 2: { // Get map graphic filename = Gosu::resourcePrefix() + L"media/maps/map2.bmp"; // Get map walls numWalls = 29; wall someWalls[] = { wall(0, 0, 240, 0), wall(0, 0, 0, 210), wall(0, 210, 240, 210), wall(240, 210, 240, 0), wall(0, 60, 30, 60), wall(30, 60, 30, 30), wall(30, 30, 90, 30), wall(90, 30, 90, 90), wall(0, 120, 90, 120), wall(30, 120, 30, 90), wall(30, 90, 60, 90), wall(60, 90, 60, 60), wall(30, 210, 30, 150), wall(30, 150, 120, 150), wall(120, 150, 120, 90), wall(120, 90, 150, 90), wall(150, 60, 150, 180), wall(120, 60, 180, 60), wall(120, 30, 120, 60), wall(180, 30, 180, 60), wall(180, 30, 210, 30), wall(210, 60, 240, 60), wall(210, 150, 240, 150), wall(180, 90, 180, 210), wall(180, 90, 210, 90), wall(210, 90, 210, 120), wall(60, 180, 120, 180), wall(90, 180, 90, 210), wall(150, 0, 150, 30) }; walls = (wall *)realloc(walls, numWalls * sizeof(wall)); memcpy(walls, someWalls, (numWalls * sizeof(wall))); // Set entrances and exits down = Stair(75, 195, false); up = Stair(15, 105, true); // Set player location if (ascending) { player.warp(down.x(), down.y()); } else { player.warp(up.x(), up.y()); } // Set items despawnItems(); items.push_back(Item(ammoAnim, AMMO, 225, 195)); items.push_back(Item(healthAnim, HEALTH, 15, 195)); // Spawn enemies spawnEnemies(); break; } case 3: { // Get map graphic filename = Gosu::resourcePrefix() + L"media/maps/map3.bmp"; // Get map walls numWalls = 25; wall someWalls[] = { wall(0, 0, 240, 0), wall(0, 0, 0, 210), wall(0, 210, 240, 210), wall(240, 210, 240, 0), wall(0, 60, 30, 60), wall(30, 60, 30, 90), wall(30, 90, 60, 90), wall(60, 60, 60, 180), wall(30, 30, 90, 30), wall(90, 30, 90, 150), wall(30, 180, 180, 180), wall(180, 180, 180, 90), wall(210, 90, 210, 180), wall(210, 180, 240, 180), wall(210, 60, 210, 30), wall(210, 30, 120, 30), wall(120, 30, 120, 0), wall(90, 60, 180, 60), wall(150, 60, 150, 90), wall(120, 90, 180, 90), wall(90, 120, 150, 120), wall(150, 120, 150, 150), wall(150, 150, 120, 150), wall(0, 120, 30, 120), wall(30, 120, 30, 150) }; walls = (wall *)realloc(walls, numWalls * sizeof(wall)); memcpy(walls, someWalls, (numWalls * sizeof(wall))); // Set entrances and exits down = Stair(15, 135, false); up = Stair(-15, -15, true); // Set player location if (ascending) { player.warp(down.x(), down.y()); } else { player.warp(up.x(), up.y()); } // Set items despawnItems(); items.push_back(Item(ammoAnim, AMMO, 135, 15)); items.push_back(Item(healthAnim, HEALTH, 45, 75)); items.push_back(Item(kittensAnim, KITTENS, 135, 75)); // Spawn enemies spawnEnemies(); break; } default: break; } if (level == 1 && ascending) player.startSong1(); else if (level != 0) Gosu::Sample(Gosu::resourcePrefix() + L"media/sounds/levelChange.wav").play(); backgroundImage.reset(new Gosu::Image(graphics(), filename, true)); environment = Environment(numWalls, walls); }
void GameScene::update() { Q_ASSERT(sender() == findChild<QTimer*>()); if(m_paused) return; GraphicsPlayerObject* player = nullptr; QList<AbstractGraphicsPlaneObject*> planes; for(auto item : items()) // find planes { switch(item->type()) { case PlayerType: player = qgraphicsitem_cast<GraphicsPlayerObject*>(item); case EnemyType: planes.append(dynamic_cast<AbstractGraphicsPlaneObject*>(item)); } } if(player && !player->isInvencible()) { Q_ASSERT(player == m_player); const auto playerRect = player->sceneBoundingRect(); for(auto plane : planes) { if(plane != player) { const auto enemyRect = plane->sceneBoundingRect(); if (playerRect.intersects(enemyRect)) player->impact(std::numeric_limits<qint32>::max()); } } } for(auto item : items()) { if(item->type() == BulletType || item->type() == EnemyBulletType) { const auto rect = item->boundingRect().translated(item->pos()); if(!sceneRect().contains(rect)) delete item; else { bool impacted = false; for(auto it = planes.begin(); it != planes.end() && !impacted; ++it) { const auto planeRect = (*it)->sceneBoundingRect(); auto plane = qgraphicsitem_cast< AbstractGraphicsPlaneObject*>(*it); const auto status = plane->status(); if(status == AbstractGraphicsPlaneObject::Status::Alive && planeRect.contains(rect)) { plane->impact(100); impacted = true; } } if(impacted) delete item; } } } if(--m_spawnEnemiesTicks == 0) // Spawn enemies { if(!(qrand() % 24)) // 1/25 spawnEnemies(GraphicsEnemyObject::EnemyType::Boss, true); else spawnEnemies(GraphicsEnemyObject::EnemyType::White, true); m_spawnEnemiesTicks = m_spawnEnemiesMaxTicks; } else if(m_spawnEnemiesTicks == m_spawnEnemiesMaxTicks / 2) { if(!(qrand() % 24)) // 1/25 spawnEnemies(GraphicsEnemyObject::EnemyType::Boss, false); else spawnEnemies(GraphicsEnemyObject::EnemyType::Green, false); } if(m_respawnTicks && --m_respawnTicks == 0) spawnPlayer(); QGraphicsScene::advance(); }