Ejemplo n.º 1
0
void MainGame::gameLoop() {
    
    Bengine::FpsLimiter fpsLimiter;
    fpsLimiter.setMaxFPS(60.0f);

    // Main loop
    while (_gameState == GameState::PLAY) {
        fpsLimiter.begin();

        checkVictory();
		if (_timeElapsed > 100 && !_soldiersSpawned)
			initSoldiers();
		else
			_timeElapsed++;

        processInput();
       
        updateAgents();

        updateBullets();

        _camera.setPosition(_player->getPosition());

        _camera.update();

        drawGame();

        _fps = fpsLimiter.end();
    }
}
Ejemplo n.º 2
0
int main(int argc, char *argv[]) {
    bool gameIsActive;

    if (!init()) {
        printf("error initializing");
        return 1;
    }
    printf("initialized\n");
    printf("Waiting for connection...\n");
    while (true) {
        acceptConnection();
        initGame();
        printf("Game initialized");
        gameIsActive = true;
        packetId=0;
        SDL_DetachThread(SDL_CreateThread(udpListener, "udpThread", NULL));
        while (1) {
            updateBullets();
            updateShips();
            createAndSendUDPPackets();
            SDL_Delay(20);
//            if (!ClientsAreReady()) {
//                gameIsActive = false;
//            }
        }
    }
    closeServer();
    return 0;
}
Ejemplo n.º 3
0
void Gun::onUpdate(sf::Time dt)
{
    if (mPlayerControlled)
    {
        handleInput();
    updateBullets();
    }
}
Ejemplo n.º 4
0
void HeartGame::update()
{
    handleEvents();
    if(m_gameover)
        return;

    updateRings();
    updateBullets();
    updateHeartAndLevel();
}
Ejemplo n.º 5
0
void penguin::update()
{
	updateBullets();
	x += speedX * dirX;
	y += speedY * dirY;
	if ( x < 0 ) x = 0;
	else if ( x > WIDTH - bitmapWidth ) x = WIDTH - bitmapWidth;
	if ( y < 0 ) y = 0;
	else if ( y > HEIGHT - bitmapHeight ) y = HEIGHT - bitmapHeight;
}
Ejemplo n.º 6
0
void updateAllBullets() {
	if (getBulletStatus()) {
		point_t tank_bullet;
		tank_bullet.x = getTankBulletPosition().x;
		tank_bullet.y = getTankBulletPosition().y-BULLET_INCREMENT;
		setTankBulletPosition(tank_bullet);
		drawTankBullet(true);
		checkHits();
	}
	updateBullets();
}
Ejemplo n.º 7
0
void updateGame(Game *game){
  game->totalTime++;
  if(game->levelTime){
    game->levelTime++;
    addEnemies(game);//Adds enemies and checks if level is finished
  }

  updateTowers(game);
  updateEnemies(game);
  updateBullets(game);
};
Ejemplo n.º 8
0
void rScene5::update(float deltaTime)
{
	pollEvents();
	m_deltaTime = deltaTime;

	playerStep();
	moveSpotlight(player->getX(), player->getY());

	updatePotatoes();
	updateEnemies();
	updateBullets();

	if (player->getX() > 928)
		message = "go:Scene6";
}
Ejemplo n.º 9
0
Archivo: Game.cpp Proyecto: zheck/rtype
void Game::updateMap()
{
    updateBullets();
    updateMinions();

    QuadTree q(Vect2(0, 0), Vect2(800, 600), _gameTime);
    q.createInfluenceTree(_ennemies);

    for (std::list<Player *>::iterator it = _playerList.begin(); it != _playerList.end(); ++it) {
        if ((*it)->isAlive()) {
            q.insert(*it);
        }
    }
    for (std::list<Entity *>::iterator it = _missiles.begin(); it != _missiles.end(); ++it) {
        if ((*it)->isAlive()) {
            q.insert(*it);
        }
    }
}
//-------------------------------------------------------------------------------------
void GalactiCombatServer::serverLoop(void)
{
    if(verbose) std::cout << "Entering serverLoop" << std::endl;
    timespec prevTime, currTime;
    clock_gettime(1, &prevTime);
    while(1)
    {
        if(verbose) std::cout << "==============Server Loop Run=============" << std::endl;
        
        //run the game loop
        if(state == PLAY)
        {
            clock_gettime(1, &currTime);
            float elapsedTime = (currTime.tv_sec*1000000000 + currTime.tv_nsec) - (prevTime.tv_sec*1000000000 + prevTime.tv_nsec);
            elapsedTime /= 1000000000; //convert from nanoseconds to seconds
            //if(elapsedTime < TIME_STEP/2) std::cerr << "Elapsed time between iterations of game loop too small.... Skipping iteration." << std::endl;
            if(elapsedTime >= TIME_STEP/2 && elapsedTime < MAX_SUB_STEPS*TIME_STEP) 
            {
                if(verbose) std::cout << "Running the Game loop with elapsed time: " << elapsedTime << std::endl;
                gameLoop(elapsedTime);
                updateMinerals();
                updateSpaceShips();
                updateBullets();
                if(verbose) std::cout << "Game loop has been run." << std::endl;
                prevTime = currTime;
            }
            else
            {
                std::cerr << "Elapsed time between iterations of game loop too LARGE. Skipping iteration." << std::endl;
                prevTime = currTime;
            }
        }
        
        //get sockets ready for connection
        if(verbose) std::cout << "Getting SocketSet." << std::endl;
        SDLNet_SocketSet set = this->createSockSet();
        if(verbose) std::cout << "Checking Number of Sockets Ready..." << std::endl;
        int numReady = NetworkUtil::CheckSockets(set, (Uint32) - 1); //NOTE: This will block until there is some network activity.
        if(verbose) std::cout << "Number of Sockets Ready: " << numReady << std::endl;
        if(numReady == -1) break;
        if(!numReady) continue;
        
        //listen for new connections
        if(verbose) std::cout << "Checking this server's socket." << std::endl;
        if(SDLNet_SocketReady(TCPServerSock))
        {
            numReady--;
            this->listenForConnections();
        }
        
        //for each connection, receive data
        for(int i = 0; numReady && i < clients.size(); i++)
        {
            if(verbose) std::cout << "Checking " << clients[i]->name << "'s socket." << std::endl;
            if(SDLNet_SocketReady(clients[i]->sock))
            {
                numReady--;
                this->receiveData(i);
            } else {
                //FIXME: THIS SHOULD CHECK FOR DISCONNECTS, BUT DOESN'T WORK:
                //std::cerr << clients[i]->name << "'s socket wasn't ready." << std::endl;
                //this->removeClient(i);
            }
        }
/*		//TODO: UDP messages
		for(int i = 0; i < clients.size(); i++)
		{
			if(verbose) std::cout<<"Checking for UDP messages."<<std::endl;
			this->receiveData(-1);
		}
*/
    }
    if(verbose) std::cout << "Exiting serverLoop" << std::endl << std::endl;
}
Ejemplo n.º 11
0
//update bullets, move aliens, move motherhsip, make new alien bullet if less than 4 on screen
void timer_interrupt_handler(){

	//xil_printf("Interrupt Handler\n\r");
	//xil_printf("firstSound: %d\n\r",firstSound);
	if(globals_tankDeath == running){
			if(readyForSound){
				samples = explosionSamples;
				num_samples = explosionNumSamples;
				readyForSound = false;
			}
            XGpio_InterruptGlobalDisable(&gpPB); // Turn off all PB interrupts for now.
            if(first){
                  timer = 1;
                  first = false;
            }
  //perform tank animation-----------------------------------------------------------------------
            if(timer < EXPLODE_TIME){//explode for two seconds
                    if(!(timer % 2))
                            write_tank_explosion1();
                    else
                            write_tank_explosion2();
            }
            else{
            		//explosionSoundIndex = 0;
                    first = true;
                    timer = 1;
                    globals_tankDeath = stopped;
                    write_tank_black();
                    globals_setTankPosition(INITIAL_TANK_POSITION);
                    write_tank_to_memory();
                    XGpio_InterruptGlobalEnable(&gpPB);                 // Re-enable PB interrupts.
            }
	}
        //tank death animation isn't happening. Do other stuff
	else{

	    u32 currentButtonState = XGpio_DiscreteRead(&gpPB, 1);  // Get the current state of the buttons.
	    // allow user to use buttons, even multiple at once. All three at once don't move the tank but do shoot a bullet
	    if(!(timer % TANK_SPEED)){
			switch(currentButtonState){
			  case 8:
				moveTankLeft();
				break;
			  case 1:
				newTankBullet();
				if(readyForSound){
					samples = tankFireArray;
					num_samples = tankFireSamples;
					readyForSound = false;
				}
				break;
			  case 2:
				moveTankRight();
				break;
			  case 9:
				moveTankLeft();
				newTankBullet();
				if(readyForSound){
					samples = tankFireArray;
					num_samples = tankFireSamples;
					readyForSound = false;
				}
				break;
			  case 3:
				moveTankRight();
				newTankBullet();
				if(readyForSound){
					samples = tankFireArray;
					num_samples = tankFireSamples;
					readyForSound = false;
				}
				break;
			  case 11:
				newTankBullet();
				if(readyForSound){
					samples = tankFireArray;
					num_samples = tankFireSamples;
					readyForSound = false;
				}
				break;
			  case 4:
				  //volume up
				  break;
			  case 16:
				  //volume down
				  break;
			  default:
				break;
			}
	    }

	  //ensure random for mothership and alien bullets
	  srand(timer);

	//if mothership is present, move mothership-----------------------------------------------------
	  if(globals_mothershipState == ALIVE && !(timer % MOTHERSHIP_SPEED)){
                //assign new position for mothership
		mothershipPosition += MOTHERSHIP_MOVEMENT;
                //mothership at the edge? 
		if(mothershipPosition + MOTHERSHIP_WIDTH >= X_MAX-MOTHERSHIP_EDGE_CORRECTION){
			mothershipSpawnCounter = rand() % (MOTHERSHIP_MAX + 1 - MOTHERSHIP_MIN) + MOTHERSHIP_MIN;//create a new spawn timer
			globals_mothershipState = DEAD; //erase mothership
			write_mothership_black_to_memory();
			mothershipPosition = 0; //reset mothership position
		}
		else
			write_mothership_to_memory(); //if not at edge, move the ship on screen
			if(readyForSound){
				samples = mothershipSamples;
				num_samples = mothershipNumSamples;
				readyForSound = false;
			}
	  }


	//draw mothership if mothershipSpawnCounter reached---------------------------------------------
	  if(!(mothershipTimer % mothershipSpawnCounter)){
		globals_mothershipState = ALIVE; //set ship to alive
		mothershipTimer = 1;//initialize/reset timer
		write_mothership_to_memory(); //draw on screen
	  }

	//stall deleting alien long enough to see explosion---------------------------------------------
	  if(beginAlienExplosion){
                  //stall till explosion done
		  if(alienExplodeCounter < ALIEN_EXPLODE_TIME){
			  ++alienExplodeCounter;
		  }
		  if(alienExplodeCounter == ALIEN_EXPLODE_TIME){
			  write_alien_dead_to_memory(globals_alien); //erase explosion
			  alienExplodeCounter = 1; //reset timer
			  beginAlienExplosion = false; //end delay
		  }
	  }

//killed mother ship stuff-------------------------------------------------------------------------
	  if(beginMotherExplosion){

		  if(readyForSound){
			  samples = mothExplosionSamples;
			  num_samples = mothExplosionNumSamples;
			  readyForSound = false;
		  }
		  //draw 150 in place of mothership
		  write_mothership_hit_score_to_memory();
                  //delay long enough for user to see
		  if(alienExplodeCounter < MOTHERSHIP_EXPLODE_TIME){
			  ++alienExplodeCounter;
		  }
                  //delay limit reached
		  if(alienExplodeCounter == MOTHERSHIP_EXPLODE_TIME){
			  write_mothership_black_to_memory(); //erase score
			  alienExplodeCounter = 1; //reset counter
			  beginMotherExplosion = false; //no longer exploding
			  mothershipSpawnCounter = rand() % (MOTHERSHIP_MAX + 1 - MOTHERSHIP_MIN) + MOTHERSHIP_MIN;//make new spawn counter
			  mothershipPosition = 0;//reset position
		  }
	  }

	//move alien block----------------------------------------------------------------------------
		//if large amount of aliens are dead, move at fastest rate
	  if(dead_alien_count > DEATH_FOR_FASEST){
		  if(!(timer%ALIEN_SPEED3)){
			  moveAlienBlock();

			  if(readyForSound){
				  samples = alienSamples;
				  num_samples = alienNumSamples;
				  readyForSound = false;
			  }

		  }
	  }
		  //if medium amount dead, move at medium speed
	  else if(dead_alien_count > DEATH_FOR_MEDIUM){
		  if(!(timer%ALIEN_SPEED2)){
			  moveAlienBlock();

			  if(readyForSound){
				  samples = alienSamples;
				  num_samples = alienNumSamples;
				  readyForSound = false;
			  }

		  }
		  //otherwise move at slowest rate
	  }else{
		  if(!(timer%ALIEN_SPEED1)){
			  moveAlienBlock();

			  if(readyForSound){
				  samples = alienSamples;
				  num_samples = alienNumSamples;
				  readyForSound = false;
			  }

		  }
	  }

	//update bullets--------------------------------------------------------------------------------
	  if(!(timer % BULLET_SPEED)){
		  updateBullets();
	  }


	//create new alien bullet-----------------------------------------------------------------------
	  if(!(timer % (rand()%(ALIEN_BULLET_MAX+1-ALIEN_BULLET_MIN)+ALIEN_BULLET_MIN))){//new alien bullet from 2-5 seconds
		 newAlienBullet();
	  }

	//inc mothership timer. Cannot overflow under correct operation---------------------------------
	  if(globals_mothershipState == DEAD)
		  ++mothershipTimer;//mothership needs its own timer. Appears at unpredictable times

	}

//inc timers-------------------------------------------------------------------------------------
  if(timer == UINT16_MAX){ 
    timer = 1;//Reset timer to 1 so as not to mess up mod operations
  }else{
    ++timer;
  }

}
Ejemplo n.º 12
0
void PlayState::update()
{
	// Shake the background up and down when bomb is used
	if(bombAnimation)
	{
		bombAnimationCount++;
		bombShakeCounter++;
		// Up
		if(bombShakeCounter < 5)
		{
			starsBG_Pos.y -= 2;
		}
		// Down + Down
		else if(bombShakeCounter < 15)
		{
			starsBG_Pos.y += 2;
		}
		// Up
		else if(bombShakeCounter < 20)
		{
			starsBG_Pos.y -= 2;
		}
		// Reset
		else
		{
			bombShakeCounter = 0;
			--starsBG_Pos.y;
		}

		if(bombAnimationCount == BOMB_ANIMATION_LENGTH)
		{
			bombAnimationCount = 0;
			bombAnimation = false;
		}
	}

	// Move BG
	++bgMoveCounter;

	if(bgMoveCounter > 2)
	{
		++starsBG_Pos.x;
		bgMoveCounter = 0;
	}

	REG_BG0VOFS = starsBG_Pos.y;
	REG_BG0HOFS = starsBG_Pos.x;

	updateText();

	// Update the stage
	currentStage->update();

	// Checks for button pushes and updates accordingly
	checkInput();

	updatePlayer();
	
	// Udates the PowerUp and checks for collisions
	updatePowerUp();

	// Updates all of the enemies and has them fire.
	updateEnemies();

	// Updates all of the bullets and checks for collisions
	updateBullets();
}
Ejemplo n.º 13
0
void MainGame::gameLoop() {

	// Some helpful constants.
	const float DESIRED_FPS = 60.0f; // FPS the game is designed to run at
	const int MAX_PHYSICS_STEPS = 6; // Max number of physics steps per frame
	const float MS_PER_SECOND = 1000; // Number of milliseconds in a second
	const float DESIRED_FRAMETIME = MS_PER_SECOND / DESIRED_FPS; // The desired frame time per frame
	const float MAX_DELTA_TIME = 1.0f; // Maximum size of deltaTime

	// Used to cap the FPS
	Adina::FpsLimiter fpsLimiter;
	fpsLimiter.setMaxtFPS(60000.0f);

	// Zoom out the camera by 4x
	const float CAMERA_SCALE = 1.0f / 3.0f;
	m_camera.setScaleFactor(CAMERA_SCALE);

	// Start our previousTicks variable
	float previousTicks = SDL_GetTicks();

	// Main loop
	while (m_gameState == GameState::PLAY) {
		fpsLimiter.begin();

		// Calculate the frameTime in milliseconds
		float newTicks = SDL_GetTicks();
		float frameTime = newTicks - previousTicks;
		previousTicks = newTicks; // Store newTicks in previousTicks so we can use it next frame
		// Get the total delta time
		float totalDeltaTime = frameTime / DESIRED_FRAMETIME;

		checkVictory();

		m_inputManager.update();

		processInput();

		int i = 0; // This counter makes sure we don't spiral to death!
		// Loop while we still have steps to process.
		while (totalDeltaTime > 0.0f && i < MAX_PHYSICS_STEPS) {
			// The deltaTime should be the the smaller of the totalDeltaTime and MAX_DELTA_TIME
			float deltaTime = std::min(totalDeltaTime, MAX_DELTA_TIME);
			// Update all physics here and pass in deltaTime
			updateAgents(deltaTime);
			updateBullets(deltaTime);
			m_particleEngine2D.update(deltaTime);
			// Since we just took a step that is length deltaTime, subtract from totalDeltaTime
			totalDeltaTime -= deltaTime;
			// Increment our frame counter so we can limit steps to MAX_PHYSICS_STEPS
			i++;
		}

		// Make sure the camera is bound to the player position
		m_camera.setPosition(m_player->getPosition());
		m_camera.update();
		m_hudCamera.update();

		drawGame();

		// End the frame, limit the FPS, and get the current FPS.
		m_fps = fpsLimiter.end();
		//std::cout << m_fps << std::endl;
	}
}